mdahm 2002/06/06 04:32:05
Modified: src/java/org/apache/bcel/util SyntheticRepository.java
Log:
Cleanup/rebuild of Synthetic repository
Revision Changes Path
1.3 +122 -83 jakarta-bcel/src/java/org/apache/bcel/util/SyntheticRepository.java
Index: SyntheticRepository.java
===================================================================
RCS file: /home/cvs/jakarta-bcel/src/java/org/apache/bcel/util/SyntheticRepository.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SyntheticRepository.java 5 Jun 2002 10:17:38 -0000 1.2
+++ SyntheticRepository.java 6 Jun 2002 11:32:05 -0000 1.3
@@ -62,96 +62,135 @@
import org.apache.bcel.classfile.*;
/**
- * This repository is used in situations where a
- * Class is created outside the realm of a ClassLoader.
- *
+ * This repository is used in situations where a Class is created
+ * outside the realm of a ClassLoader. Classes are loaded from
+ * the file systems using the paths specified in the given
+ * class path. By default, this is the value returned by
+ * ClassPath.getClassPath().
+ * <br>
* It is designed to be used as a singleton, however it
* can also be used with custom classpaths.
+ *
+ * @version $Id: SyntheticRepository.java,v 1.3 2002/06/06 11:32:05 mdahm Exp $
+ * @author <A HREF="mailto:[email protected]">M. Dahm</A>
+ * @author David Dixon-Peugh
*/
+public class SyntheticRepository implements Repository {
+ private static final String DEFAULT_PATH = ClassPath.getClassPath();
-public class SyntheticRepository
- implements Repository
-{
- private static Map instances =
- new HashMap(); // CLASSPATH X REPOSITORY
-
- private ClassPath path = null;
- private static String defaultPath = ClassPath.getClassPath();
+ private static HashMap _instances = new HashMap(); // CLASSPATH X REPOSITORY
- private Map loadedClasses =
- new HashMap(); // CLASSNAME X JAVACLASS
+ private ClassPath _path = null;
+ private HashMap _loadedClasses = new HashMap(); // CLASSNAME X JAVACLASS
- public SyntheticRepository( String classPath ) {
- path = new ClassPath( classPath );
- instances.put( classPath, this );
- }
-
- public SyntheticRepository( ) {
- path = new ClassPath( );
- instances.put( ClassPath.getClassPath(), this );
- }
-
- public static SyntheticRepository getInstance() {
- SyntheticRepository rep =(SyntheticRepository)
- instances.get( defaultPath );
-
- if(rep == null) {
- rep = new SyntheticRepository(); // adds itself to instances
+ private SyntheticRepository(ClassPath path) {
+ _path = path;
+ }
+
+ public static SyntheticRepository getInstance() {
+ return getInstance(ClassPath.SYSTEM_CLASS_PATH);
+ }
+
+ public static SyntheticRepository getInstance(ClassPath classPath) {
+ SyntheticRepository rep = (SyntheticRepository)_instances.get(classPath);
+
+ if(rep == null) {
+ rep = new SyntheticRepository(classPath);
+ _instances.put(classPath, rep);
+ }
+
+ return rep;
+ }
+
+ /**
+ * Store a new JavaClass instance into this Repository.
+ */
+ public void storeClass(JavaClass clazz) {
+ _loadedClasses.put(clazz.getClassName(), clazz);
+ clazz.setRepository(this);
+ }
+
+ /**
+ * Remove class from repository
+ */
+ public void removeClass(JavaClass clazz) {
+ _loadedClasses.remove(clazz.getClassName());
+ }
+
+ /**
+ * Find an already defined JavaClass.
+ */
+ public JavaClass findClass(String className) {
+ return (JavaClass)_loadedClasses.get(className);
+ }
+
+ /**
+ * Lookup a JavaClass object from the class name provided.
+ */
+ public JavaClass loadClass(String className)
+ throws ClassNotFoundException
+ {
+ if(className == null || className.equals("")) {
+ throw new IllegalArgumentException("Invalid class name " + className);
+ }
+
+ className = className.replace('/', '.'); // Just in case, canonical form
+
+ try {
+ return loadClass(_path.getInputStream(className), className);
+ } catch(IOException e) {
+ throw new ClassNotFoundException("Exception while looking for class " +
+ className + ": " + e.toString());
+ }
+ }
+
+ /**
+ * Try to find class source via getResourceAsStream().
+ * @see Class
+ * @return JavaClass object for given runtime class
+ */
+ public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
+ String className = clazz.getName();
+ String name = className;
+ int i = name.lastIndexOf('.');
+
+ if(i > 0) {
+ name = name.substring(i + 1);
+ }
+
+ return loadClass(clazz.getResourceAsStream(name + ".class"), className);
+ }
+
+ private JavaClass loadClass(InputStream is, String className)
+ throws ClassNotFoundException
+ {
+ JavaClass clazz = findClass(className);
+
+ if(clazz != null) {
+ return clazz;
+ }
+
+ try {
+ if(is != null) {
+ ClassParser parser = new ClassParser(is, className);
+ clazz = parser.parse();
+
+ storeClass(clazz);
+
+ return clazz;
}
-
- return rep;
+ } catch(IOException e) {
+ throw new ClassNotFoundException("Exception while looking for class " +
+ className + ": " + e.toString());
}
- public static SyntheticRepository getInstance(String classPath) {
- return (SyntheticRepository) instances.get( classPath );
- }
-
- /**
- * Store a new JavaClass into this Repository.
- */
- public void storeClass( JavaClass clazz ) {
- loadedClasses.put( clazz.getClassName(),
- clazz );
- }
-
- /**
- * Find an already defined JavaClass.
- */
- public JavaClass findClass( String className ) {
- if ( loadedClasses.containsKey( className )) {
- return (JavaClass) loadedClasses.get( className );
- } else {
- return null;
- }
- }
-
- /**
- * Lookup a JavaClass object from the Class Name provided.
- */
- public JavaClass loadClass( String className )
- throws ClassNotFoundException
- {
- JavaClass RC = findClass( className );
- if (RC != null) {
- return RC;
- }
-
- try {
-
- InputStream is = path.getInputStream( className, ".class" );
- if (is != null) {
- ClassParser parser = new ClassParser( is, className );
- RC = parser.parse();
-
- storeClass( RC );
-
- return RC;
- }
- } catch (IOException e) {
- throw new ClassNotFoundException("Exception looking for class " +
- className + ": " + e.toString());
-
- }
- throw new ClassNotFoundException("SyntheticRepository does not load new classes.");
- }
+ throw new ClassNotFoundException("SyntheticRepository could not load " +
+ className);
+ }
+
+ /** Clear all entries from cache.
+ */
+ public void clear() {
+ _loadedClasses.clear();
+ }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.