class loader:

U

Ulrich Scholz

I am still trying to load a class with a custom class loader. For more
info on why I want to do that and how I got here, please refer to
thread "class loader confusion".
http://groups.google.com/group/comp...c55aa7/af208975da0f25f2?#doc_95f65ee8cd0dec18

Now, I wrote the custom class loader MyDomainLoader as given below. (I
tried to follow http://www.javalobby.org/java/forums/t18345.html)
A call to loadClass(String) is redirected to findClass(String). This
function (successfully) reads the class file into a byte array and
calls defineClass in class ClassLoader. In turn, defineClass calles
the native function defineClass1.

My problem now is that defineClass1 does not simply returns the class
represented by the byte array. Instead, a trace with eclipse tells me
that it (recursively) calls loadClass(String,boolean).

Does it or not? Why?

I get more and more confused.

Ulrich


The problem

final class MyDomainLoader extends ClassLoader
{
public MyDomainLoader()
{
super();
}

protected Class<?> loadClass(final String name, final boolean
resolve)
throws ClassNotFoundException
{
return findClass(name);
}

public Class<?> loadClass(final String name) throws
ClassNotFoundException
{
return loadClass(name, false);
}

public Class<?> findClass(final String className){
byte classByte[];

String classString =
className.replace('.',File.separatorChar) + ".class";
String classPath = ((String)
ClassLoader.getSystemResource(classString).getFile());

try
{
classByte = loadClassData(classPath);
}
catch(IOException e)
{
throw(new PPException(IO_ERROR, "find class"));
}

return defineClass(className, classByte, 0,
classByte.length, null);
}

private byte[] loadClassData(final String className) throws
IOException{

final File f = new File(className);
final int size = (int)f.length();
byte buff[] = new byte[size];
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
dis.readFully(buff);
dis.close();
return buff;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top