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

The following examples show how to use com.sun.org.apache.bcel.internal.classfile.ConstantPool. 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 File: ConstantHTML.java    From Bytecoder with 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 #2
Source File: ClassGen.java    From Bytecoder with 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 #3
Source File: FieldOrMethod.java    From Bytecoder with 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 #4
Source File: CodeHTML.java    From Bytecoder with 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 #5
Source File: AttributeHTML.java    From Bytecoder with 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 #6
Source File: Bug8003147Test.java    From openjdk-jdk9 with 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 #7
Source File: CPInstruction.java    From Bytecoder with 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 #8
Source File: CPInstruction.java    From Bytecoder with 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 File: FieldOrMethod.java    From Bytecoder with 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 #10
Source File: FieldOrMethod.java    From Bytecoder with 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 #11
Source File: FieldOrMethod.java    From Bytecoder with 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 #12
Source File: InvokeInstruction.java    From Bytecoder with 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 #13
Source File: MULTIANEWARRAY.java    From openjdk-8-source with 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 #14
Source File: FieldInstruction.java    From hottub with 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 #15
Source File: Instruction.java    From openjdk-8-source with 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 #16
Source File: INVOKEINTERFACE.java    From openjdk-8-source with 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 #17
Source File: INVOKEINTERFACE.java    From hottub with 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 #18
Source File: MULTIANEWARRAY.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
@Override
public String toString( final ConstantPool cp ) {
    return super.toString(cp) + " " + dimensions;
}
 
Example #19
Source File: INVOKEINTERFACE.java    From openjdk-jdk9 with 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 #20
Source File: MULTIANEWARRAY.java    From openjdk-jdk9 with 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 File: Instruction.java    From openjdk-jdk9 with 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 #22
Source File: FieldInstruction.java    From openjdk-8 with 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 #23
Source File: Instruction.java    From openjdk-8 with 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 #24
Source File: INVOKEINTERFACE.java    From openjdk-8 with 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 #25
Source File: MULTIANEWARRAY.java    From openjdk-8 with 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 #26
Source File: FieldInstruction.java    From openjdk-8-source with 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 #27
Source File: INVOKEDYNAMIC.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * @return mnemonic for instruction with symbolic references resolved
 */
@Override
public String toString( final ConstantPool cp ) {
    return super.toString(cp);
}
 
Example #28
Source File: NameSignatureInstruction.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/** @return name of referenced method/field.
 */
public String getName(final ConstantPoolGen cpg) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantNameAndType cnat = getNameAndType(cpg);
    return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
}
 
Example #29
Source File: NameSignatureInstruction.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/** @return signature of referenced method/field.
 */
public String getSignature(final ConstantPoolGen cpg) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantNameAndType cnat = getNameAndType(cpg);
    return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
}
 
Example #30
Source File: NameSignatureInstruction.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public ConstantNameAndType getNameAndType(final ConstantPoolGen cpg) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
    return  (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
}