Repository.java

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

  18. import java.io.IOException;

  19. import org.apache.bcel.classfile.JavaClass;
  20. import org.apache.bcel.util.ClassPath;
  21. import org.apache.bcel.util.SyntheticRepository;

  22. /**
  23.  * The repository maintains informations about class interdependencies, e.g., whether a class is a sub-class of another.
  24.  * Delegates actual class loading to SyntheticRepository with current class path by default.
  25.  *
  26.  * @see org.apache.bcel.util.Repository
  27.  * @see SyntheticRepository
  28.  */
  29. public abstract class Repository {

  30.     private static org.apache.bcel.util.Repository repository = SyntheticRepository.getInstance();

  31.     /**
  32.      * Adds clazz to repository if there isn't an equally named class already in there.
  33.      *
  34.      * @return old entry in repository
  35.      */
  36.     public static JavaClass addClass(final JavaClass clazz) {
  37.         final JavaClass old = repository.findClass(clazz.getClassName());
  38.         repository.storeClass(clazz);
  39.         return old;
  40.     }

  41.     /**
  42.      * Clears the repository.
  43.      */
  44.     public static void clearCache() {
  45.         repository.clear();
  46.     }

  47.     /**
  48.      * @return all interfaces implemented by class and its super classes and the interfaces that those interfaces extend,
  49.      *         and so on. (Some people call this a transitive hull).
  50.      * @throws ClassNotFoundException if any of the class's superclasses or superinterfaces can't be found
  51.      */
  52.     public static JavaClass[] getInterfaces(final JavaClass clazz) throws ClassNotFoundException {
  53.         return clazz.getAllInterfaces();
  54.     }

  55.     /**
  56.      * @return all interfaces implemented by class and its super classes and the interfaces that extend those interfaces,
  57.      *         and so on
  58.      * @throws ClassNotFoundException if the named class can't be found, or if any of its superclasses or superinterfaces
  59.      *         can't be found
  60.      */
  61.     public static JavaClass[] getInterfaces(final String className) throws ClassNotFoundException {
  62.         return getInterfaces(lookupClass(className));
  63.     }

  64.     /**
  65.      * @return currently used repository instance
  66.      */
  67.     public static org.apache.bcel.util.Repository getRepository() {
  68.         return repository;
  69.     }

  70.     /**
  71.      * @return list of super classes of clazz in ascending order, i.e., Object is always the last element
  72.      * @throws ClassNotFoundException if any of the superclasses can't be found
  73.      */
  74.     public static JavaClass[] getSuperClasses(final JavaClass clazz) throws ClassNotFoundException {
  75.         return clazz.getSuperClasses();
  76.     }

  77.     /**
  78.      * @return list of super classes of clazz in ascending order, i.e., Object is always the last element.
  79.      * @throws ClassNotFoundException if the named class or any of its superclasses can't be found
  80.      */
  81.     public static JavaClass[] getSuperClasses(final String className) throws ClassNotFoundException {
  82.         return getSuperClasses(lookupClass(className));
  83.     }

  84.     /**
  85.      * @return true, if clazz is an implementation of interface inter
  86.      * @throws ClassNotFoundException if any superclasses or superinterfaces of clazz can't be found
  87.      */
  88.     public static boolean implementationOf(final JavaClass clazz, final JavaClass inter) throws ClassNotFoundException {
  89.         return clazz.implementationOf(inter);
  90.     }

  91.     /**
  92.      * @return true, if clazz is an implementation of interface inter
  93.      * @throws ClassNotFoundException if inter or any superclasses or superinterfaces of clazz can't be found
  94.      */
  95.     public static boolean implementationOf(final JavaClass clazz, final String inter) throws ClassNotFoundException {
  96.         return implementationOf(clazz, lookupClass(inter));
  97.     }

  98.     /**
  99.      * @return true, if clazz is an implementation of interface inter
  100.      * @throws ClassNotFoundException if clazz or any superclasses or superinterfaces of clazz can't be found
  101.      */
  102.     public static boolean implementationOf(final String clazz, final JavaClass inter) throws ClassNotFoundException {
  103.         return implementationOf(lookupClass(clazz), inter);
  104.     }

  105.     /**
  106.      * @return true, if clazz is an implementation of interface inter
  107.      * @throws ClassNotFoundException if clazz, inter, or any superclasses or superinterfaces of clazz can't be found
  108.      */
  109.     public static boolean implementationOf(final String clazz, final String inter) throws ClassNotFoundException {
  110.         return implementationOf(lookupClass(clazz), lookupClass(inter));
  111.     }

  112.     /**
  113.      * Equivalent to runtime "instanceof" operator.
  114.      *
  115.      * @return true, if clazz is an instance of superclass
  116.      * @throws ClassNotFoundException if any superclasses or superinterfaces of clazz can't be found
  117.      */
  118.     public static boolean instanceOf(final JavaClass clazz, final JavaClass superclass) throws ClassNotFoundException {
  119.         return clazz.instanceOf(superclass);
  120.     }

  121.     /**
  122.      * @return true, if clazz is an instance of superclass
  123.      * @throws ClassNotFoundException if superclass can't be found
  124.      */
  125.     public static boolean instanceOf(final JavaClass clazz, final String superclass) throws ClassNotFoundException {
  126.         return instanceOf(clazz, lookupClass(superclass));
  127.     }

  128.     /**
  129.      * @return true, if clazz is an instance of superclass
  130.      * @throws ClassNotFoundException if clazz can't be found
  131.      */
  132.     public static boolean instanceOf(final String clazz, final JavaClass superclass) throws ClassNotFoundException {
  133.         return instanceOf(lookupClass(clazz), superclass);
  134.     }

  135.     /**
  136.      * @return true, if clazz is an instance of superclass
  137.      * @throws ClassNotFoundException if either clazz or superclass can't be found
  138.      */
  139.     public static boolean instanceOf(final String clazz, final String superclass) throws ClassNotFoundException {
  140.         return instanceOf(lookupClass(clazz), lookupClass(superclass));
  141.     }

  142.     /**
  143.      * Tries to find class source using the internal repository instance.
  144.      *
  145.      * @see Class
  146.      * @return JavaClass object for given runtime class
  147.      * @throws ClassNotFoundException if the class could not be found or parsed correctly
  148.      */
  149.     public static JavaClass lookupClass(final Class<?> clazz) throws ClassNotFoundException {
  150.         return repository.loadClass(clazz);
  151.     }

  152.     /**
  153.      * Lookups class somewhere found on your CLASSPATH, or whereever the repository instance looks for it.
  154.      *
  155.      * @return class object for given fully qualified class name
  156.      * @throws ClassNotFoundException if the class could not be found or parsed correctly
  157.      */
  158.     public static JavaClass lookupClass(final String className) throws ClassNotFoundException {
  159.         return repository.loadClass(className);
  160.     }

  161.     /**
  162.      * @return class file object for given Java class by looking on the system class path; returns null if the class file
  163.      *         can't be found
  164.      */
  165.     public static ClassPath.ClassFile lookupClassFile(final String className) {
  166.         try (ClassPath path = repository.getClassPath()) {
  167.             return path == null ? null : path.getClassFile(className);
  168.         } catch (final IOException e) {
  169.             return null;
  170.         }
  171.     }

  172.     /**
  173.      * Removes given class from repository.
  174.      */
  175.     public static void removeClass(final JavaClass clazz) {
  176.         repository.removeClass(clazz);
  177.     }

  178.     /**
  179.      * Removes class with given (fully qualified) name from repository.
  180.      */
  181.     public static void removeClass(final String clazz) {
  182.         repository.removeClass(repository.findClass(clazz));
  183.     }

  184.     /**
  185.      * Sets repository instance to be used for class loading
  186.      */
  187.     public static void setRepository(final org.apache.bcel.util.Repository rep) {
  188.         repository = rep;
  189.     }
  190. }