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.util;
  23 
  24 
  25 import java.io.*;
  26 
  27 import java.util.Map;
  28 import java.util.HashMap;
  29 
  30 import com.sun.org.apache.bcel.internal.classfile.*;
  31 
  32 /**
  33  * The repository maintains information about which classes have
  34  * been loaded.
  35  *
  36  * It loads its data from the ClassLoader implementation
  37  * passed into its constructor.
  38  *
  39  * @see com.sun.org.apache.bcel.internal.Repository
  40  *
  41  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  42  * @author David Dixon-Peugh
  43  */
  44 public class ClassLoaderRepository
  45   implements Repository
  46 {
  47   private java.lang.ClassLoader loader;
  48   private HashMap loadedClasses =
  49     new HashMap(); // CLASSNAME X JAVACLASS
  50 
  51   public ClassLoaderRepository( java.lang.ClassLoader loader ) {
  52     this.loader = loader;
  53   }
  54 
  55   /**
  56    * Store a new JavaClass into this Repository.
  57    */
  58   public void storeClass( JavaClass clazz ) {
  59     loadedClasses.put( clazz.getClassName(),
  60                        clazz );
  61     clazz.setRepository( this );
  62   }
  63 
  64   /**
  65    * Remove class from repository
  66    */
  67   public void removeClass(JavaClass clazz) {
  68     loadedClasses.remove(clazz.getClassName());
  69   }
  70 
  71   /**
  72    * Find an already defined JavaClass.
  73    */
  74   public JavaClass findClass( String className ) {
  75     if ( loadedClasses.containsKey( className )) {
  76       return (JavaClass) loadedClasses.get( className );
  77     } else {
  78       return null;
  79     }
  80   }
  81 
  82   /**
  83    * Lookup a JavaClass object from the Class Name provided.
  84    */
  85   public JavaClass loadClass( String className )
  86     throws ClassNotFoundException
  87   {
  88     String classFile = className.replace('.', '/');
  89 
  90     JavaClass RC = findClass( className );
  91     if (RC != null) { return RC; }
  92 
  93     try {
  94       InputStream is =
  95         loader.getResourceAsStream( classFile + ".class" );
  96 
  97       if(is == null) {
  98         throw new ClassNotFoundException(className + " not found.");
  99       }
 100 
 101       ClassParser parser = new ClassParser( is, className );
 102       RC = parser.parse();
 103 
 104       storeClass( RC );
 105 
 106       return RC;
 107     } catch (IOException e) {
 108       throw new ClassNotFoundException( e.toString() );
 109     }
 110   }
 111 
 112   public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
 113     return loadClass(clazz.getName());
 114   }
 115 
 116   /** Clear all entries from cache.
 117    */
 118   public void clear() {
 119     loadedClasses.clear();
 120   }
 121 }