-1

I'm writing a tool which is supposed to determine what classes have changed when a system is upgraded. What i have as input are: 1 - the jar file used with the pre-upgraded system 2 - the jar file used with the post-upgraded system 3 - a list of classes and their purpose that I care about asset init package1.myasset.class asset terminate package1.assetterminate.class etc.

What I want to do is be able to load the jar that comes with the new system and the jar that came with the old system and determine if a given class has the same parent after the change that it did before the change. i.e. if in the new system asset init 1 - is still package1.myasset.class and 2 - is still an extension of package0.generalasset.class

I assume I can use reflection but I'm not sure how to determine what a given classes parent is in each jar to see if it changed.

The jars are not necessarily used directly (runtime) by my tool - in fact they shouldn't really be used except as an input to the analysis of the system change.

kishjeff
  • 121
  • 13
  • if I was in your shoes, since you don't actually have to use these jars, I'd extract the classes (or list the jar contents), sort and diff both using unix tools. – Leo Aug 26 '14 at 02:21
  • but, if you still want to use reflection, I suggest you to take a look at http://code.google.com/p/reflections/ – Leo Aug 26 '14 at 02:23

2 Answers2

0
  1. You need two URLClassLoaders, one for each JAR file.
  2. Process your list of classes and load each one in turn via both classloaders.
  3. Do not attempt to cast them, they are just Class<?> at this stage.
  4. Call Class.getSuperClass() on each, and then getName() on each parent class.
  5. Compare.
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Hi and thank you. I ended up using a snippet I found on this site which adds the jar file(s) to the system class loader. This is very close to what I ended up finding useful... it was the "flawlessly" answer here to another question: http://stackoverflow.com/a/60766/339274 – kishjeff Aug 29 '14 at 21:13
0

Although there are many possibly important changes to a class other than its (single) parent, if that's really all you need I wouldn't bother with code, I'd just put both jars on a system with JDK and do:

javap -classpath jar1 aclassname |grep extends 
javap -classpath jar2 aclassname |grep extends
# or substitute findstr on unimproved Windows 
# repeat for multiple classes as desired

If you want to look for any change in the "public API", the full javap output is a good start.

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70