try to read class definitions from classpath resources (submitted by "Andy2003" via db4o forums)

master
patrickroemer 18 years ago
parent f06322c01e
commit 07f2be8db4
  1. 35
      src/EDU/purdue/cs/bloat/file/ClassFileLoader.java

@ -23,6 +23,7 @@ package EDU.purdue.cs.bloat.file;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.zip.*; import java.util.zip.*;
import java.net.URL;
import EDU.purdue.cs.bloat.reflect.*; import EDU.purdue.cs.bloat.reflect.*;
@ -196,6 +197,27 @@ public class ClassFileLoader implements ClassInfoLoader {
interfaceIndexes, constants, this); interfaceIndexes, constants, this);
} }
/**
* Thhis method tries to load a Class by its ressource.
* @param name the Name of the Class
* @return the ClassInfo
*/
private ClassInfo loadClassFromRessource(String name){
name = name.replaceAll("/",".");
try {
Class clazz = Class.forName(name);
int i = name.lastIndexOf('.');
if (i >= 0 && i < name.length()){
name = name.substring(i+1);
}
URL url = clazz.getResource(name + ".class");
if (url != null){
return loadClassFromStream(new File(url.getFile()), url.openStream());
}
} catch (Exception e) {}
return null;
}
/** /**
* Loads the class with the given name. Searches the class path, including * Loads the class with the given name. Searches the class path, including
* zip files, for the class and then returns a data stream for the class * zip files, for the class and then returns a data stream for the class
@ -227,6 +249,11 @@ public class ClassFileLoader implements ClassInfoLoader {
} }
} }
if ((file = loadClassFromRessource(name)) != null){
addToCache(file);
return file;
}
// Otherwise, we have a (possibly fully-specified) class name. // Otherwise, we have a (possibly fully-specified) class name.
name = name.replace('.', '/'); name = name.replace('.', '/');
@ -377,6 +404,12 @@ public class ClassFileLoader implements ClassInfoLoader {
throw new ClassNotFoundException(name); throw new ClassNotFoundException(name);
} }
addToCache(file);
return file;
}
private void addToCache(ClassInfo file) {
// If we've reached the cache size limit, remove the oldest file // If we've reached the cache size limit, remove the oldest file
// in the cache. Then add the new file. // in the cache. Then add the new file.
if (cache.size() == ClassFileLoader.CACHE_LIMIT) { if (cache.size() == ClassFileLoader.CACHE_LIMIT) {
@ -384,8 +417,6 @@ public class ClassFileLoader implements ClassInfoLoader {
} }
cache.addFirst(file); cache.addFirst(file);
return file;
} }
/** /**

Loading…
Cancel
Save