views:

29

answers:

2

here is my case: classloader A, loaded one class("Class1"); then, I changed Class1.java and compile it. next I loaded Class1.class again by classloader B. I want to compare these 2 classes, check whether the class meta data changed by someone. Is there any way to compare 2 classes' definition data?

+1  A: 

I am not entirely sure what you mean by "the class meta data" beyond what you can find through the reflection APIs. Here is an attempt to answer the question based on my best guess.

By definition data do you mean their declared internal variables and method signatures? Because you can do that with reflection (getDeclaredMethod() and getDeclaredFields()). However, if the two classes are loaded from different class loaders, they will not be equal (see the Class javadocs on equality), even if they are loaded from the same compiled bytecode.

There is other information you can get from the Reflection APIs, including what class it inherits from, what interfaces it implements, and any Annotations that are compiled in with it (assuming 1.5 or higher of course).

You could also potentially do a hash of the Class files (finding them through the classloader is possible) and see if they are different - that would tell you if they had different code in them.

Hope that helps.

aperkins
A: 

thanks! Reflection could collect one class's meta data, but it's hard to check whether one class is changed.

I can locate that class file, but also it's hard to check whether one class is changed.

I assumed there should be a way to check loaded classes, whether they have the same data(from the same java file).

simonwang