use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.
the class FinallyTest method getTagsWithGotos.
private Set<String> getTagsWithGotos() throws IOException {
final Set<String> gotoTags = new HashSet<String>();
byte[] b = TargetLoader.getClassDataAsBytes(FinallyTarget.class);
final ClassNode classNode = new ClassNode();
InstrSupport.classReaderFor(b).accept(classNode, 0);
for (final MethodNode m : classNode.methods) {
if ("main".equals(m.name)) {
// skip it
continue;
}
int lineNumber = -1;
for (AbstractInsnNode i : m.instructions) {
if (AbstractInsnNode.LINE == i.getType()) {
lineNumber = ((LineNumberNode) i).line;
}
if (Opcodes.GOTO == i.getOpcode()) {
String tag = tags.get(Integer.valueOf(lineNumber));
if (tag == null) {
throw new AssertionError("No tag at line " + lineNumber);
}
gotoTags.add(tag);
}
}
}
return gotoTags;
}
use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.
the class MethodAnalyzer method accept.
@Override
public void accept(final MethodNode methodNode, final MethodVisitor methodVisitor) {
methodVisitor.visitCode();
for (final TryCatchBlockNode n : methodNode.tryCatchBlocks) {
n.accept(methodVisitor);
}
for (final AbstractInsnNode i : methodNode.instructions) {
currentNode = i;
i.accept(methodVisitor);
}
methodVisitor.visitEnd();
}
use of org.objectweb.asm.tree.AbstractInsnNode in project Minechem by iopleke.
the class MinechemTransformer method replaceBytes.
private byte[] replaceBytes(Method method, CodeBlock codeBlock, byte[] data) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(data);
classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
MethodNode methodNode = getMethodByName(classNode, method);
AbstractInsnNode pos = null;
boolean delete = false;
boolean done = false;
int start = codeBlock.getLinesAfterStart();
int end = codeBlock.getLinesAfterEnd() + 1;
for (Iterator<AbstractInsnNode> itr = methodNode.instructions.iterator(); itr.hasNext(); ) {
AbstractInsnNode node = itr.next();
if (node instanceof LineNumberNode) {
LineNumberNode lineNode = (LineNumberNode) node;
if (lineNode.line >= codeBlock.getStartLine()) {
delete = true;
}
if (lineNode.line >= codeBlock.getEndLine()) {
done = true;
}
}
if (delete) {
if (done) {
if (end-- > 0) {
methodNode.instructions.remove(node);
continue;
}
pos = node;
break;
}
if (--start < 0) {
methodNode.instructions.remove(node);
}
}
}
methodNode.instructions.insertBefore(pos, codeBlock.getInsnList());
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classNode.accept(writer);
return writer.toByteArray();
}
use of org.objectweb.asm.tree.AbstractInsnNode in project Minechem by iopleke.
the class MinechemTransformer method findInstructionNode.
private static AbstractInsnNode findInstructionNode(InstructionNode instructionNode, MethodNode methodNode) {
boolean close = false;
AbstractInsnNode result = null;
for (Iterator<AbstractInsnNode> itr = methodNode.instructions.iterator(); itr.hasNext(); ) {
AbstractInsnNode node = itr.next();
if (node instanceof MethodInsnNode) {
if (close) {
if (((MethodInsnNode) node).name.equals(instructionNode.getBefore())) {
return instructionNode.replace ? node : result;
} else {
close = false;
}
}
if (((MethodInsnNode) node).name.equals(instructionNode.getAfter())) {
close = true;
result = node;
}
}
}
return result == null ? methodNode.instructions.getLast().getPrevious() : result;
}
use of org.objectweb.asm.tree.AbstractInsnNode in project flink by apache.
the class ModifiedASMAnalyzer method newControlFlowEdge.
@Override
protected void newControlFlowEdge(int insn, int successor) {
try {
if (jumpModificationState == PRE_STATE) {
jumpModificationState = MOD_STATE;
} else if (jumpModificationState == MOD_STATE) {
// for a later merge
if (jumpModification == IFEQ_MOD) {
final int top = accessField(Analyzer.class, "top").getInt(this);
final int[] queue = (int[]) accessField(Analyzer.class, "queue").get(this);
final int tmp = queue[top - 2];
queue[top - 2] = queue[top - 1];
queue[top - 1] = tmp;
eventInsn = queue[top - 2] - 1;
final InsnList insns = (InsnList) accessField(Analyzer.class, "insns").get(this);
// if yes this is loop structure
if (insns.get(eventInsn) instanceof JumpInsnNode) {
jumpModificationState = WAIT_FOR_INSN_STATE;
} else // no loop -> end of modification
{
jumpModificationState = DO_NOTHING;
}
} else // this modification changes the merge priority of certain frames (the expression part of the IF)
if (jumpModification == IFNE_MOD) {
final Frame[] frames = (Frame[]) accessField(Analyzer.class, "frames").get(this);
final Field indexField = accessField(AbstractInsnNode.class, "index");
final InsnList insns = (InsnList) accessField(Analyzer.class, "insns").get(this);
final AbstractInsnNode gotoInsnn = insns.get(successor - 1);
// check for a loop
if (gotoInsnn instanceof JumpInsnNode) {
jumpModificationState = WAIT_FOR_INSN_STATE;
// sets a merge priority for all instructions (the expression of the IF)
// from the label the goto instruction points to until the evaluation with IFEQ
final int idx = indexField.getInt(accessField(JumpInsnNode.class, "label").get(gotoInsnn));
for (int i = idx; i <= insn; i++) {
((ModifiedASMFrame) frames[i]).mergePriority = true;
}
eventInsn = idx - 2;
} else {
jumpModificationState = DO_NOTHING;
}
}
} else // wait for the goto instruction
if (jumpModificationState == WAIT_FOR_INSN_STATE && insn == eventInsn) {
jumpModificationState = DO_NOTHING;
final Frame[] frames = (Frame[]) accessField(Analyzer.class, "frames").get(this);
// this ensures that local variables are kept
if (jumpModification == IFEQ_MOD) {
interpreter.rightMergePriority = true;
final Field top = accessField(Frame.class, "top");
top.setInt(frames[eventInsn], top.getInt(frames[eventInsn + 1]));
frames[eventInsn + 1].merge(frames[eventInsn], interpreter);
} else // finally set a merge priority for the last instruction of the loop (before the IF expression)
if (jumpModification == IFNE_MOD) {
((ModifiedASMFrame) frames[eventInsn + 1]).mergePriority = true;
}
}
} catch (Exception e) {
throw new CodeAnalyzerException("Unable to do jump modifications.", e);
}
}
Aggregations