Java Code Examples for org.objectweb.asm.commons.GeneratorAdapter#visitVarInsn()
The following examples show how to use
org.objectweb.asm.commons.GeneratorAdapter#visitVarInsn() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ByteCodeUtils.java From Stark with Apache License 2.0 | 7 votes |
/** * Given an array on the stack, it loads it with the values of the given variables stating at * offset. */ static void loadVariableArray( @NonNull GeneratorAdapter mv, @NonNull List<LocalVariable> variables, int offset) { // we need to maintain the stack index when loading parameters from, as for long and double // values, it uses 2 stack elements, all others use only 1 stack element. for (int i = offset; i < variables.size(); i++) { LocalVariable variable = variables.get(i); // duplicate the array of objects reference, it will be used to store the value in. mv.dup(); // index in the array of objects to store the boxed parameter. mv.push(i); // Pushes the appropriate local variable on the stack mv.visitVarInsn(variable.type.getOpcode(Opcodes.ILOAD), variable.var); // potentially box up intrinsic types. mv.box(variable.type); // store it in the array mv.arrayStore(Type.getType(Object.class)); } }
Example 2
Source File: ByteCodeUtils.java From Stark with Apache License 2.0 | 6 votes |
/** * Given an array with values at the top of the stack, the values are unboxed and stored * on the given variables. The array is popped from the stack. */ static void restoreVariables( @NonNull GeneratorAdapter mv, @NonNull List<LocalVariable> variables) { for (int i = 0; i < variables.size(); i++) { LocalVariable variable = variables.get(i); // Duplicates the array on the stack; mv.dup(); // Sets up the index mv.push(i); // Gets the Object value mv.arrayLoad(Type.getType(Object.class)); // Unboxes to the type of the local variable mv.unbox(variable.type); // Restores the local variable mv.visitVarInsn(variable.type.getOpcode(Opcodes.ISTORE), variable.var); } // Pops the array from the stack. mv.pop(); }
Example 3
Source File: ByteCodeUtils.java From Aceso with Apache License 2.0 | 6 votes |
/** * Given an array on the stack, it loads it with the values of the given variables stating at * offset. */ static void loadVariableArray( GeneratorAdapter mv, List<LocalVariable> variables, int offset) { // we need to maintain the stack index when loading parameters from, as for long and double // values, it uses 2 stack elements, all others use only 1 stack element. for (int i = offset; i < variables.size(); i++) { LocalVariable variable = variables.get(i); // duplicate the array of objects reference, it will be used to store the value in. mv.dup(); // index in the array of objects to store the boxed parameter. mv.push(i); // Pushes the appropriate local variable on the stack mv.visitVarInsn(variable.type.getOpcode(Opcodes.ILOAD), variable.var); // potentially box up intrinsic types. mv.box(variable.type); // store it in the array mv.arrayStore(Type.getType(Object.class)); } }
Example 4
Source File: RobustAsmUtils.java From Robust with Apache License 2.0 | 5 votes |
private static void prepareMethodParameters(GeneratorAdapter mv, String className, List<Type> args, Type returnType, boolean isStatic, int methodId) { //第一个参数:new Object[]{...};,如果方法没有参数直接传入new Object[0] if (args.size() == 0) { mv.visitInsn(Opcodes.ICONST_0); mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); } else { createObjectArray(mv, args, isStatic); } //第二个参数:this,如果方法是static的话就直接传入null if (isStatic) { mv.visitInsn(Opcodes.ACONST_NULL); } else { mv.visitVarInsn(Opcodes.ALOAD, 0); } //第三个参数:changeQuickRedirect mv.visitFieldInsn(Opcodes.GETSTATIC, className, REDIRECTFIELD_NAME, REDIRECTCLASSNAME); //第四个参数:false,标志是否为static mv.visitInsn(isStatic ? Opcodes.ICONST_1 : Opcodes.ICONST_0); //第五个参数: mv.push(methodId); //第六个参数:参数class数组 createClassArray(mv, args); //第七个参数:返回值类型class createReturnClass(mv, returnType); }
Example 5
Source File: PointcutClassVisitor.java From glowroot with Apache License 2.0 | 5 votes |
@Override public void visitEnd() { if (onBeforeMethodVisitor != null) { int access = onBeforeMethodVisitor.access; String name = onBeforeMethodVisitor.name; String descriptor = "(Z" + onBeforeMethodVisitor.descriptor.substring(1); String signature = onBeforeMethodVisitor.signature; String[] exceptions = onBeforeMethodVisitor.exceptions; GeneratorAdapter mv = new GeneratorAdapter( cw.visitMethod(access, name, descriptor, signature, exceptions), access, name, descriptor); mv.visitCode(); mv.visitVarInsn(ILOAD, 0); Label endWithDefaultLabel = new Label(); mv.visitJumpInsn(IFEQ, endWithDefaultLabel); mv.loadArgs(1, mv.getArgumentTypes().length - 1); mv.visitMethodInsn(INVOKESTATIC, checkNotNull(className), name, onBeforeMethodVisitor.descriptor, false); mv.returnValue(); mv.visitLabel(endWithDefaultLabel); mv.visitFrame(F_NEW, 0, new Object[0], 0, new Object[0]); if (mv.getReturnType().getSort() != Type.VOID) { // return value will be ignored when !enabled, but need to return something WeavingMethodVisitor.pushDefault(mv, mv.getReturnType()); } mv.returnValue(); mv.endMethod(); } super.visitEnd(); }
Example 6
Source File: ConstructorRedirection.java From Stark with Apache License 2.0 | 4 votes |
@Override protected void doRedirect(GeneratorAdapter mv, int change) { mv.loadLocal(change); mv.push("init$args." + constructor.args.desc); Type arrayType = Type.getType("[Ljava/lang/Object;"); // init$args args (including this) + locals mv.push(types.size() + 1); mv.newArray(Type.getType(Object.class)); int array = mv.newLocal(arrayType); mv.dup(); mv.storeLocal(array); // "this" is not ready yet, use null instead. mv.dup(); mv.push(0); mv.visitInsn(Opcodes.ACONST_NULL); mv.arrayStore(Type.getType(Object.class)); // Set the arguments in positions 1..(n-1); ByteCodeUtils.loadVariableArray(mv, ByteCodeUtils.toLocalVariables(types), 1); // Skip the this value // Add the locals array at the last position. mv.dup(); // The index of the last position of the array. mv.push(types.size()); // Create the array with all the local variables declared up to this point. ByteCodeUtils.newVariableArray(mv, constructor.variables.subList(0, constructor.localsAtLoadThis)); mv.arrayStore(Type.getType(Object.class)); mv.invokeInterface(MonitorVisitor.CHANGE_TYPE, Method.getMethod("Object access$dispatch(String, Object[])")); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); //// At this point, init$args has been called and the result Object is on the stack. //// The value of that Object is Object[] with exactly n + 2 elements. //// The first element is the resulting local variables //// The second element is a string with the qualified name of the constructor to call. //// The remaining elements are the constructor arguments. // Keep a reference to the new locals array mv.dup(); mv.push(0); mv.arrayLoad(Type.getType("[Ljava/lang/Object;")); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); mv.storeLocal(array); // Call super constructor // Put this behind the returned array mv.visitVarInsn(Opcodes.ALOAD, 0); mv.swap(); // Push a null for the marker parameter. mv.visitInsn(Opcodes.ACONST_NULL); // Invoke the constructor mv.visitMethodInsn(Opcodes.INVOKESPECIAL, constructor.owner, "<init>", DISPATCHING_THIS_SIGNATURE, false); // Dispatch to init$body mv.loadLocal(change); mv.push("init$body." + constructor.body.desc); mv.loadLocal(array); // Now "this" can be set mv.dup(); mv.push(0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.arrayStore(Type.getType(Object.class)); mv.invokeInterface(MonitorVisitor.CHANGE_TYPE, Method.getMethod("Object access$dispatch(String, Object[])")); mv.pop(); }
Example 7
Source File: TBConstructorRedirection.java From atlas with Apache License 2.0 | 4 votes |
@Override protected void doRedirect(GeneratorAdapter mv, int change) { mv.loadLocal(change); mv.push("init$args." + constructor.args.desc); Type arrayType = Type.getType("[Ljava/lang/Object;"); // init$args args (including this) + locals mv.push(types.size() + 1); mv.newArray(Type.getType(Object.class)); int array = mv.newLocal(arrayType); mv.dup(); mv.storeLocal(array); // "this" is not ready yet, use null instead. mv.dup(); mv.push(0); mv.visitInsn(Opcodes.ACONST_NULL); mv.arrayStore(Type.getType(Object.class)); // Set the arguments in positions 1..(n-1); ByteCodeUtils.loadVariableArray(mv, ByteCodeUtils.toLocalVariables(types), 1); // Skip the this value // Add the locals array at the last position. mv.dup(); // The index of the last position of the array. mv.push(types.size()); // Create the array with all the local variables declared up to this point. ByteCodeUtils.newVariableArray(mv, constructor.variables.subList(0, constructor.localsAtLoadThis)); mv.arrayStore(Type.getType(Object.class)); mv.invokeInterface(TBIncrementalVisitor.ALI_CHANGE_TYPE, Method.getMethod("Object ipc$dispatch(String, Object[])")); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); //// At this point, init$args has been called and the result Object is on the stack. //// The value of that Object is Object[] with exactly n + 2 elements. //// The first element is the resulting local variables //// The second element is a string with the qualified name of the constructor to call. //// The remaining elements are the constructor arguments. // Keep a reference to the new locals array mv.dup(); mv.push(0); mv.arrayLoad(Type.getType("[Ljava/lang/Object;")); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); mv.storeLocal(array); // Call super constructor // Put this behind the returned array mv.visitVarInsn(Opcodes.ALOAD, 0); mv.swap(); // Push a null for the marker parameter. mv.visitInsn(Opcodes.ACONST_NULL); // Invoke the constructor mv.visitMethodInsn(Opcodes.INVOKESPECIAL, constructor.owner, "<init>", DISPATCHING_THIS_SIGNATURE, false); // Dispatch to init$body mv.loadLocal(change); mv.push("init$body." + constructor.body.desc); mv.loadLocal(array); // Now "this" can be set mv.dup(); mv.push(0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.arrayStore(Type.getType(Object.class)); mv.invokeInterface(TBIncrementalVisitor.ALI_CHANGE_TYPE, Method.getMethod("Object ipc$dispatch(String, Object[])")); mv.pop(); }
Example 8
Source File: Redirection.java From AnoleFix with MIT License | 4 votes |
/** * Pushes in the stack the value that should be redirected for the given local. */ protected void redirectLocal(GeneratorAdapter mv, int local, Type arg) { mv.visitVarInsn(arg.getOpcode(Opcodes.ILOAD), local); }
Example 9
Source File: ConstructorArgsRedirection.java From AnoleFix with MIT License | 4 votes |
@Override protected void restore(GeneratorAdapter mv, List<Type> args) { // At this point, init$args has been called and the result Object is on the stack. // The value of that Object is Object[] with exactly n + 1 elements. // The first element is a string with the qualified name of the constructor to call. // The remaining elements are the constructtor arguments. // Create a new local that holds the result of init$args call. mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); int constructorArgs = mv.newLocal(Type.getType("[Ljava/lang/Object;")); mv.storeLocal(constructorArgs); // Reinstate local values mv.loadLocal(locals); int stackIndex = 0; for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) { Type arg = args.get(arrayIndex); // Do not restore "this" if (arrayIndex > 0) { // duplicates the array mv.dup(); // index in the array of objects to restore the boxed parameter. mv.push(arrayIndex); // get it from the array mv.arrayLoad(Type.getType(Object.class)); // unbox the argument ByteCodeUtils.unbox(mv, arg); // restore the argument mv.visitVarInsn(arg.getOpcode(Opcodes.ISTORE), stackIndex); } // stack index must progress according to the parameter type we just processed. stackIndex += arg.getSize(); } // pops the array mv.pop(); // Push a null for the marker parameter. mv.loadLocal(constructorArgs); mv.visitInsn(Opcodes.ACONST_NULL); // Invoke the constructor mv.visitMethodInsn(Opcodes.INVOKESPECIAL, thisClassName, "<init>", DISPATCHING_THIS_SIGNATURE, false); mv.goTo(end.getLabel()); }
Example 10
Source File: IncrementalSupportVisitor.java From AnoleFix with MIT License | 4 votes |
/*** * Inserts a trampoline to this class so that the updated methods can make calls to * constructors. * <p> * <p/> * Pseudo code for this trampoline: * <code> * ClassName(Object[] args, Marker unused) { * String name = (String) args[0]; * if (name.equals( * "java/lang/ClassName.(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) { * this((String)arg[1], arg[2]); * return * } * if (name.equals("SuperClassName.(Ljava/lang/String;I)V")) { * super((String)arg[1], (int)arg[2]); * return; * } * ... * StringBuilder $local1 = new StringBuilder(); * $local1.append("Method not found "); * $local1.append(name); * $local1.append(" in " $classType $super implementation"); * throw new $package/InstantReloadException($local1.toString()); * } * </code> */ private void createDispatchingThis() { // Gather all methods from itself and its superclasses to generate a giant constructor // implementation. // This will work fine as long as we don't support adding constructors to classes. final Map<String, MethodNode> uniqueMethods = new HashMap<String, MethodNode>(); addAllNewConstructors(uniqueMethods, classNode, true /*keepPrivateConstructors*/); for (ClassNode parentNode : parentNodes) { addAllNewConstructors(uniqueMethods, parentNode, false /*keepPrivateConstructors*/); } int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC; Method m = new Method(AsmUtils.CONSTRUCTOR, ConstructorArgsRedirection.DISPATCHING_THIS_SIGNATURE); MethodVisitor visitor = super.visitMethod(0, m.getName(), m.getDescriptor(), null, null); final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor); mv.visitCode(); // Mark this code as redirection code Label label = new Label(); mv.visitLineNumber(0, label); // Get and store the constructor canonical name. mv.visitVarInsn(Opcodes.ALOAD, 1); mv.push(0); mv.visitInsn(Opcodes.AALOAD); mv.unbox(Type.getType("Ljava/lang/String;")); final int constructorCanonicalName = mv.newLocal(Type.getType("Ljava/lang/String;")); mv.storeLocal(constructorCanonicalName); new StringSwitch() { @Override void visitString() { mv.loadLocal(constructorCanonicalName); } @Override void visitCase(String canonicalName) { MethodNode methodNode = uniqueMethods.get(canonicalName); String owner = canonicalName.split("\\.")[0]; // Parse method arguments and mv.visitVarInsn(Opcodes.ALOAD, 0); Type[] args = Type.getArgumentTypes(methodNode.desc); int argc = 0; for (Type t : args) { mv.visitVarInsn(Opcodes.ALOAD, 1); mv.push(argc + 1); mv.visitInsn(Opcodes.AALOAD); ByteCodeUtils.unbox(mv, t); argc++; } mv.visitMethodInsn(Opcodes.INVOKESPECIAL, owner, AsmUtils.CONSTRUCTOR, methodNode.desc, false); mv.visitInsn(Opcodes.RETURN); } @Override void visitDefault() { writeMissingMessageWithHash(mv, visitedClassName); } }.visit(mv, uniqueMethods.keySet()); mv.visitMaxs(1, 3); mv.visitEnd(); }
Example 11
Source File: AsmDeltaSpikeProxyClassGenerator.java From deltaspike with Apache License 2.0 | 4 votes |
private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Type proxyType) { Type methodType = Type.getType(method); ArrayList<Type> exceptionsToCatch = new ArrayList<Type>(); for (Class<?> exception : method.getExceptionTypes()) { if (!RuntimeException.class.isAssignableFrom(exception)) { exceptionsToCatch.add(Type.getType(exception)); } } // push the method definition int modifiers = (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED) & method.getModifiers(); Method asmMethod = Method.getMethod(method); GeneratorAdapter mg = new GeneratorAdapter(modifiers, asmMethod, null, getTypes(method.getExceptionTypes()), cw); // copy annotations for (Annotation annotation : method.getDeclaredAnnotations()) { mg.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd(); } mg.visitCode(); Label tryBlockStart = mg.mark(); mg.loadThis(); mg.getField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER); mg.loadThis(); loadCurrentMethod(mg, method, methodType); loadArguments(mg, method, methodType); mg.invokeVirtual(TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER, Method.getMethod("Object invoke(Object, java.lang.reflect.Method, Object[])")); // cast the result mg.unbox(methodType.getReturnType()); // build try catch Label tryBlockEnd = mg.mark(); // push return mg.returnValue(); // catch runtime exceptions and rethrow it Label rethrow = mg.mark(); mg.visitVarInsn(Opcodes.ASTORE, 1); mg.visitVarInsn(Opcodes.ALOAD, 1); mg.throwException(); mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, Type.getInternalName(RuntimeException.class)); // catch checked exceptions and rethrow it boolean throwableCatched = false; if (!exceptionsToCatch.isEmpty()) { rethrow = mg.mark(); mg.visitVarInsn(Opcodes.ASTORE, 1); mg.visitVarInsn(Opcodes.ALOAD, 1); mg.throwException(); // catch declared exceptions and rethrow it... for (Type exceptionType : exceptionsToCatch) { if (exceptionType.getClassName().equals(Throwable.class.getName())) { throwableCatched = true; } mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, exceptionType.getInternalName()); } } // if throwable isn't alreached cachted, catch it and wrap it with an UndeclaredThrowableException and throw it if (!throwableCatched) { Type uteType = Type.getType(UndeclaredThrowableException.class); Label wrapAndRethrow = mg.mark(); mg.visitVarInsn(Opcodes.ASTORE, 1); mg.newInstance(uteType); mg.dup(); mg.visitVarInsn(Opcodes.ALOAD, 1); mg.invokeConstructor(uteType, Method.getMethod("void <init>(java.lang.Throwable)")); mg.throwException(); mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, wrapAndRethrow, Type.getInternalName(Throwable.class)); } // finish the method mg.endMethod(); mg.visitMaxs(12, 12); mg.visitEnd(); }