List of usage examples for org.objectweb.asm Opcodes F_SAME
int F_SAME
To view the source code for org.objectweb.asm Opcodes F_SAME.
Click Source Link
From source file:org.simantics.databoard.tests.testASM.java
License:Open Source License
void createImpl(String className, Class<?> interfaze, ClassWriter cw) { FieldVisitor fv;/*from w ww . j a va 2s . com*/ MethodVisitor mv; AnnotationVisitor av0; java.lang.reflect.Method[] methods = interfaze.getMethods(); Arrays.sort(methods, MethodInterfaceUtil.methodComparator); String classResourceName = className.replaceAll("\\.", "/"); String classTypeDescriptor = "L" + classResourceName + ";"; String interfaceResourceName = classResourceName.substring(0, classResourceName.length() - 5); String interfaceTypeDescriptor = "L" + interfaceResourceName + ";"; cw.visit(V1_6, ACC_SUPER | ACC_PUBLIC, classResourceName, null, "java/lang/Object", new String[] { interfaceResourceName }); // Imports Set<Class<?>> imports = new HashSet<Class<?>>(); imports.add(AsyncResult.class); imports.add(ExecutionError.class); imports.add(Method.class); imports.add(interfaze); for (java.lang.reflect.Method m : methods) { for (Class<?> clazz : m.getExceptionTypes()) { imports.add(clazz); } for (Class<?> clazz : m.getParameterTypes()) { imports.add(clazz); } imports.add(m.getReturnType()); } for (Class<?> clazz : imports) { if (clazz.isPrimitive()) continue; String name = clazz.getName(); if (name.startsWith("java.lang")) continue; String resourceName = name.replaceAll("\\.", "/"); String outerName = resourceName.contains("$") ? resourceName.substring(0, resourceName.indexOf('$')) : null; String className_ = clazz.isArray() ? clazz.getSimpleName().substring(0, clazz.getSimpleName().length() - 2) : clazz.getSimpleName(); int access = ACC_PUBLIC + ACC_STATIC + (clazz.isInterface() ? ACC_INTERFACE + ACC_ABSTRACT : 0); // System.out.printf("name=%s, outerName=%s, innerName=%s\n", resourceName, outerName, className_); cw.visitInnerClass(resourceName, outerName, className_, access); } // Fields { fv = cw.visitField(ACC_FINAL + ACC_STATIC, "interfaze", "Ljava/lang/Class;", "Ljava/lang/Class<*>;", null); fv.visitEnd(); } { fv = cw.visitField(ACC_FINAL + ACC_STATIC, "bindings", "[Lorg/simantics/databoard/method/MethodBinding;", null, null); fv.visitEnd(); } { fv = cw.visitField(0, "mi", "Lorg/simantics/data/session/MethodInterface;", null, null); fv.visitEnd(); } { fv = cw.visitField(0, "methods", "[Lorg/simantics/data/session/MethodInterface$Method;", null, null); fv.visitEnd(); } // Init class - static {} { mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLdcInsn(Type.getType(interfaceTypeDescriptor)); mv.visitFieldInsn(PUTSTATIC, classResourceName, "interfaze", "Ljava/lang/Class;"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitFieldInsn(GETSTATIC, classResourceName, "interfaze", "Ljava/lang/Class;"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getMethods", "()[Ljava/lang/reflect/Method;"); mv.visitVarInsn(ASTORE, 0); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/simantics/data/network/MethodInterfaceUtil", "methodComparator", "Ljava/util/Comparator;"); mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "sort", "([Ljava/lang/Object;Ljava/util/Comparator;)V"); Label l3 = new Label(); mv.visitLabel(l3); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(ARRAYLENGTH); mv.visitTypeInsn(ANEWARRAY, "org/simantics/databoard/method/MethodBinding"); mv.visitFieldInsn(PUTSTATIC, classResourceName, "bindings", "[Lorg/simantics/databoard/method/MethodBinding;"); Label l4 = new Label(); mv.visitLabel(l4); mv.visitInsn(ICONST_0); mv.visitVarInsn(ISTORE, 1); Label l5 = new Label(); mv.visitLabel(l5); Label l6 = new Label(); mv.visitJumpInsn(GOTO, l6); Label l7 = new Label(); mv.visitLabel(l7); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "[Ljava/lang/reflect/Method;", Opcodes.INTEGER }, 0, null); mv.visitFieldInsn(GETSTATIC, classResourceName, "bindings", "[Lorg/simantics/databoard/method/MethodBinding;"); mv.visitVarInsn(ILOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ILOAD, 1); mv.visitInsn(AALOAD); mv.visitMethodInsn(INVOKESTATIC, "org/simantics/databoard/DataTypes", "getMethodBinding", "(Ljava/lang/reflect/Method;)Lorg/simantics/databoard/method/MethodBinding;"); mv.visitInsn(AASTORE); Label l8 = new Label(); mv.visitLabel(l8); mv.visitIincInsn(1, 1); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ILOAD, 1); mv.visitFieldInsn(GETSTATIC, classResourceName, "bindings", "[Lorg/simantics/databoard/method/MethodBinding;"); mv.visitInsn(ARRAYLENGTH); mv.visitJumpInsn(IF_ICMPLT, l7); Label l9 = new Label(); mv.visitLabel(l9); mv.visitInsn(RETURN); Label l10 = new Label(); mv.visitLabel(l10); mv.visitLocalVariable("methods", "[Ljava/lang/reflect/Method;", null, l2, l10, 0); mv.visitLocalVariable("i", "I", null, l5, l9, 1); mv.visitMaxs(4, 2); mv.visitEnd(); } // Constructor { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Lorg/simantics/data/session/MethodInterface;)V", null, new String[] { "org/simantics/data/error/MethodNotSupportedException" }); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, classResourceName, "mi", "Lorg/simantics/data/session/MethodInterface;"); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, classResourceName, "bindings", "[Lorg/simantics/databoard/method/MethodBinding;"); mv.visitInsn(ARRAYLENGTH); mv.visitTypeInsn(ANEWARRAY, "org/simantics/data/session/MethodInterface$Method"); mv.visitFieldInsn(PUTFIELD, classResourceName, "methods", "[Lorg/simantics/data/session/MethodInterface$Method;"); Label l3 = new Label(); mv.visitLabel(l3); mv.visitInsn(ICONST_0); mv.visitVarInsn(ISTORE, 2); Label l4 = new Label(); mv.visitLabel(l4); Label l5 = new Label(); mv.visitJumpInsn(GOTO, l5); Label l6 = new Label(); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_FULL, 3, new Object[] { classResourceName, "org/simantics/data/session/MethodInterface", Opcodes.INTEGER }, 0, new Object[] {}); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classResourceName, "methods", "[Lorg/simantics/data/session/MethodInterface$Method;"); mv.visitVarInsn(ILOAD, 2); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETSTATIC, classResourceName, "bindings", "[Lorg/simantics/databoard/method/MethodBinding;"); mv.visitVarInsn(ILOAD, 2); mv.visitInsn(AALOAD); mv.visitMethodInsn(INVOKEINTERFACE, "org/simantics/data/session/MethodInterface", "getMethod", "(Lorg/simantics/databoard/method/MethodBinding;)Lorg/simantics/data/session/MethodInterface$Method;"); mv.visitInsn(AASTORE); Label l7 = new Label(); mv.visitLabel(l7); mv.visitIincInsn(2, 1); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ILOAD, 2); mv.visitFieldInsn(GETSTATIC, classResourceName, "bindings", "[Lorg/simantics/databoard/method/MethodBinding;"); mv.visitInsn(ARRAYLENGTH); mv.visitJumpInsn(IF_ICMPLT, l6); Label l8 = new Label(); mv.visitLabel(l8); mv.visitInsn(RETURN); Label l9 = new Label(); mv.visitLabel(l9); mv.visitLocalVariable("this", classTypeDescriptor, null, l0, l9, 0); mv.visitLocalVariable("mi", "Lorg/simantics/data/session/MethodInterface;", null, l0, l9, 1); mv.visitLocalVariable("i", "I", null, l4, l8, 2); mv.visitMaxs(5, 3); mv.visitEnd(); } // Method int methodNumber = 0; for (java.lang.reflect.Method m : methods) { String typeDescription = ""; Class<?>[] params = m.getParameterTypes(); for (int i = 0; i < params.length; i++) { if (i > 0) typeDescription += ";"; typeDescription += classToTypeDescriptor(params[i]); } typeDescription = "(" + typeDescription + ")" + classToTypeDescriptor(m.getReturnType()); System.out.println(typeDescription + " " + m.getName()); Class<?>[] exceptions = m.getExceptionTypes(); String[] exceptionResourceNames = new String[exceptions.length]; for (int i = 0; i < exceptionResourceNames.length; i++) { exceptionResourceNames[i] = exceptions[i].getName().replaceAll("\\.", "/"); } // Registers // 0 - this // 1..argRegs args // argRegs+1 - method // argRegs+2 - args (the array) // argRegs+3 - AsyncResult // argRegs+4 - e // argRegs+5 - ExecutionError.cause // argRegs+6 - e int argRegs = 0; for (int i = 0; i < params.length; i++) { Class<?> clazz = params[i]; argRegs++; if (clazz == long.class || clazz == float.class || clazz == Double.class) argRegs++; } mv = cw.visitMethod(ACC_PUBLIC, m.getName(), typeDescription, null, exceptionResourceNames); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "org/simantics/data/session/MethodInterface$ExecutionError"); Label l3 = new Label(); mv.visitTryCatchBlock(l0, l1, l3, "java/lang/InterruptedException"); Label l4 = new Label(); mv.visitLabel(l4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classResourceName, "methods", "[Lorg/simantics/data/session/MethodInterface$Method;"); // Method m and puts into register mv.visitLdcInsn(Integer.valueOf(methodNumber)); mv.visitInsn(AALOAD); mv.visitVarInsn(ASTORE, argRegs + 1); Label l5 = new Label(); mv.visitLabel(l5); mv.visitLdcInsn(Integer.valueOf(params.length)); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); int register = 1; // argument register for (int i = 0; i < params.length; i++) { Class<?> clazz = params[i]; mv.visitInsn(DUP); mv.visitLdcInsn(Integer.valueOf(i)); if (clazz == byte.class) { mv.visitVarInsn(ILOAD, register++); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;"); } else if (clazz == char.class) { mv.visitVarInsn(ILOAD, register++); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;"); } else if (clazz == boolean.class) { mv.visitVarInsn(ILOAD, register++); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;"); } else if (clazz == byte.class) { mv.visitVarInsn(ILOAD, register++); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;"); } else if (clazz == int.class) { mv.visitVarInsn(ILOAD, register++); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"); } else if (clazz == long.class) { mv.visitVarInsn(LLOAD, register); register += 2; mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(F)Ljava/lang/Long;"); } else if (clazz == float.class) { mv.visitVarInsn(FLOAD, register); register += 2; mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"); } else if (clazz == Double.class) { mv.visitVarInsn(DLOAD, register); register += 2; mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;"); } else { // Push argument to stack mv.visitVarInsn(ALOAD, register++); } mv.visitInsn(AASTORE); } // Store args to argRegs+2 mv.visitVarInsn(ASTORE, argRegs + 2); Label l6 = new Label(); mv.visitLabel(l6); mv.visitVarInsn(ALOAD, argRegs + 1 /* m */); mv.visitVarInsn(ALOAD, argRegs + 2 /*args*/); mv.visitMethodInsn(INVOKEINTERFACE, "org/simantics/data/session/MethodInterface$Method", "invoke", "(Ljava/lang/Object;)Lorg/simantics/data/session/MethodInterface$AsyncResult;"); mv.visitVarInsn(ASTORE, argRegs + 3); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, argRegs + 3); mv.visitMethodInsn(INVOKEINTERFACE, "org/simantics/data/session/MethodInterface$AsyncResult", "waitForResponse", "()Ljava/lang/Object;"); // TODO Return typecase result Class<?> returnType = m.getReturnType(); if (returnType == void.class) { mv.visitInsn(POP); mv.visitInsn(RETURN); } else if (returnType == int.class) { mv.visitTypeInsn(CHECKCAST, "java/lang/Integer"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I"); mv.visitLabel(l1); mv.visitInsn(IRETURN); } else if (returnType == byte.class) { mv.visitTypeInsn(CHECKCAST, "java/lang/Byte"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "B()"); mv.visitLabel(l1); mv.visitInsn(IRETURN); } else if (returnType == char.class) { mv.visitTypeInsn(CHECKCAST, "java/lang/Character"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C"); mv.visitLabel(l1); mv.visitInsn(IRETURN); } else if (returnType == boolean.class) { mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); mv.visitLabel(l1); mv.visitInsn(IRETURN); } else if (returnType == short.class) { mv.visitTypeInsn(CHECKCAST, "java/lang/Short"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S"); mv.visitLabel(l1); mv.visitInsn(IRETURN); } else if (returnType == long.class) { mv.visitTypeInsn(CHECKCAST, "java/lang/Long"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J"); mv.visitLabel(l1); mv.visitInsn(LRETURN); } else if (returnType == double.class) { mv.visitTypeInsn(CHECKCAST, "java/lang/Double"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D"); mv.visitLabel(l1); mv.visitInsn(DRETURN); } else if (returnType == float.class) { mv.visitTypeInsn(CHECKCAST, "java/lang/Float"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F"); mv.visitLabel(l1); mv.visitInsn(FRETURN); } else { // Object mv.visitTypeInsn(CHECKCAST, returnType.getName().replaceAll("\\.", "/")); mv.visitLabel(l1); mv.visitInsn(ARETURN); } // Handle exceptions mv.visitLabel(l2); mv.visitFrame(Opcodes.F_FULL, argRegs + 4, new Object[] { classResourceName, "java/lang/Integer", "org/simantics/data/session/MethodInterface$Method", "[Ljava/lang/Object;", "org/simantics/data/session/MethodInterface$AsyncResult" }, 1, new Object[] { "org/simantics/data/session/MethodInterface$ExecutionError" }); mv.visitVarInsn(ASTORE, argRegs + 4); Label l7 = new Label(); mv.visitLabel(l7); mv.visitVarInsn(ALOAD, argRegs + 4); mv.visitMethodInsn(INVOKEVIRTUAL, "org/simantics/data/session/MethodInterface$ExecutionError", "getError", "()Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, argRegs + 5); // argRegs+5 <- ExecutionError.cause Class<?>[] exceptionClasses = m.getExceptionTypes(); Label nextException[] = new Label[exceptionClasses.length]; for (int i = 0; i < exceptionClasses.length; i++) { Class<?> exceptionClass = exceptionClasses[i]; String exceptionClassResourceName = exceptionClass.getName().replaceAll("\\.", "/"); nextException[i] = new Label(); // If instanceof MyException Label l8 = new Label(); mv.visitLabel(l8); mv.visitVarInsn(ALOAD, argRegs + 5); // Cause mv.visitTypeInsn(INSTANCEOF, exceptionClassResourceName); mv.visitJumpInsn(IFEQ, nextException[i]); // If not, go to ExecutionError // If so, throw it mv.visitVarInsn(ALOAD, argRegs + 5); // e mv.visitTypeInsn(CHECKCAST, exceptionClassResourceName); mv.visitInsn(ATHROW); mv.visitLabel(nextException[i]); } // ExecutionError mv.visitFrame( Opcodes.F_APPEND, argRegs + 1, new Object[] { "org/simantics/data/session/MethodInterface$ExecutionError", "java/lang/Object" }, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, argRegs + 4); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/Throwable;)V"); mv.visitInsn(ATHROW); // InteruptedException mv.visitLabel(l3); mv.visitFrame(Opcodes.F_FULL, argRegs + 4, new Object[] { classResourceName, "java/lang/Integer", "org/simantics/data/session/MethodInterface$Method", "[Ljava/lang/Object;", "org/simantics/data/session/MethodInterface$AsyncResult" }, 1, new Object[] { "java/lang/InterruptedException" }); mv.visitVarInsn(ASTORE, argRegs + 4); Label l10 = new Label(); mv.visitLabel(l10); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, argRegs + 4); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/Throwable;)V"); mv.visitInsn(ATHROW); Label l11 = new Label(); mv.visitLabel(l11); mv.visitLocalVariable("this", classTypeDescriptor, null, l4, l11, 0); // mv.visitLocalVariable("arg1", "Ljava/lang/Integer;", null, l4, l11, 1); register = 1; for (int i = 0; i < params.length; i++) { Class<?> clazz = params[i]; mv.visitLocalVariable("arg" + (i + 1), classToTypeDescriptor(clazz), null, l4, l11, register); register++; if (clazz == long.class || clazz == float.class || clazz == Double.class) argRegs++; } mv.visitLocalVariable("m", "Lorg/simantics/data/session/MethodInterface$Method;", null, l5, l11, argRegs + 1); mv.visitLocalVariable("args", "[Ljava/lang/Object;", null, l6, l11, argRegs + 2); mv.visitLocalVariable("result", "Lorg/simantics/data/session/MethodInterface$AsyncResult;", null, l0, l11, argRegs + 3); mv.visitLocalVariable("e", "Lorg/simantics/data/session/MethodInterface$ExecutionError;", null, l7, l3, argRegs + 4); mv.visitLocalVariable("cause", "Ljava/lang/Object;", null, l7, l3, argRegs + 5); mv.visitLocalVariable("e", "Ljava/lang/InterruptedException;", null, l10, l11, argRegs + 4); mv.visitMaxs(argRegs + 3, argRegs + 6); mv.visitEnd(); methodNumber++; } cw.visitEnd(); }
From source file:org.skfiy.typhon.dispatcher.AsmDispatcherFactory.java
License:Apache License
@Override public synchronized Dispatcher getDispatcher() { if (dispatcher != null) { return dispatcher; }/* w w w .ja v a 2 s . c om*/ helper = new ActionHelper(container); String packageName = this.getClass().getPackage().getName(); String simpleName = "__Typhon__AsmDispatcher__"; String proxyClassName = packageName + "." + simpleName; String inertalName = proxyClassName.replaceAll("\\.", "/"); ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC, inertalName, null, Type.getInternalName(Object.class), new String[] { Type.getInternalName(Dispatcher.class) }); Set<Class<?>> classes = helper.getActionMappings().keySet(); // Fields for (Class clazz : classes) { cw.visitField(ACC_PRIVATE, buildFieldName(clazz), Type.getDescriptor(clazz), null, null).visitEnd(); } MethodVisitor mv; // FIXME mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + Type.getDescriptor(Container.class) + ")V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); // put field value for (Class clazz : classes) { mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn(Type.getType(clazz)); mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Container.class), "getInstance", "(Ljava/lang/Class;)Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, Type.getInternalName(clazz)); mv.visitFieldInsn(PUTFIELD, inertalName, buildFieldName(clazz), Type.getDescriptor(clazz)); } mv.visitInsn(RETURN); mv.visitMaxs(4, 4); mv.visitEnd(); // dispatch method mv = cw.visitMethod(ACC_PUBLIC, "dispatch", "(Ljava/lang/String;" + Type.getDescriptor(Packet.class) + ")V", null, null); mv.visitCode(); // Label endIfLab = new Label(); for (Map.Entry<Class<?>, List<ActionMapping>> entry : helper.getActionMappings().entrySet()) { for (ActionMapping am : entry.getValue()) { mv.visitLdcInsn(am.getNs()); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z"); Label lab = new Label(); mv.visitJumpInsn(IFEQ, lab); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, inertalName, buildFieldName(entry.getKey()), Type.getDescriptor(entry.getKey())); mv.visitVarInsn(ALOAD, 2); mv.visitTypeInsn(CHECKCAST, Type.getInternalName(am.getPacketClass())); mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(entry.getKey()), am.getMethod().getName(), "(" + Type.getDescriptor(am.getPacketClass()) + ")V"); // mv.visitJumpInsn(GOTO, endIfLab); mv.visitLabel(lab); mv.visitFrame(F_SAME, 0, null, 0, null); } } // else String excepInternalName = Type.getInternalName(NoNamespaceDefException.class); mv.visitTypeInsn(NEW, excepInternalName); mv.visitInsn(DUP); // ===================================================================== String sbInertalName = Type.getInternalName(StringBuilder.class); mv.visitTypeInsn(NEW, sbInertalName); mv.visitInsn(DUP); mv.visitLdcInsn("Not found \""); mv.visitMethodInsn(INVOKESPECIAL, sbInertalName, "<init>", "(Ljava/lang/String;)V"); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, sbInertalName, "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;"); mv.visitLdcInsn("\" namespace"); mv.visitMethodInsn(INVOKEVIRTUAL, sbInertalName, "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, sbInertalName, "toString", "()Ljava/lang/String;"); // ===================================================================== mv.visitMethodInsn(INVOKESPECIAL, excepInternalName, "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); mv.visitLabel(endIfLab); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(5, 4); mv.visitEnd(); cw.visitEnd(); // ------------------------------------------------------------------ byte[] buf = cw.toByteArray(); Class proxyClass = defineClass(proxyClassName, buf, 0, buf.length); try { dispatcher = (Dispatcher) proxyClass.getConstructor(Container.class).newInstance(container); } catch (Exception e) { // throw new AssertionError("", e); e.printStackTrace(); } return dispatcher; }
From source file:org.sonatype.restsimple.jaxrs.impl.JAXRSServiceDefinitionGenerator.java
License:Open Source License
/** * Generate a JAX RS Resource on the fly, based on the information contained with a {@link ServiceDefinition} * @param serviceDefinition a {@link ServiceDefinition} *///from ww w . j av a 2 s . c o m @Override public void generate(final ServiceDefinition serviceDefinition, ServiceHandlerMapper mapper) { for (ServiceHandler sh : serviceDefinition.serviceHandlers()) { mapper.addServiceHandler(serviceDefinition.path(), sh); } ClassWriter cw = new ClassWriter(0); FieldVisitor fv; MethodVisitor mv; AnnotationVisitor av0; String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 4); String className = "org/sonatype/restsimple/model/ServiceDescriptionResource" + uuid; cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", null); { String path = serviceDefinition.path().contains("/:") ? convert(serviceDefinition.path()) : serviceDefinition.path(); logger.info("Generating jaxrs resource for {} with path {}", serviceDefinition, path); if (path == "") { path = "/"; } av0 = cw.visitAnnotation("Ljavax/ws/rs/Path;", true); av0.visit("value", path); av0.visitEnd(); } if (serviceDefinition.mediaToProduce().size() > 0) { { av0 = cw.visitAnnotation("Ljavax/ws/rs/Produces;", true); { AnnotationVisitor av1 = av0.visitArray("value"); for (MediaType m : serviceDefinition.mediaToProduce()) { av1.visit(null, m.toMediaType()); } av1.visitEnd(); } av0.visitEnd(); } } if (serviceDefinition.mediaToConsume().size() > 0) { { av0 = cw.visitAnnotation("Ljavax/ws/rs/Consumes;", true); { AnnotationVisitor av1 = av0.visitArray("value"); for (MediaType m : serviceDefinition.mediaToConsume()) { av1.visit(null, m.toMediaType()); } av1.visitEnd(); } av0.visitEnd(); } } cw.visitInnerClass("javax/ws/rs/core/Response$ResponseBuilder", "javax/ws/rs/core/Response", "ResponseBuilder", ACC_PUBLIC + ACC_STATIC + ACC_ABSTRACT); cw.visitInnerClass("javax/ws/rs/core/Response$Status", "javax/ws/rs/core/Response", "Status", ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM); cw.visitInnerClass("org/sonatype/restsimple/api/ServiceDefinition$HttpMethod", "org/sonatype/restsimple/api/ServiceDefinition", "HttpMethod", ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM); { fv = cw.visitField(ACC_PRIVATE, "logger", "Lorg/slf4j/Logger;", null, null); fv.visitEnd(); } { fv = cw.visitField(0, "request", "Ljavax/servlet/http/HttpServletRequest;", null, null); { av0 = fv.visitAnnotation("Ljavax/ws/rs/core/Context;", true); av0.visitEnd(); } fv.visitEnd(); } { fv = cw.visitField(0, "mapper", "Lorg/sonatype/restsimple/spi/ServiceHandlerMapper;", null, null); { av0 = fv.visitAnnotation("Lcom/google/inject/Inject;", true); av0.visitEnd(); } fv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(Type.getType("L" + className + ";")); mv.visitMethodInsn(INVOKESTATIC, "org/slf4j/LoggerFactory", "getLogger", "(Ljava/lang/Class;)Lorg/slf4j/Logger;"); mv.visitFieldInsn(PUTFIELD, className, "logger", "Lorg/slf4j/Logger;"); mv.visitInsn(RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } int duplicateCounter = -1; for (ServiceHandler serviceHandler : serviceDefinition.serviceHandlers()) { { String methodName = serviceHandler.getHttpMethod().name().toLowerCase(); if (duplicateCounter++ >= 0) { methodName = methodName + duplicateCounter; } if (serviceHandler.getHttpMethod().name().equalsIgnoreCase("get")) { mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljavax/ws/rs/core/UriInfo;)Ljava/lang/Object;", null, null); { String p = convert(serviceHandler.path()); if (!p.equals("/") && !p.isEmpty()) { av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true); av0.visit("value", p); av0.visitEnd(); } } { av0 = mv.visitAnnotation("Ljavax/ws/rs/GET;", true); av0.visitEnd(); } if (serviceHandler.consumeMediaType() != null) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Consumes;", true); { AnnotationVisitor av1 = av0.visitArray("value"); av1.visit(null, serviceHandler.consumeMediaType().toMediaType()); av1.visitEnd(); } av0.visitEnd(); } } if (serviceHandler.mediaToProduce().size() > 0) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Produces;", true); { AnnotationVisitor av1 = av0.visitArray("value"); for (MediaType m : serviceHandler.mediaToProduce()) { av1.visit(null, m.toMediaType()); } av1.visitEnd(); } av0.visitEnd(); } } { av0 = mv.visitParameterAnnotation(0, "Ljavax/ws/rs/core/Context;", true); av0.visitEnd(); } mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn("get"); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, className, "mapMatrixParam", "(Ljavax/ws/rs/core/UriInfo;)Ljava/util/Map;"); mv.visitInsn(ACONST_NULL); mv.visitMethodInsn(INVOKESPECIAL, className, "invokeAction", "(Ljava/lang/String;Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap;Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); mv.visitInsn(ARETURN); mv.visitMaxs(6, 3); mv.visitEnd(); continue; } } { if (serviceHandler.getHttpMethod().name().equalsIgnoreCase("head")) { { String methodName = serviceHandler.getHttpMethod().name().toLowerCase(); if (duplicateCounter++ >= 0) { methodName = methodName + "_" + duplicateCounter; } mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljavax/ws/rs/core/UriInfo;)Ljavax/ws/rs/core/Response;", null, null); { String p = convert(serviceHandler.path()); if (!p.equals("/") && !p.isEmpty()) { av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true); av0.visit("value", p); av0.visitEnd(); } } { av0 = mv.visitAnnotation("Ljavax/ws/rs/HEAD;", true); av0.visitEnd(); } if (serviceHandler.consumeMediaType() != null) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Consumes;", true); { AnnotationVisitor av1 = av0.visitArray("value"); av1.visit(null, serviceHandler.consumeMediaType().toMediaType()); av1.visitEnd(); } av0.visitEnd(); } } if (serviceHandler.mediaToProduce().size() > 0) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Produces;", true); { AnnotationVisitor av1 = av0.visitArray("value"); for (MediaType m : serviceHandler.mediaToProduce()) { av1.visit(null, m.toMediaType()); } av1.visitEnd(); } av0.visitEnd(); } } { av0 = mv.visitParameterAnnotation(0, "Ljavax/ws/rs/core/Context;", true); av0.visitEnd(); } mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn("head"); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, className, "mapMatrixParam", "(Ljavax/ws/rs/core/UriInfo;)Ljava/util/Map;"); mv.visitInsn(ACONST_NULL); mv.visitMethodInsn(INVOKESPECIAL, className, "invokeAction", "(Ljava/lang/String;Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap;Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, 2); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "ok", "()Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitInsn(ARETURN); mv.visitMaxs(6, 3); mv.visitEnd(); continue; } } } { if (serviceHandler.getHttpMethod().name().equalsIgnoreCase("put")) { { String methodName = serviceHandler.getHttpMethod().name().toLowerCase(); if (duplicateCounter++ >= 0) { methodName = methodName + "_" + duplicateCounter; } String methodType = serviceHandler.consumeClass() != null ? serviceHandler.consumeClass().getName().replace(".", "/") : "java/lang/String"; mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljavax/ws/rs/core/UriInfo;L" + methodType + ";)Ljavax/ws/rs/core/Response;", null, null); { String p = convert(serviceHandler.path()); if (!p.equals("/") && !p.isEmpty()) { av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true); av0.visit("value", p); av0.visitEnd(); } } { av0 = mv.visitAnnotation("Ljavax/ws/rs/PUT;", true); av0.visitEnd(); } if (serviceHandler.consumeMediaType() != null) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Consumes;", true); { AnnotationVisitor av1 = av0.visitArray("value"); av1.visit(null, serviceHandler.consumeMediaType().toMediaType()); av1.visitEnd(); } av0.visitEnd(); } } if (serviceHandler.mediaToProduce().size() > 0) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Produces;", true); { AnnotationVisitor av1 = av0.visitArray("value"); for (MediaType m : serviceHandler.mediaToProduce()) { av1.visit(null, m.toMediaType()); } av1.visitEnd(); } av0.visitEnd(); } } { av0 = mv.visitParameterAnnotation(0, "Ljavax/ws/rs/core/Context;", true); av0.visitEnd(); } mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;"); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/UriBuilder", "fromResource", "(Ljava/lang/Class;)Ljavax/ws/rs/core/UriBuilder;"); mv.visitInsn(ICONST_3); mv.visitTypeInsn(ANEWARRAY, "java/lang/String"); mv.visitInsn(DUP); mv.visitInsn(ICONST_0); mv.visitLdcInsn(""); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_1); mv.visitLdcInsn(""); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_2); mv.visitLdcInsn(""); mv.visitInsn(AASTORE); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/UriBuilder", "build", "([Ljava/lang/Object;)Ljava/net/URI;"); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn("put"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/UriInfo", "getQueryParameters", "()Ljavax/ws/rs/core/MultivaluedMap;"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, className, "mapMatrixParam", "(Ljavax/ws/rs/core/UriInfo;)Ljava/util/Map;"); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKESPECIAL, className, "invokeAction", "(Ljava/lang/String;Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap;Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "created", "(Ljava/net/URI;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "entity", "(Ljava/lang/Object;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitInsn(ARETURN); mv.visitMaxs(6, 5); mv.visitEnd(); continue; } } { if (serviceHandler.getHttpMethod().name().equalsIgnoreCase("post")) { String methodName = serviceHandler.getHttpMethod().name().toLowerCase(); if (duplicateCounter++ >= 0) { methodName = methodName + "_" + duplicateCounter; } List<String> formsParam = PostServiceHandler.class.cast(serviceHandler).formParams(); if (formsParam.size() > 0) { { mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap;)Ljavax/ws/rs/core/Response;", "(Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap<Ljava/lang/String;Ljava/lang/String;>;)Ljavax/ws/rs/core/Response;", null); { String p = convert(serviceHandler.path()); if (!p.equals("/") && !p.isEmpty()) { av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true); av0.visit("value", p); av0.visitEnd(); } } { av0 = mv.visitAnnotation("Ljavax/ws/rs/POST;", true); av0.visitEnd(); } { av0 = mv.visitAnnotation("Ljavax/ws/rs/Consumes;", true); { AnnotationVisitor av1 = av0.visitArray("value"); av1.visit(null, "application/x-www-form-urlencoded"); av1.visitEnd(); } av0.visitEnd(); } if (serviceHandler.mediaToProduce().size() > 0) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Produces;", true); { AnnotationVisitor av1 = av0.visitArray("value"); for (MediaType m : serviceHandler.mediaToProduce()) { av1.visit(null, m.toMediaType()); } av1.visitEnd(); } av0.visitEnd(); } } { av0 = mv.visitParameterAnnotation(0, "Ljavax/ws/rs/core/Context;", true); av0.visitEnd(); } mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn("post"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, className, "mapMatrixParam", "(Ljavax/ws/rs/core/UriInfo;)Ljava/util/Map;"); mv.visitInsn(ACONST_NULL); mv.visitMethodInsn(INVOKESPECIAL, className, "invokeAction", "(Ljava/lang/String;Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap;Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 3); Label l0 = new Label(); mv.visitJumpInsn(IFNONNULL, l0); mv.visitFieldInsn(GETSTATIC, "javax/ws/rs/core/Response$Status", "NO_CONTENT", "Ljavax/ws/rs/core/Response$Status;"); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "status", "(Ljavax/ws/rs/core/Response$Status;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitInsn(ARETURN); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Object" }, 0, null); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "ok", "(Ljava/lang/Object;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitInsn(ARETURN); mv.visitMaxs(6, 4); mv.visitEnd(); continue; } } else { String methodType = serviceHandler.consumeClass() != null ? serviceHandler.consumeClass().getName().replace(".", "/") : "java/lang/String"; mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljavax/ws/rs/core/UriInfo;L" + methodType + ";)Ljavax/ws/rs/core/Response;", null, null); { String p = convert(serviceHandler.path()); if (!p.equals("/") && !p.isEmpty()) { av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true); av0.visit("value", p); av0.visitEnd(); } } { av0 = mv.visitAnnotation("Ljavax/ws/rs/POST;", true); av0.visitEnd(); } if (serviceHandler.mediaToProduce().size() > 0) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Produces;", true); { AnnotationVisitor av1 = av0.visitArray("value"); for (MediaType m : serviceHandler.mediaToProduce()) { av1.visit(null, m.toMediaType()); } av1.visitEnd(); } av0.visitEnd(); } } if (serviceHandler.consumeMediaType() != null) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Consumes;", true); { AnnotationVisitor av1 = av0.visitArray("value"); av1.visit(null, serviceHandler.consumeMediaType().toMediaType()); av1.visitEnd(); } av0.visitEnd(); } } { av0 = mv.visitParameterAnnotation(0, "Ljavax/ws/rs/core/Context;", true); av0.visitEnd(); } mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn("post"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/UriInfo", "getQueryParameters", "()Ljavax/ws/rs/core/MultivaluedMap;"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, className, "mapMatrixParam", "(Ljavax/ws/rs/core/UriInfo;)Ljava/util/Map;"); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKESPECIAL, className, "invokeAction", "(Ljava/lang/String;Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap;Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 3); Label l0 = new Label(); mv.visitJumpInsn(IFNONNULL, l0); mv.visitFieldInsn(GETSTATIC, "javax/ws/rs/core/Response$Status", "NO_CONTENT", "Ljavax/ws/rs/core/Response$Status;"); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "status", "(Ljavax/ws/rs/core/Response$Status;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitInsn(ARETURN); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Object" }, 0, null); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "ok", "(Ljava/lang/Object;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitInsn(ARETURN); mv.visitMaxs(6, 4); mv.visitEnd(); continue; } } } { if (serviceHandler.getHttpMethod().name().equalsIgnoreCase("delete")) { { String methodName = serviceHandler.getHttpMethod().name().toLowerCase(); if (duplicateCounter++ >= 0) { methodName = methodName + "_" + duplicateCounter; } String methodType = serviceHandler.consumeClass() != null ? serviceHandler.consumeClass().getName().replace(".", "/") : "java/lang/String"; mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljavax/ws/rs/core/UriInfo;L" + methodType + ";)Ljavax/ws/rs/core/Response;", null, null); { String p = convert(serviceHandler.path()); if (!p.equals("/") && !p.isEmpty()) { av0 = mv.visitAnnotation("Ljavax/ws/rs/Path;", true); av0.visit("value", p); av0.visitEnd(); } } { av0 = mv.visitAnnotation("Ljavax/ws/rs/DELETE;", true); av0.visitEnd(); } if (serviceHandler.mediaToProduce().size() > 0) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Produces;", true); { AnnotationVisitor av1 = av0.visitArray("value"); for (MediaType m : serviceHandler.mediaToProduce()) { av1.visit(null, m.toMediaType()); } av1.visitEnd(); } av0.visitEnd(); } } if (serviceHandler.consumeMediaType() != null) { { av0 = mv.visitAnnotation("Ljavax/ws/rs/Consumes;", true); { AnnotationVisitor av1 = av0.visitArray("value"); av1.visit(null, serviceHandler.consumeMediaType().toMediaType()); av1.visitEnd(); } av0.visitEnd(); } } { av0 = mv.visitParameterAnnotation(0, "Ljavax/ws/rs/core/Context;", true); av0.visitEnd(); } mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn("delete"); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, className, "mapMatrixParam", "(Ljavax/ws/rs/core/UriInfo;)Ljava/util/Map;"); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKESPECIAL, className, "invokeAction", "(Ljava/lang/String;Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap;Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "ok", "(Ljava/lang/Object;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitInsn(ARETURN); mv.visitMaxs(6, 4); mv.visitEnd(); continue; } } } } } { mv = cw.visitMethod(ACC_PRIVATE, "invokeAction", "(Ljava/lang/String;Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap;Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;", "<T:Ljava/lang/Object;>(Ljava/lang/String;Ljavax/ws/rs/core/UriInfo;Ljavax/ws/rs/core/MultivaluedMap<Ljava/lang/String;Ljava/lang/String;>;Ljava/util/Map<Ljava/lang/String;Ljava/util/Collection<Ljava/lang/String;>;>;TT;)Ljava/lang/Object;", null); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "org/sonatype/restsimple/api/ActionException"); Label l3 = new Label(); mv.visitTryCatchBlock(l0, l1, l3, "java/lang/Throwable"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "mapper", "Lorg/sonatype/restsimple/spi/ServiceHandlerMapper;"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/UriInfo", "getPath", "()Ljava/lang/String;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/sonatype/restsimple/spi/ServiceHandlerMapper", "map", "(Ljava/lang/String;Ljava/lang/String;)Lorg/sonatype/restsimple/api/ServiceHandler;"); mv.visitVarInsn(ASTORE, 6); mv.visitVarInsn(ALOAD, 6); Label l4 = new Label(); mv.visitJumpInsn(IFNONNULL, l4); mv.visitTypeInsn(NEW, "javax/ws/rs/WebApplicationException"); mv.visitInsn(DUP); mv.visitIntInsn(SIPUSH, 405); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "status", "(I)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitLdcInsn("Method not allowed"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "entity", "(Ljava/lang/Object;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/WebApplicationException", "<init>", "(Ljavax/ws/rs/core/Response;)V"); mv.visitInsn(ATHROW); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "org/sonatype/restsimple/api/ServiceHandler" }, 0, null); mv.visitVarInsn(ALOAD, 6); mv.visitMethodInsn(INVOKEVIRTUAL, "org/sonatype/restsimple/api/ServiceHandler", "getHttpMethod", "()Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/sonatype/restsimple/api/ServiceDefinition$METHOD", "name", "()Ljava/lang/String;"); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equalsIgnoreCase", "(Ljava/lang/String;)Z"); Label l5 = new Label(); mv.visitJumpInsn(IFNE, l5); mv.visitTypeInsn(NEW, "javax/ws/rs/WebApplicationException"); mv.visitInsn(DUP); mv.visitIntInsn(SIPUSH, 405); mv.visitMethodInsn(INVOKESTATIC, "javax/ws/rs/core/Response", "status", "(I)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitLdcInsn("Method not allowed"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "entity", "(Ljava/lang/Object;)Ljavax/ws/rs/core/Response$ResponseBuilder;"); mv.visitMethodInsn(INVOKEVIRTUAL, "javax/ws/rs/core/Response$ResponseBuilder", "build", "()Ljavax/ws/rs/core/Response;"); mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/WebApplicationException", "<init>", "(Ljavax/ws/rs/core/Response;)V"); mv.visitInsn(ATHROW); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ASTORE, 7); mv.visitVarInsn(ALOAD, 6); mv.visitMethodInsn(INVOKEVIRTUAL, "org/sonatype/restsimple/api/ServiceHandler", "getAction", "()Lorg/sonatype/restsimple/api/Action;"); mv.visitVarInsn(ASTORE, 8); mv.visitTypeInsn(NEW, "java/util/HashMap"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashMap", "<init>", "()V"); mv.visitVarInsn(ASTORE, 9); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/UriInfo", "getPathParameters", "()Ljavax/ws/rs/core/MultivaluedMap;"); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/MultivaluedMap", "entrySet", "()Ljava/util/Set;"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "iterator", "()Ljava/util/Iterator;"); mv.visitVarInsn(ASTORE, 10); Label l6 = new Label(); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_FULL, 11, new Object[] { className, "java/lang/String", "javax/ws/rs/core/UriInfo", "javax/ws/rs/core/MultivaluedMap", "java/util/Map", "java/lang/Object", "org/sonatype/restsimple/api/ServiceHandler", "java/lang/Object", "org/sonatype/restsimple/api/Action", "java/util/Map", "java/util/Iterator" }, 0, new Object[] {}); mv.visitVarInsn(ALOAD, 10); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z"); mv.visitJumpInsn(IFEQ, l0); mv.visitVarInsn(ALOAD, 10); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/util/Map$Entry"); mv.visitVarInsn(ASTORE, 11); mv.visitVarInsn(ALOAD, 9); mv.visitVarInsn(ALOAD, 11); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map$Entry", "getKey", "()Ljava/lang/Object;"); mv.visitVarInsn(ALOAD, 11); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map$Entry", "getValue", "()Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/util/List"); mv.visitInsn(ICONST_0); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); mv.visitJumpInsn(GOTO, l6); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitTypeInsn(NEW, "org/sonatype/restsimple/api/ActionContext"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "request", "Ljavax/servlet/http/HttpServletRequest;"); mv.visitMethodInsn(INVOKEINTERFACE, "javax/servlet/http/HttpServletRequest", "getMethod", "()Ljava/lang/String;"); mv.visitMethodInsn(INVOKESPECIAL, className, "mapMethod", "(Ljava/lang/String;)Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;"); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, className, "mapHeaders", "()Ljava/util/Map;"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKESPECIAL, className, "mapFormParams", "(Ljavax/ws/rs/core/MultivaluedMap;)Ljava/util/Map;"); mv.visitVarInsn(ALOAD, 4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "request", "Ljavax/servlet/http/HttpServletRequest;"); mv.visitMethodInsn(INVOKEINTERFACE, "javax/servlet/http/HttpServletRequest", "getInputStream", "()Ljavax/servlet/ServletInputStream;"); mv.visitVarInsn(ALOAD, 9); mv.visitVarInsn(ALOAD, 5); mv.visitMethodInsn(INVOKESPECIAL, "org/sonatype/restsimple/api/ActionContext", "<init>", "(Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/io/InputStream;Ljava/util/Map;Ljava/lang/Object;)V"); mv.visitVarInsn(ASTORE, 10); mv.visitVarInsn(ALOAD, 8); mv.visitVarInsn(ALOAD, 10); mv.visitMethodInsn(INVOKEINTERFACE, "org/sonatype/restsimple/api/Action", "action", "(Lorg/sonatype/restsimple/api/ActionContext;)Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, 7); mv.visitLabel(l1); Label l7 = new Label(); mv.visitJumpInsn(GOTO, l7); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "org/sonatype/restsimple/api/ActionException" }); mv.visitVarInsn(ASTORE, 10); mv.visitTypeInsn(NEW, "javax/ws/rs/WebApplicationException"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 10); mv.visitVarInsn(ALOAD, 10); mv.visitMethodInsn(INVOKEVIRTUAL, "org/sonatype/restsimple/api/ActionException", "getStatusCode", "()I"); mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/WebApplicationException", "<init>", "(Ljava/lang/Throwable;I)V"); mv.visitInsn(ATHROW); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(ASTORE, 10); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "logger", "Lorg/slf4j/Logger;"); mv.visitLdcInsn("invokeAction"); mv.visitVarInsn(ALOAD, 10); mv.visitMethodInsn(INVOKEINTERFACE, "org/slf4j/Logger", "error", "(Ljava/lang/String;Ljava/lang/Throwable;)V"); mv.visitTypeInsn(NEW, "javax/ws/rs/WebApplicationException"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 10); mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/WebApplicationException", "<init>", "(Ljava/lang/Throwable;)V"); mv.visitInsn(ATHROW); mv.visitLabel(l7); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 7); mv.visitInsn(ARETURN); mv.visitMaxs(9, 12); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PRIVATE, "mapFormParams", "(Ljavax/ws/rs/core/MultivaluedMap;)Ljava/util/Map;", "(Ljavax/ws/rs/core/MultivaluedMap<Ljava/lang/String;Ljava/lang/String;>;)Ljava/util/Map<Ljava/lang/String;Ljava/util/Collection<Ljava/lang/String;>;>;", null); mv.visitCode(); mv.visitTypeInsn(NEW, "java/util/HashMap"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashMap", "<init>", "()V"); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 1); Label l0 = new Label(); mv.visitJumpInsn(IFNULL, l0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/MultivaluedMap", "keySet", "()Ljava/util/Set;"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "iterator", "()Ljava/util/Iterator;"); mv.visitVarInsn(ASTORE, 3); Label l1 = new Label(); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/util/Map", "java/util/Iterator" }, 0, null); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z"); mv.visitJumpInsn(IFEQ, l0); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/lang/String"); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); Label l2 = new Label(); mv.visitJumpInsn(IFNULL, l2); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/util/Collection"); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/MultivaluedMap", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/util/Collection"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Collection", "addAll", "(Ljava/util/Collection;)Z"); mv.visitInsn(POP); Label l3 = new Label(); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/String" }, 0, null); mv.visitTypeInsn(NEW, "java/util/ArrayList"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V"); mv.visitVarInsn(ASTORE, 5); mv.visitVarInsn(ALOAD, 5); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/MultivaluedMap", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/util/Collection"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "addAll", "(Ljava/util/Collection;)Z"); mv.visitInsn(POP); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 4); mv.visitVarInsn(ALOAD, 5); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitJumpInsn(GOTO, l1); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(ALOAD, 2); mv.visitInsn(ARETURN); mv.visitMaxs(3, 6); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PRIVATE, "mapMethod", "(Ljava/lang/String;)Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn("GET"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equalsIgnoreCase", "(Ljava/lang/String;)Z"); Label l0 = new Label(); mv.visitJumpInsn(IFEQ, l0); mv.visitFieldInsn(GETSTATIC, "org/sonatype/restsimple/api/ServiceDefinition$METHOD", "GET", "Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;"); mv.visitInsn(ARETURN); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn("PUT"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equalsIgnoreCase", "(Ljava/lang/String;)Z"); Label l1 = new Label(); mv.visitJumpInsn(IFEQ, l1); mv.visitFieldInsn(GETSTATIC, "org/sonatype/restsimple/api/ServiceDefinition$METHOD", "PUT", "Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;"); mv.visitInsn(ARETURN); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn("POST"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equalsIgnoreCase", "(Ljava/lang/String;)Z"); Label l2 = new Label(); mv.visitJumpInsn(IFEQ, l2); mv.visitFieldInsn(GETSTATIC, "org/sonatype/restsimple/api/ServiceDefinition$METHOD", "POST", "Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;"); mv.visitInsn(ARETURN); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn("DELETE"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equalsIgnoreCase", "(Ljava/lang/String;)Z"); Label l3 = new Label(); mv.visitJumpInsn(IFEQ, l3); mv.visitFieldInsn(GETSTATIC, "org/sonatype/restsimple/api/ServiceDefinition$METHOD", "DELETE", "Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;"); mv.visitInsn(ARETURN); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn("HEAD"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equalsIgnoreCase", "(Ljava/lang/String;)Z"); Label l4 = new Label(); mv.visitJumpInsn(IFEQ, l4); mv.visitFieldInsn(GETSTATIC, "org/sonatype/restsimple/api/ServiceDefinition$METHOD", "HEAD", "Lorg/sonatype/restsimple/api/ServiceDefinition$METHOD;"); mv.visitInsn(ARETURN); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitTypeInsn(NEW, "java/lang/IllegalStateException"); mv.visitInsn(DUP); mv.visitLdcInsn("Invalid Method"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); mv.visitMaxs(3, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PRIVATE, "mapHeaders", "()Ljava/util/Map;", "()Ljava/util/Map<Ljava/lang/String;Ljava/util/Collection<Ljava/lang/String;>;>;", null); mv.visitCode(); mv.visitTypeInsn(NEW, "java/util/HashMap"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashMap", "<init>", "()V"); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "request", "Ljavax/servlet/http/HttpServletRequest;"); mv.visitMethodInsn(INVOKEINTERFACE, "javax/servlet/http/HttpServletRequest", "getHeaderNames", "()Ljava/util/Enumeration;"); mv.visitVarInsn(ASTORE, 2); Label l0 = new Label(); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/util/Map", "java/util/Enumeration" }, 0, null); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Enumeration", "hasMoreElements", "()Z"); Label l1 = new Label(); mv.visitJumpInsn(IFEQ, l1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Enumeration", "nextElement", "()Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/lang/String"); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); Label l2 = new Label(); mv.visitJumpInsn(IFNULL, l2); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/util/Collection"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "request", "Ljavax/servlet/http/HttpServletRequest;"); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, "javax/servlet/http/HttpServletRequest", "getHeader", "(Ljava/lang/String;)Ljava/lang/String;"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Collection", "add", "(Ljava/lang/Object;)Z"); mv.visitInsn(POP); mv.visitJumpInsn(GOTO, l0); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/String" }, 0, null); mv.visitTypeInsn(NEW, "java/util/ArrayList"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V"); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "request", "Ljavax/servlet/http/HttpServletRequest;"); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, "javax/servlet/http/HttpServletRequest", "getHeader", "(Ljava/lang/String;)Ljava/lang/String;"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z"); mv.visitInsn(POP); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 3); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); mv.visitJumpInsn(GOTO, l0); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESTATIC, "java/util/Collections", "unmodifiableMap", "(Ljava/util/Map;)Ljava/util/Map;"); mv.visitInsn(ARETURN); mv.visitMaxs(3, 5); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PRIVATE, "mapMatrixParam", "(Ljavax/ws/rs/core/UriInfo;)Ljava/util/Map;", "(Ljavax/ws/rs/core/UriInfo;)Ljava/util/Map<Ljava/lang/String;Ljava/util/Collection<Ljava/lang/String;>;>;", null); mv.visitCode(); mv.visitTypeInsn(NEW, "java/util/HashMap"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashMap", "<init>", "()V"); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/UriInfo", "getPathSegments", "()Ljava/util/List;"); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "iterator", "()Ljava/util/Iterator;"); mv.visitVarInsn(ASTORE, 4); Label l0 = new Label(); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_APPEND, 3, new Object[] { "java/util/Map", "java/util/List", "java/util/Iterator" }, 0, null); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z"); Label l1 = new Label(); mv.visitJumpInsn(IFEQ, l1); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "javax/ws/rs/core/PathSegment"); mv.visitVarInsn(ASTORE, 5); mv.visitVarInsn(ALOAD, 5); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/PathSegment", "getMatrixParameters", "()Ljavax/ws/rs/core/MultivaluedMap;"); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/MultivaluedMap", "keySet", "()Ljava/util/Set;"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "iterator", "()Ljava/util/Iterator;"); mv.visitVarInsn(ASTORE, 6); Label l2 = new Label(); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "javax/ws/rs/core/PathSegment", "java/util/Iterator" }, 0, null); mv.visitVarInsn(ALOAD, 6); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z"); Label l3 = new Label(); mv.visitJumpInsn(IFEQ, l3); mv.visitVarInsn(ALOAD, 6); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, "java/lang/String"); mv.visitVarInsn(ASTORE, 7); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 7); mv.visitVarInsn(ALOAD, 5); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/PathSegment", "getMatrixParameters", "()Ljavax/ws/rs/core/MultivaluedMap;"); mv.visitVarInsn(ALOAD, 7); mv.visitMethodInsn(INVOKEINTERFACE, "javax/ws/rs/core/MultivaluedMap", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); mv.visitJumpInsn(GOTO, l2); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_CHOP, 2, null, 0, null); mv.visitJumpInsn(GOTO, l0); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKESTATIC, "java/util/Collections", "unmodifiableMap", "(Ljava/util/Map;)Ljava/util/Map;"); mv.visitInsn(ARETURN); mv.visitMaxs(4, 8); mv.visitEnd(); } cw.visitEnd(); byte[] bytes = cw.toByteArray(); try { String classToLoad = className.replace("/", "."); ClassLoader cl = new ByteClassloader(bytes, this.getClass().getClassLoader(), classToLoad); Class<?> clazz = cl.loadClass(classToLoad); moduleConfig.bind(clazz); moduleConfig.bind(GenericMessageBodyWriter.class); bindExtension(serviceDefinition); } catch (Throwable e) { logger.error("generate", e); } }
From source file:org.spongepowered.test.decompile.SwitchTests.java
License:Open Source License
private static void generateSwitchSyntheticEclipse(ClassWriter cw) { MethodVisitor mv = cw.visitMethod(ACC_STATIC + ACC_SYNTHETIC, "$SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum", "()[I", null, null); mv.visitCode();//from w ww . ja va2s . com Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/NoSuchFieldError"); Label l3 = new Label(); Label l4 = new Label(); Label l5 = new Label(); mv.visitTryCatchBlock(l3, l4, l5, "java/lang/NoSuchFieldError"); Label l6 = new Label(); Label l7 = new Label(); Label l8 = new Label(); mv.visitTryCatchBlock(l6, l7, l8, "java/lang/NoSuchFieldError"); Label l9 = new Label(); Label l10 = new Label(); Label l11 = new Label(); mv.visitTryCatchBlock(l9, l10, l11, "java/lang/NoSuchFieldError"); Label l12 = new Label(); Label l13 = new Label(); Label l14 = new Label(); mv.visitTryCatchBlock(l12, l13, l14, "java/lang/NoSuchFieldError"); Label l15 = new Label(); Label l16 = new Label(); Label l17 = new Label(); mv.visitTryCatchBlock(l15, l16, l17, "java/lang/NoSuchFieldError"); Label l18 = new Label(); Label l19 = new Label(); Label l20 = new Label(); mv.visitTryCatchBlock(l18, l19, l20, "java/lang/NoSuchFieldError"); Label l21 = new Label(); Label l22 = new Label(); Label l23 = new Label(); mv.visitTryCatchBlock(l21, l22, l23, "java/lang/NoSuchFieldError"); Label l24 = new Label(); Label l25 = new Label(); Label l26 = new Label(); mv.visitTryCatchBlock(l24, l25, l26, "java/lang/NoSuchFieldError"); Label l27 = new Label(); Label l28 = new Label(); Label l29 = new Label(); mv.visitTryCatchBlock(l27, l28, l29, "java/lang/NoSuchFieldError"); Label l30 = new Label(); mv.visitLabel(l30); mv.visitLineNumber(28, l30); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests", "$SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum", "[I"); Label l31 = new Label(); mv.visitJumpInsn(IFNULL, l31); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests", "$SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum", "[I"); mv.visitInsn(ARETURN); mv.visitLabel(l31); mv.visitMethodInsn(INVOKESTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "values", "()[Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;", false); mv.visitInsn(ARRAYLENGTH); mv.visitIntInsn(NEWARRAY, T_INT); mv.visitVarInsn(ASTORE, 0); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "EIGHT", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitIntInsn(BIPUSH, 8); mv.visitInsn(IASTORE); mv.visitLabel(l1); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitInsn(POP); mv.visitLabel(l3); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "FIVE", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitInsn(ICONST_5); mv.visitInsn(IASTORE); mv.visitLabel(l4); mv.visitJumpInsn(GOTO, l6); mv.visitLabel(l5); mv.visitInsn(POP); mv.visitLabel(l6); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "FOUR", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitInsn(ICONST_4); mv.visitInsn(IASTORE); mv.visitLabel(l7); mv.visitJumpInsn(GOTO, l9); mv.visitLabel(l8); mv.visitInsn(POP); mv.visitLabel(l9); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "NINE", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitIntInsn(BIPUSH, 9); mv.visitInsn(IASTORE); mv.visitLabel(l10); mv.visitJumpInsn(GOTO, l12); mv.visitLabel(l11); mv.visitInsn(POP); mv.visitLabel(l12); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ONE", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitInsn(ICONST_1); mv.visitInsn(IASTORE); mv.visitLabel(l13); mv.visitJumpInsn(GOTO, l15); mv.visitLabel(l14); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/NoSuchFieldError" }); mv.visitInsn(POP); mv.visitLabel(l15); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "SEVEN", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitIntInsn(BIPUSH, 7); mv.visitInsn(IASTORE); mv.visitLabel(l16); mv.visitJumpInsn(GOTO, l18); mv.visitLabel(l17); mv.visitInsn(POP); mv.visitLabel(l18); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "SIX", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitIntInsn(BIPUSH, 6); mv.visitInsn(IASTORE); mv.visitLabel(l19); mv.visitJumpInsn(GOTO, l21); mv.visitLabel(l20); mv.visitInsn(POP); mv.visitLabel(l21); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "TEN", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitIntInsn(BIPUSH, 10); mv.visitInsn(IASTORE); mv.visitLabel(l22); mv.visitJumpInsn(GOTO, l24); mv.visitLabel(l23); mv.visitInsn(POP); mv.visitLabel(l24); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "THREE", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitInsn(ICONST_3); mv.visitInsn(IASTORE); mv.visitLabel(l25); mv.visitJumpInsn(GOTO, l27); mv.visitLabel(l26); mv.visitInsn(POP); mv.visitLabel(l27); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "TWO", "Lorg/spongepowered/test/decompile/SwitchTests$TestEnum;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/spongepowered/test/decompile/SwitchTests$TestEnum", "ordinal", "()I", false); mv.visitInsn(ICONST_2); mv.visitInsn(IASTORE); mv.visitLabel(l28); Label l32 = new Label(); mv.visitJumpInsn(GOTO, l32); mv.visitLabel(l29); mv.visitInsn(POP); mv.visitLabel(l32); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(DUP); mv.visitFieldInsn(PUTSTATIC, "org/spongepowered/test/decompile/SwitchTests", "$SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum", "[I"); mv.visitInsn(ARETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.yx.asm.ProxyMethodWritor.java
License:Apache License
public static void writeVoid(MethodVisitor mv, AsmMethod asmMethod) { Box db = asmMethod.method.getAnnotation(Box.class); if (db == null) { return;/*from w w w . j a v a 2s. c o m*/ } String superowener = Type.getInternalName(asmMethod.superClz); String currentClz = asmMethod.currentClz.replace('.', '/'); List<Object> argTypes = AsmUtils.getImplicitFrame( asmMethod.desc.substring(asmMethod.desc.indexOf("(") + 1, asmMethod.desc.indexOf(")"))); int localVariableIndex = argLength(argTypes); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); Label l3 = new Label(); Label l4 = new Label(); mv.visitTryCatchBlock(l0, l3, l4, null); mv.visitTypeInsn(NEW, AOPEXCUTOR); mv.visitInsn(DUP); mv.visitInsn(db.embed() ? ICONST_1 : ICONST_0); mv.visitMethodInsn(INVOKESPECIAL, AOPEXCUTOR, "<init>", "(Z)V", false); mv.visitVarInsn(ASTORE, localVariableIndex + 1); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitLdcInsn(db.dbName()); mv.visitFieldInsn(GETSTATIC, "org/yx/db/DBType", db.dbType().toString(), "Lorg/yx/db/DBType;"); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "begin", "(Ljava/lang/String;Lorg/yx/db/DBType;)V", false); mv.visitVarInsn(ALOAD, 0); loadArgs(mv, argTypes, 0); mv.visitMethodInsn(INVOKESPECIAL, superowener, asmMethod.name, asmMethod.desc, false); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "commit", "()V", false); mv.visitLabel(l1); Label l5 = new Label(); mv.visitJumpInsn(GOTO, l5); mv.visitLabel(l2); List<Object> list = new ArrayList<Object>(); list.add(currentClz); list.addAll(argTypes); list.add(AOPEXCUTOR); Object[] frames = list.toArray(new Object[list.size()]); mv.visitFrame(Opcodes.F_FULL, frames.length, frames, 1, new Object[] { "java/lang/Exception" }); mv.visitVarInsn(ASTORE, localVariableIndex + 2); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitVarInsn(ALOAD, localVariableIndex + 2); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "rollback", "(Ljava/lang/Throwable;)V", false); mv.visitLabel(l3); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "close", "()V", false); Label l6 = new Label(); mv.visitJumpInsn(GOTO, l6); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(ASTORE, localVariableIndex + 3); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "close", "()V", false); mv.visitVarInsn(ALOAD, localVariableIndex + 3); mv.visitInsn(ATHROW); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "close", "()V", false); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(Math.max(3, localVariableIndex + 1), 4 + localVariableIndex); mv.visitEnd(); }
From source file:org.yx.asm.ProxyMethodWritor.java
License:Apache License
public static void writeWithReturn(MethodVisitor mv, AsmMethod asmMethod) { Box db = asmMethod.method.getAnnotation(Box.class); if (db == null) { return;//from ww w . j av a2s .com } String superowener = Type.getInternalName(asmMethod.superClz); String currentClz = asmMethod.currentClz.replace('.', '/'); List<Object> argTypes = AsmUtils.getImplicitFrame( asmMethod.desc.substring(asmMethod.desc.indexOf("(") + 1, asmMethod.desc.indexOf(")"))); Object returnType = AsmUtils.getImplicitFrame(asmMethod.desc.substring(asmMethod.desc.indexOf(")") + 1)) .get(0); int localVariableIndex = argLength(argTypes); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); Label l3 = new Label(); mv.visitTryCatchBlock(l0, l1, l3, null); Label l4 = new Label(); mv.visitTryCatchBlock(l2, l4, l3, null); mv.visitTypeInsn(NEW, AOPEXCUTOR); mv.visitInsn(DUP); mv.visitInsn(db.embed() ? ICONST_1 : ICONST_0); mv.visitMethodInsn(INVOKESPECIAL, AOPEXCUTOR, "<init>", "(Z)V", false); mv.visitVarInsn(ASTORE, localVariableIndex + 1); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitLdcInsn(db.dbName()); mv.visitFieldInsn(GETSTATIC, "org/yx/db/DBType", db.dbType().toString(), "Lorg/yx/db/DBType;"); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "begin", "(Ljava/lang/String;Lorg/yx/db/DBType;)V", false); mv.visitVarInsn(ALOAD, 0); loadArgs(mv, argTypes, 0); mv.visitMethodInsn(INVOKESPECIAL, superowener, asmMethod.name, asmMethod.desc, false); int methodReturnFrameIndex = localVariableIndex + 2; store(mv, returnType, methodReturnFrameIndex); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "commit", "()V", false); mv.visitLabel(l1); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "close", "()V", false); load(mv, returnType, methodReturnFrameIndex); jReturn(mv, returnType); mv.visitLabel(l2); List<Object> list = new ArrayList<Object>(); list.add(currentClz); list.addAll(argTypes); list.add(AOPEXCUTOR); Object[] frames = list.toArray(new Object[list.size()]); mv.visitFrame(Opcodes.F_FULL, frames.length, frames, 1, new Object[] { "java/lang/Exception" }); mv.visitVarInsn(ASTORE, localVariableIndex + 2); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitVarInsn(ALOAD, localVariableIndex + 2); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "rollback", "(Ljava/lang/Throwable;)V", false); mv.visitLabel(l4); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "close", "()V", false); Label l5 = new Label(); mv.visitJumpInsn(GOTO, l5); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(ASTORE, localVariableIndex + 3); mv.visitVarInsn(ALOAD, localVariableIndex + 1); mv.visitMethodInsn(INVOKEVIRTUAL, AOPEXCUTOR, "close", "()V", false); mv.visitVarInsn(ALOAD, localVariableIndex + 3); mv.visitInsn(ATHROW); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitTypeInsn(NEW, "org/yx/exception/SumkException"); mv.visitInsn(DUP); mv.visitLdcInsn(new Integer(-364533425)); mv.visitLdcInsn("you are locky to see me^_^"); mv.visitMethodInsn(INVOKESPECIAL, "org/yx/exception/SumkException", "<init>", "(ILjava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitMaxs(Math.max(4, localVariableIndex + 1), 4 + localVariableIndex); mv.visitEnd(); }
From source file:pl.asie.foamfix.coremod.patches.ReturnIfBooleanTruePatch.java
License:Open Source License
public ReturnIfBooleanTruePatch(String optionName, String... methods) { this.optionName = optionName; this.methods = ImmutableSet.copyOf(methods); list = new InsnList(); Label l = new Label(); LabelNode ln = new LabelNode(l); list.add(new FieldInsnNode(Opcodes.GETSTATIC, "pl/asie/foamfix/shared/FoamFixShared", "config", "Lpl/asie/foamfix/shared/FoamFixConfig;")); list.add(new FieldInsnNode(Opcodes.GETFIELD, "pl/asie/foamfix/shared/FoamFixConfig", optionName, "Z")); list.add(new JumpInsnNode(Opcodes.IFEQ, ln)); list.add(new InsnNode(Opcodes.RETURN)); list.add(ln);/*from w ww . j av a 2 s . c om*/ list.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null)); }
From source file:pl.clareo.coroutines.core.ClassTransformer.java
License:Apache License
@SuppressWarnings("unchecked") void transform() { for (MethodNode coroutine : coroutines) { if (log.isLoggable(Level.FINEST)) { log.finest("Generating method for coroutine " + coroutine.name + coroutine.desc); }/*from w w w . j a v a2 s.c o m*/ String coroutineName = getCoroutineName(coroutine); MethodTransformer methodTransformer = new MethodTransformer(coroutine, thisType); MethodNode coroutineImpl = methodTransformer.transform(coroutineName, generateDebugCode); thisNode.methods.add(coroutineImpl); /* * generate co iterators and method stubs */ log.finest("Generating CoIterator implementation and method stubs"); String baseCoIteratorName; Map<String, Object> annotation = getCoroutineAnnotationValues(coroutine); if (getBoolean(annotation, "threadLocal")) { baseCoIteratorName = Type.getInternalName(ThreadLocalCoIterator.class); } else { baseCoIteratorName = Type.getInternalName(SingleThreadedCoIterator.class); } String coIteratorClassName = "pl/clareo/coroutines/core/CoIterator" + num; ClassNode coIteratorClass = new ClassNode(); coIteratorClass.version = Opcodes.V1_6; coIteratorClass.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER; coIteratorClass.name = coIteratorClassName; coIteratorClass.superName = baseCoIteratorName; if (generateDebugCode) { /* * If debugging code is emitted create field keeping JDK logger */ FieldNode loggerField = new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "logger", "Ljava/util/logging/Logger;", null, null); coIteratorClass.fields.add(loggerField); MethodNode clinit = new MethodNode(); clinit.access = Opcodes.ACC_STATIC; clinit.name = "<clinit>"; clinit.desc = "()V"; clinit.exceptions = Collections.EMPTY_LIST; String loggerName = thisType.getClassName(); InsnList clinitCode = clinit.instructions; clinitCode.add(new LdcInsnNode(loggerName)); clinitCode.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/logging/Logger", "getLogger", "(Ljava/lang/String;)Ljava/util/logging/Logger;")); clinitCode.add(new FieldInsnNode(Opcodes.PUTSTATIC, coIteratorClassName, "logger", "Ljava/util/logging/Logger;")); clinitCode.add(new InsnNode(Opcodes.RETURN)); clinit.maxStack = 1; clinit.maxLocals = 0; coIteratorClass.methods.add(clinit); } /* * Generate constructor */ MethodNode init = new MethodNode(); init.access = Opcodes.ACC_PUBLIC; init.name = "<init>"; init.desc = CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR; init.exceptions = Collections.EMPTY_LIST; InsnList initCode = init.instructions; initCode.add(new VarInsnNode(Opcodes.ALOAD, 0)); initCode.add(new VarInsnNode(Opcodes.ALOAD, 1)); initCode.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, baseCoIteratorName, "<init>", CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR)); initCode.add(new InsnNode(Opcodes.RETURN)); init.maxStack = 2; init.maxLocals = 2; coIteratorClass.methods.add(init); /* * Generate overriden call to coroutine */ MethodNode call = new MethodNode(); call.access = Opcodes.ACC_PROTECTED; call.name = "call"; call.desc = CALL_METHOD_DESCRIPTOR; call.exceptions = Collections.EMPTY_LIST; InsnList callCode = call.instructions; /* * if debug needed generate call details */ if (generateDebugCode) { String coroutineId = "Coroutine " + coroutine.name; callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER, coroutineId + " call. Caller sent: ", 2)); callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK)); callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINEST, coroutineId + " state ", 1)); callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK)); } /* * push call arguments: this (if not static), frame, input, output */ boolean isStatic = (coroutine.access & Opcodes.ACC_STATIC) != 0; if (!isStatic) { callCode.add(new VarInsnNode(Opcodes.ALOAD, 1)); callCode.add( new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getThis", "()Ljava/lang/Object;")); callCode.add(new TypeInsnNode(Opcodes.CHECKCAST, thisType.getInternalName())); } callCode.add(new VarInsnNode(Opcodes.ALOAD, 1)); callCode.add(new InsnNode(Opcodes.ACONST_NULL)); callCode.add(new VarInsnNode(Opcodes.ALOAD, 2)); callCode.add(new MethodInsnNode(isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, thisType.getInternalName(), coroutineName, COROUTINE_METHOD_DESCRIPTOR)); // stack: * if (!generateDebugCode) { callCode.add(new InsnNode(Opcodes.ARETURN)); } else { // save result display suspension point (two more locals // needed) callCode.add(new VarInsnNode(Opcodes.ASTORE, 3)); callCode.add(new VarInsnNode(Opcodes.ALOAD, 1)); callCode.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLineOfCode", "()I")); callCode.add(box_int(Type.INT)); callCode.add(new VarInsnNode(Opcodes.ASTORE, 4)); callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER, "Coroutine suspended at line ", 4, ". Yielded:", 3)); callCode.add(new FrameNode(Opcodes.F_APPEND, 2, new Object[] { "java/lang/Object", "java/lang/Integer" }, 0, EMPTY_STACK)); callCode.add(new VarInsnNode(Opcodes.ALOAD, 3)); callCode.add(new InsnNode(Opcodes.ARETURN)); } coIteratorClass.methods.add(call); // if debugging code is emitted it needs space for two // additional locals and 5 stack operand if (generateDebugCode) { call.maxStack = 5; call.maxLocals = 5; } else { if (isStatic) { call.maxStack = 3; } else { call.maxStack = 4; } call.maxLocals = 3; } /* * CoIterator created - define it in the runtime and verify if * needed */ if (log.isLoggable(Level.FINEST)) { log.finest("Generated class " + coIteratorClassName); } ClassWriter cw = new ClassWriter(0); coIteratorClass.accept(cw); byte[] classBytes = cw.toByteArray(); try { CoroutineInstrumentator.dumpClass(coIteratorClassName, classBytes); } catch (IOException e) { throw new CoroutineGenerationException("Unable to write class " + coIteratorClassName, e); } /* * start generating method - new method is named as the method in * user code, it: returns instance of appropriate CoIterator (see * above), saves arguments of call */ if (log.isLoggable(Level.FINEST)) { log.finest("Instrumenting method " + coroutine.name); } InsnList code = coroutine.instructions; code.clear(); /* * create new Frame */ boolean isDebugFramePossible = generateDebugCode && coroutine.localVariables != null; if (isDebugFramePossible) { code.add(createDebugFrame(coroutine)); } else { code.add(createFrame(coroutine)); } /* * save frame in the first, and locals array in the second local * variable */ int argsSize = Type.getArgumentsAndReturnSizes(coroutine.desc) >> 2; if (isStatic) { argsSize -= 1; } code.add(new VarInsnNode(Opcodes.ASTORE, argsSize)); code.add(new VarInsnNode(Opcodes.ALOAD, argsSize)); code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLocals", "()[Ljava/lang/Object;")); int localsArrayIndex = argsSize + 1; code.add(new VarInsnNode(Opcodes.ASTORE, localsArrayIndex)); /* * save all call arguments (along with this if this method is not * static) into locals array */ Type[] argsTypes = Type.getArgumentTypes(coroutine.desc); if (!isStatic) { code.add(saveloc(localsArrayIndex, 0, 0, JAVA_LANG_OBJECT)); code.add(savelocs(localsArrayIndex, 1, 1, argsTypes)); } else { code.add(savelocs(localsArrayIndex, 0, 0, argsTypes)); } /* * create CoIterator instance with saved frame, make initial call to * next if needed and return to caller */ code.add(new TypeInsnNode(Opcodes.NEW, coIteratorClassName)); code.add(new InsnNode(Opcodes.DUP)); code.add(new VarInsnNode(Opcodes.ALOAD, argsSize)); code.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, coIteratorClassName, "<init>", CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR)); if (!getBoolean(annotation, "generator", true)) { code.add(new InsnNode(Opcodes.DUP)); code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, coIteratorClassName, "next", "()Ljava/lang/Object;")); code.add(new InsnNode(Opcodes.POP)); } code.add(new InsnNode(Opcodes.ARETURN)); /* * end method generation; maxs can be statically determined 3 * operands on stack (call to frame setLocals and CoIterator * constructor) + 1 if any argument is long or double (debug frame * needs 7 operands for variable names creation); locals = argsSize * + 1 reference to frame + 1 array of locals */ if (isDebugFramePossible) { coroutine.maxStack = 7; } else { boolean isCategory2ArgumentPresent = false; for (Type argType : argsTypes) { int sort = argType.getSort(); if (sort == Type.LONG || sort == Type.DOUBLE) { isCategory2ArgumentPresent = true; break; } } coroutine.maxStack = isCategory2ArgumentPresent ? 4 : 3; } coroutine.maxLocals = localsArrayIndex + 1; coroutine.localVariables.clear(); coroutine.tryCatchBlocks.clear(); num++; } }
From source file:st.redline.compiler.ClassBytecodeWriter.java
License:Open Source License
public void setupCatchForBlockReturn(SimpleExpression simpleExpression, String blockReturnType) { // System.out.println("setupCatchForBlockReturn: " + simpleExpression); Label l1 = (Label) simpleExpression.label1(); Label l2 = (Label) simpleExpression.label2(); mv.visitLabel(l1);/*w ww . j a v a 2s . c o m*/ Label l3 = new Label(); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { blockReturnType }); mv.visitMethodInsn(INVOKEVIRTUAL, blockReturnType, "answer", "()Lst/redline/lang/ProtoObject;"); mv.visitInsn(ARETURN); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); }