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; }
void checkClassFile(final File cfile, String methodToFind) throws Exception { ClassFile classFile = ClassFile.read(cfile); boolean methodFound = false; for (Method method : classFile.methods) { if (method.getName(classFile.constant_pool).equals(methodToFind)) { methodFound = true; Code_attribute code = (Code_attribute) method.attributes.get("Code"); LineNumberTable_attribute lnt = (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); Assert.check(lnt.line_number_table_length == expectedLNT.length, "The LineNumberTable found has a length different to the expected one"); int i = 0; for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { Assert.check(entry.line_number == expectedLNT[i][0] && entry.start_pc == expectedLNT[i][1], "LNT entry at pos " + i + " differ from expected." + "Found " + entry.line_number + ":" + entry.start_pc + ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); i++; } } } Assert.check(methodFound, "The seek method was not found"); }
static List<Entry> findEntries() throws IOException, ConstantPoolException { ClassFile self = ClassFile.read(NullCheckLineNumberTest.Test.class.getResourceAsStream("NullCheckLineNumberTest$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 Arrays.stream(((LineNumberTable_attribute)at).line_number_table) .map(e -> new SimpleEntry<> (e.line_number, e.start_pc)) .collect(Collectors.toList()); } } } } return null; }
void checkClassFile(File file) throws IOException, ConstantPoolException, InvalidDescriptor { ClassFile classFile = ClassFile.read(file); ConstantPool constantPool = classFile.constant_pool; //lets get all the methods in the class file. for (Method method : classFile.methods) { for (ElementKey elementKey: aliveRangeMap.keySet()) { String methodDesc = method.getName(constantPool) + method.descriptor.getParameterTypes(constantPool).replace(" ", ""); if (methodDesc.equals(elementKey.elem.toString())) { checkMethod(constantPool, method, aliveRangeMap.get(elementKey)); seenAliveRanges.add(elementKey); } } } }
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()); } }
void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception { ClassFile classFile = ClassFile.read(cfile); boolean methodFound = false; for (Method method : classFile.methods) { if (method.getName(classFile.constant_pool).equals(methodToFind)) { methodFound = true; Code_attribute code = (Code_attribute) method.attributes.get("Code"); LineNumberTable_attribute lnt = (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); Assert.check(lnt.line_number_table_length == expectedLNT.length, "The LineNumberTable found has a length different to the expected one"); int i = 0; for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { Assert.check(entry.line_number == expectedLNT[i][0] && entry.start_pc == expectedLNT[i][1], "LNT entry at pos " + i + " differ from expected." + "Found " + entry.line_number + ":" + entry.start_pc + ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); i++; } } } Assert.check(methodFound, "The seek method was not found"); }
void check(String dir, String... fileNames) throws IOException, ConstantPoolException, Descriptor.InvalidDescriptor { for (String fileName : fileNames) { ClassFile classFileToCheck = ClassFile.read(new File(dir, fileName)); for (Method method : classFileToCheck.methods) { if ((method.access_flags.flags & ACC_STRICT) == 0) { errors.add(String.format(offendingMethodErrorMessage, method.getName(classFileToCheck.constant_pool), classFileToCheck.getName())); } } } }
private void testMethods(Map<String, ExpectedSignature> expectedSignatures, ClassFile classFile) throws ConstantPoolException, Descriptor.InvalidDescriptor { String className = classFile.getName(); Set<String> foundMethods = new HashSet<>(); for (Method method : classFile.methods) { String methodName = getMethodName(classFile, method); printf("Testing method %s\n", methodName); if (method.access_flags.getMethodFlags().contains(ACC_BRIDGE)) { printf("Bridge method is skipped : %s\n", methodName); continue; } testAttribute( methodName, classFile, () -> (Signature_attribute) method.attributes.get(Attribute.Signature), expectedSignatures.get(methodName)); foundMethods.add(methodName); } checkAllMembersFound(foundMethods, expectedSignatures, "Checking that all methods of class " + className + " with Signature attribute found"); }
void checkClassFile(final File cfile, String methodToFind) throws Exception { ClassFile classFile = ClassFile.read(cfile); boolean methodFound = false; for (Method method : classFile.methods) { if (method.getName(classFile.constant_pool).equals(methodToFind)) { methodFound = true; Code_attribute code = (Code_attribute) method.attributes.get("Code"); LineNumberTable_attribute lnt = (LineNumberTable_attribute) code.attributes.get("LineNumberTable"); Assert.check(lnt.line_number_table_length == expectedLNT.length, foundLNTLengthDifferentThanExpMsg); int i = 0; for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) { Assert.check(entry.line_number == expectedLNT[i][0] && entry.start_pc == expectedLNT[i][1], "LNT entry at pos " + i + " differ from expected." + "Found " + entry.line_number + ":" + entry.start_pc + ". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]); i++; } } } Assert.check(methodFound, seekMethodNotFoundMsg); }
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"); }
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"); }
private static void findAnnotations(ClassFile cf, Method m, String name, List<TypeAnnotation> annos) { int index = m.attributes.getIndex(cf.constant_pool, name); if (index != -1) { Attribute attr = m.attributes.get(index); assert attr instanceof RuntimeTypeAnnotations_attribute; RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; annos.addAll(Arrays.asList(tAttr.annotations)); } int cindex = m.attributes.getIndex(cf.constant_pool, Attribute.Code); if (cindex != -1) { Attribute cattr = m.attributes.get(cindex); assert cattr instanceof Code_attribute; Code_attribute cAttr = (Code_attribute)cattr; index = cAttr.attributes.getIndex(cf.constant_pool, name); if (index != -1) { Attribute attr = cAttr.attributes.get(index); assert attr instanceof RuntimeTypeAnnotations_attribute; RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; annos.addAll(Arrays.asList(tAttr.annotations)); } } }
void checkClassFile(final File cfile, String[] methodsToFind) throws Exception { ClassFile classFile = ClassFile.read(cfile); int numberOfmethodsFound = 0; for (String methodToFind : methodsToFind) { for (Method method : classFile.methods) { if (method.getName(classFile.constant_pool).equals(methodToFind)) { numberOfmethodsFound++; Code_attribute code = (Code_attribute) method.attributes.get("Code"); Assert.check(code.exception_table_length == expectedExceptionTable.length, "The ExceptionTable found has a length different to the expected one"); int i = 0; for (Exception_data entry: code.exception_table) { Assert.check(entry.start_pc == expectedExceptionTable[i][0] && entry.end_pc == expectedExceptionTable[i][1] && entry.handler_pc == expectedExceptionTable[i][2] && entry.catch_type == expectedExceptionTable[i][3], "Exception table entry at pos " + i + " differ from expected."); i++; } } } } Assert.check(numberOfmethodsFound == 2, "Some seek methods were not found"); }
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); } }
private void checkDebugAttributes(byte[] strippedClassFile) throws IOException, ConstantPoolException { ClassFile classFile = ClassFile.read(new ByteArrayInputStream(strippedClassFile)); String[] debugAttributes = new String[]{ Attribute.LineNumberTable, Attribute.LocalVariableTable, Attribute.LocalVariableTypeTable }; for (Method method : classFile.methods) { String methodName = method.getName(classFile.constant_pool); Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code); for (String attr : debugAttributes) { if (code.attributes.get(attr) != null) { throw new AssertionError("Debug attribute was not removed: " + attr + " from method " + classFile.getName() + "#" + methodName); } } } }
boolean isNativeClass(JarFile jar, JarEntry entry) throws IOException, ConstantPoolException { String name = entry.getName(); if (name.startsWith("META-INF") || !name.endsWith(".class")) return false; //String className = name.substring(0, name.length() - 6).replace("/", "."); //System.err.println("check " + className); InputStream in = jar.getInputStream(entry); ClassFile cf = ClassFile.read(in); for (int i = 0; i < cf.methods.length; i++) { Method m = cf.methods[i]; if (m.access_flags.is(AccessFlags.ACC_NATIVE)) { // System.err.println(className); return true; } } return false; }
boolean isNativeClass(JarFile jar, JarEntry entry) throws IOException, ConstantPoolException { String name = entry.getName(); if (name.startsWith("META-INF") || !name.endsWith(".class")) return false; //String className = name.substring(0, name.length() - 6).replace("/", "."); //System.err.println("check " + className); InputStream in = jar.getInputStream(entry); ClassFile cf = ClassFile.read(in); in.close(); for (int i = 0; i < cf.methods.length; i++) { Method m = cf.methods[i]; if (m.access_flags.is(AccessFlags.ACC_NATIVE)) { // System.err.println(className); return true; } } return false; }