use of org.objectweb.asm.Opcodes.INVOKEINTERFACE in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method all_opcodes_should_be_visited.
@Test
public void all_opcodes_should_be_visited() throws Exception {
Instructions ins = new Instructions();
Predicate<Integer> filterReturnAndThrow = opcode -> !((Opcodes.IRETURN <= opcode && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW);
NO_OPERAND_INSN.stream().filter(filterReturnAndThrow).forEach(ins::visitInsn);
INT_INSN.forEach(i -> ins.visitIntInsn(i, 0));
VAR_INSN.forEach(i -> ins.visitVarInsn(i, 0));
TYPE_INSN.forEach(i -> ins.visitTypeInsn(i, "java/lang/Object"));
FIELD_INSN.forEach(i -> ins.visitFieldInsn(i, "java/lang/Object", "foo", "D(D)"));
METHOD_INSN.forEach(i -> ins.visitMethodInsn(i, "java/lang/Object", "foo", "()V", i == INVOKEINTERFACE));
JUMP_INSN.forEach(i -> {
Label jumpLabel = new Label();
ins.visitJumpInsn(i, jumpLabel);
ins.visitLabel(jumpLabel);
});
ins.visitLdcInsn("a");
ins.visitIincInsn(0, 1);
Handle handle = new Handle(H_INVOKESTATIC, "", "", "()V", false);
ins.visitInvokeDynamicInsn("sleep", "()V", handle);
ins.visitLookupSwitchInsn(new Label(), new int[] {}, new Label[] {});
ins.visitMultiANewArrayInsn("B", 1);
Label l0 = new Label();
Label dflt = new Label();
Label case0 = new Label();
ins.visitTableSwitchInsn(0, 1, dflt, case0);
ins.visitLabel(dflt);
ins.visitInsn(NOP);
ins.visitLabel(l0);
ins.visitInsn(NOP);
BytecodeCFG cfg = ins.cfg();
Multiset<String> cfgOpcodes = cfgOpcodes(cfg);
List<String> collect = Instructions.ASM_OPCODES.stream().filter(filterReturnAndThrow).map(op -> Printer.OPCODES[op]).collect(Collectors.toList());
assertThat(cfgOpcodes).containsAll(collect);
}
use of org.objectweb.asm.Opcodes.INVOKEINTERFACE in project groovy by apache.
the class StaticTypesCallSiteWriter method writeListDotProperty.
private void writeListDotProperty(final Expression receiver, final String propertyName, final boolean safe) {
// for lists, replace list.foo with:
// def result = new ArrayList(list.size())
// for (item in list) result.add(item.foo)
// result
ClassNode componentType = receiver.getNodeMetaData(StaticCompilationMetadataKeys.COMPONENT_TYPE);
if (componentType == null) {
componentType = OBJECT_TYPE;
}
CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
Label exit = new Label();
if (safe) {
receiver.visit(controller.getAcg());
Label doGet = new Label();
mv.visitJumpInsn(IFNONNULL, doGet);
controller.getOperandStack().remove(1);
mv.visitInsn(ACONST_NULL);
mv.visitJumpInsn(GOTO, exit);
mv.visitLabel(doGet);
}
Variable tmpList = varX("tmpList", ClassHelper.make(ArrayList.class));
int var = compileStack.defineTemporaryVariable(tmpList, false);
Variable iterator = varX("iterator", Iterator_TYPE);
int it = compileStack.defineTemporaryVariable(iterator, false);
Variable nextVar = varX("next", componentType);
final int next = compileStack.defineTemporaryVariable(nextVar, false);
mv.visitTypeInsn(NEW, "java/util/ArrayList");
mv.visitInsn(DUP);
receiver.visit(controller.getAcg());
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I", true);
controller.getOperandStack().remove(1);
mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "(I)V", false);
mv.visitVarInsn(ASTORE, var);
Label l1 = new Label();
mv.visitLabel(l1);
receiver.visit(controller.getAcg());
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "iterator", "()Ljava/util/Iterator;", true);
controller.getOperandStack().remove(1);
mv.visitVarInsn(ASTORE, it);
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitVarInsn(ALOAD, it);
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true);
Label l3 = new Label();
mv.visitJumpInsn(IFEQ, l3);
mv.visitVarInsn(ALOAD, it);
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true);
mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(componentType));
mv.visitVarInsn(ASTORE, next);
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitVarInsn(ALOAD, var);
PropertyExpression pexp = propX(bytecodeX(componentType, v -> v.visitVarInsn(ALOAD, next)), propertyName);
pexp.visit(controller.getAcg());
controller.getOperandStack().box();
controller.getOperandStack().remove(1);
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
mv.visitInsn(POP);
Label l5 = new Label();
mv.visitLabel(l5);
mv.visitJumpInsn(GOTO, l2);
mv.visitLabel(l3);
mv.visitVarInsn(ALOAD, var);
if (safe) {
mv.visitLabel(exit);
}
controller.getOperandStack().push(ClassHelper.make(ArrayList.class));
controller.getCompileStack().removeVar(next);
controller.getCompileStack().removeVar(it);
controller.getCompileStack().removeVar(var);
}
Aggregations