List of usage examples for org.objectweb.asm Opcodes INVOKEVIRTUAL
int INVOKEVIRTUAL
To view the source code for org.objectweb.asm Opcodes INVOKEVIRTUAL.
Click Source Link
From source file:org.codehaus.groovy.reflection.MethodHandleFactory.java
License:Apache License
private static void genInvokeWithFixedParams(ClassWriter cw, Method method) { MethodVisitor mv;// w w w .j av a 2 s . co m final int pc = method.getParameterTypes().length; if (pc <= 4) { StringBuilder pdescb = new StringBuilder(); for (int i = 0; i != pc; ++i) pdescb.append("Ljava/lang/Object;"); String pdesc = pdescb.toString(); mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke", "(Ljava/lang/Object;" + pdesc + ")Ljava/lang/Object;", null, EXCEPTIONS); mv.visitCode(); Class callClass = method.getDeclaringClass(); boolean useInterface = callClass.isInterface(); String type = BytecodeHelper.getClassInternalName(callClass.getName()); String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameterTypes()); // make call if (Modifier.isStatic(method.getModifiers())) { MethodHandleFactory.genLoadParametersDirect(2, mv, method); mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor); } else { mv.visitVarInsn(Opcodes.ALOAD, 1); BytecodeHelper.doCast(mv, callClass); MethodHandleFactory.genLoadParametersDirect(2, mv, method); mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type, method.getName(), descriptor); } BytecodeHelper.box(mv, method.getReturnType()); if (method.getReturnType() == void.class) { mv.visitInsn(Opcodes.ACONST_NULL); } mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } }
From source file:org.codehaus.groovy.reflection.MethodHandleFactory.java
License:Apache License
private static void genInvokeWithFixedPrimitiveParams(ClassWriter cw, Method method) { MethodVisitor mv;//from w w w. j a v a 2s . c o m final Class<?>[] pt = method.getParameterTypes(); final int pc = pt.length; if (pc > 0 && pc <= 3) { StringBuilder pdescb = new StringBuilder(); boolean hasPrimitive = false; for (int i = 0; i != pc; ++i) if (pt[i].isPrimitive()) { hasPrimitive = true; pdescb.append(BytecodeHelper.getTypeDescription(pt[i])); } else pdescb.append("Ljava/lang/Object;"); if (!hasPrimitive) return; String pdesc = pdescb.toString(); mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke", "(Ljava/lang/Object;" + pdesc + ")Ljava/lang/Object;", null, EXCEPTIONS); mv.visitCode(); Class callClass = method.getDeclaringClass(); boolean useInterface = callClass.isInterface(); String type = BytecodeHelper.getClassInternalName(callClass.getName()); String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameterTypes()); // make call if (Modifier.isStatic(method.getModifiers())) { MethodHandleFactory.genLoadParametersPrimitiveDirect(2, mv, method); mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor); } else { mv.visitVarInsn(Opcodes.ALOAD, 1); BytecodeHelper.doCast(mv, callClass); MethodHandleFactory.genLoadParametersPrimitiveDirect(2, mv, method); mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type, method.getName(), descriptor); } BytecodeHelper.box(mv, method.getReturnType()); if (method.getReturnType() == void.class) { mv.visitInsn(Opcodes.ACONST_NULL); } mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } }
From source file:org.codehaus.groovy.runtime.callsite.CallSiteGenerator.java
License:Apache License
private static MethodVisitor writeMethod(ClassWriter cw, String name, int argumentCount, final String superClass, CachedMethod cachedMethod, String receiverType, String parameterDescription, boolean useArray) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "call" + name, "(L" + receiverType + ";" + parameterDescription + ")Ljava/lang/Object;", null, null); mv.visitCode();/*from w w w . java 2 s.c o m*/ final Label tryStart = new Label(); mv.visitLabel(tryStart); // call for checking if method is still valid for (int i = 0; i < argumentCount; ++i) mv.visitVarInsn(Opcodes.ALOAD, i); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClass, "checkCall", "(Ljava/lang/Object;" + parameterDescription + ")Z", false); Label l0 = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, l0); // valid method branch Class callClass = cachedMethod.getDeclaringClass().getTheClass(); boolean useInterface = callClass.isInterface(); String type = BytecodeHelper.getClassInternalName(callClass.getName()); String descriptor = BytecodeHelper.getMethodDescriptor(cachedMethod.getReturnType(), cachedMethod.getNativeParameterTypes()); // prepare call int invokeMethodCode = Opcodes.INVOKEVIRTUAL; if (cachedMethod.isStatic()) { invokeMethodCode = Opcodes.INVOKESTATIC; } else { mv.visitVarInsn(Opcodes.ALOAD, 1); BytecodeHelper.doCast(mv, callClass); if (useInterface) invokeMethodCode = Opcodes.INVOKEINTERFACE; } Class<?>[] parameters = cachedMethod.getPT(); int size = parameters.length; for (int i = 0; i < size; i++) { if (useArray) { // unpack argument from Object[] mv.visitVarInsn(Opcodes.ALOAD, 2); BytecodeHelper.pushConstant(mv, i); mv.visitInsn(Opcodes.AALOAD); } else { mv.visitVarInsn(Opcodes.ALOAD, i + 2); } // cast argument to parameter class, inclusive unboxing // for methods with primitive types BytecodeHelper.doCast(mv, parameters[i]); } // make call mv.visitMethodInsn(invokeMethodCode, type, cachedMethod.getName(), descriptor, useInterface); // produce result BytecodeHelper.box(mv, cachedMethod.getReturnType()); if (cachedMethod.getReturnType() == void.class) { mv.visitInsn(Opcodes.ACONST_NULL); } // return mv.visitInsn(Opcodes.ARETURN); // fall back after method change mv.visitLabel(l0); for (int i = 0; i < argumentCount; ++i) mv.visitVarInsn(Opcodes.ALOAD, i); if (!useArray) { mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ArrayUtil", "createArray", "(" + parameterDescription + ")[Ljava/lang/Object;", false); } mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/callsite/CallSiteArray", "defaultCall" + name, "(Lorg/codehaus/groovy/runtime/callsite/CallSite;L" + receiverType + ";[Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitInsn(Opcodes.ARETURN); // exception unwrapping for stackless exceptions final Label tryEnd = new Label(); mv.visitLabel(tryEnd); final Label catchStart = new Label(); mv.visitLabel(catchStart); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "unwrap", "(Lgroovy/lang/GroovyRuntimeException;)Ljava/lang/Throwable;", false); mv.visitInsn(Opcodes.ATHROW); mv.visitTryCatchBlock(tryStart, tryEnd, catchStart, "groovy/lang/GroovyRuntimeException"); mv.visitMaxs(0, 0); mv.visitEnd(); return mv; }
From source file:org.coldswap.asm.method.PublicFloatMethodReplacer.java
License:Open Source License
private InsnList replaceReturn(InsnList insnList, Type retType) { final Type rretType = retType; int retOpcode = MethodUtil.getRetOpcodeToReplace(retType); for (int i = 0; i < insnList.size(); i++) { AbstractInsnNode absIns = insnList.get(i); int opcode = absIns.getOpcode(); if (opcode == retOpcode) { // if tries to return a Reference type into a primitive then // remove the unbox( we return an Object). If a primitive is returned // into a primitive then we must try to box from primitive to Object/Integer, etc.. // check if an unbox takes place before return final boolean[] isBoxUnbox = { false, false }; AbstractInsnNode valueOf = null; AbstractInsnNode primitiveValue = null; if (i > 1) { valueOf = insnList.get(i - 1); primitiveValue = insnList.get(i - 2); if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) { valueOf.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if (AutoBoxing.isPrimitive(rretType.getDescriptor())) { if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) { isBoxUnbox[0] = true; }/*w w w . j a v a2s . c om*/ } super.visitMethodInsn(i, s, s2, s3); } }); } if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) { primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) { isBoxUnbox[1] = true; } super.visitMethodInsn(i, s, s2, s3); } }); } } if (isBoxUnbox[0] && isBoxUnbox[1]) { // remove indexes insnList.remove(valueOf); insnList.remove(primitiveValue); } else { InsnList iList = new InsnList(); iList.add(AutoBoxing.box(retType)); iList.add(new InsnNode(Opcodes.ARETURN)); insnList.insertBefore(absIns, iList); insnList.remove(absIns); } } } return insnList; }
From source file:org.coldswap.asm.method.PublicObjectMethodReplacer.java
License:Open Source License
private InsnList replaceReturn(InsnList insnList, Type retType) { final Type rretType = retType; int retOpcode = MethodUtil.getRetOpcodeToReplace(retType); for (int i = 0; i < insnList.size(); i++) { AbstractInsnNode absIns = insnList.get(i); int opcode = absIns.getOpcode(); if (opcode == retOpcode) { // if tries to return a Reference type into a primitive then // remove the unbox( we return an Object). If a primitive is returned // into a primitive then we must try to box from primitive to Object/Integer, etc.. // check if an unbox takes place before return final boolean[] isBoxUnbox = { false, false }; AbstractInsnNode valueOf = null; AbstractInsnNode primitiveValue = null; if (i > 1) { valueOf = insnList.get(i - 1); primitiveValue = insnList.get(i - 2); if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) { valueOf.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if (AutoBoxing.isPrimitive(rretType.getDescriptor())) { if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) { isBoxUnbox[0] = true; }/*from ww w. j a va2 s . c om*/ } super.visitMethodInsn(i, s, s2, s3); } }); } if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) { primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) { isBoxUnbox[1] = true; } super.visitMethodInsn(i, s, s2, s3); } }); } } if (isBoxUnbox[0] && isBoxUnbox[1]) { // remove indexes insnList.remove(valueOf); insnList.remove(primitiveValue); } else { InsnList iList = new InsnList(); iList.add(AutoBoxing.box(retType)); iList.add(new InsnNode(Opcodes.ARETURN)); insnList.insertBefore(absIns, iList); insnList.remove(absIns); } } } return insnList; }
From source file:org.coldswap.asm.VirtualMethodReplacer.java
License:Open Source License
@Override public MethodNode replaceInvoke(MethodNode methodNode) { InsnList instructions = methodNode.instructions; Iterator it = instructions.iterator(); while (it.hasNext()) { AbstractInsnNode code = (AbstractInsnNode) it.next(); if (code.getOpcode() == Opcodes.INVOKEVIRTUAL) { // check if methodToReplace is called final boolean[] callFounded = new boolean[] { false }; code.accept(new MethodVisitor(Opcodes.ASM5) { @Override/*from ww w . ja v a2s. co m*/ public void visitMethodInsn(int i, String s, String s2, String s3) { if (s.equals(classContainer) && s2.equals(methodName)) { callFounded[0] = true; } super.visitMethodInsn(i, s, s2, s3); } }); if (callFounded[0]) { // if the return type is primitive and the value is not discarded, unbox if (AutoBoxing.isPrimitive(retType.getDescriptor())) { AbstractInsnNode codeNext = code.getNext(); boolean discarded = false; // if returning primitive double or long and it is discarded with a pop2 than discard with // simple pop, because we use an Object as return value. if (codeNext.getOpcode() == Opcodes.POP2 && (retType.getDescriptor().equals("D") || retType.getDescriptor().equals("J"))) { instructions.set(codeNext, new InsnNode(Opcodes.POP)); } if (codeNext.getOpcode() == Opcodes.POP || codeNext.getOpcode() == Opcodes.POP2) { discarded = true; } if (!discarded) { instructions.insert(code, AutoBoxing.unbox(retType)); } } // replace call with a custom call String newMethodName; AbstractInsnNode newInvoke = null; if (Constants.VAROBJECT.equals(methodType)) { newMethodName = TransformerNameGenerator.getObjectMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "([Ljava/lang/Object;)Ljava/lang/Object;"); } else if (Constants.INT.equals(methodType)) { newMethodName = TransformerNameGenerator.getIntMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "(I)Ljava/lang/Object;"); } else if (Constants.FLOAT.equals(methodType)) { newMethodName = TransformerNameGenerator.getFloatMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "(F)Ljava/lang/Object;"); } else if (Constants.STRING.equals(methodType)) { newMethodName = TransformerNameGenerator.getStringMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "(Ljava/lang/String;)Ljava/lang/Object;"); } else if (Constants.LONG.equals(methodType)) { newMethodName = TransformerNameGenerator.getLongMethodNameWithCounter(classContainer, methodNumber); newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName, "(J)Ljava/lang/Object;"); } if (newInvoke != null) { instructions.set(code, newInvoke); } } } } return methodNode; }
From source file:org.coldswap.util.AutoBoxing.java
License:Open Source License
private static InsnList unboxBoolean() { InsnList il = new InsnList(); il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Boolean")); il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z")); return il;//from w w w . ja va 2s .c om }
From source file:org.coldswap.util.AutoBoxing.java
License:Open Source License
private static InsnList unboxByte() { InsnList il = new InsnList(); il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number")); il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "byteValue", "()B")); return il;/*from w w w. j a va 2s .co m*/ }
From source file:org.coldswap.util.AutoBoxing.java
License:Open Source License
private static InsnList unboxChar() { InsnList il = new InsnList(); il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Character")); il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C")); return il;/*ww w . j a v a 2 s. c om*/ }
From source file:org.coldswap.util.AutoBoxing.java
License:Open Source License
private static InsnList unboxShort() { InsnList il = new InsnList(); il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number")); il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "shortValue", "()S")); return il;// ww w . ja v a 2 s . c om }