private String parseInst(Instruction inst, int offset, Map<Integer, Integer> labels) throws ConstantPoolException { StringBuffer tmp = new StringBuffer(); StringBuilder tmp2 = new StringBuilder(); ParseVisitor parseVisitor = new ParseVisitorM6(labels, tmp2); // System.out.println(inst.toString()); tmp.append("(" + offset + " ("); if (labels.containsKey(inst.getPC())) { String label = "TAG_" + labels.get(inst.getPC()); tagTable.put(label, new Integer(offset)); tmp2.append(";;at " + label); } tmp.append(inst.accept(parseVisitor, null)).append("))"); // round the line length to multiple of 8. if (!tmp2.equals("")) { int pos = 20; while (pos <= tmp.length()) { pos += 20; } int bc = pos - tmp.length(); for (int j = 0; j < bc; j++) { tmp.append(" "); } tmp.append(tmp2); } return tmp.toString().trim(); }
/** * This method processes the MethodInfo given in the constructor. It is necessary to call this * method before querying this method. * * @param constant_pool The constant pool element from the M6Class object representing the outer * class from which this method is taken from. */ public void processMethod() throws ConstantPoolException, DescriptorException { /* The name of the method */ if (debug) { System.out.println("\nProcessing method: " + name + "Parameters: " + params); } /* Process the code attribute */ if (cai != null && !no_method_body) { LinkedHashMap<Integer, Integer> labels = new LinkedHashMap<>(); LabelsVisitor labelsVisitor = new LabelsVisitor(); for (Instruction inst : cai.getInstructions()) { for (int offset : inst.accept(labelsVisitor, null)) { int label = inst.getPC() + offset; if (!labels.containsKey(label)) { labels.put(label, labels.size()); } } } for (Code_attribute.Exception_data e : cai.exception_table) { if (!labels.containsKey(e.start_pc)) { labels.put(e.start_pc, labels.size()); } if (!labels.containsKey(e.end_pc)) { labels.put(e.end_pc, labels.size()); } if (!labels.containsKey(e.handler_pc)) { labels.put(e.handler_pc, labels.size()); } } int offset = 0; int li = 0; // index in linenumber table // get the parsed code part for (Instruction instr : cai.getInstructions()) { if (lnt != null && li < lnt.line_number_table.length && lnt.line_number_table[li].start_pc == instr.getPC()) { m6instructions.add("; " + "line_number #" + lnt.line_number_table[li].line_number); li++; } String resultStr = parseInst(instr, offset, labels); if (resultStr != null) { m6instructions.add(resultStr); offset = offset + instr.length(); } } // resolve any tag to absolute offsets. resolveTagInCode(); // add an end marker to the code. m6instructions.add("(endofcode " + code_length + ")"); } processed = true; }
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"); }