1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 package com.sun.org.apache.bcel.internal;
   6 
   7 /* ====================================================================
   8  * The Apache Software License, Version 1.1
   9  *
  10  * Copyright (c) 2001 The Apache Software Foundation.  All rights
  11  * reserved.
  12  *
  13  * Redistribution and use in source and binary forms, with or without
  14  * modification, are permitted provided that the following conditions
  15  * are met:
  16  *
  17  * 1. Redistributions of source code must retain the above copyright
  18  *    notice, this list of conditions and the following disclaimer.
  19  *
  20  * 2. Redistributions in binary form must reproduce the above copyright
  21  *    notice, this list of conditions and the following disclaimer in
  22  *    the documentation and/or other materials provided with the
  23  *    distribution.
  24  *
  25  * 3. The end-user documentation included with the redistribution,
  26  *    if any, must include the following acknowledgment:
  27  *       "This product includes software developed by the
  28  *        Apache Software Foundation (http://www.apache.org/)."
  29  *    Alternately, this acknowledgment may appear in the software itself,
  30  *    if and wherever such third-party acknowledgments normally appear.
  31  *
  32  * 4. The names "Apache" and "Apache Software Foundation" and
  33  *    "Apache BCEL" must not be used to endorse or promote products
  34  *    derived from this software without prior written permission. For
  35  *    written permission, please contact apache@apache.org.
  36  *
  37  * 5. Products derived from this software may not be called "Apache",
  38  *    "Apache BCEL", nor may "Apache" appear in their name, without
  39  *    prior written permission of the Apache Software Foundation.
  40  *
  41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52  * SUCH DAMAGE.
  53  * ====================================================================
  54  *
  55  * This software consists of voluntary contributions made by many
  56  * individuals on behalf of the Apache Software Foundation.  For more
  57  * information on the Apache Software Foundation, please see
  58  * <http://www.apache.org/>.
  59  */
  60 
  61 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
  62 import com.sun.org.apache.bcel.internal.util.*;
  63 import java.io.*;
  64 
  65 /**
  66  * The repository maintains informations about class interdependencies, e.g.,
  67  * whether a class is a sub-class of another. Delegates actual class loading
  68  * to SyntheticRepository with current class path by default.
  69  *
  70  * @see com.sun.org.apache.bcel.internal.util.Repository
  71  * @see com.sun.org.apache.bcel.internal.util.SyntheticRepository
  72  *
  73  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  74  */
  75 public abstract class Repository {
  76   private static com.sun.org.apache.bcel.internal.util.Repository _repository =
  77     SyntheticRepository.getInstance();
  78 
  79   /** @return currently used repository instance
  80    */
  81   public static com.sun.org.apache.bcel.internal.util.Repository getRepository() {
  82     return _repository;
  83   }
  84 
  85   /** Set repository instance to be used for class loading
  86    */
  87   public static void setRepository(com.sun.org.apache.bcel.internal.util.Repository rep) {
  88     _repository = rep;
  89   }
  90 
  91   /** Lookup class somewhere found on your CLASSPATH, or whereever the
  92    * repository instance looks for it.
  93    *
  94    * @return class object for given fully qualified class name, or null
  95    * if the class could not be found or parsed correctly
  96    */
  97   public static JavaClass lookupClass(String class_name) {
  98     try {
  99       JavaClass clazz = _repository.findClass(class_name);
 100 
 101       if(clazz == null) {
 102         return _repository.loadClass(class_name);
 103       } else {
 104         return clazz;
 105       }
 106     } catch(ClassNotFoundException ex) { return null; }
 107   }
 108 
 109   /**
 110    * Try to find class source via getResourceAsStream().
 111    * @see Class
 112    * @return JavaClass object for given runtime class
 113    */
 114   public static JavaClass lookupClass(Class clazz) {
 115     try {
 116       return _repository.loadClass(clazz);
 117     } catch(ClassNotFoundException ex) { return null; }
 118   }
 119 
 120   /** @return class file object for given Java class.
 121    */
 122   public static ClassPath.ClassFile lookupClassFile(String class_name) {
 123     try {
 124       return ClassPath.SYSTEM_CLASS_PATH.getClassFile(class_name);
 125     } catch(IOException e) { return null; }
 126   }
 127 
 128   /** Clear the repository.
 129    */
 130   public static void clearCache() {
 131     _repository.clear();
 132   }
 133 
 134   /**
 135    * Add clazz to repository if there isn't an equally named class already in there.
 136    *
 137    * @return old entry in repository
 138    */
 139   public static JavaClass addClass(JavaClass clazz) {
 140     JavaClass old = _repository.findClass(clazz.getClassName());
 141     _repository.storeClass(clazz);
 142     return old;
 143   }
 144 
 145   /**
 146    * Remove class with given (fully qualified) name from repository.
 147    */
 148   public static void removeClass(String clazz) {
 149     _repository.removeClass(_repository.findClass(clazz));
 150   }
 151 
 152   /**
 153    * Remove given class from repository.
 154    */
 155   public static void removeClass(JavaClass clazz) {
 156     _repository.removeClass(clazz);
 157   }
 158 
 159   /**
 160    * @return list of super classes of clazz in ascending order, i.e.,
 161    * Object is always the last element
 162    */
 163   public static JavaClass[] getSuperClasses(JavaClass clazz) {
 164     return clazz.getSuperClasses();
 165   }
 166 
 167   /**
 168    * @return list of super classes of clazz in ascending order, i.e.,
 169    * Object is always the last element. return "null", if class
 170    * cannot be found.
 171    */
 172   public static JavaClass[] getSuperClasses(String class_name) {
 173     JavaClass jc = lookupClass(class_name);
 174     return (jc == null? null : getSuperClasses(jc));
 175   }
 176 
 177   /**
 178    * @return all interfaces implemented by class and its super
 179    * classes and the interfaces that those interfaces extend, and so on.
 180    * (Some people call this a transitive hull).
 181    */
 182   public static JavaClass[] getInterfaces(JavaClass clazz) {
 183     return clazz.getAllInterfaces();
 184   }
 185 
 186   /**
 187    * @return all interfaces implemented by class and its super
 188    * classes and the interfaces that extend those interfaces, and so on
 189    */
 190   public static JavaClass[] getInterfaces(String class_name) {
 191     return getInterfaces(lookupClass(class_name));
 192   }
 193 
 194   /**
 195    * Equivalent to runtime "instanceof" operator.
 196    * @return true, if clazz is an instance of super_class
 197    */
 198   public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {
 199     return clazz.instanceOf(super_class);
 200   }
 201 
 202   /**
 203    * @return true, if clazz is an instance of super_class
 204    */
 205   public static boolean instanceOf(String clazz, String super_class) {
 206     return instanceOf(lookupClass(clazz), lookupClass(super_class));
 207   }
 208 
 209   /**
 210    * @return true, if clazz is an instance of super_class
 211    */
 212   public static boolean instanceOf(JavaClass clazz, String super_class) {
 213     return instanceOf(clazz, lookupClass(super_class));
 214   }
 215 
 216   /**
 217    * @return true, if clazz is an instance of super_class
 218    */
 219   public static boolean instanceOf(String clazz, JavaClass super_class) {
 220     return instanceOf(lookupClass(clazz), super_class);
 221   }
 222 
 223   /**
 224    * @return true, if clazz is an implementation of interface inter
 225    */
 226   public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
 227     return clazz.implementationOf(inter);
 228   }
 229 
 230   /**
 231    * @return true, if clazz is an implementation of interface inter
 232    */
 233   public static boolean implementationOf(String clazz, String inter) {
 234     return implementationOf(lookupClass(clazz), lookupClass(inter));
 235   }
 236 
 237   /**
 238    * @return true, if clazz is an implementation of interface inter
 239    */
 240   public static boolean implementationOf(JavaClass clazz, String inter) {
 241     return implementationOf(clazz, lookupClass(inter));
 242   }
 243 
 244   /**
 245    * @return true, if clazz is an implementation of interface inter
 246    */
 247   public static boolean implementationOf(String clazz, JavaClass inter) {
 248     return implementationOf(lookupClass(clazz), inter);
 249   }
 250 }