List of usage examples for org.objectweb.asm MethodVisitor visitMaxs
public void visitMaxs(final int maxStack, final int maxLocals)
From source file:net.minecrell.quartz.event.EventHandlerClassFactory.java
License:MIT License
private static byte[] generateClass(String name, Class<?> handle, Method method, Class<?> eventClass) { name = name.replace('.', '/'); final String handleName = Type.getInternalName(handle); final String handleDescriptor = Type.getDescriptor(handle); final String eventName = Type.getInternalName(eventClass); ClassWriter cw = new ClassWriter(0); FieldVisitor fv;//from w w w.j ava2s . c o m MethodVisitor mv; cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, "java/lang/Object", new String[] { EVENT_HANDLER_CLASS }); { fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "handle", handleDescriptor, null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", '(' + handleDescriptor + ")V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, name, "handle", handleDescriptor); mv.visitInsn(RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "getHandle", "()Ljava/lang/Object;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name, "handle", handleDescriptor); mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "handle", EVENT_HANDLER_METHOD, null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name, "handle", handleDescriptor); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, eventName); mv.visitMethodInsn(INVOKEVIRTUAL, handleName, method.getName(), "(L" + eventName + ";)V", false); mv.visitInsn(RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:net.openhft.smoothie.SegmentClassGenerator.java
License:Open Source License
private static byte[] classBytes(String segmentClassName, int allocationCapacity) { ClassWriter cw = new ClassWriter(0); String segmentClassNameWithSlashes = segmentClassName.replace('.', '/'); cw.visit(52, ACC_PUBLIC + ACC_SUPER, segmentClassNameWithSlashes + allocationCapacity, null, segmentClassNameWithSlashes, null); for (int i = 1; i < allocationCapacity; i++) { FieldVisitor fv;/*from ww w .ja v a2 s . c om*/ fv = cw.visitField(0, "k" + i, "Ljava/lang/Object;", null, null); fv.visitEnd(); fv = cw.visitField(0, "v" + i, "Ljava/lang/Object;", null, null); fv.visitEnd(); } { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, segmentClassNameWithSlashes, "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:net.orfjackal.dimdwarf.aop.AddEqualsAndHashCodeMethodsForEntities.java
License:Open Source License
private void addEqualsMethod() { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null); mv.visitCode();//from w w w . j av a2 s .c om mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESTATIC, entityHelperClass, "equals", "(Ljava/lang/Object;Ljava/lang/Object;)Z"); mv.visitInsn(IRETURN); mv.visitMaxs(2, 2); mv.visitEnd(); }
From source file:net.orfjackal.dimdwarf.aop.AddEqualsAndHashCodeMethodsForEntities.java
License:Open Source License
private void addHashCodeMethod() { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null); mv.visitCode();// w w w. ja va2s .com mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, entityHelperClass, "hashCode", "(Ljava/lang/Object;)I"); mv.visitInsn(IRETURN); mv.visitMaxs(1, 1); mv.visitEnd(); }
From source file:net.orfjackal.retrolambda.defaultmethods.ClassModifier.java
License:Open Source License
@Override public void visitEnd() { for (MethodContainer m : defaultMethods) { if (visitedMethods.contains(new VisitedMethod(m.methodName, m.methodDesc))) { continue; }/*w w w . j av a 2 s . c o m*/ System.out.println("VISITEND, CREATING PROXY " + m); MethodVisitor tmp = super.visitMethod(ACC_PUBLIC, m.methodName, m.methodDesc, m.signature, m.exceptions); tmp.visitVarInsn(ALOAD, 0); int i = 1; for (Type arg : Type.getArgumentTypes(m.methodDesc)) { tmp.visitVarInsn(arg.getOpcode(ILOAD), i++); } String rightInterface = findRightInterace(m, interfaces); System.out.println("It thinks that the right interface is " + rightInterface); String mDesc = Helpers.addParam(m.methodDesc, rightInterface); tmp.visitMethodInsn(INVOKESTATIC, rightInterface + "$helper", m.methodName, mDesc, false); tmp.visitInsn(Type.getReturnType(m.methodDesc).getOpcode(IRETURN)); tmp.visitMaxs(0, 0); tmp.visitEnd(); } super.visitEnd(); }
From source file:net.orfjackal.retrolambda.defaultmethods.InterfaceModifier.java
License:Open Source License
private void createBridge(Method meth, MethodContainer m) { int access = ACC_PUBLIC | ACC_STATIC | ACC_BRIDGE; String desc = Helpers.addParam(m.methodDesc, className); String returnType = Type.getReturnType(meth).getInternalName(); desc = Helpers.changeReturnType(desc, returnType); MethodVisitor tmp = getHelperClassVisitor().visitMethod(access, m.methodName, desc, m.signature, m.exceptions);/*from w w w. j a v a 2s .c om*/ tmp.visitVarInsn(ALOAD, 0); int i = 1; for (Type arg : Type.getArgumentTypes(m.methodDesc)) { tmp.visitVarInsn(arg.getOpcode(ILOAD), i++); } String mDesc = Helpers.addParam(m.methodDesc, className); tmp.visitMethodInsn(INVOKESTATIC, className + "$helper", m.methodName, mDesc, false); tmp.visitInsn(ARETURN); tmp.visitMaxs(0, 0); tmp.visitEnd(); }
From source file:net.orfjackal.retrolambda.defaultmethods.InterfaceModifier.java
License:Open Source License
private ClassWriter mkHelperClassVisitor() { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(targetByteCode, ACC_PUBLIC + ACC_SUPER, helperClassName(), null, "java/lang/Object", null); MethodVisitor mv = cw.visitMethod(ACC_PRIVATE, "<init>", "()V", null, null); mv.visitVarInsn(ALOAD, 0);/*from w w w . j a va2s .c om*/ mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); return cw; }
From source file:net.orfjackal.retrolambda.lambdas.BackportLambdaClass.java
License:Open Source License
private void makeSingleton() { FieldVisitor fv = super.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, SINGLETON_FIELD_NAME, singletonFieldDesc(), null, null); fv.visitEnd();//w ww . j a v a2s . c o m MethodVisitor mv = super.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, lambdaClass); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, lambdaClass, "<init>", "()V", false); mv.visitFieldInsn(PUTSTATIC, lambdaClass, SINGLETON_FIELD_NAME, singletonFieldDesc()); mv.visitInsn(RETURN); mv.visitMaxs(-1, -1); // rely on ClassWriter.COMPUTE_MAXS mv.visitEnd(); }
From source file:net.orfjackal.retrolambda.lambdas.BackportLambdaClass.java
License:Open Source License
private void generateFactoryMethod() { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_STATIC, factoryMethod.getName(), factoryMethod.getDesc(), null, null);//w w w . j av a 2 s .com mv.visitCode(); if (isStateless()) { mv.visitFieldInsn(GETSTATIC, lambdaClass, SINGLETON_FIELD_NAME, singletonFieldDesc()); mv.visitInsn(ARETURN); } else { mv.visitTypeInsn(NEW, lambdaClass); mv.visitInsn(DUP); int varIndex = 0; for (Type type : constructor.getArgumentTypes()) { mv.visitVarInsn(type.getOpcode(ILOAD), varIndex); varIndex += type.getSize(); } mv.visitMethodInsn(INVOKESPECIAL, lambdaClass, "<init>", constructor.getDescriptor(), false); mv.visitInsn(ARETURN); } mv.visitMaxs(-1, -1); // rely on ClassWriter.COMPUTE_MAXS mv.visitEnd(); }
From source file:net.orfjackal.retrolambda.util.Bytecode.java
License:Open Source License
public static void generateDelegateMethod(ClassVisitor cv, int access, Handle method, Handle target) { MethodVisitor mv = cv.visitMethod(access, method.getName(), method.getDesc(), null, null); mv.visitCode();/*from ww w. ja v a 2 s . c o m*/ // if the target method is constructor, then we must NEW up the instance inside the delegate method if (target.getTag() == H_NEWINVOKESPECIAL) { mv.visitTypeInsn(NEW, target.getOwner()); mv.visitInsn(DUP); } // we assume one of the methods to be static and the other virtual, i.e. it has an implicit 'this' argument Type[] args = longest(Type.getArgumentTypes(method.getDesc()), Type.getArgumentTypes(target.getDesc())); int varIndex = 0; for (Type arg : args) { mv.visitVarInsn(arg.getOpcode(ILOAD), varIndex); varIndex += arg.getSize(); } mv.visitMethodInsn(Handles.getOpcode(target), target.getOwner(), target.getName(), target.getDesc(), target.getTag() == H_INVOKEINTERFACE); mv.visitInsn(Type.getReturnType(method.getDesc()).getOpcode(IRETURN)); mv.visitMaxs(-1, -1); // rely on ClassWriter.COMPUTE_MAXS mv.visitEnd(); }