Solved

Is it possible to inspect classes without triggering static initialisation?

Posted on 2008-10-23
8
270 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.

760 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

23 Experts available now in Live!

Get 1:1 Help Now