Solved

Is it possible to inspect classes without triggering static initialisation?

Posted on 2008-10-23
8
274 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 86
Java Eclipse Loop 3 45
jmeter usage 4 56
Strange router problem - can't access hotmail.com 14 105
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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