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

The following examples show how to use com.sun.org.apache.bcel.internal.classfile.ConstantPool. 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: ClassGen.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @return the (finally) built up Java class object.
 */
public JavaClass getJavaClass() {
    final int[] interfaces = getInterfaces();
    final Field[] fields = getFields();
    final Method[] methods = getMethods();
    Attribute[] attributes = null;
    if (annotation_vec.isEmpty()) {
        attributes = getAttributes();
    } else {
        // TODO: Sometime later, trash any attributes called 'RuntimeVisibleAnnotations' or 'RuntimeInvisibleAnnotations'
        final Attribute[] annAttributes  = AnnotationEntryGen.getAnnotationAttributes(cp, getAnnotationEntries());
        attributes = new Attribute[attribute_vec.size()+annAttributes.length];
        attribute_vec.toArray(attributes);
        System.arraycopy(annAttributes,0,attributes,attribute_vec.size(),annAttributes.length);
    }
    // Must be last since the above calls may still add something to it
    final ConstantPool _cp = this.cp.getFinalConstantPool();
    return new JavaClass(class_name_index, superclass_name_index, file_name, major, minor,
            super.getAccessFlags(), _cp, interfaces, fields, methods, attributes);
}
 
Example 2
Source Project: Bytecoder   Source File: ConstantHTML.java    License: Apache License 2.0 6 votes vote down vote up
ConstantHTML(final String dir, final String class_name, final String class_package, final Method[] methods,
        final ConstantPool constant_pool) throws IOException {
    this.class_name = class_name;
    this.class_package = class_package;
    this.constant_pool = constant_pool;
    this.methods = methods;
    constants = constant_pool.getConstantPool();
    file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
    constant_ref = new String[constants.length];
    constant_ref[0] = "<unknown>";
    file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
    // Loop through constants, constants[0] is reserved
    for (int i = 1; i < constants.length; i++) {
        if (i % 2 == 0) {
            file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
        } else {
            file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
        }
        if (constants[i] != null) {
            writeConstant(i);
        }
        file.print("</TD></TR>\n");
    }
    file.println("</TABLE></BODY></HTML>");
    file.close();
}
 
Example 3
Source Project: Bytecoder   Source File: InvokeInstruction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
@Override
public String toString( final ConstantPool cp ) {
    final Constant c = cp.getConstant(super.getIndex());
    final StringTokenizer tok = new StringTokenizer(cp.constantToString(c));
    return Const.getOpcodeName(super.getOpcode()) + " " + tok.nextToken().replace('.', '/')
            + tok.nextToken();
}
 
Example 4
Source Project: Bytecoder   Source File: FieldOrMethod.java    License: Apache License 2.0 5 votes vote down vote up
/** @return signature of referenced method/field.
 */
public String getSignature( final ConstantPoolGen cpg ) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
    final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
    return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
}
 
Example 5
Source Project: Bytecoder   Source File: FieldOrMethod.java    License: Apache License 2.0 5 votes vote down vote up
/** @return name of referenced method/field.
 */
public String getName( final ConstantPoolGen cpg ) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
    final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
    return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
}
 
Example 6
Source Project: Bytecoder   Source File: FieldOrMethod.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return name of the referenced class/interface
 * @deprecated If the instruction references an array class,
 *    this method will return "java.lang.Object".
 *    For code generated by Java 1.5, this answer is
 *    sometimes wrong (e.g., if the "clone()" method is
 *    called on an array).  A better idea is to use
 *    the {@link #getReferenceType(ConstantPoolGen)} method, which correctly distinguishes
 *    between class types and array types.
 *
 */
@Deprecated
public String getClassName( final ConstantPoolGen cpg ) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
    final String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
    if (className.startsWith("[")) {
        // Turn array classes into java.lang.Object.
        return "java.lang.Object";
    }
    return className.replace('/', '.');
}
 
Example 7
Source Project: Bytecoder   Source File: FieldOrMethod.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the reference type representing the class, interface,
 * or array class referenced by the instruction.
 * @param cpg the ConstantPoolGen used to create the instruction
 * @return an ObjectType (if the referenced class type is a class
 *   or interface), or an ArrayType (if the referenced class
 *   type is an array class)
 */
public ReferenceType getReferenceType( final ConstantPoolGen cpg ) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
    String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
    if (className.startsWith("[")) {
        return (ArrayType) Type.getType(className);
    }
    className = className.replace('/', '.');
    return ObjectType.getInstance(className);
}
 
Example 8
Source Project: Bytecoder   Source File: CPInstruction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
@Override
public String toString( final ConstantPool cp ) {
    final Constant c = cp.getConstant(index);
    String str = cp.constantToString(c);
    if (c instanceof ConstantClass) {
        str = str.replace('.', '/');
    }
    return com.sun.org.apache.bcel.internal.Const.getOpcodeName(super.getOpcode()) + " " + str;
}
 
Example 9
Source Project: Bytecoder   Source File: CPInstruction.java    License: Apache License 2.0 5 votes vote down vote up
/** @return type related with this instruction.
 */
@Override
public Type getType( final ConstantPoolGen cpg ) {
    final ConstantPool cp = cpg.getConstantPool();
    String name = cp.getConstantString(index, com.sun.org.apache.bcel.internal.Const.CONSTANT_Class);
    if (!name.startsWith("[")) {
        name = "L" + name + ";";
    }
    return Type.getType(name);
}
 
Example 10
Source Project: Bytecoder   Source File: AttributeHTML.java    License: Apache License 2.0 5 votes vote down vote up
AttributeHTML(final String dir, final String class_name, final ConstantPool constant_pool,
        final ConstantHTML constant_html) throws IOException {
    this.class_name = class_name;
    this.constant_pool = constant_pool;
    this.constant_html = constant_html;
    file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html"));
    file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
}
 
Example 11
Source Project: Bytecoder   Source File: CodeHTML.java    License: Apache License 2.0 5 votes vote down vote up
CodeHTML(final String dir, final String class_name, final Method[] methods, final ConstantPool constant_pool,
            final ConstantHTML constant_html) throws IOException {
        this.class_name = class_name;
//        this.methods = methods;
        this.constant_pool = constant_pool;
        this.constant_html = constant_html;
        file = new PrintWriter(new FileOutputStream(dir + class_name + "_code.html"));
        file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\">");
        for (int i = 0; i < methods.length; i++) {
            writeMethod(methods[i], i);
        }
        file.println("</BODY></HTML>");
        file.close();
    }
 
Example 12
Source Project: openjdk-jdk9   Source File: Bug8003147Test.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    // Note: com.sun.org.apache.bcel.internal.classfile.JavaClass doesn't
    // support InvokeDynamic, so can't use lambda, also can't use string1 +
    // string2, because javac will generate a dynamic call where invoking
    // string1.concat(string2), so create a separate Bug8003147TestClass
    JAXPTestUtilities.tryRunWithTmpPermission(() -> {
        String classfile = getSystemProperty("test.classes") + "/parsers/Bug8003147TestClass.class";
        JavaClass jc = new ClassParser(classfile).parse();

        // rename class
        ConstantPool cp = jc.getConstantPool();
        int cpIndex = ((ConstantClass) cp.getConstant(jc.getClassNameIndex())).getNameIndex();
        cp.setConstant(cpIndex, new ConstantUtf8("parsers/Bug8003147TestClassPrime"));
        ClassGen gen = new ClassGen(jc);
        Method[] methods = jc.getMethods();
        int index;
        for (index = 0; index < methods.length; index++) {
            if (methods[index].getName().equals("doSomething")) {
                break;
            }
        }
        Method m = methods[index];
        MethodGen mg = new MethodGen(m, gen.getClassName(), gen.getConstantPool());
        gen.replaceMethod(m, mg.getMethod());
        String path = classfile.replace("Bug8003147TestClass", "Bug8003147TestClassPrime");
        gen.getJavaClass().dump(new FileOutputStream(path));

        try {
            Class.forName("parsers.Bug8003147TestClassPrime");
        } catch (ClassFormatError cfe) {
            cfe.printStackTrace();
            Assert.fail("modified version of class does not pass verification");
        }
    }, new FilePermission(getSystemProperty("test.classes") + "/-", "read,write"));
}
 
Example 13
Source Project: jdk1.8-source-analysis   Source File: FieldInstruction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return com.sun.org.apache.bcel.internal.Constants.OPCODE_NAMES[opcode] + " " +
    cp.constantToString(index, com.sun.org.apache.bcel.internal.Constants.CONSTANT_Fieldref);
}
 
Example 14
Source Project: jdk1.8-source-analysis   Source File: Instruction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with sumbolic references resolved
 */
public String toString(ConstantPool cp) {
  return toString(false);
}
 
Example 15
Source Project: jdk1.8-source-analysis   Source File: INVOKEINTERFACE.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return super.toString(cp) + " " + nargs;
}
 
Example 16
Source Project: jdk1.8-source-analysis   Source File: MULTIANEWARRAY.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return super.toString(cp) + " " + dimensions;
}
 
Example 17
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return com.sun.org.apache.bcel.internal.Constants.OPCODE_NAMES[opcode] + " " +
    cp.constantToString(index, com.sun.org.apache.bcel.internal.Constants.CONSTANT_Fieldref);
}
 
Example 18
Source Project: TencentKona-8   Source File: Instruction.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with sumbolic references resolved
 */
public String toString(ConstantPool cp) {
  return toString(false);
}
 
Example 19
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return super.toString(cp) + " " + nargs;
}
 
Example 20
Source Project: TencentKona-8   Source File: MULTIANEWARRAY.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return super.toString(cp) + " " + dimensions;
}
 
Example 21
Source Project: jdk8u60   Source File: FieldInstruction.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return com.sun.org.apache.bcel.internal.Constants.OPCODE_NAMES[opcode] + " " +
    cp.constantToString(index, com.sun.org.apache.bcel.internal.Constants.CONSTANT_Fieldref);
}
 
Example 22
Source Project: jdk8u60   Source File: Instruction.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with sumbolic references resolved
 */
public String toString(ConstantPool cp) {
  return toString(false);
}
 
Example 23
Source Project: jdk8u60   Source File: INVOKEINTERFACE.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return super.toString(cp) + " " + nargs;
}
 
Example 24
Source Project: jdk8u60   Source File: MULTIANEWARRAY.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return super.toString(cp) + " " + dimensions;
}
 
Example 25
Source Project: JDKSourceCode1.8   Source File: FieldInstruction.java    License: MIT License 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return com.sun.org.apache.bcel.internal.Constants.OPCODE_NAMES[opcode] + " " +
    cp.constantToString(index, com.sun.org.apache.bcel.internal.Constants.CONSTANT_Fieldref);
}
 
Example 26
Source Project: JDKSourceCode1.8   Source File: Instruction.java    License: MIT License 4 votes vote down vote up
/**
 * @return mnemonic for instruction with sumbolic references resolved
 */
public String toString(ConstantPool cp) {
  return toString(false);
}
 
Example 27
Source Project: JDKSourceCode1.8   Source File: INVOKEINTERFACE.java    License: MIT License 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return super.toString(cp) + " " + nargs;
}
 
Example 28
Source Project: JDKSourceCode1.8   Source File: MULTIANEWARRAY.java    License: MIT License 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return super.toString(cp) + " " + dimensions;
}
 
Example 29
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
public String toString(ConstantPool cp) {
  return com.sun.org.apache.bcel.internal.Constants.OPCODE_NAMES[opcode] + " " +
    cp.constantToString(index, com.sun.org.apache.bcel.internal.Constants.CONSTANT_Fieldref);
}
 
Example 30
Source Project: openjdk-jdk8u   Source File: Instruction.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with sumbolic references resolved
 */
public String toString(ConstantPool cp) {
  return toString(false);
}