Java Reflection, laoding external classes and casting
OK here is the meat of the issue:
1) Start up a java program, inputting a directory
2) program search directory for .class files
3) program takes class files and makes a new Object instance of said class
3a) new objects are sub classes of a file in the program (designed with malice and for thought)
4) program casts the new objects made from external class fields to the internal super class for usability.
Here's the challenge: not allowed to use hand coded class loaders.
I have 1,2,3,3a completed, however any sneaky way i attempt #4 dies against a class not found exception or a class def not found exception. I am a solid java programmer, but the deep dark forest of java meta and reflection is throwing me for a loop. Help in breaking this would be appreciated.
Re: Java Reflection, laoding external classes and casting
Hello there,
I would assume you see the class not found or def not found exception because you're trying to load a class that does not exist on the classpath rather than trying to cast it to the internal superclass.
What does your code look like and have you made sure that the class it is trying to load is actually on the classpath?
Re: Java Reflection, laoding external classes and casting
dead on the money, striking both of those. looking at the first post i'm leaving out some of the process so I'm going to go over that first.
I have three files:
Foo
Bar
and Main
Bar is a subclass of Foo.
All three are then compiled into .class files
Bar is then moved to a sub folder in the execution directory.
the intent and purpose of this exercise is to get modular object loading (low impact coding, on the fly updates etc). I think you might be hitting the head with the Classpath variables... I'm going to investigate this. Below is Main's code (With some things censored out)
Code Java:
public class Main {
public static void main(String[] args) {
try {
Testing t = new Testing();
ArrayList<Class> test = t.test();
Object invoke = test.get(0).newInstance();
Foo foo = (Foo)(invoke);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public ArrayList<Class> test() {
ArrayList<Class> classList = new ArrayList<Class>();
File dir = new File("subdir");
try {
if (dir.exists()) {
ArrayList<File> paths = new ArrayList<File>();
ArrayList<String> classes = new ArrayList<String>();
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
paths.add(f);
}
}
paths.add(dir);
for (File f : paths) {
for (File ff : f.listFiles()) {
if (!ff.isDirectory() && ff.getName().endsWith(".class")) {
classes.add((f != dir ? f.getName() + "." : "") + ff.getName().substring(0, ff.getName().length() - 6));
}
}
}
ClassLoader cl = new URLClassLoader(new URL[]{dir.toURI().toURL()});
for (String s : classes) {
try {
classList.add(cl.loadClass(s));
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classList;
}
}
Bar.class is the only class in the directory subdir. Class path i think is it... if i make Bar extend Foo it chokes inside of the test method with exceptions of class def not found. If i slide an additional Foo inside subdir it pushes the error back to the point of casing in the main method. I will look into the classpath stuff (eep... been far to long since i have messed with that.)
Re: Java Reflection, laoding external classes and casting
Thanks Json, you hit it perfectly, {SOLVED}! Once i added the subdir to the classpath (netbeans here) it ran smooth as ever, and casting worked wonderfully. Excellent, not i just have to work out some minor positioning details and I'm golden. Once again thanks, Json!