jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/util/ClassLoaderRepository.java
author lana
Tue, 08 Aug 2017 22:52:41 +0000
changeset 46173 5546b5710844
parent 44797 8b3b3b911b8a
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     5
/*
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    12
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    14
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    18
 * See the License for the specific language governing permissions and
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    19
 * limitations under the License.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    20
 */
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.bcel.internal.util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.util.Map;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.util.HashMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.bcel.internal.classfile.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * The repository maintains information about which classes have
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * been loaded.
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * It loads its data from the ClassLoader implementation
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * passed into its constructor.
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * @see com.sun.org.apache.bcel.internal.Repository
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * @author David Dixon-Peugh
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
public class ClassLoaderRepository
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
  implements Repository
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  private java.lang.ClassLoader loader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  private HashMap loadedClasses =
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    new HashMap(); // CLASSNAME X JAVACLASS
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
  public ClassLoaderRepository( java.lang.ClassLoader loader ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
    this.loader = loader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   * Store a new JavaClass into this Repository.
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
  public void storeClass( JavaClass clazz ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    loadedClasses.put( clazz.getClassName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
                       clazz );
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    clazz.setRepository( this );
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
   * Remove class from repository
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
  public void removeClass(JavaClass clazz) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    loadedClasses.remove(clazz.getClassName());
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   * Find an already defined JavaClass.
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
  public JavaClass findClass( String className ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    if ( loadedClasses.containsKey( className )) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
      return (JavaClass) loadedClasses.get( className );
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   * Lookup a JavaClass object from the Class Name provided.
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  public JavaClass loadClass( String className )
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    throws ClassNotFoundException
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    String classFile = className.replace('.', '/');
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    JavaClass RC = findClass( className );
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    if (RC != null) { return RC; }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
      InputStream is =
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        loader.getResourceAsStream( classFile + ".class" );
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
      if(is == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        throw new ClassNotFoundException(className + " not found.");
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
      ClassParser parser = new ClassParser( is, className );
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
      RC = parser.parse();
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
      storeClass( RC );
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
      return RC;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    } catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
      throw new ClassNotFoundException( e.toString() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
  public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    return loadClass(clazz.getName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
  /** Clear all entries from cache.
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
  public void clear() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    loadedClasses.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
}