Solved

Is it possible to inspect classes without triggering static initialisation?

Posted on 2008-10-23
8
272 Views
Last Modified: 2010-04-21
Hi all,

We're using the chain of command design pattern and are in the process of creating a visualization tool for the commands. This allows us to see the execution path(s) the chain provides. We've got most of it working but there are a couple of classes which is causing headache. These classes have static members which, when we use Class.forClass(className) to load up for use with reflection, are then initialised. Because certain components are not available (transport and config related) these fail with a fatal error and the program stops.

Is there a way to inspect classes without triggering the static initalisation? More specifically, what I need to know is if any of the class' superclasses are the generic in-house created command class.

Both source and class files are available. I could for example create the javadoc and use that but it's a bit convoluted and I'm wondering if there's a better solution out there?

Thanks,

Sven-Ivar
0
Comment
Question by:fjelds
  • 4
  • 4
8 Comments
 

Author Comment

by:fjelds
ID: 22794731
Increasing the points value. I'm now certain that the static init can't be avoided unless one uses a custom JVM. But the question still remains, is there another way of inspecting the class hierarchy of my java classes? As I mentioned, Javadoc would be one solution. Perhaps there's a way to read the source files, some sort of 3rd library/lexer out there that can help with it?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22827081
Since no one else has chimed in on this yet, I will give it a shot although Reflection API is what I know of do what you need.

This may not be feasible depending on the architecture, but could the classes with the static values be changed to implement Factory pattern.  Basically define the constructor as private and then get instances through factory method that will check for value in statics and set them.

I did this to test and seems to work, or at least I am not getting the printout that value is being initialized and get null when tested at the end.

This is probably a silly idea, but like I said just throwing things out maybe will help you see something from a different angle...
/* ClassTest.java */
public class ClassTest {
	public static String value;
 
	private ClassTest() {
		
	}
 
	public static ClassTest getInstance() {
		ClassTest instance = new ClassTest();
		synchronized (value) {
			if (value == null) {
				System.out.println("initializing value in ClassTest");
				value = "Static string";
			}
		}
		return instance;
	}
}
 
/* Reflector.java */
import java.lang.reflect.Method;
 
public class Reflector {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Class c = Class.forName("ClassTest");
			Method m[] = c.getDeclaredMethods();
			for (int i = 0; i < m.length; i++)
				System.out.println(m[i].toString());
		} catch (Throwable e) {
			System.err.println(e);
		}
		
		System.out.println(ClassTest.value);
 
	}
 
}

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22827114
The downside is this only works with code you can change source on; you have to change source; now statics can only be used after first instantiation occurs.

AH, in flushing out a thought, I found something I hadn't used but is there in the API.

Class.forName(name, initialize, loader)

Think you can say false for initialize and it will work.  Going to test out.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 59

Accepted Solution

by:
Kevin Cross earned 350 total points
ID: 22827154
Oh, sweetness.  Hope you are still monitoring this.

Use the Class.forName with three parameters shown above.  Try it out with my updated Reflector class.  I altered my ClassTest to have a static initialize more like what would really be used and so I took out test at end for null as the act of calling ClassTest.value initializes class.

static {
    System.out.println("initializing value in ClassTest");
    value = "Static string";
}
import java.lang.reflect.Method;
 
public class Reflector {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Reflector instance = new Reflector();
		try {
			Class c = Class.forName("ClassTest", false, instance.getClass().getClassLoader());
			Method m[] = c.getDeclaredMethods();
			for (int i = 0; i < m.length; i++)
				System.out.println(m[i].toString());
		} catch (Throwable e) {
			System.err.println(e);
		}
		
 
	}
 
}

Open in new window

0
 

Author Comment

by:fjelds
ID: 22829449
Hi all,

Yes, I'm still reading - I'll check this out as soon as I can and get back to you. Thanks for the suggestions.
0
 

Author Closing Comment

by:fjelds
ID: 31509126
I can not believe I missed that part when I was reading over the API (and the reflection tutorial for that matter). It works for what I need it to do - thanks a lot and much appreciated.
0
 

Author Comment

by:fjelds
ID: 22829676
I can not believe I missed that part when I was reading over the API (and the reflection tutorial for that matter). It works for what I need it to do - thanks a lot and much appreciated.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22830044
You are welcome.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 41
javap not working 8 43
maven module vs maven project 3 21
How do I remove an object from a 3 23
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now