AbstractClassPathRepository.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.util;

  18. import java.io.IOException;
  19. import java.io.InputStream;

  20. import org.apache.bcel.classfile.ClassParser;
  21. import org.apache.bcel.classfile.JavaClass;
  22. import org.apache.bcel.classfile.Utility;

  23. /**
  24.  * This abstract class provides a logic of a loading {@link JavaClass} objects class names via {@link ClassPath}.
  25.  *
  26.  * <p>
  27.  * Subclasses can choose caching strategy of the objects by implementing the abstract methods (e.g.,
  28.  * {@link #storeClass(JavaClass)} and {@link #findClass(String)}).
  29.  * </p>
  30.  *
  31.  * @since 6.4.0
  32.  */
  33. abstract class AbstractClassPathRepository implements Repository {

  34.     private final ClassPath classPath;

  35.     AbstractClassPathRepository(final ClassPath classPath) {
  36.         this.classPath = classPath;
  37.     }

  38.     @Override
  39.     public abstract void clear();

  40.     @Override
  41.     public abstract JavaClass findClass(final String className);

  42.     @Override
  43.     public ClassPath getClassPath() {
  44.         return classPath;
  45.     }

  46.     /**
  47.      * Finds the JavaClass object for a runtime Class object. If a class with the same name is already in this Repository,
  48.      * the Repository version is returned. Otherwise, getResourceAsStream() is called on the Class object to find the
  49.      * class's representation. If the representation is found, it is added to the Repository.
  50.      *
  51.      * @see Class
  52.      * @param clazz the runtime Class object
  53.      * @return JavaClass object for given runtime class
  54.      * @throws ClassNotFoundException if the class is not in the Repository, and its representation could not be found
  55.      */
  56.     @Override
  57.     public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException {
  58.         final String className = clazz.getName();
  59.         final JavaClass repositoryClass = findClass(className);
  60.         if (repositoryClass != null) {
  61.             return repositoryClass;
  62.         }
  63.         String name = className;
  64.         final int i = name.lastIndexOf('.');
  65.         if (i > 0) {
  66.             name = name.substring(i + 1);
  67.         }

  68.         try (InputStream clsStream = clazz.getResourceAsStream(name + JavaClass.EXTENSION)) {
  69.             return loadClass(clsStream, className);
  70.         } catch (final IOException e) {
  71.             return null;
  72.         }
  73.     }

  74.     private JavaClass loadClass(final InputStream inputStream, final String className) throws ClassNotFoundException {
  75.         try {
  76.             if (inputStream != null) {
  77.                 final ClassParser parser = new ClassParser(inputStream, className);
  78.                 final JavaClass clazz = parser.parse();
  79.                 storeClass(clazz);
  80.                 return clazz;
  81.             }
  82.         } catch (final IOException e) {
  83.             throw new ClassNotFoundException("Exception while looking for class " + className + ": " + e, e);
  84.         }
  85.         throw new ClassNotFoundException("ClassRepository could not load " + className);
  86.     }

  87.     /**
  88.      * Finds a JavaClass object by name. If it is already in this Repository, the Repository version is returned. Otherwise,
  89.      * the Repository's classpath is searched for the class (and it is added to the Repository if found).
  90.      *
  91.      * @param className the name of the class
  92.      * @return the JavaClass object
  93.      * @throws ClassNotFoundException if the class is not in the Repository, and could not be found on the classpath
  94.      */
  95.     @Override
  96.     public JavaClass loadClass(String className) throws ClassNotFoundException {
  97.         if (className == null || className.isEmpty()) {
  98.             throw new IllegalArgumentException("Invalid class name " + className);
  99.         }
  100.         className = Utility.pathToPackage(className); // Just in case, canonical form
  101.         final JavaClass clazz = findClass(className);
  102.         if (clazz != null) {
  103.             return clazz;
  104.         }
  105.         try (InputStream inputStream = classPath.getInputStream(className)) {
  106.             return loadClass(inputStream, className);
  107.         } catch (final IOException e) {
  108.             throw new ClassNotFoundException("Exception while looking for class " + className + ": " + e, e);
  109.         }
  110.     }

  111.     @Override
  112.     public abstract void removeClass(final JavaClass javaClass);

  113.     @Override
  114.     public abstract void storeClass(final JavaClass javaClass);
  115. }