List of usage examples for org.objectweb.asm MethodVisitor visitEnd
public void visitEnd()
From source file:fr.liglab.adele.cilia.dependency.ProxyGenerator.java
License:Apache License
/** * Generates a delegated method.//from ww w .j a va2 s.c o m * * @param cw the class writer * @param method the method object to delegate * @param className the generated class name * @param itfName the internal specification class name */ private static void generateDelegator(ClassWriter cw, Method method, String className, String itfName) { String methodName = method.getName(); String desc = Type.getMethodDescriptor(method); String[] exceptions = getInternalClassNames(method.getExceptionTypes()); int modifiers = method.getModifiers() & ~(Modifier.ABSTRACT | Modifier.NATIVE | Modifier.SYNCHRONIZED); Type[] types = Type.getArgumentTypes(method); int freeRoom = 1; for (int t = 0; t < types.length; t++) { freeRoom = freeRoom + types[t].getSize(); } MethodVisitor mv = cw.visitMethod(modifiers, methodName, desc, null, exceptions); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, DEPENDENCY, DEPENDENCY_DESC); // The dependency is on the stack. mv.visitMethodInsn(INVOKEVIRTUAL, DEPENDENCY_INTERNAL_NAME, "getService", // Call getService "()Ljava/lang/Object;"); // The service object is on the stack. int varSvc = freeRoom; freeRoom = freeRoom + 1; // Object Reference. mv.visitVarInsn(ASTORE, varSvc); // Store the service object. Label notNull = new Label(); Label isNull = new Label(); mv.visitVarInsn(ALOAD, varSvc); // Load the service mv.visitJumpInsn(IFNONNULL, notNull); // If not null go to not null // Null branch - throw the exception mv.visitLabel(isNull); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitLdcInsn("No service available"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); // End of the null branch // Not null, go one the execution mv.visitLabel(notNull); // Invoke the method on the service object. mv.visitVarInsn(ALOAD, varSvc); // Push argument on the stack. int i = 1; // Arguments. (non static method) for (int t = 0; t < types.length; t++) { mv.visitVarInsn(types[t].getOpcode(ILOAD), i); i = i + types[t].getSize(); } // Invocation mv.visitMethodInsn(INVOKEINTERFACE, itfName, methodName, desc); // Return the result Type returnType = Type.getReturnType(desc); if (returnType.getSort() != Type.VOID) { mv.visitInsn(returnType.getOpcode(IRETURN)); } else { mv.visitInsn(RETURN); } // End of the method. mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:fr.liglab.adele.cilia.dependency.ProxyGenerator.java
License:Apache License
/** * Generates the constructors. The constructor receives a dependency * and set the {@link ProxyGenerator#DEPENDENCY} field. * * @param cw the class writer/* w ww. jav a2s . c o m*/ * @param className the generated class name. */ private static void generateConstructor(ClassWriter cw, String className, String parent) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", '(' + DEPENDENCY_DESC + ")V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); // Load this mv.visitInsn(DUP); // Dup mv.visitMethodInsn(INVOKESPECIAL, parent, "<init>", "()V"); // Call super mv.visitVarInsn(ALOAD, 1); // Load the argument mv.visitFieldInsn(PUTFIELD, className, DEPENDENCY, DEPENDENCY_DESC); // Assign the dependency field mv.visitInsn(RETURN); // Return void mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:global.namespace.neuron.di.internal.ProxyClassVisitor.java
License:Apache License
private void insertConstructor() { final MethodVisitor mv = cv.visitMethod(ACC_PRIVATE_SYNTHETIC, CONSTRUCTOR_NAME, ACCEPTS_NOTHING_AND_RETURNS_VOID_DESC, null, null); mv.visitCode();// ww w . ja v a 2s .c o m mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, superName, CONSTRUCTOR_NAME, ACCEPTS_NOTHING_AND_RETURNS_VOID_DESC, false); for (final Method method : bindableMethods) { if (0 == (method.getModifiers() & ACC_ABSTRACT)) { final Class<?> declaringClass = method.getDeclaringClass(); final boolean isInterface = declaringClass.isInterface(); final String owner = getInternalName(declaringClass); final String name = method.getName(); final String desc = getMethodDescriptor(method); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitInvokeDynamicInsn("get", "(" + proxyDesc + ")" + dependencyProviderDesc, metaFactoryHandle, acceptsNothingAndReturnsObjectType, new Handle(H_INVOKESPECIAL, owner, name, desc, isInterface), acceptsNothingAndReturnsObjectType); mv.visitFieldInsn(PUTFIELD, proxyName, name + PROVIDER, dependencyProviderDesc); } } mv.visitInsn(RETURN); mv.visitMaxs(-1, -1); mv.visitEnd(); }
From source file:global.namespace.neuron.di.internal.ProxyClassVisitor.java
License:Apache License
private void insertMethods() { for (final Method method : bindableMethods) { new Object() { final int access = method.getModifiers() & ~ACC_ABSTRACT_NATIVE | ACC_SYNTHETIC; final String name = method.getName(); final String desc = getMethodDescriptor(method); final Class<?> returnType = method.getReturnType(); final String returnTypeName = getInternalName(returnType); final String returnTypeDesc = getDescriptor(returnType); final Class<?> boxedReturnType = boxed(returnType); final String boxedReturnTypeName = getInternalName(boxedReturnType); final String boxedReturnTypeDesc = getDescriptor(boxedReturnType); final int returnOpCode = returnOpCode(method); {/*from ww w . ja va 2s . c om*/ generateProxyField(); generateProxyCallMethod(); } void generateProxyField() { cv.visitField(ACC_PRIVATE_SYNTHETIC, name + PROVIDER, dependencyProviderDesc, null, null) .visitEnd(); } void generateProxyCallMethod() { final MethodVisitor mv = beginMethod(name); mv.visitFieldInsn(GETFIELD, proxyName, name + PROVIDER, dependencyProviderDesc); mv.visitMethodInsn(INVOKEINTERFACE, dependencyProviderName, "get", ACCEPTS_NOTHING_AND_RETURNS_OBJECT_DESC, true); if (!boxedReturnType.isAssignableFrom(Object.class)) { mv.visitTypeInsn(CHECKCAST, boxedReturnType.isArray() ? boxedReturnTypeDesc : boxedReturnTypeName); } if (boxedReturnType != returnType) { assert !returnType.isArray(); mv.visitMethodInsn(INVOKEVIRTUAL, boxedReturnTypeName, returnTypeName.concat("Value"), "()".concat(returnTypeDesc), false); } endMethod(mv); } MethodVisitor beginMethod(final String name) { final MethodVisitor mv = cv.visitMethod(access, name, desc, null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); return mv; } void endMethod(final MethodVisitor mv) { mv.visitInsn(returnOpCode); mv.visitMaxs(-1, -1); mv.visitEnd(); } }; } }
From source file:groovy.util.ProxyGeneratorAdapter.java
License:Apache License
/** * When an object doesn't implement the GroovyObject interface, we generate bytecode for the * {@link GroovyObject} interface methods. Otherwise, the superclass is expected to implement them. *//* w w w . jav a 2 s .co m*/ private void createGroovyObjectSupport() { visitField(ACC_PRIVATE + ACC_TRANSIENT, "metaClass", "Lgroovy/lang/MetaClass;", null, null); // getMetaClass MethodVisitor mv; { mv = super.visitMethod(ACC_PUBLIC, "getMetaClass", "()Lgroovy/lang/MetaClass;", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;"); Label l1 = new Label(); mv.visitJumpInsn(IFNONNULL, l1); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;"); mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "getMetaClass", "(Ljava/lang/Class;)Lgroovy/lang/MetaClass;"); mv.visitFieldInsn(PUTFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitLabel(l1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitInsn(ARETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } // getProperty { mv = super.visitMethod(ACC_PUBLIC, "getProperty", "(Ljava/lang/String;)Ljava/lang/Object;", null, null); mv.visitCode(); mv.visitIntInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEINTERFACE, "groovy/lang/GroovyObject", "getMetaClass", "()Lgroovy/lang/MetaClass;"); mv.visitIntInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, "groovy/lang/MetaClass", "getProperty", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;"); mv.visitInsn(ARETURN); mv.visitMaxs(3, 2); mv.visitEnd(); } // setProperty { mv = super.visitMethod(ACC_PUBLIC, "setProperty", "(Ljava/lang/String;Ljava/lang/Object;)V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, proxyName, "getMetaClass", "()Lgroovy/lang/MetaClass;"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEINTERFACE, "groovy/lang/MetaClass", "setProperty", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitMaxs(4, 3); mv.visitEnd(); } // invokeMethod { mv = super.visitMethod(ACC_PUBLIC, "invokeMethod", "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;", null, null); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, proxyName, "getMetaClass", "()Lgroovy/lang/MetaClass;"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEINTERFACE, "groovy/lang/MetaClass", "invokeMethod", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitMaxs(4, 3); mv.visitEnd(); } // setMetaClass { mv = super.visitMethod(ACC_PUBLIC, "setMetaClass", "(Lgroovy/lang/MetaClass;)V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitMaxs(2, 2); mv.visitEnd(); } }
From source file:groovy.util.ProxyGeneratorAdapter.java
License:Apache License
@Override public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { Object key = Arrays.asList(name, desc); if (visitedMethods.contains(key)) return EMPTY_VISITOR; if (Modifier.isPrivate(access) || Modifier.isNative(access) || ((access & ACC_SYNTHETIC) != 0)) { // do not generate bytecode for private methods return EMPTY_VISITOR; }/*from w w w . j av a 2s . c o m*/ int accessFlags = access; visitedMethods.add(key); if ((objectDelegateMethods.contains(name) || delegatedClosures.containsKey(name) || (!"<init>".equals(name) && hasWildcard)) && !Modifier.isStatic(access) && !Modifier.isFinal(access)) { if (!GROOVYOBJECT_METHOD_NAMESS.contains(name)) { if (Modifier.isAbstract(access)) { // prevents the proxy from being abstract accessFlags -= ACC_ABSTRACT; } if (delegatedClosures.containsKey(name) || (!"<init>".equals(name) && hasWildcard)) { delegatedClosures.put(name, Boolean.TRUE); return makeDelegateToClosureCall(name, desc, signature, exceptions, accessFlags); } if (generateDelegateField && objectDelegateMethods.contains(name)) { return makeDelegateCall(name, desc, signature, exceptions, accessFlags); } delegatedClosures.put(name, Boolean.TRUE); return makeDelegateToClosureCall(name, desc, signature, exceptions, accessFlags); } } else if ("<init>".equals(name) && (Modifier.isPublic(access) || Modifier.isProtected(access))) { return createConstructor(access, name, desc, signature, exceptions); } else if (Modifier.isAbstract(access) && !GROOVYOBJECT_METHOD_NAMESS.contains(name)) { accessFlags -= ACC_ABSTRACT; MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions); mv.visitCode(); Type[] args = Type.getArgumentTypes(desc); if (emptyBody) { Type returnType = Type.getReturnType(desc); if (returnType == Type.VOID_TYPE) { mv.visitInsn(RETURN); } else { int loadIns = getLoadInsn(returnType); switch (loadIns) { case ILOAD: mv.visitInsn(ICONST_0); break; case LLOAD: mv.visitInsn(LCONST_0); break; case FLOAD: mv.visitInsn(FCONST_0); break; case DLOAD: mv.visitInsn(DCONST_0); break; default: mv.visitInsn(ACONST_NULL); } mv.visitInsn(getReturnInsn(returnType)); mv.visitMaxs(2, args.length + 1); } } else { // for compatibility with the legacy proxy generator, we should throw an UnsupportedOperationException // instead of an AbtractMethodException mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "()V"); mv.visitInsn(ATHROW); mv.visitMaxs(2, args.length + 1); } mv.visitEnd(); } return EMPTY_VISITOR; }
From source file:groovy.util.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()); }//from w ww . jav 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(); mv.visitVarInsn(ALOAD, 0); for (int i = 0; i < args.length; i++) { Type arg = args[i]; if (isPrimitive(arg)) { mv.visitIntInsn(getLoadInsn(arg), i + 1); } else { mv.visitVarInsn(ALOAD, i + 1); // load argument i } } mv.visitMethodInsn(INVOKESPECIAL, BytecodeHelper.getClassInternalName(superClass), "<init>", desc); initializeDelegateClosure(mv, args.length); if (generateDelegateField) { initializeDelegateObject(mv, args.length + 1); } mv.visitInsn(RETURN); int max = args.length + 2 + (generateDelegateField ? 1 : 0); mv.visitMaxs(max, max); mv.visitEnd(); return EMPTY_VISITOR; }
From source file:groovy.util.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. j av a 2s .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; 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 stackSize = 3; // primitive types must be boxed if (isPrimitive(arg)) { mv.visitIntInsn(getLoadInsn(arg), i + 1); String wrappedType = getWrappedClassDescriptor(arg); mv.visitMethodInsn(INVOKESTATIC, wrappedType, "valueOf", "(" + arg.getDescriptor() + ")L" + wrappedType + ";"); } else { mv.visitVarInsn(ALOAD, i + 1); // load argument i } stackSize = 4; 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;"); 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;"); 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;"); mv.visitVarInsn(ALOAD, arrayIndex); // load argument array stackSize++; mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Closure", "call", "([Ljava/lang/Object;)Ljava/lang/Object;"); // call closure unwrapResult(mv, desc); mv.visitMaxs(stackSize, arrayStore + 1); mv.visitEnd(); // System.out.println("tmv.getText() = " + tmv.getText()); return EMPTY_VISITOR; }
From source file:hudson.util.SubClassGenerator.java
License:Open Source License
public <T> Class<? extends T> generate(Class<T> base, String name) { ClassWriter cw = new ClassWriter(false, false);//? cw.visit(49, ACC_PUBLIC, name.replace('.', '/'), null, Type.getInternalName(base), null); for (Constructor c : base.getDeclaredConstructors()) { Class[] et = c.getExceptionTypes(); String[] exceptions = new String[et.length]; for (int i = 0; i < et.length; i++) exceptions[i] = Type.getInternalName(et[i]); String methodDescriptor = getMethodDescriptor(c); MethodVisitor m = cw.visitMethod(c.getModifiers(), "<init>", methodDescriptor, null, exceptions); m.visitCode();/*from w ww . jav a 2 s .c o m*/ int index = 1; m.visitVarInsn(ALOAD, 0); for (Class param : c.getParameterTypes()) { Type t = Type.getType(param); m.visitVarInsn(t.getOpcode(ILOAD), index); index += t.getSize(); } m.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(base), "<init>", methodDescriptor); m.visitInsn(RETURN); m.visitMaxs(index, index); m.visitEnd(); } cw.visitEnd(); byte[] image = cw.toByteArray(); Class<? extends T> c = defineClass(name, image, 0, image.length).asSubclass(base); Jenkins h = Jenkins.getInstance(); if (h != null) // null only during tests. ((UberClassLoader) h.pluginManager.uberClassLoader).addNamedClass(name, c); // can't change the field type as it breaks binary compatibility return c; }
From source file:icom.weaver.JpaClassAdapter.java
License:Open Source License
public void visitEnd() { if (classDefn.icomTopLevelEntityClass || classDefn.icomTopLevelIdentifiableClass) { MethodVisitor getObjectId = super.visitMethod(ACC_PUBLIC, "getObjectId", "()L" + jpaObjectId + ";", null, null);//from w w w .j av a 2s .c om getObjectId.visitCode(); // stack has () // push [this] getObjectId.visitVarInsn(ALOAD, 0); // stack has ([this]) // invoke getObjectId()Licom/Id; getObjectId.visitMethodInsn(INVOKEVIRTUAL, classDefn.internalClassName, "getId", "()Licom/Id;"); // stack has ([this.getObjectId()]) // return getObjectId.visitInsn(ARETURN); getObjectId.visitEnd(); getObjectId.visitMaxs(1, 0); MethodVisitor getVersionId = super.visitMethod(ACC_PUBLIC, "getVersionId", "()L" + jpaVersionId + ";", null, null); getVersionId.visitCode(); // stack has () // push [this] getVersionId.visitVarInsn(ALOAD, 0); // stack has ([this]) // invoke getChangeToken(); getVersionId.visitMethodInsn(INVOKEVIRTUAL, classDefn.internalClassName, "getChangeToken", "()Licom/ChangeToken;"); // stack has ([this.getChangeToken()]) // return getVersionId.visitInsn(ARETURN); getVersionId.visitEnd(); getVersionId.visitMaxs(1, 0); } else if (classDefn.icomParticipantClass) { MethodVisitor getParticipant = super.visitMethod(ACC_PUBLIC, "getParticipant", "()Ljava/lang/Object;", null, null); getParticipant.visitCode(); // stack has () // push [this] getParticipant.visitVarInsn(ALOAD, 0); // stack has ([this]) // invoke getParticipant(); getParticipant.visitMethodInsn(INVOKEVIRTUAL, classDefn.internalClassName, "getParticipant", "()Licom/Addressable;"); // stack has ([getParticipant()]) // return getParticipant.visitInsn(ARETURN); getParticipant.visitEnd(); getParticipant.visitMaxs(1, 0); } super.visitEnd(); }