cvs commit: jakarta-bcel/src/java/org/apache/bcel/verifier Verifier.java

[email protected]
Newsgroups gmane.comp.jakarta.bcel.devel
Message-ID <[email protected]>
ddp         02/05/08 13:59:29

  Modified:    src/java/org/apache/bcel/classfile JavaClass.java
               src/java/org/apache/bcel/util ClassLoader.java
               src/java/org/apache/bcel/verifier Verifier.java
  Added:       src/java/org/apache/bcel/util ClassLoaderRepository.java
                        Repository.java SyntheticRepository.java
  Log:
  These changes are to allow BCEL to pull Classfiles from
  either a ClassLoader (such as AntClassLoader) or off of
  the ClassPath the way it used to.
  
  Revision  Changes    Path
  1.6       +82 -2     jakarta-bcel/src/java/org/apache/bcel/classfile/JavaClass.java
  
  Index: JavaClass.java
  ===================================================================
  RCS file: /home/cvs/jakarta-bcel/src/java/org/apache/bcel/classfile/JavaClass.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JavaClass.java	21 Mar 2002 08:30:38 -0000	1.5
  +++ JavaClass.java	8 May 2002 20:59:29 -0000	1.6
  @@ -56,6 +56,11 @@
   
   import  org.apache.bcel.Constants;
   import  org.apache.bcel.Repository;
  +import  org.apache.bcel.util.Repository;
  +import  org.apache.bcel.util.SyntheticRepository;
  +import  org.apache.bcel.util.ClassVector;
  +import  org.apache.bcel.util.ClassQueue;
  +
   import  java.io.*;
   import  java.util.StringTokenizer;
   
  @@ -69,7 +74,7 @@
    * class file.  Those interested in programatically generating classes
    * should see the <a href="../generic/ClassGen.html">ClassGen</a> class.
   
  - * @version $Id: JavaClass.java,v 1.5 2002/03/21 08:30:38 mdahm Exp $
  + * @version $Id: JavaClass.java,v 1.6 2002/05/08 20:59:29 ddp Exp $
    * @see org.apache.bcel.generic.ClassGen
    * @author  <A HREF="mailto:[email protected]">M. Dahm</A>
    */
  @@ -97,6 +102,14 @@
     static boolean debug = false; // Debugging on/off
     static char    sep   = '/';   // directory separator
   
  +    /**
  +     * In cases where we go ahead and create something,
  +     * use the default SyntheticRepository, because we
  +     * don't know any better.
  +     */
  +    private org.apache.bcel.util.Repository repository = 
  +	SyntheticRepository.getInstance();
  +
     /**
      * Constructor gets all contents as arguments.
      *
  @@ -622,7 +635,7 @@
     }
   
     public final boolean instanceOf(JavaClass super_class) {
  -    return Repository.instanceOf(this, super_class);
  +    return org.apache.bcel.Repository.instanceOf(this, super_class);
     }
   
     public final boolean isSuper() {
  @@ -638,4 +651,71 @@
     public final byte getSource() {
       return source;
     }
  +
  +    // Added on 4/16/2002 to deprecate the old Repository class. -- DDP
  +
  +    /**
  +     * Gets the ClassRepository which holds its definition.
  +     */
  +    public org.apache.bcel.util.Repository getRepository() {
  +	return repository;
  +    }
  +
  +    /**
  +     * Sets the ClassRepository which loaded the JavaClass.
  +     * Should be called immediately after parsing is done.
  +     */
  +    public void setRepository( org.apache.bcel.util.Repository repository ) {
  +	this.repository = repository;
  +    }
  +
  +    /**
  +     * Get the Superclass for this JavaClass object.
  +     */
  +    public JavaClass getSuperclass() {
  +	try {
  +	    return repository.loadClass( getSuperclassName() );
  +	} catch (ClassNotFoundException cnfe) {
  +	    System.err.println("WARNING:  Could not find Superclass.");
  +	    cnfe.printStackTrace();
  +	    return null;
  +	}
  +    }
  +
  +    /**
  +     * Get all interfaces implemented by the JavaClass object.
  +     */
  +    public JavaClass [] getAllInterfaces() {
  +	try {
  +	    ClassQueue queue = new ClassQueue();
  +	    ClassVector vec = new ClassVector();
  +	    
  +	    queue.enqueue( this );
  +	    
  +	    while (!queue.empty()) {
  +		JavaClass clazz = queue.dequeue();
  +
  +		JavaClass souper    = clazz.getSuperclass();
  +		String interfaces[] = clazz.getInterfaceNames();
  +		
  +		if (clazz.isInterface()) {
  +		    vec.addElement( clazz );
  +		} else {
  +		    if (souper != null) {
  +			queue.enqueue( souper );
  +		    }
  +		}
  +		
  +		for (int i = 0; i < interfaces.length; i++) {
  +		    queue.enqueue( repository.loadClass( interfaces[i] ));
  +		}
  +	    }
  +	    
  +	    return vec.toArray();
  +	} catch (ClassNotFoundException cnfe) {
  +	    System.err.println("WARNING: Class not found.");
  +	    cnfe.printStackTrace();
  +	    return null;
  +	}
  +    }
   }
  
  
  
  1.3       +3 -2      jakarta-bcel/src/java/org/apache/bcel/util/ClassLoader.java
  
  Index: ClassLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-bcel/src/java/org/apache/bcel/util/ClassLoader.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ClassLoader.java	14 Dec 2001 18:31:18 -0000	1.2
  +++ ClassLoader.java	8 May 2002 20:59:29 -0000	1.3
  @@ -79,7 +79,7 @@
    * where to use the system class loader in the constructor. The default value contains
    * "java.", "sun.", "javax."</p>
    *
  - * @version $Id: ClassLoader.java,v 1.2 2001/12/14 18:31:18 mdahm Exp $
  + * @version $Id: ClassLoader.java,v 1.3 2002/05/08 20:59:29 ddp Exp $
    * @author  <A HREF="mailto:[email protected]">M. Dahm</A>
    * @see JavaWrapper
    * @see ClassPath
  @@ -89,6 +89,7 @@
     private String[] ignored_packages = {
       "java.", "javax.", "sun."
     };
  +    private Repository repository = null;
   
     public ClassLoader() {
     }
  @@ -132,7 +133,7 @@
   	if(class_name.indexOf("$$BCEL$$") >= 0)
   	  clazz = createClass(class_name);
   	else { // Fourth try: Load classes via repository
  -	  if((clazz = Repository.lookupClass(class_name)) != null)
  +	  if((clazz = org.apache.bcel.Repository.lookupClass(class_name)) != null)
   	    clazz = modifyClass(clazz);
   	  else
   	    throw new ClassNotFoundException(class_name);
  
  
  
  1.1                  jakarta-bcel/src/java/org/apache/bcel/util/ClassLoaderRepository.java
  
  Index: ClassLoaderRepository.java
  ===================================================================
  package org.apache.bcel.util;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache BCEL" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [email protected].
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache BCEL", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.*;
  
  import java.util.Map;
  import java.util.HashMap;
  
  import org.apache.bcel.classfile.*;
  
  /**
   * The repository maintains information about which classes have
   * been loaded.
   *
   * It loads its data from the ClassLoader implementation
   * passed into its constructor.
   */
  
  public class ClassLoaderRepository
      implements Repository
  {
      private java.lang.ClassLoader loader;
      private Map loadedClasses =
  	new HashMap(); // CLASSNAME X JAVACLASS
  
      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 );
      }
  
      /**
       * 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
      {
  	String classFile = className.replace('.', '/');
  
  	JavaClass RC = findClass( className );
  	if (RC != null) { return RC; }
  
  	try {
  	    InputStream is = 
  		loader.getResourceAsStream( classFile + ".class" );
  
  	    ClassParser parser = new ClassParser( is, className );
  	    RC = parser.parse();
  	    
  	    storeClass( RC );
  
  	    return RC;
  	} catch (IOException e) {
  	    throw new ClassNotFoundException( e.toString() );
  	}
      }
  }
  
  
  
  1.1                  jakarta-bcel/src/java/org/apache/bcel/util/Repository.java
  
  Index: Repository.java
  ===================================================================
  package org.apache.bcel.util;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache BCEL" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [email protected].
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache BCEL", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.bcel.classfile.JavaClass;
  
  public interface Repository
  {
      /**
       * Store the provided class under "clazz.getClassName()" 
       */
      public void storeClass( JavaClass clazz );
      
      /**
       * Find the class with the name provided, if the class
       * isn't there, return NULL.
       */
      public JavaClass findClass( String className );
  
      /**
       * Find the class with the name provided, if the class
       * isn't there, make an attempt to load it.
       */
      public JavaClass loadClass( String className ) 
  	throws java.lang.ClassNotFoundException;
  }
  
  
  
  1.1                  jakarta-bcel/src/java/org/apache/bcel/util/SyntheticRepository.java
  
  Index: SyntheticRepository.java
  ===================================================================
  package org.apache.bcel.util;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache BCEL" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [email protected].
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache BCEL", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.*;
  
  import java.util.Map;
  import java.util.HashMap;
  
  import org.apache.bcel.classfile.*;
  
  /**
   * This repository is used in situations where a 
   * Class is created outside the realm of a ClassLoader.
   *
   * It is designed to be used as a singleton, however it
   * can also be used with custom classpaths.
   */
  
  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 Map 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() {
  	return (SyntheticRepository) instances.get( defaultPath );
      }
  
      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.");
      }
  }
  
  
  
  1.4       +17 -6     jakarta-bcel/src/java/org/apache/bcel/verifier/Verifier.java
  
  Index: Verifier.java
  ===================================================================
  RCS file: /home/cvs/jakarta-bcel/src/java/org/apache/bcel/verifier/Verifier.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Verifier.java	7 Feb 2002 23:22:30 -0000	1.3
  +++ Verifier.java	8 May 2002 20:59:29 -0000	1.4
  @@ -77,7 +77,7 @@
    * A Verifier creates PassVerifier instances to perform the actual verification.
    * Verifier instances are usually generated by the VerifierFactory.
    * 
  - * @version $Id: Verifier.java,v 1.3 2002/02/07 23:22:30 enver Exp $
  + * @version $Id: Verifier.java,v 1.4 2002/05/08 20:59:29 ddp Exp $
    * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
    * @see org.apache.bcel.verifier.VerifierFactory
    * @see org.apache.bcel.verifier.PassVerifier
  @@ -203,7 +203,12 @@
   			String[] p3am = pv.getMessages();
   			int meth = pv.getMethodNo();
   			for (int i=0; i<p3am.length; i++){
  -				messages.add("Pass 3a, method "+meth+" ('"+Repository.lookupClass(classname).getMethods()[meth]+"'): "+p3am[i]);
  +				messages.add("Pass 3a, method "+meth+
  +					     " ('"+
  +					     org.apache.bcel.Repository
  +					     .lookupClass(classname)
  +					     .getMethods()[meth] +
  +					     "'): "+p3am[i]);
   			}
   		}
   		Iterator p3bs = p3bvs.values().iterator();
  @@ -212,7 +217,12 @@
   			String[] p3bm = pv.getMessages();
   			int meth = pv.getMethodNo();
   			for (int i=0; i<p3bm.length; i++){
  -				messages.add("Pass 3b, method "+meth+" ('"+Repository.lookupClass(classname).getMethods()[meth]+"'): "+p3bm[i]);
  +				messages.add("Pass 3b, method "+meth+
  +					     " ('"+
  +					     org.apache.bcel.Repository.
  +					     lookupClass(classname).
  +					     getMethods()[meth] +
  +					     "'): "+p3bm[i]);
   			}
   		}
   
  @@ -257,7 +267,8 @@
   			System.out.println("Pass 2:\n"+vr);
   
   			if (vr == VerificationResult.VR_OK){
  -				JavaClass jc = Repository.lookupClass(args[k]);
  +				JavaClass jc = org.apache.bcel.Repository
  +				    .lookupClass(args[k]);
   				for (int i=0; i<jc.getMethods().length; i++){
   					vr = v.doPass3a(i);
   					System.out.println("Pass 3a, method number "+i+" ['"+jc.getMethods()[i]+"']:\n"+vr);
  @@ -278,8 +289,8 @@
   	  
   			// avoid swapping.
   	  	v.flush();
  -	  	Repository.clearCache();
  -			System.gc();
  +	  	org.apache.bcel.Repository.clearCache();
  +		System.gc();
   	  }
   	}
   }
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.