List of usage examples for org.objectweb.asm Opcodes ARETURN
int ARETURN
To view the source code for org.objectweb.asm Opcodes ARETURN.
Click Source Link
From source file:org.jacoco.core.runtime.RuntimeDataTest.java
License:Open Source License
@Test public void testGenerateArgumentArray() throws Exception { final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) }); // Constructor MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]); mv.visitCode();//from w ww. j a va 2 s. com mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); // call() mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]); mv.visitCode(); RuntimeData.generateArgumentArray(1000, "Sample", 15, mv); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(5, 1); mv.visitEnd(); writer.visitEnd(); final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray()); Callable<?> callable = (Callable<?>) loader.newTargetInstance(); final Object[] args = (Object[]) callable.call(); assertEquals(3, args.length, 0.0); assertEquals(Long.valueOf(1000), args[0]); assertEquals("Sample", args[1]); assertEquals(Integer.valueOf(15), args[2]); }
From source file:org.jacoco.core.runtime.RuntimeDataTest.java
License:Open Source License
@Test public void testGenerateAccessCall() throws Exception { final boolean[] probes = data.getExecutionData(Long.valueOf(1234), "Sample", 5).getProbes(); final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) }); // Constructor MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, new String[0]); mv.visitCode();/* w w w .j a v a2 s . c o m*/ mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); // call() mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;"); RuntimeData.generateAccessCall(1234, "Sample", 5, mv); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(6, 1); mv.visitEnd(); writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null); writer.visitEnd(); final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray()); Callable<?> callable = (Callable<?>) loader.getTargetClass().getConstructor(Object.class).newInstance(data); assertSame(probes, callable.call()); }
From source file:org.jacoco.core.test.validation.java5.StructuredLockingTest.java
License:Open Source License
private void assertStructuredLocking(String owner, MethodNode mn) throws Exception { Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(new BasicInterpreter()) { @Override/* w w w . j ava 2s .c o m*/ protected Frame<BasicValue> newFrame(int nLocals, int nStack) { return new LockFrame(nLocals, nStack); } @Override protected Frame<BasicValue> newFrame(Frame<? extends BasicValue> src) { return new LockFrame(src); } }; Frame<BasicValue>[] frames = analyzer.analyze(owner, mn); // Make sure no locks are left when method exits: for (int i = 0; i < frames.length; i++) { AbstractInsnNode insn = mn.instructions.get(i); switch (insn.getOpcode()) { case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.ARETURN: case Opcodes.RETURN: ((LockFrame) frames[i]).assertNoLock("Exit with lock"); break; case Opcodes.ATHROW: List<TryCatchBlockNode> handlers = analyzer.getHandlers(i); if (handlers == null || handlers.isEmpty()) { ((LockFrame) frames[i]).assertNoLock("Exit with lock"); } break; } } // Only instructions protected by a catch-all handler can hold locks: for (int i = 0; i < frames.length; i++) { AbstractInsnNode insn = mn.instructions.get(i); if (insn.getOpcode() > 0) { boolean catchAll = false; List<TryCatchBlockNode> handlers = analyzer.getHandlers(i); if (handlers != null) { for (TryCatchBlockNode node : handlers) { catchAll |= node.type == null; } } if (!catchAll) { ((LockFrame) frames[i]).assertNoLock("No handlers for insn with lock"); } } } }
From source file:org.jboss.byteman.agent.adapter.RuleTriggerMethodAdapter.java
License:Open Source License
@Override public void visitInsn(int opcode) { super.visitInsn(opcode); // look for interesting instructions which need inserting into the CFG switch (opcode) { case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.ARETURN: case Opcodes.RETURN: case Opcodes.ATHROW: { // add this instruction to the current block and then start a new current block cfg.add(opcode);//from w ww . j a v a 2s .co m Label newStart = super.newLabel(); // must call split before visiting the label cfg.split(newStart); visitLabel(newStart); } break; case Opcodes.MONITORENTER: case Opcodes.MONITOREXIT: { // just add this instruction to the current block cfg.add(opcode); } break; default: { cfg.add(opcode); } } }
From source file:org.jooby.internal.apitool.BytecodeRouteParser.java
License:Apache License
@SuppressWarnings("unchecked") private java.lang.reflect.Type returnType(final ClassLoader loader, final MethodNode m) throws Exception { return new Insns(m).last().prev().filter(and(is(InsnNode.class), opcode(Opcodes.ARETURN))).findFirst() .map(AbstractInsnNode::getPrevious).map(previous -> { /** return 1; return true; return new Foo(); */ if (previous instanceof MethodInsnNode) { MethodInsnNode minnsn = ((MethodInsnNode) previous); if (minnsn.name.equals("<init>")) { return loadType(loader, minnsn.owner); }/*from ww w . j a v a2 s . c o m*/ String desc = minnsn.desc; java.lang.reflect.Type type = TypeDescriptorParser.parse(loader, desc); if (type.getTypeName().equals(Result.class.getName())) { return new TypeWithStatus(type, statusCodeFor(minnsn)); } return type; } /** return "String" | int | double */ if (previous instanceof LdcInsnNode) { Object cst = ((LdcInsnNode) previous).cst; if (cst instanceof Type) { return TypeDescriptorParser.parse(loader, ((Type) cst).getDescriptor()); } return cst.getClass(); } /** return variable */ if (previous instanceof VarInsnNode) { VarInsnNode varInsn = (VarInsnNode) previous; return localVariable(loader, m, varInsn); } return Object.class; }).orElse(Object.class); }
From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java
License:Open Source License
/** * Reads the current {@link InsnNode} instruction and returns a {@link Statement} or {@code null} * if the instruction is not a full statement (in that case, the instruction is stored in the * given Expression {@link Stack})./*from w w w . ja v a2s.co m*/ * * @param insnNode the instruction to read * @param expressionStack the expression stack to put on or pop from. * @param localVariables the local variables * @return a {@link List} of {@link Statement} or empty list if no {@link Statement} was created * after reading the current instruction. * @see <a href="https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings">Java bytcode * instruction listings on Wikipedia</a> */ private List<Statement> readInstruction(final InsnCursor insnCursor, final Stack<Expression> expressionStack, final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) { final List<Statement> statements = new ArrayList<>(); final AbstractInsnNode insnNode = insnCursor.getCurrent(); switch (insnNode.getOpcode()) { // return a reference from a method case Opcodes.ARETURN: // return an integer from a method case Opcodes.IRETURN: statements.add(new ReturnStatement(expressionStack.pop())); break; // return void from method case Opcodes.RETURN: // wrap all pending expressions into ExpressionStatements while (!expressionStack.isEmpty()) { final Expression pendingExpression = expressionStack.pop(); statements.add(new ExpressionStatement(pendingExpression)); } break; // push a null reference onto the stack case Opcodes.ACONST_NULL: expressionStack.add(new NullLiteral()); break; // load the int value 0 onto the stack case Opcodes.ICONST_0: // applies for byte, short, int and boolean expressionStack.add(new NumberLiteral(0)); break; // load the int value 1 onto the stack case Opcodes.ICONST_1: // applies for byte, short, int and boolean expressionStack.add(new NumberLiteral(1)); break; // load the int value 2 onto the stack case Opcodes.ICONST_2: expressionStack.add(new NumberLiteral(2)); break; // load the int value 3 onto the stack case Opcodes.ICONST_3: expressionStack.add(new NumberLiteral(3)); break; // load the int value 4 onto the stack case Opcodes.ICONST_4: expressionStack.add(new NumberLiteral(4)); break; // load the int value 5 onto the stack case Opcodes.ICONST_5: expressionStack.add(new NumberLiteral(5)); break; // push the long 0 onto the stack case Opcodes.LCONST_0: expressionStack.add(new NumberLiteral(0L)); break; // push the long 1 onto the stack case Opcodes.LCONST_1: expressionStack.add(new NumberLiteral(1L)); break; // push the 0.0f onto the stack case Opcodes.FCONST_0: expressionStack.add(new NumberLiteral(0f)); break; // push the 1.0f onto the stack case Opcodes.FCONST_1: expressionStack.add(new NumberLiteral(1f)); break; // push the 2.0f onto the stack case Opcodes.FCONST_2: expressionStack.add(new NumberLiteral(2f)); break; // push the constant 0.0 onto the stack case Opcodes.DCONST_0: expressionStack.add(new NumberLiteral(0d)); break; // push the constant 1.0 onto the stack case Opcodes.DCONST_1: expressionStack.add(new NumberLiteral(1d)); break; // compare two longs values case Opcodes.LCMP: // compare two doubles case Opcodes.DCMPL: // compare two doubles case Opcodes.DCMPG: // compare two floats case Opcodes.FCMPL: // compare two floats case Opcodes.FCMPG: statements.addAll( readJumpInstruction(insnCursor.next(), expressionStack, capturedArguments, localVariables)); break; // add 2 ints case Opcodes.IADD: expressionStack.add(readOperation(Operator.ADD, expressionStack)); break; // int subtract case Opcodes.ISUB: expressionStack.add(readOperation(Operator.SUBTRACT, expressionStack)); break; // multiply 2 integers case Opcodes.IMUL: expressionStack.add(readOperation(Operator.MULTIPLY, expressionStack)); break; // divide 2 integers case Opcodes.IDIV: expressionStack.add(readOperation(Operator.DIVIDE, expressionStack)); break; // negate int case Opcodes.INEG: expressionStack.add(inverseInteger(expressionStack)); break; // discard the top value on the stack case Opcodes.POP: statements.add(new ExpressionStatement(expressionStack.pop())); break; // duplicate the value on top of the stack case Opcodes.DUP: expressionStack.push(expressionStack.peek()); break; // insert a copy of the top value into the stack two values from the top. case Opcodes.DUP_X1: expressionStack.add(expressionStack.size() - 2, expressionStack.peek()); break; // store into a reference in an array case Opcodes.AASTORE: readArrayStoreInstruction(insnNode, expressionStack); break; // converts Float to Double -> ignored. case Opcodes.F2D: break; default: throw new AnalyzeException( "Bytecode instruction with OpCode '" + insnNode.getOpcode() + "' is not supported."); } return statements; }
From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java
License:Apache License
public static void doReturn(MethodVisitor mv, ClassNode returnType) { if (returnType == double_TYPE) { mv.visitInsn(Opcodes.DRETURN);// w ww . j a v a2s . c o m } else if (returnType == float_TYPE) { mv.visitInsn(Opcodes.FRETURN); } else if (returnType == long_TYPE) { mv.visitInsn(Opcodes.LRETURN); } else if (returnType == boolean_TYPE || returnType == char_TYPE || returnType == byte_TYPE || returnType == int_TYPE || returnType == short_TYPE) { //byte,short,boolean,int are all IRETURN mv.visitInsn(Opcodes.IRETURN); } else if (returnType == VOID_TYPE) { mv.visitInsn(Opcodes.RETURN); } else { mv.visitInsn(Opcodes.ARETURN); } }
From source file:org.mbte.groovypp.compiler.ClosureUtil.java
License:Apache License
public static void createClosureConstructor(final ClassNode newType, final Parameter[] constrParams, Expression superArgs, CompilerTransformer compiler) { final ClassNode superClass = newType.getSuperClass(); final Parameter[] finalConstrParams; final ArgumentListExpression superCallArgs = new ArgumentListExpression(); if (superArgs != null) { final ArgumentListExpression args = (ArgumentListExpression) superArgs; if (args.getExpressions().size() > 0) { Parameter[] newParams = new Parameter[constrParams.length + args.getExpressions().size()]; System.arraycopy(constrParams, 0, newParams, 0, constrParams.length); for (int i = 0; i != args.getExpressions().size(); ++i) { final Parameter parameter = new Parameter(args.getExpressions().get(i).getType(), "$super$param$" + i); newParams[i + constrParams.length] = parameter; superCallArgs.addExpression(new VariableExpression(parameter)); }// w ww . java 2 s . c o m finalConstrParams = newParams; } else finalConstrParams = constrParams; } else { if (superClass == ClassHelper.CLOSURE_TYPE) { if (constrParams.length > 0) { superCallArgs.addExpression(new VariableExpression(constrParams[0])); superCallArgs.addExpression(new VariableExpression(constrParams[0])); } else if (compiler.methodNode.isStatic() && !compiler.classNode.getName().endsWith("$TraitImpl")) { ClassNode cn = compiler.classNode; superCallArgs.addExpression(new ClassExpression(cn)); superCallArgs.addExpression(new ClassExpression(getOutermostClass(cn))); } else { superCallArgs.addExpression(ConstantExpression.NULL); superCallArgs.addExpression(ConstantExpression.NULL); } } finalConstrParams = constrParams; } ConstructorCallExpression superCall = new ConstructorCallExpression(ClassNode.SUPER, superCallArgs); BytecodeSequence fieldInit = new BytecodeSequence(new BytecodeInstruction() { public void visit(MethodVisitor mv) { for (int i = 0, k = 1; i != constrParams.length; i++) { mv.visitVarInsn(Opcodes.ALOAD, 0); final ClassNode type = constrParams[i].getType(); if (ClassHelper.isPrimitiveType(type)) { if (type == ClassHelper.long_TYPE) { mv.visitVarInsn(Opcodes.LLOAD, k++); k++; } else if (type == ClassHelper.double_TYPE) { mv.visitVarInsn(Opcodes.DLOAD, k++); k++; } else if (type == ClassHelper.float_TYPE) { mv.visitVarInsn(Opcodes.FLOAD, k++); } else { mv.visitVarInsn(Opcodes.ILOAD, k++); } } else { mv.visitVarInsn(Opcodes.ALOAD, k++); } mv.visitFieldInsn(Opcodes.PUTFIELD, BytecodeHelper.getClassInternalName(newType), constrParams[i].getName(), BytecodeHelper.getTypeDescription(type)); } mv.visitInsn(Opcodes.RETURN); } }); BlockStatement code = new BlockStatement(); code.addStatement(new ExpressionStatement(superCall)); ConstructorNode cn = new ConstructorNode(Opcodes.ACC_PUBLIC, finalConstrParams, ClassNode.EMPTY_ARRAY, code); newType.addConstructor(cn); code.addStatement(fieldInit); CleaningVerifier.getCleaningVerifier().visitClass(newType); compiler.replaceMethodCode(newType, cn); if (newType.getOuterClass() != null && newType.getMethods("methodMissing").isEmpty()) { final ClassNode this0Type = (!compiler.methodNode.isStatic() || compiler.classNode.getName().endsWith("$TraitImpl")) ? newType.getOuterClass() : ClassHelper.CLASS_Type; newType.addMethod("methodMissing", Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE, new Parameter[] { new Parameter(ClassHelper.STRING_TYPE, "name"), new Parameter(ClassHelper.OBJECT_TYPE, "args") }, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() { public void visit(MethodVisitor mv) { mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, BytecodeHelper.getClassInternalName(newType), "this$0", BytecodeHelper.getTypeDescription(this0Type)); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "invokeMethod", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(Opcodes.ARETURN); } })); } }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * @param type//from w ww.ja va2 s .co m * @return the RETURN, IRETURN,... op code, depending on type. */ private static int getReturnOpCode(JavaTypeName type) { switch (type.getTag()) { case JavaTypeName.VOID_TAG: return Opcodes.RETURN; case JavaTypeName.BOOLEAN_TAG: case JavaTypeName.BYTE_TAG: case JavaTypeName.SHORT_TAG: case JavaTypeName.CHAR_TAG: case JavaTypeName.INT_TAG: return Opcodes.IRETURN; case JavaTypeName.LONG_TAG: return Opcodes.LRETURN; case JavaTypeName.DOUBLE_TAG: return Opcodes.DRETURN; case JavaTypeName.FLOAT_TAG: return Opcodes.FRETURN; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: return Opcodes.ARETURN; default: { throw new IllegalArgumentException("invalid type: " + type); } } }
From source file:org.parboiled.compiler.notNullVerification.MyMethodAdapter.java
License:Apache License
@Override public void visitInsn(int opcode) { if (opcode == Opcodes.ARETURN && myIsNotNull) { mv.visitInsn(Opcodes.DUP);//from w w w . ja va2 s.c o m if (myThrowLabel == null) { Label skipLabel = new Label(); mv.visitJumpInsn(Opcodes.IFNONNULL, skipLabel); myThrowLabel = new Label(); mv.visitLabel(myThrowLabel); generateThrow("java/lang/IllegalStateException", "@NotNull method " + getFullMethodName() + " must not return null", skipLabel); } else { mv.visitJumpInsn(Opcodes.IFNULL, myThrowLabel); } } mv.visitInsn(opcode); }