jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/util/ClassLoader.java
author joehw
Mon, 17 Apr 2017 16:24:10 -0700
changeset 44797 8b3b3b911b8a
parent 25868 686eef1e7a79
permissions -rw-r--r--
8162572: Update License Header for all JAXP sources Reviewed-by: lancea
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.util.Hashtable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.bcel.internal.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.bcel.internal.classfile.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * <p>Drop in replacement for the standard class loader of the JVM. You can use it
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * in conjunction with the JavaWrapper to dynamically modify/create classes
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * as they're requested.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * <p>This class loader recognizes special requests in a distinct
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * format, i.e., when the name of the requested class contains with
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * "$$BCEL$$" it calls the createClass() method with that name
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * (everything bevor the $$BCEL$$ is considered to be the package
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * name. You can subclass the class loader and override that
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * method. "Normal" classes class can be modified by overriding the
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * modifyClass() method which is called just before defineClass().</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * <p>There may be a number of packages where you have to use the default
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * class loader (which may also be faster). You can define the set of packages
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * where to use the system class loader in the constructor. The default value contains
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * "java.", "sun.", "javax."</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * @see JavaWrapper
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * @see ClassPath
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
public class ClassLoader extends java.lang.ClassLoader {
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  private Hashtable classes = new Hashtable(); // Hashtable is synchronized thus thread-safe
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
  private String[] ignored_packages = {
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    "java.", "javax.", "sun."
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
  };
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  private Repository repository = SyntheticRepository.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
  private java.lang.ClassLoader deferTo = ClassLoader.getSystemClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
  public ClassLoader() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  public ClassLoader(java.lang.ClassLoader deferTo) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    this.deferTo = deferTo;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    this.repository = new ClassLoaderRepository(deferTo);
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
  /** @param ignored_packages classes contained in these packages will be loaded
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * with the system class loader
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
  public ClassLoader(String[] ignored_packages) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    addIgnoredPkgs(ignored_packages);
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  public ClassLoader(java.lang.ClassLoader deferTo, String [] ignored_packages) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    this.deferTo = deferTo;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    this.repository = new ClassLoaderRepository(deferTo);
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    addIgnoredPkgs(ignored_packages);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
  private void addIgnoredPkgs(String[] ignored_packages) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    String[] new_p = new String[ignored_packages.length + this.ignored_packages.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    System.arraycopy(this.ignored_packages, 0, new_p, 0, this.ignored_packages.length);
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    System.arraycopy(ignored_packages, 0, new_p, this.ignored_packages.length,
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
                     ignored_packages.length);
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    this.ignored_packages = new_p;
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  protected Class loadClass(String class_name, boolean resolve)
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    throws ClassNotFoundException
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    Class cl = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    /* First try: lookup hash table.
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    if((cl=(Class)classes.get(class_name)) == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
      /* Second try: Load system class using system class loader. You better
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
       * don't mess around with them.
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
      for(int i=0; i < ignored_packages.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        if(class_name.startsWith(ignored_packages[i])) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
          cl = deferTo.loadClass(class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
          break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
      if(cl == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        JavaClass clazz = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        /* Third try: Special request?
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        if(class_name.indexOf("$$BCEL$$") >= 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
          clazz = createClass(class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        else { // Fourth try: Load classes via repository
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
          if ((clazz = repository.loadClass(class_name)) != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            clazz = modifyClass(clazz);
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
          else
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
            throw new ClassNotFoundException(class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        if(clazz != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
          byte[] bytes  = clazz.getBytes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
          cl = defineClass(class_name, bytes, 0, bytes.length);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        } else // Fourth try: Use default class loader
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
          cl = Class.forName(class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
      if(resolve)
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        resolveClass(cl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
    classes.put(class_name, cl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    return cl;
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
  /** Override this method if you want to alter a class before it gets actually
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
   * loaded. Does nothing by default.
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
  protected JavaClass modifyClass(JavaClass clazz) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    return clazz;
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
   * Override this method to create you own classes on the fly. The
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
   * name contains the special token $$BCEL$$. Everything before that
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
   * token is consddered to be a package name. You can encode you own
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
   * arguments into the subsequent string. You must regard however not
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
   * to use any "illegal" characters, i.e., characters that may not
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
   * appear in a Java class name too<br>
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
   * The default implementation interprets the string as a encoded compressed
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
   * Java class, unpacks and decodes it with the Utility.decode() method, and
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
   * parses the resulting byte array and returns the resulting JavaClass object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   * @param class_name compressed byte code with "$$BCEL$$" in it
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
  protected JavaClass createClass(String class_name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    int    index     = class_name.indexOf("$$BCEL$$");
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
    String real_name = class_name.substring(index + 8);
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    JavaClass clazz = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
      byte[]      bytes  = Utility.decode(real_name, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
      ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
      clazz = parser.parse();
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    } catch(Throwable e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
      e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    // Adapt the class name to the passed value
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
    ConstantPool cp = clazz.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    ConstantClass cl = (ConstantClass)cp.getConstant(clazz.getClassNameIndex(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
                                                     Constants.CONSTANT_Class);
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    ConstantUtf8 name = (ConstantUtf8)cp.getConstant(cl.getNameIndex(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
                                                     Constants.CONSTANT_Utf8);
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    name.setBytes(class_name.replace('.', '/'));
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    return clazz;
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
}