Java Code Examples for com.sun.org.apache.bcel.internal.classfile.JavaClass

The following examples show how to use com.sun.org.apache.bcel.internal.classfile.JavaClass. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bytecoder   Source File: SyntheticRepository.java    License: Apache License 2.0 5 votes vote down vote up
/**
     * Find an already defined (cached) JavaClass object by name.
     */
    @Override
    public JavaClass findClass(final String className) {
        final SoftReference<JavaClass> ref = loadedClasses.get(className);
        if (ref == null) {
            return null;
}
        return ref.get();
    }
 
Example 2
Source Project: hottub   Source File: ObjectType.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * If "this" doesn't reference an interface, it references a class
 * or a non-existant entity.
 */
public boolean referencesInterface(){
  JavaClass jc = Repository.lookupClass(class_name);
  if (jc == null)
    return false;
  else
    return !jc.isClass();
}
 
Example 3
Source Project: JDKSourceCode1.8   Source File: ObjectType.java    License: MIT License 5 votes vote down vote up
/**
 * Java Virtual Machine Specification edition 2, 5.4.4 Access Control
 */
public boolean accessibleTo(ObjectType accessor) {
  JavaClass jc = Repository.lookupClass(class_name);

  if(jc.isPublic()) {
    return true;
  } else {
    JavaClass acc = Repository.lookupClass(accessor.class_name);
    return acc.getPackageName().equals(jc.getPackageName());
  }
}
 
Example 4
Source Project: JDKSourceCode1.8   Source File: ObjectType.java    License: MIT License 5 votes vote down vote up
/**
 * If "this" doesn't reference a class, it references an interface
 * or a non-existant entity.
 */
public boolean referencesClass(){
  JavaClass jc = Repository.lookupClass(class_name);
  if (jc == null)
    return false;
  else
    return jc.isClass();
}
 
Example 5
Source Project: Bytecoder   Source File: Class2HTML.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Write contents of the given JavaClass into HTML files.
 *
 * @param java_class The class to write
 * @param dir The directory to put the files in
 */
public Class2HTML(final JavaClass java_class, final String dir) throws IOException {
    final Method[] methods = java_class.getMethods();
    this.java_class = java_class;
    this.dir = dir;
    class_name = java_class.getClassName(); // Remember full name
    constant_pool = java_class.getConstantPool();
    // Get package name by tacking off everything after the last `.'
    final int index = class_name.lastIndexOf('.');
    if (index > -1) {
        class_package = class_name.substring(0, index);
    } else {
        class_package = ""; // default package
    }
    final ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods,
            constant_pool);
    /* Attributes can't be written in one step, so we just open a file
     * which will be written consequently.
     */
    final AttributeHTML attribute_html = new AttributeHTML(dir, class_name, constant_pool,
            constant_html);
    new MethodHTML(dir, class_name, methods, java_class.getFields(),
            constant_html, attribute_html);
    // Write main file (with frames, yuk)
    writeMainHTML(attribute_html);
    new CodeHTML(dir, class_name, methods, constant_pool, constant_html);
    attribute_html.close();
}
 
Example 6
Source Project: openjdk-8   Source File: ObjectType.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Java Virtual Machine Specification edition 2, 5.4.4 Access Control
 */
public boolean accessibleTo(ObjectType accessor) {
  JavaClass jc = Repository.lookupClass(class_name);

  if(jc.isPublic()) {
    return true;
  } else {
    JavaClass acc = Repository.lookupClass(accessor.class_name);
    return acc.getPackageName().equals(jc.getPackageName());
  }
}
 
Example 7
Source Project: openjdk-jdk9   Source File: XSLTC.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate output JAR-file and packages
 */
public void outputToJar() throws IOException {
    // create the manifest
    final Manifest manifest = new Manifest();
    final java.util.jar.Attributes atrs = manifest.getMainAttributes();
    atrs.put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.2");

    final Map<String, Attributes> map = manifest.getEntries();
    // create manifest
    final String now = (new Date()).toString();
    final java.util.jar.Attributes.Name dateAttr =
        new java.util.jar.Attributes.Name("Date");

    final File jarFile = new File(_destDir, _jarFileName);
    final JarOutputStream jos =
        new JarOutputStream(new FileOutputStream(jarFile), manifest);

    for (JavaClass clazz : _bcelClasses) {
        final String className = clazz.getClassName().replace('.', '/');
        final java.util.jar.Attributes attr = new java.util.jar.Attributes();
        attr.put(dateAttr, now);
        map.put(className + ".class", attr);
        jos.putNextEntry(new JarEntry(className + ".class"));
        final ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
        clazz.dump(out); // dump() closes it's output stream
        out.writeTo(jos);
    }
    jos.close();
}
 
Example 8
Source Project: Bytecoder   Source File: XSLTC.java    License: Apache License 2.0 5 votes vote down vote up
public void dumpClass(JavaClass clazz) {

        if (_outputType == BYTEARRAY_AND_FILE_OUTPUT)
        {
            File outFile = getOutputFile(clazz.getClassName());
            String parentDir = outFile.getParent();
            if (parentDir != null) {
                File parentFile = new File(parentDir);
                if (!SecuritySupport.doesFileExist(parentFile))
                    parentFile.mkdirs();
            }
        }

        try {
            switch (_outputType) {
            case JAR_OUTPUT:
                _bcelClasses.add(clazz);
                break;
            case BYTEARRAY_OUTPUT:
            case BYTEARRAY_AND_FILE_OUTPUT:
            case BYTEARRAY_AND_JAR_OUTPUT:
            case CLASSLOADER_OUTPUT:
                ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
                clazz.dump(out);
                _classes.add(out);

                if (_outputType == BYTEARRAY_AND_FILE_OUTPUT)
                  clazz.dump(getOutputFile(clazz.getClassName()));
                else if (_outputType == BYTEARRAY_AND_JAR_OUTPUT)
                  _bcelClasses.add(clazz);

                break;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 9
Source Project: TencentKona-8   Source File: ClassSet.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean add(JavaClass clazz) {
  boolean result = false;

  if(!_map.containsKey(clazz.getClassName())) {
    result = true;
    _map.put(clazz.getClassName(), clazz);
  }

  return result;
}
 
Example 10
Source Project: openjdk-jdk8u   Source File: ObjectType.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * If "this" doesn't reference a class, it references an interface
 * or a non-existant entity.
 */
public boolean referencesClass(){
  JavaClass jc = Repository.lookupClass(class_name);
  if (jc == null)
    return false;
  else
    return jc.isClass();
}
 
Example 11
Source Project: jdk8u60   Source File: ClassSet.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean add(JavaClass clazz) {
  boolean result = false;

  if(!_map.containsKey(clazz.getClassName())) {
    result = true;
    _map.put(clazz.getClassName(), clazz);
  }

  return result;
}
 
Example 12
Source Project: jdk8u60   Source File: ObjectType.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * If "this" doesn't reference a class, it references an interface
 * or a non-existant entity.
 */
public boolean referencesClass(){
  JavaClass jc = Repository.lookupClass(class_name);
  if (jc == null)
    return false;
  else
    return jc.isClass();
}
 
Example 13
Source Project: openjdk-jdk9   Source File: ClassSet.java    License: GNU General Public License v2.0 4 votes vote down vote up
public JavaClass[] toArray() {
  Collection values = _map.values();
  JavaClass[] classes = new JavaClass[values.size()];
  values.toArray(classes);
  return classes;
}
 
Example 14
Source Project: Bytecoder   Source File: ClassStack.java    License: Apache License 2.0 4 votes vote down vote up
public JavaClass pop() {
    return stack.pop();
}
 
Example 15
Source Project: jdk1.8-source-analysis   Source File: ReferenceType.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * This commutative operation returns the first common superclass (narrowest ReferenceType
 * referencing a class, not an interface).
 * If one of the types is a superclass of the other, the former is returned.
 * If "this" is Type.NULL, then t is returned.
 * If t is Type.NULL, then "this" is returned.
 * If "this" equals t ['this.equals(t)'] "this" is returned.
 * If "this" or t is an ArrayType, then Type.OBJECT is returned.
 * If "this" or t is a ReferenceType referencing an interface, then Type.OBJECT is returned.
 * If not all of the two classes' superclasses cannot be found, "null" is returned.
 * See the JVM specification edition 2, "4.9.2 The Bytecode Verifier".
 *
 * @deprecated use getFirstCommonSuperclass(ReferenceType t) which has
 *             slightly changed semantics.
 */
public ReferenceType firstCommonSuperclass(ReferenceType t) {
  if (this.equals(Type.NULL)) return t;
  if (t.equals(Type.NULL)) return this;
  if (this.equals(t)) return this;
  /*
   * TODO: Above sounds a little arbitrary. On the other hand, there is
   * no object referenced by Type.NULL so we can also say all the objects
   * referenced by Type.NULL were derived from java.lang.Object.
   * However, the Java Language's "instanceof" operator proves us wrong:
   * "null" is not referring to an instance of java.lang.Object :)
   */

  if ((this instanceof ArrayType) || (t instanceof ArrayType))
    return Type.OBJECT;
  // TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType?

  if (((this instanceof ObjectType) && ((ObjectType) this).referencesInterface()) ||
      ((t instanceof ObjectType) && ((ObjectType) t).referencesInterface()))
    return Type.OBJECT;
  // TODO: The above line is correct comparing to the vmspec2. But one could
  // make class file verification a bit stronger here by using the notion of
  // superinterfaces or even castability or assignment compatibility.


  // this and t are ObjectTypes, see above.
  ObjectType thiz = (ObjectType) this;
  ObjectType other = (ObjectType) t;
  JavaClass[] thiz_sups = Repository.getSuperClasses(thiz.getClassName());
  JavaClass[] other_sups = Repository.getSuperClasses(other.getClassName());

  if ((thiz_sups == null) || (other_sups == null)) {
    return null;
  }

  // Waaahh...
  JavaClass[] this_sups = new JavaClass[thiz_sups.length + 1];
  JavaClass[] t_sups = new JavaClass[other_sups.length + 1];
  System.arraycopy(thiz_sups, 0, this_sups, 1, thiz_sups.length);
  System.arraycopy(other_sups, 0, t_sups, 1, other_sups.length);
  this_sups[0] = Repository.lookupClass(thiz.getClassName());
  t_sups[0] = Repository.lookupClass(other.getClassName());

  for (int i = 0; i < t_sups.length; i++) {
    for (int j = 0; j < this_sups.length; j++) {
      if (this_sups[j].equals(t_sups[i])) return new ObjectType(this_sups[j].getClassName());
    }
  }

  // Huh? Did you ask for Type.OBJECT's superclass??
  return null;
}
 
Example 16
Source Project: JDKSourceCode1.8   Source File: ClassQueue.java    License: MIT License 4 votes vote down vote up
public JavaClass dequeue()                {
  return (JavaClass)vec.removeFirst();
}
 
Example 17
Source Project: JDKSourceCode1.8   Source File: Repository.java    License: MIT License 4 votes vote down vote up
/**
 * Add clazz to repository if there isn't an equally named class already in there.
 *
 * @return old entry in repository
 */
public static JavaClass addClass(JavaClass clazz) {
  JavaClass old = _repository.findClass(clazz.getClassName());
  _repository.storeClass(clazz);
  return old;
}
 
Example 18
Source Project: jdk1.8-source-analysis   Source File: Repository.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return true, if clazz is an implementation of interface inter
 */
public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
  return clazz.implementationOf(inter);
}
 
Example 19
Source Project: openjdk-jdk9   Source File: ClassQueue.java    License: GNU General Public License v2.0 4 votes vote down vote up
public JavaClass dequeue()                {
  return (JavaClass)vec.removeFirst();
}
 
Example 20
/**
 * This commutative operation returns the first common superclass (narrowest ReferenceType
 * referencing a class, not an interface).
 * If one of the types is a superclass of the other, the former is returned.
 * If "this" is Type.NULL, then t is returned.
 * If t is Type.NULL, then "this" is returned.
 * If "this" equals t ['this.equals(t)'] "this" is returned.
 * If "this" or t is an ArrayType, then Type.OBJECT is returned.
 * If "this" or t is a ReferenceType referencing an interface, then Type.OBJECT is returned.
 * If not all of the two classes' superclasses cannot be found, "null" is returned.
 * See the JVM specification edition 2, "4.9.2 The Bytecode Verifier".
 *
 * @deprecated use getFirstCommonSuperclass(ReferenceType t) which has
 *             slightly changed semantics.
 */
public ReferenceType firstCommonSuperclass(ReferenceType t) {
  if (this.equals(Type.NULL)) return t;
  if (t.equals(Type.NULL)) return this;
  if (this.equals(t)) return this;
  /*
   * TODO: Above sounds a little arbitrary. On the other hand, there is
   * no object referenced by Type.NULL so we can also say all the objects
   * referenced by Type.NULL were derived from java.lang.Object.
   * However, the Java Language's "instanceof" operator proves us wrong:
   * "null" is not referring to an instance of java.lang.Object :)
   */

  if ((this instanceof ArrayType) || (t instanceof ArrayType))
    return Type.OBJECT;
  // TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType?

  if (((this instanceof ObjectType) && ((ObjectType) this).referencesInterface()) ||
      ((t instanceof ObjectType) && ((ObjectType) t).referencesInterface()))
    return Type.OBJECT;
  // TODO: The above line is correct comparing to the vmspec2. But one could
  // make class file verification a bit stronger here by using the notion of
  // superinterfaces or even castability or assignment compatibility.


  // this and t are ObjectTypes, see above.
  ObjectType thiz = (ObjectType) this;
  ObjectType other = (ObjectType) t;
  JavaClass[] thiz_sups = Repository.getSuperClasses(thiz.getClassName());
  JavaClass[] other_sups = Repository.getSuperClasses(other.getClassName());

  if ((thiz_sups == null) || (other_sups == null)) {
    return null;
  }

  // Waaahh...
  JavaClass[] this_sups = new JavaClass[thiz_sups.length + 1];
  JavaClass[] t_sups = new JavaClass[other_sups.length + 1];
  System.arraycopy(thiz_sups, 0, this_sups, 1, thiz_sups.length);
  System.arraycopy(other_sups, 0, t_sups, 1, other_sups.length);
  this_sups[0] = Repository.lookupClass(thiz.getClassName());
  t_sups[0] = Repository.lookupClass(other.getClassName());

  for (int i = 0; i < t_sups.length; i++) {
    for (int j = 0; j < this_sups.length; j++) {
      if (this_sups[j].equals(t_sups[i])) return new ObjectType(this_sups[j].getClassName());
    }
  }

  // Huh? Did you ask for Type.OBJECT's superclass??
  return null;
}
 
Example 21
Source Project: jdk8u60   Source File: Repository.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add clazz to repository if there isn't an equally named class already in there.
 *
 * @return old entry in repository
 */
public static JavaClass addClass(JavaClass clazz) {
  JavaClass old = _repository.findClass(clazz.getClassName());
  _repository.storeClass(clazz);
  return old;
}
 
Example 22
Source Project: openjdk-8   Source File: Repository.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Remove given class from repository.
 */
public static void removeClass(JavaClass clazz) {
  _repository.removeClass(clazz);
}
 
Example 23
Source Project: openjdk-jdk8u   Source File: ReferenceType.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * This commutative operation returns the first common superclass (narrowest ReferenceType
 * referencing a class, not an interface).
 * If one of the types is a superclass of the other, the former is returned.
 * If "this" is Type.NULL, then t is returned.
 * If t is Type.NULL, then "this" is returned.
 * If "this" equals t ['this.equals(t)'] "this" is returned.
 * If "this" or t is an ArrayType, then Type.OBJECT is returned.
 * If "this" or t is a ReferenceType referencing an interface, then Type.OBJECT is returned.
 * If not all of the two classes' superclasses cannot be found, "null" is returned.
 * See the JVM specification edition 2, "4.9.2 The Bytecode Verifier".
 *
 * @deprecated use getFirstCommonSuperclass(ReferenceType t) which has
 *             slightly changed semantics.
 */
public ReferenceType firstCommonSuperclass(ReferenceType t) {
  if (this.equals(Type.NULL)) return t;
  if (t.equals(Type.NULL)) return this;
  if (this.equals(t)) return this;
  /*
   * TODO: Above sounds a little arbitrary. On the other hand, there is
   * no object referenced by Type.NULL so we can also say all the objects
   * referenced by Type.NULL were derived from java.lang.Object.
   * However, the Java Language's "instanceof" operator proves us wrong:
   * "null" is not referring to an instance of java.lang.Object :)
   */

  if ((this instanceof ArrayType) || (t instanceof ArrayType))
    return Type.OBJECT;
  // TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType?

  if (((this instanceof ObjectType) && ((ObjectType) this).referencesInterface()) ||
      ((t instanceof ObjectType) && ((ObjectType) t).referencesInterface()))
    return Type.OBJECT;
  // TODO: The above line is correct comparing to the vmspec2. But one could
  // make class file verification a bit stronger here by using the notion of
  // superinterfaces or even castability or assignment compatibility.


  // this and t are ObjectTypes, see above.
  ObjectType thiz = (ObjectType) this;
  ObjectType other = (ObjectType) t;
  JavaClass[] thiz_sups = Repository.getSuperClasses(thiz.getClassName());
  JavaClass[] other_sups = Repository.getSuperClasses(other.getClassName());

  if ((thiz_sups == null) || (other_sups == null)) {
    return null;
  }

  // Waaahh...
  JavaClass[] this_sups = new JavaClass[thiz_sups.length + 1];
  JavaClass[] t_sups = new JavaClass[other_sups.length + 1];
  System.arraycopy(thiz_sups, 0, this_sups, 1, thiz_sups.length);
  System.arraycopy(other_sups, 0, t_sups, 1, other_sups.length);
  this_sups[0] = Repository.lookupClass(thiz.getClassName());
  t_sups[0] = Repository.lookupClass(other.getClassName());

  for (int i = 0; i < t_sups.length; i++) {
    for (int j = 0; j < this_sups.length; j++) {
      if (this_sups[j].equals(t_sups[i])) return new ObjectType(this_sups[j].getClassName());
    }
  }

  // Huh? Did you ask for Type.OBJECT's superclass??
  return null;
}
 
Example 24
Source Project: jdk1.8-source-analysis   Source File: ClassQueue.java    License: Apache License 2.0 4 votes vote down vote up
public JavaClass dequeue()                {
  return (JavaClass)vec.removeFirst();
}
 
Example 25
/**
 * Add clazz to repository if there isn't an equally named class already in there.
 *
 * @return old entry in repository
 */
public static JavaClass addClass(JavaClass clazz) {
  JavaClass old = _repository.findClass(clazz.getClassName());
  _repository.storeClass(clazz);
  return old;
}
 
Example 26
Source Project: openjdk-8-source   Source File: ClassSet.java    License: GNU General Public License v2.0 4 votes vote down vote up
public JavaClass[] toArray() {
  Collection values = _map.values();
  JavaClass[] classes = new JavaClass[values.size()];
  values.toArray(classes);
  return classes;
}
 
Example 27
Source Project: Bytecoder   Source File: ClassStack.java    License: Apache License 2.0 4 votes vote down vote up
public void push( final JavaClass clazz ) {
    stack.push(clazz);
}
 
Example 28
Source Project: Bytecoder   Source File: ClassSet.java    License: Apache License 2.0 4 votes vote down vote up
public JavaClass[] toArray() {
    final Collection<JavaClass> values = map.values();
    final JavaClass[] classes = new JavaClass[values.size()];
    values.toArray(classes);
    return classes;
}
 
Example 29
Source Project: Bytecoder   Source File: Repository.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Add clazz to repository if there isn't an equally named class already in there.
 *
 * @return old entry in repository
 */
public static JavaClass addClass( final JavaClass clazz ) {
    final JavaClass old = repository.findClass(clazz.getClassName());
    repository.storeClass(clazz);
    return old;
}
 
Example 30
/**
 * Remove given class from repository.
 */
public static void removeClass(JavaClass clazz) {
  _repository.removeClass(clazz);
}