SyntheticRepository bug, patch

David Hovemeyer <[email protected]>
Newsgroups gmane.comp.jakarta.bcel.devel
Message-ID <[email protected]>
The loadClass(String) method of org.apache.util.SyntheticRepository
has a bug where classes already in the repository but not on the
classpath are ignored.  The problem is that the check to see if
the class is already present in the repository is done in
the private loadClass(InputStream, String) method.  However,
when the ClassPath object is used to generate the InputStream
in loadClass(String), it will throw an IOException (because the class
is not on the classpath), and this will cause ClassNotFoundException to
be thrown.

The solution is to move the check to see if the class is in the repository
into the public loadClass() methods.  I've attached a patch to do this.
If someone could commit this, I'd appreciate it.

Thanks,
Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
SyntheticRepository.patch (text/plain, 2.8 KB)
Index: SyntheticRepository.java
===================================================================
RCS file: /home/cvspublic/jakarta-bcel/src/java/org/apache/bcel/util/SyntheticRepository.java,v
retrieving revision 1.7
diff -u -r1.7 SyntheticRepository.java
--- SyntheticRepository.java	23 May 2003 07:54:07 -0000	1.7
+++ SyntheticRepository.java	4 Aug 2003 17:32:13 -0000
@@ -132,8 +132,15 @@
   }
 
   /**
-   * Load a JavaClass object for the given class name using
-   * the CLASSPATH environment variable.
+   * Find a JavaClass object by name.
+   * If it is already in this Repository, the Repository version
+   * is returned.  Otherwise, the Repository's classpath is searched for
+   * the class (and it is added to the Repository if found).
+   *
+   * @param className the name of the class
+   * @return the JavaClass object
+   * @throws ClassNotFoundException if the class is not in the
+   *   Repository, and could not be found on the classpath
    */
   public JavaClass loadClass(String className) throws ClassNotFoundException {
     if (className == null || className.equals("")) {
@@ -142,6 +149,11 @@
 
     className = className.replace('/', '.'); // Just in case, canonical form
 
+    JavaClass clazz = findClass(className);
+    if (clazz != null) {
+      return clazz;
+    }
+
     try {
       return loadClass(_path.getInputStream(className), className);
     } catch (IOException e) {
@@ -151,12 +163,26 @@
   }
 
   /**
-   * Try to find class source via getResourceAsStream().
+   * Find the JavaClass object for a runtime Class object.
+   * If a class with the same name is already in this Repository,
+   * the Repository version is returned.  Otherwise, getResourceAsStream()
+   * is called on the Class object to find the class's representation.
+   * If the representation is found, it is added to the Repository.
+   *
    * @see Class
+   * @param clazz the runtime Class object
    * @return JavaClass object for given runtime class
+   * @throws ClassNotFoundException if the class is not in the
+   *   Repository, and its representation could not be found
    */
   public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
     String className = clazz.getName();
+
+    JavaClass repositoryClass = findClass(className);
+    if (repositoryClass != null) {
+      return repositoryClass;
+    }
+
     String name = className;
     int i = name.lastIndexOf('.');
 
@@ -169,16 +195,11 @@
 
   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();
+        JavaClass clazz = parser.parse();
 
         storeClass(clazz);
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.