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

The following examples show how to use com.sun.org.apache.bcel.internal.classfile.Method. 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: MethodHTML.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
MethodHTML(final String dir, final String class_name, final Method[] methods, final Field[] fields,
        final ConstantHTML constant_html, final AttributeHTML attribute_html) throws IOException {
    this.class_name = class_name;
    this.attribute_html = attribute_html;
    this.constant_html = constant_html;
    file = new PrintWriter(new FileOutputStream(dir + class_name + "_methods.html"));
    file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
    file.println("<TR><TH ALIGN=LEFT>Access&nbsp;flags</TH><TH ALIGN=LEFT>Type</TH>"
            + "<TH ALIGN=LEFT>Field&nbsp;name</TH></TR>");
    for (final Field field : fields) {
        writeField(field);
    }
    file.println("</TABLE>");
    file.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access&nbsp;flags</TH>"
            + "<TH ALIGN=LEFT>Return&nbsp;type</TH><TH ALIGN=LEFT>Method&nbsp;name</TH>"
            + "<TH ALIGN=LEFT>Arguments</TH></TR>");
    for (int i = 0; i < methods.length; i++) {
        writeMethod(methods[i], i);
    }
    file.println("</TABLE></BODY></HTML>");
    file.close();
}
 
Example #2
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] = "&lt;unknown&gt;";
    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 File: BCELifier.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public void visitMethod( final Method method ) {
    final MethodGen mg = new MethodGen(method, _clazz.getClassName(), _cp);
    _out.println("    InstructionList il = new InstructionList();");
    _out.println("    MethodGen method = new MethodGen("
            + printFlags(method.getAccessFlags(), FLAGS.METHOD) + ", "
            + printType(mg.getReturnType()) + ", "
            + printArgumentTypes(mg.getArgumentTypes()) + ", "
            + "new String[] { " + Utility.printArray(mg.getArgumentNames(), false, true)
            + " }, \"" + method.getName() + "\", \"" + _clazz.getClassName() + "\", il, _cp);");
    _out.println();
    final BCELFactory factory = new BCELFactory(mg, _out);
    factory.start();
    _out.println("    method.setMaxStack();");
    _out.println("    method.setMaxLocals();");
    _out.println("    _cg.addMethod(method.getMethod());");
    _out.println("    il.dispose();");
}
 
Example #4
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 #5
Source File: MethodGenerator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #6
Source File: MethodGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #7
Source File: MethodGenerator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #8
Source File: MethodGenerator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #9
Source File: MethodGenerator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #10
Source File: MethodGenerator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #11
Source File: MethodGenerator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #12
Source File: MethodGenerator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #13
Source File: MethodGenerator.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #14
Source File: MethodGenerator.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #15
Source File: MethodGenerator.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #16
Source File: MethodGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #17
Source File: MethodGenerator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #18
Source File: MethodGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #19
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 #20
Source File: MethodGenerator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #21
Source File: MethodGenerator.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #22
Source File: MethodGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected Method getThisMethod() {
    stripAttributes(true);
    setMaxLocals();
    setMaxStack();
    removeNOPs();

    return getMethod();
}
 
Example #23
Source File: MethodGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** @return deep copy of this method
 */
public MethodGen copy( final String class_name, final ConstantPoolGen cp ) {
    final Method m = ((MethodGen) clone()).getMethod();
    final MethodGen mg = new MethodGen(m, class_name, super.getConstantPool());
    if (super.getConstantPool() != cp) {
        mg.setConstantPool(cp);
        mg.getInstructionList().replaceConstantPool(super.getConstantPool(), cp);
    }
    return mg;
}
 
Example #24
Source File: ClassGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize with existing class.
 * @param clazz JavaClass object (e.g. read from file)
 */
public ClassGen(final JavaClass clazz) {
    super(clazz.getAccessFlags());
    class_name_index = clazz.getClassNameIndex();
    superclass_name_index = clazz.getSuperclassNameIndex();
    class_name = clazz.getClassName();
    super_class_name = clazz.getSuperclassName();
    file_name = clazz.getSourceFileName();
    cp = new ConstantPoolGen(clazz.getConstantPool());
    major = clazz.getMajor();
    minor = clazz.getMinor();
    final Attribute[] attributes = clazz.getAttributes();
    // J5TODO: Could make unpacking lazy, done on first reference
    final AnnotationEntryGen[] annotations = unpackAnnotations(attributes);
    final Method[] methods = clazz.getMethods();
    final Field[] fields = clazz.getFields();
    final String[] interfaces = clazz.getInterfaceNames();
    for (final String interface1 : interfaces) {
        addInterface(interface1);
    }
    for (final Attribute attribute : attributes) {
        if (!(attribute instanceof Annotations)) {
            addAttribute(attribute);
        }
    }
    for (final AnnotationEntryGen annotation : annotations) {
        addAnnotationEntry(annotation);
    }
    for (final Method method : methods) {
        addMethod(method);
    }
    for (final Field field : fields) {
        addField(field);
    }
}
 
Example #25
Source File: MethodGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Get all {@link Method}s generated by this {@link MethodGenerator}.
 * The {@link MethodGen#getMethod()} only returns a single
 * <code>Method</code> object.  This method takes into account the Java
 * Virtual Machine Specification limit of 64KB on the size of a method, and
 * may return more than one <code>Method</code>.</p>
 * <p>If the code associated with the <code>MethodGenerator</code> would
 * exceed the 64KB limit, this method will attempt to split the code in
 * the {@link InstructionList} associated with this
 * <code>MethodGenerator</code> into several methods.</p>
 * @param classGen the {@link ClassGenerator} of which these methods are
 *                 members
 * @return an array of all the <code>Method</code>s generated
 */
Method[] getGeneratedMethods(ClassGenerator classGen) {
    Method[] generatedMethods;
    InstructionList il = getInstructionList();
    InstructionHandle last = il.getEnd();

    il.setPositions();

    int instructionListSize =
                last.getPosition() + last.getInstruction().getLength();

    // Need to look for any branch target offsets that exceed the range
    // [-32768,32767]
    if (instructionListSize > MAX_BRANCH_TARGET_OFFSET) {
        boolean ilChanged = widenConditionalBranchTargetOffsets();

        // If any branch instructions needed widening, recompute the size
        // of the byte code for the method
        if (ilChanged) {
            il.setPositions();
            last = il.getEnd();
            instructionListSize =
                    last.getPosition() + last.getInstruction().getLength();
        }
    }

    if (instructionListSize > MAX_METHOD_SIZE) {
        generatedMethods = outlineChunks(classGen, instructionListSize);
    } else {
        generatedMethods = new Method[] {getThisMethod()};
    }
    return generatedMethods;
}
 
Example #26
Source File: ClassGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** @return method object with given name and signature, or null
 */
public Method containsMethod( final String name, final String signature ) {
    for (final Method m : method_vec) {
        if (m.getName().equals(name) && m.getSignature().equals(signature)) {
            return m;
        }
    }
    return null;
}
 
Example #27
Source File: ClassGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** Replace given method with new one. If the old one does not exist
 * add the new_ method to the class anyway.
 */
public void replaceMethod( final Method old, final Method new_ ) {
    if (new_ == null) {
        throw new ClassGenException("Replacement method must not be null");
    }
    final int i = method_vec.indexOf(old);
    if (i < 0) {
        method_vec.add(new_);
    } else {
        method_vec.set(i, new_);
    }
}
 
Example #28
Source File: BCELifier.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void printCreate() {
    _out.println("  public void create(OutputStream out) throws IOException {");
    final Field[] fields = _clazz.getFields();
    if (fields.length > 0) {
        _out.println("    createFields();");
    }
    final Method[] methods = _clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        _out.println("    createMethod_" + i + "();");
    }
    _out.println("    _cg.getJavaClass().dump(out);");
    _out.println("  }");
    _out.println();
}
 
Example #29
Source File: Class2HTML.java    From Bytecoder with 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 #30
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();
    }