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); } }
@Override public Element visitNoOperands(Instruction i, Void p) { Opcode o = i.getOpcode(); Element e = new Element(i.getMnemonic()); if (o.opcode > 0xab && o.opcode <= 0xb1) { e.setAttr("pc", "" + i.getPC()); } return e; }
@Override public Element visitConstantPoolRefAndValue(Instruction i, int i1, int i2, Void p) { // workaround for a potential bug in classfile Element ie = new Element(i.getMnemonic()); if (i.getOpcode().equals(Opcode.IINC_W)) { ie.setAttr("loc", "" + i1); ie.setAttr("num", "" + i2); } else { ie.setAttr("ref", x.getCpString(i1)); ie.setAttr("val", "" + i2); } return ie; }
void verifyDefaultBody(File f) { System.err.println("verify: " + f); try { ClassFile cf = ClassFile.read(f); Method testMethod = null; Code_attribute codeAttr = null; for (Method m : cf.methods) { codeAttr = (Code_attribute)m.attributes.get(Attribute.Code); String mname = m.getName(cf.constant_pool); if (mname.equals(TEST_METHOD_NAME)) { testMethod = m; break; } else { codeAttr = null; } } if (testMethod == null) { throw new Error("Test method not found"); } if (testMethod.access_flags.is(AccessFlags.ACC_ABSTRACT)) { throw new Error("Test method is abstract"); } if (codeAttr == null) { throw new Error("Code attribute in test method not found"); } boolean found = false; for (Instruction instr : codeAttr.getInstructions()) { if (instr.getOpcode() == Opcode.INVOKESTATIC) { found = true; int pc_index = instr.getShort(1); CONSTANT_Methodref_info mref = (CONSTANT_Methodref_info)cf.constant_pool.get(pc_index); String className = mref.getClassName(); String targetName = mref.getNameAndTypeInfo().getName(); String targetType = mref.getNameAndTypeInfo().getType(); if (!className.equals(TARGET_CLASS_NAME)) { throw new Error("unexpected class in default method body " + className); } if (!targetName.equals(TARGET_NAME)) { throw new Error("unexpected method name in default method body " + targetName); } if (!targetType.equals(TARGET_TYPE)) { throw new Error("unexpected method type in default method body " + targetType); } break; } } if (!found) { throw new Error("no invokestatic found in default method body"); } } catch (Exception e) { e.printStackTrace(); throw new Error("error reading " + f +": " + e); } }