jakarta-bcel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From md...@apache.org
Subject cvs commit: jakarta-bcel/src/java/org/apache/bcel/util SyntheticRepository.java ClassLoaderRepository.java
Date Fri, 23 May 2003 07:54:08 GMT
mdahm       2003/05/23 00:54:08

  Modified:    src/java/org/apache/bcel Repository.java
               src/java/org/apache/bcel/util SyntheticRepository.java
                        ClassLoaderRepository.java
  Log:
  Repository methods throw ClassNotFound instead of returning null
  Should provide more readable code
  
  Revision  Changes    Path
  1.12      +78 -40    jakarta-bcel/src/java/org/apache/bcel/Repository.java
  
  Index: Repository.java
  ===================================================================
  RCS file: /home/cvs/jakarta-bcel/src/java/org/apache/bcel/Repository.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Repository.java	11 Oct 2002 20:34:47 -0000	1.11
  +++ Repository.java	23 May 2003 07:54:07 -0000	1.12
  @@ -67,7 +67,7 @@
    * @see org.apache.bcel.util.SyntheticRepository
    *
    * @version $Id$
  - * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  + * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    */
   public abstract class Repository {
     private static org.apache.bcel.util.Repository _repository =
  @@ -88,39 +88,45 @@
     /** Lookup class somewhere found on your CLASSPATH, or whereever the
      * repository instance looks for it.
      *
  -   * @return class object for given fully qualified class name, or null
  -   * if the class could not be found or parsed correctly
  +   * @return class object for given fully qualified class name
  +   * @throws ClassNotFoundException if the class could not be found or
  +   * parsed correctly
      */
  -  public static JavaClass lookupClass(String class_name) {
  -    try {
  -      JavaClass clazz = _repository.findClass(class_name);
  +  public static JavaClass lookupClass(String class_name)
  +    throws ClassNotFoundException {
   
  -      if(clazz == null) {
  -	return _repository.loadClass(class_name);
  -      } else {
  -	return clazz;
  -      }
  -    } catch(ClassNotFoundException ex) { return null; }
  +    return _repository.loadClass(class_name);
     }
   
     /**
  -   * Try to find class source via getResourceAsStream().
  +   * Try to find class source using the internal repository instance.
      * @see Class
      * @return JavaClass object for given runtime class
  +   * @throws ClassNotFoundException if the class could not be found or
  +   * parsed correctly
      */
  -  public static JavaClass lookupClass(Class clazz) {
  -    try {
  -      return _repository.loadClass(clazz);
  -    } catch(ClassNotFoundException ex) { return null; }
  +  public static JavaClass lookupClass(Class clazz)
  +    throws ClassNotFoundException {
  +    return _repository.loadClass(clazz);
     }
   
  -  /** @return class file object for given Java class.
  -   */
  -  public static ClassPath.ClassFile lookupClassFile(String class_name) {
  -    try {
  -      return ClassPath.SYSTEM_CLASS_PATH.getClassFile(class_name);
  -    } catch(IOException e) { return null; }
  -  }
  +  /**
  +   * @return class file object for given Java class by looking on the
  +   *  system class path; returns null if the class file can't be
  +   *  found
  +   */
  +	public static ClassPath.ClassFile lookupClassFile(String class_name) {
  +			try {
  +					ClassPath path = _repository.getClassPath();
  +
  +					if(path != null) {
  +							return path.getClassFile(class_name);
  +					}	else {
  +						return null;
  +					}
  +							
  +			} catch(IOException e) { return null; }
  +	}
   
     /** Clear the repository.
      */
  @@ -156,92 +162,124 @@
     /**
      * @return list of super classes of clazz in ascending order, i.e.,
      * Object is always the last element
  +   * @throws ClassNotFoundException if any of the superclasses can't be found
      */
  -  public static JavaClass[] getSuperClasses(JavaClass clazz) {
  +  public static JavaClass[] getSuperClasses(JavaClass clazz)
  +    throws ClassNotFoundException {
       return clazz.getSuperClasses();
     }
   
     /**
      * @return list of super classes of clazz in ascending order, i.e.,
  -   * Object is always the last element. return "null", if class
  -   * cannot be found.
  +   * Object is always the last element.
  +   * @throws ClassNotFoundException if the named class or any of its
  +   *  superclasses can't be found
      */
  -  public static JavaClass[] getSuperClasses(String class_name) {
  +  public static JavaClass[] getSuperClasses(String class_name)
  +    throws ClassNotFoundException {
       JavaClass jc = lookupClass(class_name);
  -    return (jc == null? null : getSuperClasses(jc));
  +    return getSuperClasses(jc);
     }
   
     /**
      * @return all interfaces implemented by class and its super
      * classes and the interfaces that those interfaces extend, and so on.
      * (Some people call this a transitive hull).
  +   * @throws ClassNotFoundException if any of the class's
  +   *  superclasses or superinterfaces can't be found
      */
  -  public static JavaClass[] getInterfaces(JavaClass clazz) {
  +  public static JavaClass[] getInterfaces(JavaClass clazz)
  +    throws ClassNotFoundException {
       return clazz.getAllInterfaces();
     }
   
     /**
      * @return all interfaces implemented by class and its super
      * classes and the interfaces that extend those interfaces, and so on
  +   * @throws ClassNotFoundException if the named class can't be found,
  +   *   or if any of its superclasses or superinterfaces can't be found
      */
  -  public static JavaClass[] getInterfaces(String class_name) {
  +  public static JavaClass[] getInterfaces(String class_name)
  +    throws ClassNotFoundException {
       return getInterfaces(lookupClass(class_name));
     }
   
     /**
      * Equivalent to runtime "instanceof" operator.
      * @return true, if clazz is an instance of super_class
  +   * @throws ClassNotFoundException if any superclasses or superinterfaces
  +   *   of clazz can't be found
      */
  -  public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {
  +  public static boolean instanceOf(JavaClass clazz, JavaClass super_class)
  +    throws ClassNotFoundException {
       return clazz.instanceOf(super_class);
     }
   
     /**
      * @return true, if clazz is an instance of super_class
  +   * @throws ClassNotFoundException if either clazz or super_class
  +   *   can't be found
      */
  -  public static boolean instanceOf(String clazz, String super_class) {
  +  public static boolean instanceOf(String clazz, String super_class)
  +    throws ClassNotFoundException {
       return instanceOf(lookupClass(clazz), lookupClass(super_class));
     }
   
     /**
      * @return true, if clazz is an instance of super_class
  +   * @throws ClassNotFoundException if super_class can't be found
      */
  -  public static boolean instanceOf(JavaClass clazz, String super_class) {
  +  public static boolean instanceOf(JavaClass clazz, String super_class)
  +    throws ClassNotFoundException {
       return instanceOf(clazz, lookupClass(super_class));
     }
   
     /**
      * @return true, if clazz is an instance of super_class
  +   * @throws ClassNotFoundException if clazz can't be found
      */
  -  public static boolean instanceOf(String clazz, JavaClass super_class) {
  +  public static boolean instanceOf(String clazz, JavaClass super_class)
  +    throws ClassNotFoundException {
       return instanceOf(lookupClass(clazz), super_class);
     }
   
     /**
      * @return true, if clazz is an implementation of interface inter
  +   * @throws ClassNotFoundException if any superclasses or superinterfaces
  +   *   of clazz can't be found
      */
  -  public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
  +  public static boolean implementationOf(JavaClass clazz, JavaClass inter)
  +    throws ClassNotFoundException {
       return clazz.implementationOf(inter);
     }
   
     /**
      * @return true, if clazz is an implementation of interface inter
  +   * @throws ClassNotFoundException if clazz, inter, or any superclasses
  +   *   or superinterfaces of clazz can't be found
      */
  -  public static boolean implementationOf(String clazz, String inter) {
  +  public static boolean implementationOf(String clazz, String inter)
  +    throws ClassNotFoundException {
       return implementationOf(lookupClass(clazz), lookupClass(inter));
     }
   
     /**
      * @return true, if clazz is an implementation of interface inter
  +   * @throws ClassNotFoundException if inter or any superclasses
  +   *   or superinterfaces of clazz can't be found
      */
  -  public static boolean implementationOf(JavaClass clazz, String inter) {
  +  public static boolean implementationOf(JavaClass clazz, String inter)
  +    throws ClassNotFoundException {
       return implementationOf(clazz, lookupClass(inter));
     }
   
     /**
      * @return true, if clazz is an implementation of interface inter
  +   * @throws ClassNotFoundException if clazz or any superclasses or
  +   *   superinterfaces of clazz can't be found
      */
  -  public static boolean implementationOf(String clazz, JavaClass inter) {
  +  public static boolean implementationOf(String clazz, JavaClass inter)
  +    throws ClassNotFoundException {
       return implementationOf(lookupClass(clazz), inter);
     }
   }
  
  
  
  1.7       +37 -34    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SyntheticRepository.java	10 Nov 2002 18:30:05 -0000	1.6
  +++ SyntheticRepository.java	23 May 2003 07:54:07 -0000	1.7
  @@ -54,12 +54,12 @@
    * <http://www.apache.org/>.
    */
   
  -import java.io.*;
  -
  -import java.util.Map;
  +import java.io.IOException;
  +import java.io.InputStream;
   import java.util.HashMap;
   
  -import org.apache.bcel.classfile.*;
  +import org.apache.bcel.classfile.ClassParser;
  +import org.apache.bcel.classfile.JavaClass;
   
   /**
    * This repository is used in situations where a Class is created
  @@ -79,7 +79,7 @@
    * @see org.apache.bcel.Repository
    *
    * @version $Id$
  - * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  + * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    * @author David Dixon-Peugh
    */
   public class SyntheticRepository implements Repository {
  @@ -88,7 +88,7 @@
     private static HashMap _instances = new HashMap(); // CLASSPATH X REPOSITORY
   
     private ClassPath _path = null;
  -  private HashMap   _loadedClasses = new HashMap(); // CLASSNAME X JAVACLASS
  +  private HashMap _loadedClasses = new HashMap(); // CLASSNAME X JAVACLASS
   
     private SyntheticRepository(ClassPath path) {
       _path = path;
  @@ -101,7 +101,7 @@
     public static SyntheticRepository getInstance(ClassPath classPath) {
       SyntheticRepository rep = (SyntheticRepository)_instances.get(classPath);
   
  -    if(rep == null) {
  +    if (rep == null) {
         rep = new SyntheticRepository(classPath);
         _instances.put(classPath, rep);
       }
  @@ -115,7 +115,7 @@
     public void storeClass(JavaClass clazz) {
       _loadedClasses.put(clazz.getClassName(), clazz);
       clazz.setRepository(this);
  - }
  +  }
   
     /**
      * Remove class from repository
  @@ -135,10 +135,8 @@
      * Load a JavaClass object for the given class name using
      * the CLASSPATH environment variable.
      */
  -  public JavaClass loadClass(String className) 
  -    throws ClassNotFoundException
  -  {
  -    if(className == null || className.equals("")) {
  +  public JavaClass loadClass(String className) throws ClassNotFoundException {
  +    if (className == null || className.equals("")) {
         throw new IllegalArgumentException("Invalid class name " + className);
       }
   
  @@ -146,9 +144,9 @@
   
       try {
         return loadClass(_path.getInputStream(className), className);
  -    } catch(IOException e) {
  -      throw new ClassNotFoundException("Exception while looking for class " + 
  -				       className + ": " + e.toString());
  +    } catch (IOException e) {
  +      throw new ClassNotFoundException(
  +        "Exception while looking for class " + className + ": " + e.toString());
       }
     }
   
  @@ -159,10 +157,10 @@
      */
     public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
       String className = clazz.getName();
  -    String name      = className;
  -    int    i         = name.lastIndexOf('.');
  +    String name = className;
  +    int i = name.lastIndexOf('.');
   
  -    if(i > 0) {
  +    if (i > 0) {
         name = name.substring(i + 1);
       }
   
  @@ -170,30 +168,35 @@
     }
   
     private JavaClass loadClass(InputStream is, String className)
  -    throws ClassNotFoundException
  -  {
  +    throws ClassNotFoundException {
       JavaClass clazz = findClass(className);
   
  -    if(clazz != null) { 
  +    if (clazz != null) {
         return clazz;
       }
   
       try {
  -      if(is != null) {
  -	ClassParser parser = new ClassParser(is, className);
  -	clazz = parser.parse();
  -	
  -	storeClass(clazz);
  -	
  -	return clazz;
  +      if (is != null) {
  +        ClassParser parser = new ClassParser(is, className);
  +        clazz = parser.parse();
  +
  +        storeClass(clazz);
  +
  +        return clazz;
         }
  -    } catch(IOException e) {
  -      throw new ClassNotFoundException("Exception while looking for class " + 
  -				       className + ": " + e.toString());
  +    } catch (IOException e) {
  +      throw new ClassNotFoundException(
  +        "Exception while looking for class " + className + ": " + e.toString());
       }
   
  -    throw new ClassNotFoundException("SyntheticRepository could not load " +
  -				     className);    
  +    throw new ClassNotFoundException(
  +      "SyntheticRepository could not load " + className);
  +  }
  +
  +  /** ClassPath associated with the Repository.
  +   */
  +  public ClassPath getClassPath() {
  +    return _path;
     }
   
     /** Clear all entries from cache.
  
  
  
  1.5       +35 -34    jakarta-bcel/src/java/org/apache/bcel/util/ClassLoaderRepository.java
  
  Index: ClassLoaderRepository.java
  ===================================================================
  RCS file: /home/cvs/jakarta-bcel/src/java/org/apache/bcel/util/ClassLoaderRepository.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ClassLoaderRepository.java	11 Oct 2002 20:34:47 -0000	1.4
  +++ ClassLoaderRepository.java	23 May 2003 07:54:07 -0000	1.5
  @@ -54,12 +54,12 @@
    * <http://www.apache.org/>.
    */
   
  -import java.io.*;
  -
  -import java.util.Map;
  +import java.io.IOException;
  +import java.io.InputStream;
   import java.util.HashMap;
   
  -import org.apache.bcel.classfile.*;
  +import org.apache.bcel.classfile.ClassParser;
  +import org.apache.bcel.classfile.JavaClass;
   
   /**
    * The repository maintains information about which classes have
  @@ -71,27 +71,23 @@
    * @see org.apache.bcel.Repository
    *
    * @version $Id$
  - * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  + * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    * @author David Dixon-Peugh
    */
  -public class ClassLoaderRepository
  -  implements Repository
  -{
  +public class ClassLoaderRepository implements Repository {
     private java.lang.ClassLoader loader;
  -  private HashMap loadedClasses =
  -    new HashMap(); // CLASSNAME X JAVACLASS
  +  private HashMap loadedClasses = new HashMap(); // CLASSNAME X JAVACLASS
   
  -  public ClassLoaderRepository( java.lang.ClassLoader loader ) {
  +  public ClassLoaderRepository(java.lang.ClassLoader loader) {
       this.loader = loader;
     }
   
     /**
      * Store a new JavaClass into this Repository.
      */
  -  public void storeClass( JavaClass clazz ) {
  -    loadedClasses.put( clazz.getClassName(),
  -		       clazz );
  -    clazz.setRepository( this );
  +  public void storeClass(JavaClass clazz) {
  +    loadedClasses.put(clazz.getClassName(), clazz);
  +    clazz.setRepository(this);
     }
   
     /**
  @@ -104,9 +100,9 @@
     /**
      * Find an already defined JavaClass.
      */
  -  public JavaClass findClass( String className ) {
  -    if ( loadedClasses.containsKey( className )) {
  -      return (JavaClass) loadedClasses.get( className );
  +  public JavaClass findClass(String className) {
  +    if (loadedClasses.containsKey(className)) {
  +      return (JavaClass)loadedClasses.get(className);
       } else {
         return null;
       }
  @@ -115,30 +111,29 @@
     /**
      * Lookup a JavaClass object from the Class Name provided.
      */
  -  public JavaClass loadClass( String className ) 
  -    throws ClassNotFoundException
  -  {
  +  public JavaClass loadClass(String className) throws ClassNotFoundException {
       String classFile = className.replace('.', '/');
   
  -    JavaClass RC = findClass( className );
  -    if (RC != null) { return RC; }
  +    JavaClass RC = findClass(className);
  +    if (RC != null) {
  +      return RC;
  +    }
   
       try {
  -      InputStream is = 
  -	loader.getResourceAsStream( classFile + ".class" );
  -	    
  -      if(is == null) {
  -	throw new ClassNotFoundException(className + " not found.");
  +      InputStream is = loader.getResourceAsStream(classFile + ".class");
  +
  +      if (is == null) {
  +        throw new ClassNotFoundException(className + " not found.");
         }
   
  -      ClassParser parser = new ClassParser( is, className );
  +      ClassParser parser = new ClassParser(is, className);
         RC = parser.parse();
  -	    
  -      storeClass( RC );
  +
  +      storeClass(RC);
   
         return RC;
       } catch (IOException e) {
  -      throw new ClassNotFoundException( e.toString() );
  +      throw new ClassNotFoundException(e.toString());
       }
     }
   
  @@ -151,5 +146,11 @@
     public void clear() {
       loadedClasses.clear();
     }
  -}
   
  +  /*
  +   * @return null
  +   */
  +  public ClassPath getClassPath() {
  +    return null;
  +  }
  +}
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Mime
View raw message