Solved

Is it possible to inspect classes without triggering static initialisation?

Posted on 2008-10-23
8
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 60

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 60

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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 60

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 60

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

729 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