com.sun.tools.classfile.Attribute Java Examples

The following examples show how to use com.sun.tools.classfile.Attribute. 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: openjdk-8   Author: bpupadhyaya   File: DeadCodeGeneratedForEmptyTryTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
void checkClassFile(final Path path) throws Exception {
    ClassFile classFile = ClassFile.read(
            new BufferedInputStream(Files.newInputStream(path)));
    constantPool = classFile.constant_pool;
    utf8Index = constantPool.getUTF8Index("STR_TO_LOOK_FOR");
    for (Method method: classFile.methods) {
        if (method.getName(constantPool).equals("methodToLookFor")) {
            Code_attribute codeAtt = (Code_attribute)method.attributes.get(Attribute.Code);
            for (Instruction inst: codeAtt.getInstructions()) {
                inst.accept(codeVisitor, null);
            }
        }
    }
    Assert.check(numberOfRefToStr == 1,
            "There should only be one reference to a CONSTANT_String_info structure in the generated code");
}
 
Example #2
Source Project: openjdk-8-source   Author: keerath   File: SourceWriter.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void setLineMap(Code_attribute attr) {
    SortedMap<Integer, SortedSet<Integer>> map =
            new TreeMap<Integer, SortedSet<Integer>>();
    SortedSet<Integer> allLines = new TreeSet<Integer>();
    for (Attribute a: attr.attributes) {
        if (a instanceof LineNumberTable_attribute) {
            LineNumberTable_attribute t = (LineNumberTable_attribute) a;
            for (LineNumberTable_attribute.Entry e: t.line_number_table) {
                int start_pc = e.start_pc;
                int line = e.line_number;
                SortedSet<Integer> pcLines = map.get(start_pc);
                if (pcLines == null) {
                    pcLines = new TreeSet<Integer>();
                    map.put(start_pc, pcLines);
                }
                pcLines.add(line);
                allLines.add(line);
            }
        }
    }
    lineMap = map;
    lineList = new ArrayList<Integer>(allLines);
}
 
Example #3
Source Project: TencentKona-8   Author: Tencent   File: LVTHarness.java    License: GNU General Public License v2.0 6 votes vote down vote up
void checkMethod(ConstantPool constantPool, Method method, AliveRanges ranges)
        throws InvalidIndex, UnexpectedEntry, ConstantPoolException {
    Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
    LocalVariableTable_attribute lvt =
        (LocalVariableTable_attribute) (code.attributes.get(Attribute.LocalVariableTable));
    List<String> infoFromRanges = convertToStringList(ranges);
    List<String> infoFromLVT = convertToStringList(constantPool, lvt);

    // infoFromRanges most be contained in infoFromLVT
    int i = 0;
    int j = 0;
    while (i < infoFromRanges.size() && j < infoFromLVT.size()) {
        int comparison = infoFromRanges.get(i).compareTo(infoFromLVT.get(j));
        if (comparison == 0) {
            i++; j++;
        } else if (comparison > 0) {
            j++;
        } else {
            break;
        }
    }

    if (i < infoFromRanges.size()) {
        error(infoFromLVT, infoFromRanges, method.getName(constantPool).toString());
    }
}
 
Example #4
Source Project: openjdk-8-source   Author: keerath   File: AttributeWriter.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Void visitBootstrapMethods(BootstrapMethods_attribute attr, Void p) {
    println(Attribute.BootstrapMethods + ":");
    for (int i = 0; i < attr.bootstrap_method_specifiers.length ; i++) {
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm = attr.bootstrap_method_specifiers[i];
        indent(+1);
        print(i + ": #" + bsm.bootstrap_method_ref + " ");
        println(constantWriter.stringValue(bsm.bootstrap_method_ref));
        indent(+1);
        println("Method arguments:");
        indent(+1);
        for (int j = 0; j < bsm.bootstrap_arguments.length; j++) {
            print("#" + bsm.bootstrap_arguments[j] + " ");
            println(constantWriter.stringValue(bsm.bootstrap_arguments[j]));
        }
        indent(-3);
    }
    return null;
}
 
Example #5
Source Project: hottub   Author: dsrg-uoft   File: LVTHarness.java    License: GNU General Public License v2.0 6 votes vote down vote up
void checkMethod(ConstantPool constantPool, Method method, AliveRanges ranges)
        throws InvalidIndex, UnexpectedEntry, ConstantPoolException {
    Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
    LocalVariableTable_attribute lvt =
        (LocalVariableTable_attribute) (code.attributes.get(Attribute.LocalVariableTable));
    List<String> infoFromRanges = convertToStringList(ranges);
    List<String> infoFromLVT = convertToStringList(constantPool, lvt);

    // infoFromRanges most be contained in infoFromLVT
    int i = 0;
    int j = 0;
    while (i < infoFromRanges.size() && j < infoFromLVT.size()) {
        int comparison = infoFromRanges.get(i).compareTo(infoFromLVT.get(j));
        if (comparison == 0) {
            i++; j++;
        } else if (comparison > 0) {
            j++;
        } else {
            break;
        }
    }

    if (i < infoFromRanges.size()) {
        error(infoFromLVT, infoFromRanges, method.getName(constantPool).toString());
    }
}
 
Example #6
Source Project: TencentKona-8   Author: Tencent   File: TypeAnnotationPropagationTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void run() throws Exception {
    ClassFile cf = getClassFile("TypeAnnotationPropagationTest$Test.class");

    Method f = null;
    for (Method m : cf.methods) {
        if (m.getName(cf.constant_pool).contains("f")) {
            f = m;
            break;
        }
    }

    int idx = f.attributes.getIndex(cf.constant_pool, Attribute.Code);
    Code_attribute cattr = (Code_attribute) f.attributes.get(idx);
    idx = cattr.attributes.getIndex(cf.constant_pool, Attribute.RuntimeVisibleTypeAnnotations);
    RuntimeVisibleTypeAnnotations_attribute attr =
            (RuntimeVisibleTypeAnnotations_attribute) cattr.attributes.get(idx);

    TypeAnnotation anno = attr.annotations[0];
    assertEquals(anno.position.lvarOffset, new int[] {3}, "start_pc");
    assertEquals(anno.position.lvarLength, new int[] {8}, "length");
    assertEquals(anno.position.lvarIndex, new int[] {1}, "index");
}
 
Example #7
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: DeadCodeGeneratedForEmptyTryTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
void checkClassFile(final Path path) throws Exception {
    ClassFile classFile = ClassFile.read(
            new BufferedInputStream(Files.newInputStream(path)));
    constantPool = classFile.constant_pool;
    utf8Index = constantPool.getUTF8Index("STR_TO_LOOK_FOR");
    for (Method method: classFile.methods) {
        if (method.getName(constantPool).equals("methodToLookFor")) {
            Code_attribute codeAtt = (Code_attribute)method.attributes.get(Attribute.Code);
            for (Instruction inst: codeAtt.getInstructions()) {
                inst.accept(codeVisitor, null);
            }
        }
    }
    Assert.check(numberOfRefToStr == 1,
            "There should only be one reference to a CONSTANT_String_info structure in the generated code");
}
 
Example #8
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: TestDirectSuperInterfaceInvoke.java    License: GNU General Public License v2.0 6 votes vote down vote up
void verifyDefaultBody(String classFile) {
    String workDir = System.getProperty("test.classes");
    File file = new File(workDir, classFile);
    try {
        final ClassFile cf = ClassFile.read(file);
        for (Method m : cf.methods) {
            Code_attribute codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
            for (Instruction instr : codeAttr.getInstructions()) {
                if (instr.getOpcode() == Opcode.INVOKESPECIAL) {
                    int pc_index = instr.getShort(1);
                    CPRefInfo ref = (CPRefInfo)cf.constant_pool.get(pc_index);
                    String className = ref.getClassName();
                    if (className.equals("BaseInterface"))
                        throw new IllegalStateException("Must not directly refer to TestedInterface");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + file +": " + e);
    }
}
 
Example #9
Source Project: TencentKona-8   Author: Tencent   File: LambdaAsm.java    License: GNU General Public License v2.0 6 votes vote down vote up
static int checkMethod(ClassFile cf, String mthd) throws Exception {
    if (cf.major_version < 52) {
        throw new RuntimeException("unexpected class file version, in "
                + cf.getName() + "expected 52, got " + cf.major_version);
    }
    int count = 0;
    for (Method m : cf.methods) {
        String mname = m.getName(cf.constant_pool);
        if (mname.equals(mthd)) {
            for (Attribute a : m.attributes) {
                if ("Code".equals(a.getName(cf.constant_pool))) {
                    count++;
                    checkMethod(cf.getName(), mname, cf.constant_pool,
                            (Code_attribute) a);
                }
            }
        }
    }
    return count;
}
 
Example #10
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: LambdaAsm.java    License: GNU General Public License v2.0 6 votes vote down vote up
static int checkMethod(ClassFile cf, String mthd) throws Exception {
    if (cf.major_version < 52) {
        throw new RuntimeException("unexpected class file version, in "
                + cf.getName() + "expected 52, got " + cf.major_version);
    }
    int count = 0;
    for (Method m : cf.methods) {
        String mname = m.getName(cf.constant_pool);
        if (mname.equals(mthd)) {
            for (Attribute a : m.attributes) {
                if ("Code".equals(a.getName(cf.constant_pool))) {
                    count++;
                    checkMethod(cf.getName(), mname, cf.constant_pool,
                            (Code_attribute) a);
                }
            }
        }
    }
    return count;
}
 
Example #11
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AttributeWriter.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitBootstrapMethods(BootstrapMethods_attribute attr, Void p) {
    println(Attribute.BootstrapMethods + ":");
    for (int i = 0; i < attr.bootstrap_method_specifiers.length ; i++) {
        BootstrapMethods_attribute.BootstrapMethodSpecifier bsm = attr.bootstrap_method_specifiers[i];
        indent(+1);
        print(i + ": #" + bsm.bootstrap_method_ref + " ");
        println(constantWriter.stringValue(bsm.bootstrap_method_ref));
        indent(+1);
        println("Method arguments:");
        indent(+1);
        for (int j = 0; j < bsm.bootstrap_arguments.length; j++) {
            print("#" + bsm.bootstrap_arguments[j] + " ");
            println(constantWriter.stringValue(bsm.bootstrap_arguments[j]));
        }
        indent(-3);
    }
    return null;
}
 
Example #12
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: LVTHarness.java    License: GNU General Public License v2.0 6 votes vote down vote up
void checkMethod(ConstantPool constantPool, Method method, AliveRanges ranges)
        throws InvalidIndex, UnexpectedEntry, ConstantPoolException {
    Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
    LocalVariableTable_attribute lvt =
        (LocalVariableTable_attribute) (code.attributes.get(Attribute.LocalVariableTable));
    List<String> infoFromRanges = convertToStringList(ranges);
    List<String> infoFromLVT = convertToStringList(constantPool, lvt);

    // infoFromRanges most be contained in infoFromLVT
    int i = 0;
    int j = 0;
    while (i < infoFromRanges.size() && j < infoFromLVT.size()) {
        int comparison = infoFromRanges.get(i).compareTo(infoFromLVT.get(j));
        if (comparison == 0) {
            i++; j++;
        } else if (comparison > 0) {
            j++;
        } else {
            break;
        }
    }

    if (i < infoFromRanges.size()) {
        error(infoFromLVT, infoFromRanges, method.getName(constantPool).toString());
    }
}
 
Example #13
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: SourceWriter.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void setLineMap(Code_attribute attr) {
    SortedMap<Integer, SortedSet<Integer>> map =
            new TreeMap<Integer, SortedSet<Integer>>();
    SortedSet<Integer> allLines = new TreeSet<Integer>();
    for (Attribute a: attr.attributes) {
        if (a instanceof LineNumberTable_attribute) {
            LineNumberTable_attribute t = (LineNumberTable_attribute) a;
            for (LineNumberTable_attribute.Entry e: t.line_number_table) {
                int start_pc = e.start_pc;
                int line = e.line_number;
                SortedSet<Integer> pcLines = map.get(start_pc);
                if (pcLines == null) {
                    pcLines = new TreeSet<Integer>();
                    map.put(start_pc, pcLines);
                }
                pcLines.add(line);
                allLines.add(line);
            }
        }
    }
    lineMap = map;
    lineList = new ArrayList<Integer>(allLines);
}
 
Example #14
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: TypeAnnotationPropagationTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void run() throws Exception {
    ClassFile cf = getClassFile("TypeAnnotationPropagationTest$Test.class");

    Method f = null;
    for (Method m : cf.methods) {
        if (m.getName(cf.constant_pool).contains("f")) {
            f = m;
            break;
        }
    }

    int idx = f.attributes.getIndex(cf.constant_pool, Attribute.Code);
    Code_attribute cattr = (Code_attribute) f.attributes.get(idx);
    idx = cattr.attributes.getIndex(cf.constant_pool, Attribute.RuntimeVisibleTypeAnnotations);
    RuntimeVisibleTypeAnnotations_attribute attr =
            (RuntimeVisibleTypeAnnotations_attribute) cattr.attributes.get(idx);

    TypeAnnotation anno = attr.annotations[0];
    assertEquals(anno.position.lvarOffset, new int[] {3}, "start_pc");
    assertEquals(anno.position.lvarLength, new int[] {8}, "length");
    assertEquals(anno.position.lvarIndex, new int[] {1}, "index");
}
 
Example #15
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: LambdaAsm.java    License: GNU General Public License v2.0 6 votes vote down vote up
static int checkMethod(ClassFile cf, String mthd) throws Exception {
    if (cf.major_version < 52) {
        throw new RuntimeException("unexpected class file version, in "
                + cf.getName() + "expected 52, got " + cf.major_version);
    }
    int count = 0;
    for (Method m : cf.methods) {
        String mname = m.getName(cf.constant_pool);
        if (mname.equals(mthd)) {
            for (Attribute a : m.attributes) {
                if ("Code".equals(a.getName(cf.constant_pool))) {
                    count++;
                    checkMethod(cf.getName(), mname, cf.constant_pool,
                            (Code_attribute) a);
                }
            }
        }
    }
    return count;
}
 
Example #16
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
private List<String> readBSM() {
    BootstrapMethods_attribute bsmAttr =
            (BootstrapMethods_attribute) cf.getAttribute(Attribute.BootstrapMethods);
    if (bsmAttr != null) {
        List<String> out =
                new ArrayList<>(bsmAttr.bootstrap_method_specifiers.length);
        for (BootstrapMethods_attribute.BootstrapMethodSpecifier bsms :
                bsmAttr.bootstrap_method_specifiers) {
            int index = bsms.bootstrap_method_ref;
            try {
                String value = slist.get(index);
                String bsmStr = value;
                if (value == null) {
                    value = visit(cfpool.get(index), index);
                    slist.set(index, value);
                }
                bsmStr = value;
                for (int idx : bsms.bootstrap_arguments) {
                    value = slist.get(idx);
                    if (value == null) {
                        value = visit(cfpool.get(idx), idx);
                        slist.set(idx, value);
                    }
                    bsmStr = bsmStr.concat("," + value);
                }
                out.add(bsmStr);
            } catch (InvalidIndex ex) {
                ex.printStackTrace();
            }
        }
        return out;
    }
    return new ArrayList<>(0);
}
 
Example #17
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NestedLineNumberTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Entry[] findEntries() throws IOException, ConstantPoolException {
    ClassFile self = ClassFile.read(NestedLineNumberTest.Test.class.getResourceAsStream("NestedLineNumberTest$Test.class"));
    for (Method m : self.methods) {
        if ("<init>".equals(m.getName(self.constant_pool))) {
            Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code);
            for (Attribute at : code_attribute.attributes) {
                if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) {
                    return ((LineNumberTable_attribute)at).line_number_table;
                }
            }
        }
    }
    return null;
}
 
Example #18
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
private List<String> readBSM() {
    BootstrapMethods_attribute bsmAttr =
            (BootstrapMethods_attribute) cf.getAttribute(Attribute.BootstrapMethods);
    if (bsmAttr != null) {
        List<String> out =
                new ArrayList<>(bsmAttr.bootstrap_method_specifiers.length);
        for (BootstrapMethods_attribute.BootstrapMethodSpecifier bsms :
                bsmAttr.bootstrap_method_specifiers) {
            int index = bsms.bootstrap_method_ref;
            try {
                String value = slist.get(index);
                String bsmStr = value;
                if (value == null) {
                    value = visit(cfpool.get(index), index);
                    slist.set(index, value);
                }
                bsmStr = value;
                for (int idx : bsms.bootstrap_arguments) {
                    value = slist.get(idx);
                    if (value == null) {
                        value = visit(cfpool.get(idx), idx);
                        slist.set(idx, value);
                    }
                    bsmStr = bsmStr.concat("," + value);
                }
                out.add(bsmStr);
            } catch (InvalidIndex ex) {
                ex.printStackTrace();
            }
        }
        return out;
    }
    return new ArrayList<>(0);
}
 
Example #19
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AttributeWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void write(Object owner, Attribute attr, ConstantPool constant_pool) {
    if (attr != null) {
        Assert.checkNonNull(constant_pool);
        Assert.checkNonNull(owner);
        this.constant_pool = constant_pool;
        this.owner = owner;
        attr.accept(this, null);
    }
}
 
Example #20
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: CreateSymbols.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void addAttributes(FieldDescription desc, List<CPInfo> constantPool, Map<String, Attribute> attributes) {
    addGenericAttributes(desc, constantPool, attributes);
    if (desc.constantValue != null) {
        Pair<Integer, Character> constantPoolEntry =
                addConstant(constantPool, desc.constantValue, false);
        Assert.checkNonNull(constantPoolEntry);
        int constantValueString = addString(constantPool, Attribute.ConstantValue);
        attributes.put(Attribute.ConstantValue,
                       new ConstantValue_attribute(constantValueString, constantPoolEntry.fst));
    }
}
 
Example #21
Source Project: TencentKona-8   Author: Tencent   File: LocalVariableTypeTableWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void reset(Code_attribute attr) {
    codeAttr = attr;
    pcMap = new HashMap<Integer, List<LocalVariableTypeTable_attribute.Entry>>();
    LocalVariableTypeTable_attribute lvt =
            (LocalVariableTypeTable_attribute) (attr.attributes.get(Attribute.LocalVariableTypeTable));
    if (lvt == null)
        return;

    for (int i = 0; i < lvt.local_variable_table.length; i++) {
        LocalVariableTypeTable_attribute.Entry entry = lvt.local_variable_table[i];
        put(entry.start_pc, entry);
        put(entry.start_pc + entry.length, entry);
    }
}
 
Example #22
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: InnerClassAttrMustNotHaveStrictFPFlagTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
void analyzeClassFile(File path) throws Exception {
    ClassFile classFile = ClassFile.read(path);
    InnerClasses_attribute innerClasses =
            (InnerClasses_attribute) classFile.attributes.get(Attribute.InnerClasses);
    for (Info classInfo : innerClasses.classes) {
        Assert.check(!classInfo.inner_class_access_flags.is(AccessFlags.ACC_STRICT),
                "Inner classes attribute must not have the ACC_STRICT flag set");
    }
}
 
Example #23
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: T4241573.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
        String found = sfa.getSourceFile(cf.constant_pool);
        String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
        if (!expect.equals(found)) {
            error("bad value found: " + found + ", expected: " + expect);
        }
    } catch (Exception e) {
        error("error reading " + f +": " + e);
    }
}
 
Example #24
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: FinallyLineNumberTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Entry[] findEntries() throws IOException, ConstantPoolException {
    ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class"));
    for (Method m : self.methods) {
        if ("method".equals(m.getName(self.constant_pool))) {
            Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code);
            for (Attribute at : code_attribute.attributes) {
                if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) {
                    return ((LineNumberTable_attribute)at).line_number_table;
                }
            }
        }
    }
    return null;
}
 
Example #25
Source Project: TencentKona-8   Author: Tencent   File: FinallyLineNumberTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Entry[] findEntries() throws IOException, ConstantPoolException {
    ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class"));
    for (Method m : self.methods) {
        if ("method".equals(m.getName(self.constant_pool))) {
            Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code);
            for (Attribute at : code_attribute.attributes) {
                if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) {
                    return ((LineNumberTable_attribute)at).line_number_table;
                }
            }
        }
    }
    return null;
}
 
Example #26
Source Project: hottub   Author: dsrg-uoft   File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
private List<String> readBSM() {
    BootstrapMethods_attribute bsmAttr =
            (BootstrapMethods_attribute) cf.getAttribute(Attribute.BootstrapMethods);
    if (bsmAttr != null) {
        List<String> out =
                new ArrayList<>(bsmAttr.bootstrap_method_specifiers.length);
        for (BootstrapMethods_attribute.BootstrapMethodSpecifier bsms :
                bsmAttr.bootstrap_method_specifiers) {
            int index = bsms.bootstrap_method_ref;
            try {
                String value = slist.get(index);
                String bsmStr = value;
                if (value == null) {
                    value = visit(cfpool.get(index), index);
                    slist.set(index, value);
                }
                bsmStr = value;
                for (int idx : bsms.bootstrap_arguments) {
                    value = slist.get(idx);
                    if (value == null) {
                        value = visit(cfpool.get(idx), idx);
                        slist.set(idx, value);
                    }
                    bsmStr = bsmStr.concat("," + value);
                }
                out.add(bsmStr);
            } catch (InvalidIndex ex) {
                ex.printStackTrace();
            }
        }
        return out;
    }
    return new ArrayList<>(0);
}
 
Example #27
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: TestBCI.java    License: GNU General Public License v2.0 5 votes vote down vote up
MethodInfo(ClassFile cf, Method m) {
    this.method = m;

    String name;
    String paramTypes;
    String returnType;
    LineNumberTable_attribute.Entry[] lineNumberTable;
    try {
        // method name
        name = m.getName(cf.constant_pool);
        // signature
        paramTypes = m.descriptor.getParameterTypes(cf.constant_pool);
        returnType = m.descriptor.getReturnType(cf.constant_pool);
        Code_attribute codeAttr = (Code_attribute)
            m.attributes.get(Attribute.Code);
        lineNumberTable = ((LineNumberTable_attribute)
            codeAttr.attributes.get(Attribute.LineNumberTable)).line_number_table;
    } catch (ConstantPoolException|Descriptor.InvalidDescriptor e) {
        throw new RuntimeException(e);
    }
    this.name = name;
    this.paramTypes = paramTypes;
    this.returnType = returnType;
    Arrays.stream(lineNumberTable).forEach(entry ->
        bciToLineNumbers.computeIfAbsent(entry.start_pc, _n -> new TreeSet<>())
            .add(entry.line_number));
}
 
Example #28
void analyzeClassFile(File path) throws Exception {
    ClassFile classFile = ClassFile.read(path);
    InnerClasses_attribute innerClasses =
            (InnerClasses_attribute) classFile.attributes.get(Attribute.InnerClasses);
    for (Info classInfo : innerClasses.classes) {
        Assert.check(!classInfo.inner_class_access_flags.is(AccessFlags.ACC_STRICT),
                "Inner classes attribute must not have the ACC_STRICT flag set");
    }
}
 
Example #29
Source Project: TencentKona-8   Author: Tencent   File: T6199075.java    License: GNU General Public License v2.0 5 votes vote down vote up
void verifyBytecode(VarargsMethod selected) {
    bytecodeCheckCount++;
    File compiledTest = new File("Test.class");
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokevirtual")) {
                int cp_entry = i.getUnsignedShort(1);
                CONSTANT_Methodref_info methRef =
                        (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
                String type = methRef.getNameAndTypeInfo().getType();
                if (!type.contains(selected.varargsElement.bytecodeString)) {
                    throw new Error("Unexpected type method call: " + type);
                }
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example #30
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: DontGenerateLVTForGNoneOpTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
void checkClassFile(final File cfile) throws Exception {
    ClassFile classFile = ClassFile.read(cfile);
    for (Method method : classFile.methods) {
        Code_attribute code = (Code_attribute)method.attributes.get(Attribute.Code);
        if (code != null) {
            if (code.attributes.get(Attribute.LocalVariableTable) != null) {
                throw new AssertionError("LVT shouldn't be generated for g:none");
            }
        }
    }
}