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 File: LambdaAsm.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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 #2
Source File: LambdaAsm.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
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 #3
Source File: DeadCodeGeneratedForEmptyTryTest.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
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 #4
Source File: AttributeWriter.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
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 File: LVTHarness.java From hottub with GNU General Public License v2.0 | 6 votes |
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 File: LVTHarness.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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 #7
Source File: AttributeWriter.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@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 #8
Source File: TypeAnnotationPropagationTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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 #9
Source File: SourceWriter.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
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 #10
Source File: DeadCodeGeneratedForEmptyTryTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
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 #11
Source File: LVTHarness.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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 #12
Source File: SourceWriter.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
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 #13
Source File: TypeAnnotationPropagationTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
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 #14
Source File: TestDirectSuperInterfaceInvoke.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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 #15
Source File: LambdaAsm.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
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 File: LocalVariableTableWriter.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public void reset(Code_attribute attr) { codeAttr = attr; pcMap = new HashMap<Integer, List<LocalVariableTable_attribute.Entry>>(); LocalVariableTable_attribute lvt = (LocalVariableTable_attribute) (attr.attributes.get(Attribute.LocalVariableTable)); if (lvt == null) return; for (int i = 0; i < lvt.local_variable_table.length; i++) { LocalVariableTable_attribute.Entry entry = lvt.local_variable_table[i]; put(entry.start_pc, entry); put(entry.start_pc + entry.length, entry); } }
Example #17
Source File: AnnotationsAreNotCopiedToBridgeMethodsTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
void checkClassFile(final Path cfilePath) throws Exception { ClassFile classFile = ClassFile.read( new BufferedInputStream(Files.newInputStream(cfilePath))); for (Method method : classFile.methods) { if (method.access_flags.is(AccessFlags.ACC_BRIDGE)) { checkForAttr(method.attributes, "Annotations hasn't been copied to bridge method", Attribute.RuntimeVisibleAnnotations, Attribute.RuntimeVisibleParameterAnnotations); } } }
Example #18
Source File: CreateSymbols.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private List<AnnotationDescription> annotations2Description(ConstantPool cp, Attribute attr) throws ConstantPoolException { RuntimeAnnotations_attribute annotationsAttr = (RuntimeAnnotations_attribute) attr; List<AnnotationDescription> descs = new ArrayList<>(); for (Annotation a : annotationsAttr.annotations) { descs.add(annotation2Description(cp, a)); } return descs; }
Example #19
Source File: ByteCodeTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private Map<Integer, String> readBSM() { BootstrapMethods_attribute bsmAttr = (BootstrapMethods_attribute) cf.getAttribute(Attribute.BootstrapMethods); if (bsmAttr != null) { Map<Integer, String> out = new HashMap<>(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); if (value == null) { value = visit(cfpool.get(index), index); debugln("[SG]: index " + index); debugln("[SG]: value " + value); slist.set(index, value); out.put(index, value); } for (int idx : bsms.bootstrap_arguments) { value = slist.get(idx); if (value == null) { value = visit(cfpool.get(idx), idx); debugln("[SG]: idx " + idx); debugln("[SG]: value " + value); slist.set(idx, value); out.put(idx, value); } } } catch (InvalidIndex ex) { ex.printStackTrace(); } } return out; } return new HashMap<>(0); }
Example #20
Source File: RedundantByteCodeInArrayTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
void checkClassFile(File file) throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex { ClassFile classFile = ClassFile.read(file); ConstantPool constantPool = classFile.constant_pool; //lets get all the methods in the class file. for (Method method : classFile.methods) { if (method.getName(constantPool).equals("arrMethod")) { Code_attribute code = (Code_attribute) method.attributes .get(Attribute.Code); if (code.max_locals > 4) throw new AssertionError("Too many locals for method arrMethod"); } } }
Example #21
Source File: T4241573.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** 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 #22
Source File: ReferenceInfoUtil.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
private static void findAnnotations(ClassFile cf, List<TypeAnnotation> annos) { findAnnotations(cf, Attribute.RuntimeVisibleTypeAnnotations, annos); findAnnotations(cf, Attribute.RuntimeInvisibleTypeAnnotations, annos); for (Field f : cf.fields) { findAnnotations(cf, f, annos); } for (Method m: cf.methods) { findAnnotations(cf, m, annos); } }
Example #23
Source File: T6199075.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
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 #24
Source File: Pos05.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
void verifyMulticatchExceptionRanges(File f) { System.err.println("verify: " + f); try { int count = 0; ClassFile cf = ClassFile.read(f); Method testMethod = null; for (Method m : cf.methods) { if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) { 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"); } Exception_data firstExceptionTable = null; for (int i = 0 ; i < ea.exception_table_length; i++) { if (firstExceptionTable == null) { firstExceptionTable = ea.exception_table[i]; } if (ea.exception_table[i].handler_pc != firstExceptionTable.handler_pc || ea.exception_table[i].start_pc != firstExceptionTable.start_pc || ea.exception_table[i].end_pc != firstExceptionTable.end_pc) { throw new Error("Multiple overlapping catch clause found in generated code"); } count++; } if (count != TYPES_IN_MULTICATCH) { throw new Error("Wrong number of exception data found: " + count); } } catch (Exception e) { e.printStackTrace(); throw new Error("error reading " + f +": " + e); } }
Example #25
Source File: T4241573.java From hottub with GNU General Public License v2.0 | 5 votes |
/** 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 #26
Source File: AttributeWriter.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public void write(Object owner, Attribute attr, ConstantPool constant_pool) { if (attr != null) { // null checks owner.getClass(); constant_pool.getClass(); this.constant_pool = constant_pool; this.owner = owner; attr.accept(this, null); } }
Example #27
Source File: DontGenerateLVTForGNoneOpTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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"); } } } }
Example #28
Source File: LocalVariableTableWriter.java From hottub with GNU General Public License v2.0 | 5 votes |
public void reset(Code_attribute attr) { codeAttr = attr; pcMap = new HashMap<Integer, List<LocalVariableTable_attribute.Entry>>(); LocalVariableTable_attribute lvt = (LocalVariableTable_attribute) (attr.attributes.get(Attribute.LocalVariableTable)); if (lvt == null) return; for (int i = 0; i < lvt.local_variable_table.length; i++) { LocalVariableTable_attribute.Entry entry = lvt.local_variable_table[i]; put(entry.start_pc, entry); put(entry.start_pc + entry.length, entry); } }
Example #29
Source File: ClassReader.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
protected void readAttributesFor(ClassFile c, Attributes attrs, Element x) { Element container = new Element(); AttributeVisitor av = new AttributeVisitor(this, c); for (Attribute a : attrs) { av.visit(a, container); } if (!keepOrder) { container.sort(); } x.addAll(container); }
Example #30
Source File: FinallyLineNumberTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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; }