Solved

Is it possible to inspect classes without triggering static initialisation?

Posted on 2008-10-23
8
271 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 124
split string containing \r\n in Java 46 38
Oracle SQL syntax check  without executing 6 34
java stored proc example 9 21
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

910 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

22 Experts available now in Live!

Get 1:1 Help Now