< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/Repository.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal;
  23 
  24 
  25 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
  26 import com.sun.org.apache.bcel.internal.util.*;
  27 import java.io.*;
  28 
  29 /**
  30  * The repository maintains informations about class interdependencies, e.g.,
  31  * whether a class is a sub-class of another. Delegates actual class loading
  32  * to SyntheticRepository with current class path by default.
  33  *
  34  * @see com.sun.org.apache.bcel.internal.util.Repository
  35  * @see com.sun.org.apache.bcel.internal.util.SyntheticRepository
  36  *
  37  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  38  */
  39 public abstract class Repository {
  40   private static com.sun.org.apache.bcel.internal.util.Repository _repository =
  41     SyntheticRepository.getInstance();
  42 
  43   /** @return currently used repository instance




  44    */
  45   public static com.sun.org.apache.bcel.internal.util.Repository getRepository() {
  46     return _repository;
  47   }
  48 
  49   /** Set repository instance to be used for class loading

  50    */
  51   public static void setRepository(com.sun.org.apache.bcel.internal.util.Repository rep) {
  52     _repository = rep;
  53   }
  54 
  55   /** Lookup class somewhere found on your CLASSPATH, or whereever the

  56    * repository instance looks for it.
  57    *
  58    * @return class object for given fully qualified class name, or null
  59    * if the class could not be found or parsed correctly

  60    */
  61   public static JavaClass lookupClass(String class_name) {
  62     try {
  63       JavaClass clazz = _repository.findClass(class_name);
  64 
  65       if(clazz == null) {
  66         return _repository.loadClass(class_name);
  67       } else {
  68         return clazz;
  69       }
  70     } catch(ClassNotFoundException ex) { return null; }
  71   }
  72 
  73   /**
  74    * Try to find class source via getResourceAsStream().

  75    * @see Class
  76    * @return JavaClass object for given runtime class


  77    */
  78   public static JavaClass lookupClass(Class clazz) {
  79     try {
  80       return _repository.loadClass(clazz);
  81     } catch(ClassNotFoundException ex) { return null; }
  82   }
  83 
  84   /** Clear the repository.

  85    */
  86   public static void clearCache() {
  87     _repository.clear();
  88   }
  89 
  90   /**
  91    * Add clazz to repository if there isn't an equally named class already in there.

  92    *
  93    * @return old entry in repository
  94    */
  95   public static JavaClass addClass(JavaClass clazz) {
  96     JavaClass old = _repository.findClass(clazz.getClassName());
  97     _repository.storeClass(clazz);
  98     return old;
  99   }
 100 
 101   /**
 102    * Remove class with given (fully qualified) name from repository.
 103    */
 104   public static void removeClass(String clazz) {
 105     _repository.removeClass(_repository.findClass(clazz));
 106   }
 107 
 108   /**
 109    * Remove given class from repository.
 110    */
 111   public static void removeClass(JavaClass clazz) {
 112     _repository.removeClass(clazz);
 113   }
 114 
 115   /**
 116    * @return list of super classes of clazz in ascending order, i.e.,
 117    * Object is always the last element

 118    */
 119   public static JavaClass[] getSuperClasses(JavaClass clazz) {
 120     return clazz.getSuperClasses();
 121   }
 122 
 123   /**
 124    * @return list of super classes of clazz in ascending order, i.e.,
 125    * Object is always the last element. return "null", if class
 126    * cannot be found.

 127    */
 128   public static JavaClass[] getSuperClasses(String class_name) {
 129     JavaClass jc = lookupClass(class_name);
 130     return (jc == null? null : getSuperClasses(jc));
 131   }
 132 
 133   /**
 134    * @return all interfaces implemented by class and its super
 135    * classes and the interfaces that those interfaces extend, and so on.
 136    * (Some people call this a transitive hull).


 137    */
 138   public static JavaClass[] getInterfaces(JavaClass clazz) {
 139     return clazz.getAllInterfaces();
 140   }
 141 
 142   /**
 143    * @return all interfaces implemented by class and its super
 144    * classes and the interfaces that extend those interfaces, and so on


 145    */
 146   public static JavaClass[] getInterfaces(String class_name) {
 147     return getInterfaces(lookupClass(class_name));
 148   }
 149 
 150   /**
 151    * Equivalent to runtime "instanceof" operator.

 152    * @return true, if clazz is an instance of super_class


 153    */
 154   public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {

 155     return clazz.instanceOf(super_class);
 156   }
 157 
 158   /**
 159    * @return true, if clazz is an instance of super_class


 160    */
 161   public static boolean instanceOf(String clazz, String super_class) {

 162     return instanceOf(lookupClass(clazz), lookupClass(super_class));
 163   }
 164 
 165   /**
 166    * @return true, if clazz is an instance of super_class

 167    */
 168   public static boolean instanceOf(JavaClass clazz, String super_class) {

 169     return instanceOf(clazz, lookupClass(super_class));
 170   }
 171 
 172   /**
 173    * @return true, if clazz is an instance of super_class

 174    */
 175   public static boolean instanceOf(String clazz, JavaClass super_class) {

 176     return instanceOf(lookupClass(clazz), super_class);
 177   }
 178 
 179   /**
 180    * @return true, if clazz is an implementation of interface inter


 181    */
 182   public static boolean implementationOf(JavaClass clazz, JavaClass inter) {

 183     return clazz.implementationOf(inter);
 184   }
 185 
 186   /**
 187    * @return true, if clazz is an implementation of interface inter


 188    */
 189   public static boolean implementationOf(String clazz, String inter) {

 190     return implementationOf(lookupClass(clazz), lookupClass(inter));
 191   }
 192 
 193   /**
 194    * @return true, if clazz is an implementation of interface inter


 195    */
 196   public static boolean implementationOf(JavaClass clazz, String inter) {

 197     return implementationOf(clazz, lookupClass(inter));
 198   }
 199 
 200   /**
 201    * @return true, if clazz is an implementation of interface inter


 202    */
 203   public static boolean implementationOf(String clazz, JavaClass inter) {

 204     return implementationOf(lookupClass(clazz), inter);
 205   }
 206 }
   1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.

   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */

  20 package com.sun.org.apache.bcel.internal;
  21 
  22 
  23 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
  24 import com.sun.org.apache.bcel.internal.util.SyntheticRepository;

  25 
  26 /**
  27  * The repository maintains informations about class interdependencies, e.g.,
  28  * whether a class is a sub-class of another. Delegates actual class loading to
  29  * SyntheticRepository with current class path by default.
  30  *
  31  * @see com.sun.org.apache.bcel.internal.util.Repository
  32  * @see SyntheticRepository
  33  *
  34  * @version $Id: Repository.java 1749603 2016-06-21 20:50:19Z ggregory $
  35  */
  36 public abstract class Repository {


  37 
  38     private static com.sun.org.apache.bcel.internal.util.Repository repository
  39             = SyntheticRepository.getInstance();
  40 
  41     /**
  42      * @return currently used repository instance
  43      */
  44     public static com.sun.org.apache.bcel.internal.util.Repository getRepository() {
  45         return repository;
  46     }
  47 
  48     /**
  49      * Set repository instance to be used for class loading
  50      */
  51     public static void setRepository(final com.sun.org.apache.bcel.internal.util.Repository rep) {
  52         repository = rep;
  53     }
  54 
  55     /**
  56      * Lookup class somewhere found on your CLASSPATH, or whereever the
  57      * repository instance looks for it.
  58      *
  59      * @return class object for given fully qualified class name
  60      * @throws ClassNotFoundException if the class could not be found or parsed
  61      * correctly
  62      */
  63     public static JavaClass lookupClass(final String class_name)
  64             throws ClassNotFoundException {
  65         return repository.loadClass(class_name);







  66     }
  67 
  68     /**
  69      * Try to find class source using the internal repository instance.
  70      *
  71      * @see Class
  72      * @return JavaClass object for given runtime class
  73      * @throws ClassNotFoundException if the class could not be found or parsed
  74      * correctly
  75      */
  76     public static JavaClass lookupClass(final Class<?> clazz)
  77             throws ClassNotFoundException {
  78         return repository.loadClass(clazz);

  79     }
  80 
  81     /**
  82      * Clear the repository.
  83      */
  84     public static void clearCache() {
  85         repository.clear();
  86     }
  87 
  88     /**
  89      * Add clazz to repository if there isn't an equally named class already in
  90      * there.
  91      *
  92      * @return old entry in repository
  93      */
  94     public static JavaClass addClass(final JavaClass clazz) {
  95         final JavaClass old = repository.findClass(clazz.getClassName());
  96         repository.storeClass(clazz);
  97         return old;
  98     }
  99 
 100     /**
 101      * Remove class with given (fully qualified) name from repository.
 102      */
 103     public static void removeClass(final String clazz) {
 104         repository.removeClass(repository.findClass(clazz));
 105     }
 106 
 107     /**
 108      * Remove given class from repository.
 109      */
 110     public static void removeClass(final JavaClass clazz) {
 111         repository.removeClass(clazz);
 112     }
 113 
 114     /**
 115      * @return list of super classes of clazz in ascending order, i.e., Object
 116      * is always the last element
 117      * @throws ClassNotFoundException if any of the superclasses can't be found
 118      */
 119     public static JavaClass[] getSuperClasses(final JavaClass clazz) throws ClassNotFoundException {
 120         return clazz.getSuperClasses();
 121     }
 122 
 123     /**
 124      * @return list of super classes of clazz in ascending order, i.e., Object
 125      * is always the last element.
 126      * @throws ClassNotFoundException if the named class or any of its
 127      * superclasses can't be found
 128      */
 129     public static JavaClass[] getSuperClasses(final String class_name) throws ClassNotFoundException {
 130         final JavaClass jc = lookupClass(class_name);
 131         return getSuperClasses(jc);
 132     }
 133 
 134     /**
 135      * @return all interfaces implemented by class and its super classes and the
 136      * interfaces that those interfaces extend, and so on. (Some people call
 137      * this a transitive hull).
 138      * @throws ClassNotFoundException if any of the class's superclasses or
 139      * superinterfaces can't be found
 140      */
 141     public static JavaClass[] getInterfaces(final JavaClass clazz) throws ClassNotFoundException {
 142         return clazz.getAllInterfaces();
 143     }
 144 
 145     /**
 146      * @return all interfaces implemented by class and its super classes and the
 147      * interfaces that extend those interfaces, and so on
 148      * @throws ClassNotFoundException if the named class can't be found, or if
 149      * any of its superclasses or superinterfaces can't be found
 150      */
 151     public static JavaClass[] getInterfaces(final String class_name) throws ClassNotFoundException {
 152         return getInterfaces(lookupClass(class_name));
 153     }
 154 
 155     /**
 156      * Equivalent to runtime "instanceof" operator.
 157      *
 158      * @return true, if clazz is an instance of super_class
 159      * @throws ClassNotFoundException if any superclasses or superinterfaces of
 160      * clazz can't be found
 161      */
 162     public static boolean instanceOf(final JavaClass clazz, final JavaClass super_class)
 163             throws ClassNotFoundException {
 164         return clazz.instanceOf(super_class);
 165     }
 166 
 167     /**
 168      * @return true, if clazz is an instance of super_class
 169      * @throws ClassNotFoundException if either clazz or super_class can't be
 170      * found
 171      */
 172     public static boolean instanceOf(final String clazz, final String super_class)
 173             throws ClassNotFoundException {
 174         return instanceOf(lookupClass(clazz), lookupClass(super_class));
 175     }
 176 
 177     /**
 178      * @return true, if clazz is an instance of super_class
 179      * @throws ClassNotFoundException if super_class can't be found
 180      */
 181     public static boolean instanceOf(final JavaClass clazz, final String super_class)
 182             throws ClassNotFoundException {
 183         return instanceOf(clazz, lookupClass(super_class));
 184     }
 185 
 186     /**
 187      * @return true, if clazz is an instance of super_class
 188      * @throws ClassNotFoundException if clazz can't be found
 189      */
 190     public static boolean instanceOf(final String clazz, final JavaClass super_class)
 191             throws ClassNotFoundException {
 192         return instanceOf(lookupClass(clazz), super_class);
 193     }
 194 
 195     /**
 196      * @return true, if clazz is an implementation of interface inter
 197      * @throws ClassNotFoundException if any superclasses or superinterfaces of
 198      * clazz can't be found
 199      */
 200     public static boolean implementationOf(final JavaClass clazz, final JavaClass inter)
 201             throws ClassNotFoundException {
 202         return clazz.implementationOf(inter);
 203     }
 204 
 205     /**
 206      * @return true, if clazz is an implementation of interface inter
 207      * @throws ClassNotFoundException if clazz, inter, or any superclasses or
 208      * superinterfaces of clazz can't be found
 209      */
 210     public static boolean implementationOf(final String clazz, final String inter)
 211             throws ClassNotFoundException {
 212         return implementationOf(lookupClass(clazz), lookupClass(inter));
 213     }
 214 
 215     /**
 216      * @return true, if clazz is an implementation of interface inter
 217      * @throws ClassNotFoundException if inter or any superclasses or
 218      * superinterfaces of clazz can't be found
 219      */
 220     public static boolean implementationOf(final JavaClass clazz, final String inter)
 221             throws ClassNotFoundException {
 222         return implementationOf(clazz, lookupClass(inter));
 223     }
 224 
 225     /**
 226      * @return true, if clazz is an implementation of interface inter
 227      * @throws ClassNotFoundException if clazz or any superclasses or
 228      * superinterfaces of clazz can't be found
 229      */
 230     public static boolean implementationOf(final String clazz, final JavaClass inter)
 231             throws ClassNotFoundException {
 232         return implementationOf(lookupClass(clazz), inter);
 233     }
 234 }
< prev index next >