List of usage examples for org.objectweb.asm Opcodes INVOKESTATIC
int INVOKESTATIC
To view the source code for org.objectweb.asm Opcodes INVOKESTATIC.
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;/*from w ww .j a v a2s . 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 www . j ava 2 s . 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();/* w w w . ja v a 2 s . com*/ 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; }/*from w ww . j av a 2s .co m*/ } 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; }// w w w. ja v a2 s .c o m } 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.util.AutoBoxing.java
License:Open Source License
private static InsnList boxBoolean() { InsnList il = new InsnList(); il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;")); return il;//from w ww . j a v a 2 s .c o m }
From source file:org.coldswap.util.AutoBoxing.java
License:Open Source License
private static InsnList boxByte() { InsnList il = new InsnList(); il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;")); return il;// ww w . j av a2 s . co m }
From source file:org.coldswap.util.AutoBoxing.java
License:Open Source License
private static InsnList boxChar() { InsnList il = new InsnList(); il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;")); return il;/*from ww w . ja v a2 s . c om*/ }
From source file:org.coldswap.util.AutoBoxing.java
License:Open Source License
private static InsnList boxShort() { InsnList il = new InsnList(); il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;")); return il;/*ww w . ja v a 2s .co m*/ }
From source file:org.coldswap.util.AutoBoxing.java
License:Open Source License
private static InsnList boxInt() { InsnList il = new InsnList(); il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;")); return il;//from w ww . j av a2 s . com }