private static void createClassArray(GeneratorAdapter mv, List<Type> args) { // create an array of objects capable of containing all the parameters and optionally the "this" createLocals(mv, args); // 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. int stackIndex = 0; for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) { Type arg = args.get(arrayIndex); // 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(arrayIndex); // Pushes the appropriate local variable on the stack redirectLocal(mv, arg); // mv.visitLdcInsn(Type.getType(arg.getDescriptor())); // potentially box up intrinsic types. // mv.box(arg); mv.arrayStore(Type.getType(Class.class)); // stack index must progress according to the parameter type we just processed. // stackIndex += arg.getSize(); } }
proxyConstructorGenerator.loadThis(); proxyConstructorGenerator.newInstance(TYPE_READ_WRITE_LOCK); proxyConstructorGenerator.dup(); proxyConstructorGenerator.invokeConstructor(TYPE_READ_WRITE_LOCK, METHOD_DEFAULT_CONSTRUCTOR); proxyConstructorGenerator.putField(proxyType, FIELD_READ_WRITE_LOCK, TYPE_READ_WRITE_LOCK); Type methodType = Type.getObjectType(executableMethodWriter.getInternalName()); proxyConstructorGenerator.newInstance(methodType); proxyConstructorGenerator.dup(); proxyConstructorGenerator.loadThis(); proxyConstructorGenerator.invokeConstructor(methodType, new Method(CONSTRUCTOR_NAME, getConstructorDescriptor(proxyFullName)));
/** See {@link GeneratorAdapter#dup()} */ public void dup() { adapter.dup(); }
/** See {@link GeneratorAdapter#dup()} */ public void dup() { adapter.dup(); }
private void storeLockInstance(String mdesc) { Type mtype = Type.getMethodType(mdesc); Type[] argumentTypes = mtype.getArgumentTypes(); int n = argumentTypes.length; int[] localVars = new int[n]; for (int i = 0; i < n; i++) { localVars[i] = mv.newLocal(argumentTypes[n - i - 1]); mv.storeLocal(localVars[i]); } mv.dup(); mv.storeLocal(lockLocal); for (int i = argumentTypes.length - 1; i >= 0; i--) { mv.loadLocal(localVars[i]); } } }
private void add_types(ClassWriter classWriter, Map<String, FieldDescription> beanFields) { Type CLASS_TYPE = Type.getType(Class.class); Method _types = Method.getMethod("java.lang.Class[] _types()"); GeneratorAdapter tg = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, _types, null, null, classWriter); tg.push(beanFields.size()); // array length tg.newArray(CLASS_TYPE); // ar = new Object[size] int i = 0; for (Map.Entry<String, FieldDescription> field : beanFields.entrySet()) { Type fieldType = Type.getType(field.getValue().getTypeDescriptor()); tg.dup();// ar tg.push(i); // index tg.push(fieldType); // value = Type.class tg.arrayStore(CLASS_TYPE); // ar[i]=value; i++; } tg.returnValue(); tg.endMethod(); }
private void add_args(ClassWriter classWriter, Map<String, FieldDescription> beanFields) { Type OBJECT_TYPE = Type.getType(Object.class); Type beanType = Type.getType(getBeanNameWithPackage()); Method _args = Method.getMethod("java.lang.Object[] _args()"); GeneratorAdapter ag = new GeneratorAdapter(Opcodes.ACC_PUBLIC, _args, null, null, classWriter); ag.push(beanFields.size()); // array length ag.newArray(OBJECT_TYPE); // ar = new Object[size] int i = 0; for (Map.Entry<String, FieldDescription> field : beanFields.entrySet()) { Type fieldType = Type.getType(field.getValue().getTypeDescriptor()); ag.dup();// ar ag.push(i); // index ag.loadThis(); // this. ag.getField(beanType, field.getKey(), fieldType); // field ag.valueOf(fieldType); // value = Type.valueOf(this.field) ag.arrayStore(OBJECT_TYPE); // ar[i]=value; i++; } ag.returnValue(); ag.endMethod(); }
/** * Generates the instructions to create and throw an exception. The exception class must have a * constructor with a single String argument. * * @param type the class of the exception to be thrown. * @param message the detailed message of the exception. */ public void throwException(final Type type, final String message) { newInstance(type); dup(); push(message); invokeConstructor(type, Method.getMethod("void <init> (String)")); throwException(); }
/** * 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(); }
/** * Generates the instructions to create and throw an exception. The exception class must have a * constructor with a single String argument. * * @param type the class of the exception to be thrown. * @param message the detailed message of the exception. */ public void throwException(final Type type, final String message) { newInstance(type); dup(); push(message); invokeConstructor(type, Method.getMethod("void <init> (String)")); throwException(); }
/** * Generates the instructions to create and throw an exception. The exception class must have a * constructor with a single String argument. * * @param type the class of the exception to be thrown. * @param message the detailed message of the exception. */ public void throwException(final Type type, final String message) { newInstance(type); dup(); push(message); invokeConstructor(type, Method.getMethod("void <init> (String)")); throwException(); }
private static GeneratorAdapter createClassInitializer(Type selfType, ClassVisitor cw) { GeneratorAdapter clazzInit; clazzInit = new GeneratorAdapter(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, new org.objectweb.asm.commons.Method( "<clinit>", Type.VOID_TYPE, EMPTY_TYPE_ARR), null, EMPTY_TYPE_ARR, cw); clazzInit.visitLdcInsn(selfType.getClassName()); clazzInit.invokeStatic(JAVA_LANG_CLASS_TYPE, CLASS_FORNAME_METHOD); clazzInit .invokeStatic(PROXY_HELPER_TYPE, HELPER_GET_PROXY_CLASS_METHOD); clazzInit.dup(); clazzInit.putStatic(selfType, PROXY_CLASS_FIELD_NAME, PROXY_CLASS_TYPE); return clazzInit; }
/** * Generates the instructions to create and throw an exception. The * exception class must have a constructor with a single String argument. * * @param type * the class of the exception to be thrown. * @param msg * the detailed message of the exception. */ public void throwException(final Type type, final String msg) { newInstance(type); dup(); push(msg); invokeConstructor(type, Method.getMethod("void <init> (String)")); throwException(); }
private void invokeOnLockAcquireOrRelease(Method method) { int monitorLocal = mv.newLocal(OBJECT_TYPE); mv.dup(); mv.storeLocal(monitorLocal); loadCurrentThreadNumber(); loadNewCodeLocation(); mv.loadLocal(monitorLocal); mv.invokeVirtual(MANAGED_STRATEGY_TYPE, method); }
/** * Generates the instructions to load all the method arguments on the stack, as a single object * array. */ public void loadArgArray() { push(argumentTypes.length); newArray(OBJECT_TYPE); for (int i = 0; i < argumentTypes.length; i++) { dup(); push(i); loadArg(i); box(argumentTypes[i]); arrayStore(OBJECT_TYPE); } }
/** * Generates the instructions to load all the method arguments on the stack, * as a single object array. */ public void loadArgArray() { push(argumentTypes.length); newArray(OBJECT_TYPE); for (int i = 0; i < argumentTypes.length; i++) { dup(); push(i); loadArg(i); box(argumentTypes[i]); arrayStore(OBJECT_TYPE); } }
/** * Generates the instructions to load all the method arguments on the stack, as a single object * array. */ public void loadArgArray() { push(argumentTypes.length); newArray(OBJECT_TYPE); for (int i = 0; i < argumentTypes.length; i++) { dup(); push(i); loadArg(i); box(argumentTypes[i]); arrayStore(OBJECT_TYPE); } }
/** * Generates the instructions to load all the method arguments on the stack, as a single object * array. */ public void loadArgArray() { push(argumentTypes.length); newArray(OBJECT_TYPE); for (int i = 0; i < argumentTypes.length; i++) { dup(); push(i); loadArg(i); box(argumentTypes[i]); arrayStore(OBJECT_TYPE); } }
/** * Generates a factory method to delegate to constructors. */ private static void generateStaticCtorDelegator(ClassWriter cw, int access, String delegatorMethodName, Type delegateClassType, MethodType delegateMethodType) { Method wrapperMethod = new Method(delegatorMethodName, delegateMethodType.toMethodDescriptorString()); Method constructorMethod = new Method(CTOR_METHOD_NAME, delegateMethodType.changeReturnType(void.class).toMethodDescriptorString()); int modifiers = access | ACC_STATIC; GeneratorAdapter factory = new GeneratorAdapter(modifiers, wrapperMethod, cw.visitMethod(modifiers, delegatorMethodName, delegateMethodType.toMethodDescriptorString(), null, null)); factory.visitCode(); factory.newInstance(delegateClassType); factory.dup(); factory.loadArgs(); factory.invokeConstructor(delegateClassType, constructorMethod); factory.returnValue(); factory.endMethod(); }