List of usage examples for org.objectweb.asm MethodVisitor visitIntInsn
public void visitIntInsn(final int opcode, final int operand)
From source file:org.codehaus.groovy.classgen.asm.InvocationWriter.java
License:Apache License
protected boolean writeDirectMethodCall(final MethodNode target, final boolean implicitThis, final Expression receiver, final TupleExpression args) { if (target == null) return false; String methodName = target.getName(); CompileStack compileStack = controller.getCompileStack(); OperandStack operandStack = controller.getOperandStack(); ClassNode declaringClass = target.getDeclaringClass(); ClassNode classNode = controller.getClassNode(); MethodVisitor mv = controller.getMethodVisitor(); int opcode = INVOKEVIRTUAL; if (target.isStatic()) { opcode = INVOKESTATIC;/*w w w . ja va 2s . c om*/ } else if (declaringClass.isInterface()) { opcode = INVOKEINTERFACE; } else if (target.isPrivate() || AsmClassGenerator.isSuperExpression(receiver)) { opcode = INVOKESPECIAL; } // handle receiver int argumentsToRemove = 0; if (opcode != INVOKESTATIC) { if (receiver != null) { // load receiver if not static invocation // todo: fix inner class case if (implicitThis && classNode.getOuterClass() != null && !classNode.isDerivedFrom(declaringClass) && !classNode.implementsInterface(declaringClass)) { // we are calling an outer class method compileStack.pushImplicitThis(false); if (controller.isInClosure()) { new VariableExpression("thisObject").visit(controller.getAcg()); } else { Expression expr = new PropertyExpression(new ClassExpression(declaringClass), "this"); expr.visit(controller.getAcg()); } } else { compileStack.pushImplicitThis(implicitThis); receiver.visit(controller.getAcg()); } operandStack.doGroovyCast(declaringClass); compileStack.popImplicitThis(); argumentsToRemove += 1; } else { mv.visitIntInsn(ALOAD, 0); operandStack.push(classNode); argumentsToRemove += 1; } } int stackSize = operandStack.getStackLength(); String owner = BytecodeHelper.getClassInternalName(declaringClass); ClassNode receiverType = receiver != null ? controller.getTypeChooser().resolveType(receiver, classNode) : declaringClass; if (opcode == INVOKEVIRTUAL && ClassHelper.OBJECT_TYPE.equals(declaringClass)) { // avoid using a narrowed type if the method is defined on object because it can interfere // with delegate type inference in static compilation mode and trigger a ClassCastException receiverType = declaringClass; } if (opcode == INVOKEVIRTUAL) { if (!receiverType.equals(declaringClass) && !ClassHelper.OBJECT_TYPE.equals(declaringClass) && !receiverType.isArray() && !receiverType.isInterface() && !ClassHelper.isPrimitiveType(receiverType) // e.g int.getClass() && receiverType.isDerivedFrom(declaringClass)) { owner = BytecodeHelper.getClassInternalName(receiverType); ClassNode top = operandStack.getTopOperand(); if (!receiverType.equals(top)) { mv.visitTypeInsn(CHECKCAST, owner); } } else if (target.isPublic() && (!Modifier.isPublic(declaringClass.getModifiers()) && !receiverType.equals(declaringClass)) && receiverType.isDerivedFrom(declaringClass) && !receiverType.getPackageName().equals(classNode.getPackageName())) { // GROOVY-6962: package private class, public method owner = BytecodeHelper.getClassInternalName(receiverType); } } loadArguments(args.getExpressions(), target.getParameters()); String desc = BytecodeHelper.getMethodDescriptor(target.getReturnType(), target.getParameters()); mv.visitMethodInsn(opcode, owner, methodName, desc, declaringClass.isInterface()); ClassNode ret = target.getReturnType().redirect(); if (ret == ClassHelper.VOID_TYPE) { ret = ClassHelper.OBJECT_TYPE; mv.visitInsn(ACONST_NULL); } argumentsToRemove += (operandStack.getStackLength() - stackSize); controller.getOperandStack().remove(argumentsToRemove); controller.getOperandStack().push(ret); return true; }
From source file:org.codehaus.groovy.classgen.asm.OperandStack.java
License:Apache License
/** * ensure last marked parameter on the stack is a primitive boolean * if mark==stack size, we assume an empty expression or statement. * was used and we will use the value given in emptyDefault as boolean * if mark==stack.size()-1 the top element will be cast to boolean using * Groovy truth.// w w w .ja v a 2 s. c o m * In other cases we throw a GroovyBugError */ public void castToBool(int mark, boolean emptyDefault) { int size = stack.size(); MethodVisitor mv = controller.getMethodVisitor(); if (mark == size) { // no element, so use emptyDefault if (emptyDefault) { mv.visitIntInsn(BIPUSH, 1); } else { mv.visitIntInsn(BIPUSH, 0); } stack.add(null); } else if (mark == size - 1) { ClassNode last = stack.get(size - 1); // nothing to do in that case if (last == ClassHelper.boolean_TYPE) return; // not a primitive type, so call booleanUnbox if (!ClassHelper.isPrimitiveType(last)) { controller.getInvocationWriter().castNonPrimitiveToBool(last); } else { BytecodeHelper.convertPrimitiveToBoolean(mv, last); } } else { throw new GroovyBugError("operand stack contains " + size + " elements, but we expected only " + mark); } stack.set(mark, ClassHelper.boolean_TYPE); }
From source file:org.codehaus.groovy.classgen.AsmClassGenerator.java
License:Apache License
@Override public void visitArrayExpression(final ArrayExpression expression) { MethodVisitor mv = controller.getMethodVisitor(); ClassNode elementType = expression.getElementType(); String arrayTypeName = BytecodeHelper.getClassInternalName(elementType); List<Expression> sizeExpression = expression.getSizeExpression(); int size = 0; int dimensions = 0; if (sizeExpression != null) { for (Expression element : sizeExpression) { if (element == ConstantExpression.EMPTY_EXPRESSION) break; dimensions += 1;// w w w .j av a 2 s . c o m // let's convert to an int element.visit(this); controller.getOperandStack().doGroovyCast(ClassHelper.int_TYPE); } controller.getOperandStack().remove(dimensions); } else { size = expression.getExpressions().size(); BytecodeHelper.pushConstant(mv, size); } int storeIns = AASTORE; if (sizeExpression != null) { arrayTypeName = BytecodeHelper.getTypeDescription(expression.getType()); mv.visitMultiANewArrayInsn(arrayTypeName, dimensions); } else if (ClassHelper.isPrimitiveType(elementType)) { int primType = 0; if (elementType == ClassHelper.boolean_TYPE) { primType = T_BOOLEAN; storeIns = BASTORE; } else if (elementType == ClassHelper.char_TYPE) { primType = T_CHAR; storeIns = CASTORE; } else if (elementType == ClassHelper.float_TYPE) { primType = T_FLOAT; storeIns = FASTORE; } else if (elementType == ClassHelper.double_TYPE) { primType = T_DOUBLE; storeIns = DASTORE; } else if (elementType == ClassHelper.byte_TYPE) { primType = T_BYTE; storeIns = BASTORE; } else if (elementType == ClassHelper.short_TYPE) { primType = T_SHORT; storeIns = SASTORE; } else if (elementType == ClassHelper.int_TYPE) { primType = T_INT; storeIns = IASTORE; } else if (elementType == ClassHelper.long_TYPE) { primType = T_LONG; storeIns = LASTORE; } mv.visitIntInsn(NEWARRAY, primType); } else { mv.visitTypeInsn(ANEWARRAY, arrayTypeName); } for (int i = 0; i < size; i += 1) { mv.visitInsn(DUP); BytecodeHelper.pushConstant(mv, i); Expression elementExpression = expression.getExpression(i); if (elementExpression == null) { ConstantExpression.NULL.visit(this); } else { elementExpression.visit(this); controller.getOperandStack().doGroovyCast(elementType); } mv.visitInsn(storeIns); controller.getOperandStack().remove(1); } controller.getOperandStack().push(expression.getType()); }
From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java
License:Apache License
private MethodVisitor createGetProxyTargetMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { MethodVisitor mv = super.visitMethod(ACC_PUBLIC | ACC_FINAL, name, desc, signature, exceptions); mv.visitCode();/*from www . java 2 s. c om*/ mv.visitIntInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, proxyName, DELEGATE_OBJECT_FIELD, BytecodeHelper.getTypeDescription(delegateClass)); mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); return null; }
From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java
License:Apache License
private MethodVisitor createConstructor(final int access, final String name, final String desc, final String signature, final String[] exceptions) { Type[] args = Type.getArgumentTypes(desc); StringBuilder newDesc = new StringBuilder("("); for (Type arg : args) { newDesc.append(arg.getDescriptor()); }// w ww .j a v a 2 s . co m newDesc.append("Ljava/util/Map;"); // the closure map if (generateDelegateField) { newDesc.append(BytecodeHelper.getTypeDescription(delegateClass)); } newDesc.append(")V"); MethodVisitor mv = super.visitMethod(access, name, newDesc.toString(), signature, exceptions); mv.visitCode(); initializeDelegateClosure(mv, args); if (generateDelegateField) { initializeDelegateObject(mv, args); } mv.visitVarInsn(ALOAD, 0); int idx = 1; for (Type arg : args) { if (isPrimitive(arg)) { mv.visitIntInsn(getLoadInsn(arg), idx); } else { mv.visitVarInsn(ALOAD, idx); // load argument i } idx += registerLen(arg); } mv.visitMethodInsn(INVOKESPECIAL, BytecodeHelper.getClassInternalName(superClass), "<init>", desc, false); mv.visitInsn(RETURN); int max = idx + 1 + (generateDelegateField ? 1 : 0); mv.visitMaxs(max, max); mv.visitEnd(); return null; }
From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java
License:Apache License
private void initializeDelegateClosure(final MethodVisitor mv, Type[] args) { int idx = 1 + getTypeArgsRegisterLength(args); mv.visitIntInsn(ALOAD, 0); // this mv.visitIntInsn(ALOAD, idx); // constructor arg n is the closure map mv.visitFieldInsn(PUTFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); }
From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java
License:Apache License
private void initializeDelegateObject(final MethodVisitor mv, Type[] args) { int idx = 2 + getTypeArgsRegisterLength(args); mv.visitIntInsn(ALOAD, 0); // this mv.visitIntInsn(ALOAD, idx); // constructor arg n is the closure map mv.visitFieldInsn(PUTFIELD, proxyName, DELEGATE_OBJECT_FIELD, BytecodeHelper.getTypeDescription(delegateClass)); }
From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java
License:Apache License
protected MethodVisitor makeDelegateToClosureCall(final String name, final String desc, final String signature, final String[] exceptions, final int accessFlags) { MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions); // TraceMethodVisitor tmv = new TraceMethodVisitor(mv); // mv = tmv; mv.visitCode();/* w ww .ja v a 2 s. c o m*/ int stackSize = 0; // method body should be: // this.$delegate$closure$methodName.call(new Object[] { method arguments }) Type[] args = Type.getArgumentTypes(desc); int arrayStore = args.length + 1; BytecodeHelper.pushConstant(mv, args.length); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); // stack size = 1 stackSize = 1; int idx = 1; for (int i = 0; i < args.length; i++) { Type arg = args[i]; mv.visitInsn(DUP); // stack size = 2 BytecodeHelper.pushConstant(mv, i); // array index, stack size = 3 // primitive types must be boxed boxPrimitiveType(mv, idx, arg); idx += registerLen(arg); stackSize = Math.max(4, 3 + registerLen(arg)); mv.visitInsn(AASTORE); // store value into array } mv.visitVarInsn(ASTORE, arrayStore); // store array int arrayIndex = arrayStore; mv.visitVarInsn(ALOAD, 0); // load this mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map mv.visitLdcInsn(name); // load method name mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true); arrayStore++; mv.visitVarInsn(ASTORE, arrayStore); // if null, test if wildcard exists Label notNull = new Label(); mv.visitIntInsn(ALOAD, arrayStore); mv.visitJumpInsn(IFNONNULL, notNull); mv.visitVarInsn(ALOAD, 0); // load this mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map mv.visitLdcInsn("*"); // load wildcard mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitVarInsn(ASTORE, arrayStore); mv.visitLabel(notNull); mv.visitVarInsn(ALOAD, arrayStore); mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(this.getClass()), "ensureClosure", "(Ljava/lang/Object;)Lgroovy/lang/Closure;", false); mv.visitVarInsn(ALOAD, arrayIndex); // load argument array stackSize++; mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Closure", "call", "([Ljava/lang/Object;)Ljava/lang/Object;", false); // call closure unwrapResult(mv, desc); mv.visitMaxs(stackSize, arrayStore + 1); mv.visitEnd(); // System.out.println("tmv.getText() = " + tmv.getText()); return null; }
From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java
License:Apache License
private void boxPrimitiveType(MethodVisitor mv, int idx, Type arg) { if (isPrimitive(arg)) { mv.visitIntInsn(getLoadInsn(arg), idx); String wrappedType = getWrappedClassDescriptor(arg); mv.visitMethodInsn(INVOKESTATIC, wrappedType, "valueOf", "(" + arg.getDescriptor() + ")L" + wrappedType + ";", false); } else {//from w ww . jav a2 s. c o m mv.visitVarInsn(ALOAD, idx); // load argument i } }
From source file:org.compass.core.util.reflection.asm.AsmReflectionMethodGenerator.java
License:Apache License
/** * Creates the method invoking wrapper method. */// w w w .jav a 2 s. c o m private static void createMethod(Class clazz, String name, Method refMethod, ClassWriter cw, String methodName, String desc, boolean argsParams, boolean returnValue, Class... parameterTypes) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS, methodName, desc, null, null); boolean isStatic = Modifier.isStatic(refMethod.getModifiers()); boolean isInteface = Modifier.isInterface(refMethod.getDeclaringClass().getModifiers()); final int invokeCode; if (isStatic) { invokeCode = Opcodes.INVOKESTATIC; } else { invokeCode = isInteface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL; mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(clazz)); } if (argsParams) { for (int i = 0; i < parameterTypes.length; ++i) { mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitIntInsn(Opcodes.BIPUSH, i); mv.visitInsn(Opcodes.AALOAD); prepareParameter(mv, Type.getType(parameterTypes[i])); } } else { for (int i = 0; i < parameterTypes.length; ++i) { mv.visitVarInsn(Opcodes.ALOAD, i + 2); prepareParameter(mv, Type.getType(parameterTypes[i])); } } mv.visitMethodInsn(invokeCode, Type.getInternalName(clazz), name, Type.getMethodDescriptor(refMethod)); if (returnValue) { prepareResult(mv, refMethod); mv.visitInsn(Opcodes.ARETURN); } else { mv.visitInsn(Opcodes.RETURN); } mv.visitMaxs(1, 1); // ignored since ClassWriter set as ClassWriter.COMPUTE_MAXS mv.visitEnd(); }