List of usage examples for org.objectweb.asm Opcodes RETURN
int RETURN
To view the source code for org.objectweb.asm Opcodes RETURN.
Click Source Link
From source file:com.googlecode.ddom.weaver.ext.ModelExtensionFactoryDelegateImplementation.java
License:Apache License
public void accept(ClassVisitor classVisitor) { String factoryName = Util .classNameToInternalName(modelExtensionClassInfo.getFactoryDelegateImplementationClassName()); classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, factoryName, null, "java/lang/Object", new String[] { Util.classNameToInternalName(implementationInfo.getFactoryDelegateInterfaceName()) }); String className = Util.classNameToInternalName(modelExtensionClassInfo.getClassName()); {/*ww w . j a v a 2 s. c o m*/ MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } } for (ConstructorInfo constructor : implementationInfo.getConstructors()) { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "create", constructor.getFactoryDelegateMethodDescriptor(), constructor.getSignature(), constructor.getExceptions()); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitTypeInsn(Opcodes.NEW, className); mv.visitInsn(Opcodes.DUP); Type[] argumentTypes = constructor.getArgumentTypes(); for (int i = 0; i < argumentTypes.length; i++) { mv.visitVarInsn(argumentTypes[i].getOpcode(Opcodes.ILOAD), i + 1); } mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", constructor.getDescriptor()); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + factoryName + ";", null, l0, l1, 0); for (int i = 0; i < argumentTypes.length; i++) { mv.visitLocalVariable("arg" + i, argumentTypes[i].getDescriptor(), null, l0, l1, i + 1); } mv.visitMaxs(argumentTypes.length + 2, argumentTypes.length + 1); mv.visitEnd(); } } classVisitor.visitEnd(); }
From source file:com.googlecode.ddom.weaver.ext.ModelExtensionFactoryImplementation.java
License:Apache License
public void accept(ClassVisitor classVisitor) { // Note: the name chosen here must match what is expected in ExtensionFactoryLocator String name = Util.classNameToInternalName(implementationInfo.getFactoryInterface().getName() + "$$Impl"); String factoryInterfaceName = Util .classNameToInternalName(implementationInfo.getFactoryInterface().getName()); classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", new String[] { factoryInterfaceName }); {//from w w w. j av a2 s. c o m FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "INSTANCE", "L" + factoryInterfaceName + ";", null, null); if (fw != null) { fw.visitEnd(); } } { FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "delegates", "Ljava/util/Map;", null, null); if (fw != null) { fw.visitEnd(); } } { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "<init>", "()V", null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); // Call constructor from superclass mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); // Create delegates map mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V"); mv.visitFieldInsn(Opcodes.PUTFIELD, name, "delegates", "Ljava/util/Map;"); // Populate delegates map for (ModelExtensionInfo modelExtensionInfo : implementationInfo.getModelExtensions()) { for (ModelExtensionInterfaceInfo extensionInterface : modelExtensionInfo .getExtensionInterfaces()) { if (!extensionInterface.isAbstract()) { // TODO: this is stupid; we should not recreate the info object here ModelExtensionClassInfo modelExtensionClassInfo = new ModelExtensionClassInfo( implementationInfo.getImplementation(), modelExtensionInfo.getRootInterface(), extensionInterface); String factoryDelegateImplName = Util.classNameToInternalName( modelExtensionClassInfo.getFactoryDelegateImplementationClassName()); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;"); mv.visitLdcInsn(Type.getObjectType(Util.classNameToInternalName( modelExtensionClassInfo.getExtensionInterface().getClassInfo().getName()))); mv.visitTypeInsn(Opcodes.NEW, factoryDelegateImplName); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, factoryDelegateImplName, "<init>", "()V"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(Opcodes.POP); } } } mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0); mv.visitMaxs(4, 1); mv.visitEnd(); } } String factoryDelegateInterfaceName = Util .classNameToInternalName(implementationInfo.getFactoryDelegateInterfaceName()); String getDelegateDesc = "(Ljava/lang/Class;)L" + factoryDelegateInterfaceName + ";"; { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "getDelegate", getDelegateDesc, null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;"); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(Opcodes.CHECKCAST, factoryDelegateInterfaceName); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0); mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l1, 1); mv.visitMaxs(2, 2); mv.visitEnd(); } } String implementationName = Util.classNameToInternalName(implementationInfo.getImplementation().getName()); for (ConstructorInfo constructor : implementationInfo.getConstructors()) { Type[] constructorArgumentTypes = constructor.getArgumentTypes(); Type[] argumentTypes = new Type[constructorArgumentTypes.length + 1]; argumentTypes[0] = Type.getObjectType("java/lang/Class"); System.arraycopy(constructorArgumentTypes, 0, argumentTypes, 1, constructorArgumentTypes.length); MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "create", Type.getMethodDescriptor(Type.getObjectType(implementationName), argumentTypes), null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 1); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.IFNONNULL, l1); mv.visitTypeInsn(Opcodes.NEW, implementationName); mv.visitInsn(Opcodes.DUP); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2); } mv.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationName, "<init>", constructor.getDescriptor()); mv.visitInsn(Opcodes.ARETURN); mv.visitLabel(l1); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, "getDelegate", getDelegateDesc); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2); } mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, factoryDelegateInterfaceName, "create", constructor.getFactoryDelegateMethodDescriptor()); mv.visitInsn(Opcodes.ARETURN); Label l3 = new Label(); mv.visitLabel(l3); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l3, 0); mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l3, 1); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitLocalVariable("arg" + i, constructorArgumentTypes[i].getDescriptor(), null, l0, l3, i + 2); } mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1); mv.visitEnd(); } } { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); if (mv != null) { mv.visitCode(); mv.visitTypeInsn(Opcodes.NEW, name); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", "()V"); mv.visitFieldInsn(Opcodes.PUTSTATIC, name, "INSTANCE", "L" + factoryInterfaceName + ";"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 0); mv.visitEnd(); } } classVisitor.visitEnd(); }
From source file:com.googlecode.ddom.weaver.inject.InjectionAdapter.java
License:Apache License
@Override public void visitEnd() { for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, fieldInfo.getFactoryMethodName(), fieldInfo.getFactoryMethodDesc(), null, new String[0]); if (mv != null) { fieldInfo.getInjector().generateFactoryMethodCode(mv); }// w w w .j a v a2s. c o m } if (injectionInfo.hasInjectableInstanceFields()) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE, "inject$$instance", "()V", null, new String[0]); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) { if (!fieldInfo.isStatic()) { mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, fieldInfo.getFactoryMethodName(), fieldInfo.getFactoryMethodDesc()); mv.visitFieldInsn(Opcodes.PUTFIELD, className, fieldInfo.getFieldName(), fieldInfo.getFieldDesc()); } } mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(2, 1); mv.visitEnd(); } } if (injectionInfo.hasInjectableClassFields()) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, hasClassInitializer ? "inject$$class" : "<clinit>", "()V", null, new String[0]); if (mv != null) { mv.visitCode(); for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) { if (fieldInfo.isStatic()) { mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, fieldInfo.getFactoryMethodName(), fieldInfo.getFactoryMethodDesc()); mv.visitFieldInsn(Opcodes.PUTSTATIC, className, fieldInfo.getFieldName(), fieldInfo.getFieldDesc()); } } mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 0); mv.visitEnd(); } } super.visitEnd(); }
From source file:com.googlecode.ddom.weaver.mixin.MixinInfoBuilder.java
License:Apache License
public MixinInfo build(ClassRealm realm) { // TODO: do we still need this? // if (!contributedInterfaces.remove(target.getInternalName())) { // log.warning("Mixin class doesn't implement target interface"); // }// ww w . j av a 2s. c om List<ClassInfo> targets = new ArrayList<ClassInfo>(targetTypes.size()); for (Type type : targetTypes) { targets.add(realm.getClassInfo(type.getClassName())); } Extensions extensions = new Extensions(); boolean emptyInitMethod = true; for (Iterator<?> it = initMethod.instructions.iterator(); it.hasNext();) { int opcode = ((AbstractInsnNode) it.next()).getOpcode(); if (opcode != -1 && opcode != Opcodes.RETURN) { emptyInitMethod = false; break; } } MixinInfo mixinInfo = new MixinInfo(mixinName, targets, contributedInterfaces, emptyInitMethod ? null : initMethod, fields, methods, extensions); for (MixinInfoBuilderCollaborator collaborator : collaborators) { collaborator.process(realm, mixinInfo, extensions); } return mixinInfo; }
From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java
License:Open Source License
private MethodNode createNewSetWorldTimeMethod(String methodName, boolean obfuscated) { // > void setWorldTime(long time) { // > TimeTweaksManager.setWorldTime(this, time); // > }//from w ww. ja va2 s .c om MethodNode setWorldTimeMethod = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, methodName, "(J)V", null, null); setWorldTimeMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); setWorldTimeMethod.instructions.add(new VarInsnNode(Opcodes.LLOAD, 1)); setWorldTimeMethod.instructions .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hea3ven/hardmodetweaks/TimeTweaksManager", "setWorldTime", "(L" + WORLD_INFO.getPath(obfuscated) + ";J)V")); setWorldTimeMethod.instructions.add(new InsnNode(Opcodes.RETURN)); return setWorldTimeMethod; }
From source file:com.khubla.jvmbasic.jvmbasicc.JVMBasicCompiler.java
License:Open Source License
/** * generate init.//w w w. j a v a2 s. c om * <p> * This inits the class members and assigns class members, such as the ExecutionContext. * </p> * <p> * <code> * public ExecutionContext executionContext = new ExecutionContext(); * </code> * </p> */ protected void generateInit(String className, ClassWriter classWriter) throws Exception { try { final MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); methodVisitor.visitCode(); /* * call init() */ final Label l0 = new Label(); methodVisitor.visitLabel(l0); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); /* * create a new execution context and assign it */ final Label l1 = new Label(); methodVisitor.visitLabel(l1); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitTypeInsn(Opcodes.NEW, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT); methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "<init>", "()V"); methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, className, RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE); /* * return */ final Label l2 = new Label(); methodVisitor.visitLabel(l2); methodVisitor.visitLineNumber(8, l2); methodVisitor.visitInsn(Opcodes.RETURN); /* * declare variables */ final Label l3 = new Label(); methodVisitor.visitLabel(l3); methodVisitor.visitLocalVariable("this", "L" + className + ";", null, l0, l3, 0); methodVisitor.visitMaxs(3, 1); methodVisitor.visitEnd(); } catch (final Exception e) { throw new Exception("Exception in generateInit", e); } }
From source file:com.khubla.jvmbasic.jvmbasicc.JVMBasicCompiler.java
License:Open Source License
/** * generate void main(String[])/*ww w. j a va 2s. co m*/ * <p> * <code> * public static void main(String[] args) { * ExampleProgram exampleProgram = new ExampleProgram(); * try { * exampleProgram.inputStream = System.in; * exampleProgram.outputStream = System.out; * exampleProgram.program(); * } catch (Exception e) { * e.printStackTrace(); * } * } * </code> * </p> */ protected void generateMain(String classname, ClassWriter classWriter) throws Exception { try { /* * make method */ final MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); methodVisitor.visitCode(); final Label l0 = new Label(); final Label l1 = new Label(); final Label l2 = new Label(); methodVisitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); final Label l3 = new Label(); methodVisitor.visitLabel(l3); /* * declare a local instance of the class in the void() main, store as variable 1. */ methodVisitor.visitTypeInsn(Opcodes.NEW, classname); methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, classname, "<init>", "()V"); methodVisitor.visitVarInsn(Opcodes.ASTORE, 1); /* * assign the input stream */ methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "in", "Ljava/io/InputStream;"); methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, classname, "inputStream", "Ljava/io/InputStream;"); /* * assign the output stream */ methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, classname, "outputStream", "Ljava/io/PrintStream;"); /* * load the class instance from variable 1 and call "program" */ methodVisitor.visitLabel(l0); methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classname, "program", "()V"); methodVisitor.visitLabel(l1); final Label l4 = new Label(); methodVisitor.visitJumpInsn(Opcodes.GOTO, l4); methodVisitor.visitLabel(l2); methodVisitor.visitFrame(Opcodes.F_FULL, 2, new Object[] { "[Ljava/lang/String;", classname }, 1, new Object[] { "java/lang/Exception" }); methodVisitor.visitVarInsn(Opcodes.ASTORE, 2); final Label l5 = new Label(); methodVisitor.visitLabel(l5); methodVisitor.visitLineNumber(21, l5); methodVisitor.visitVarInsn(Opcodes.ALOAD, 2); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V"); /* * return */ methodVisitor.visitLabel(l4); methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); methodVisitor.visitInsn(Opcodes.RETURN); /* * declare the parameters */ final Label l6 = new Label(); methodVisitor.visitLabel(l6); methodVisitor.visitLocalVariable("args", "[Ljava/lang/String;", null, l3, l6, 0); methodVisitor.visitLocalVariable("exampleProgram", "L" + classname + ";", null, l0, l6, 1); methodVisitor.visitLocalVariable("e", "Ljava/lang/Exception;", null, l5, l4, 2); /* * done */ methodVisitor.visitMaxs(2, 3); methodVisitor.visitEnd(); } catch (final Exception e) { throw new Exception("Exception in generateMain", e); } }
From source file:com.khubla.jvmbasic.jvmbasicc.JVMBasicCompiler.java
License:Open Source License
/** * Generate program/* w w w. j a v a 2 s. c o m*/ * <p> * Java prototype is "public program()" * </p> */ protected void generateProgram(String classname, ClassWriter classWriter, ProgContext progContext) throws Exception { try { final MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "program", "()V", null, new String[] { "java/lang/Exception" }); methodVisitor.visitCode(); /* * label for the start of the method */ final Label l0 = new Label(); methodVisitor.visitLabel(l0); /* * do the static analysis */ final StaticAnalysis programStaticAnalysis = new StaticAnalysis(); programStaticAnalysis.performStaticAnalysis(progContext); /* * show the static analysis */ logger.info("Static analyis results for '" + classname + "'"); programStaticAnalysis.showAnalysisResults(); /* * recurse into the parse tree */ final Function function = new progRule(); final GenerationContext generationContext = new GenerationContext(classname, methodVisitor, classWriter, progContext, programStaticAnalysis); function.execute(generationContext); /* * return */ final Label l1 = new Label(); methodVisitor.visitLabel(l1); methodVisitor.visitInsn(Opcodes.RETURN); /* * declare the *this* local variable */ final Label l2 = new Label(); methodVisitor.visitLabel(l2); methodVisitor.visitLocalVariable("this", "L" + classname + ";", null, l0, l2, 0); /* * show all the other local variables */ logger.info("JVM local variables"); for (int i = 1; i < (GenerationContext.getLocalvariables().size() + 1); i++) { final LocalVariableDeclaration lvd = GenerationContext.getLocalvariables().get(i); logger.info("variable: " + lvd.getName() + " declared on line: " + lvd.getBasicLine() + " frame index: " + lvd.getIndex()); } /* * we are done */ methodVisitor.visitMaxs(0, 1); methodVisitor.visitEnd(); } catch (final Exception e) { throw new Exception("Exception in generateProgram", e); } }
From source file:com.liferay.alloy.mvc.jsonwebservice.AlloyControllerInvokerManager.java
License:Open Source License
protected byte[] generateAlloyControllerInvokerClassData(Class<?> controllerClass, String alloyControllerInvokerClassName) throws NoClassNecessaryException { boolean jsonWebServiceMethodsPresent = false; ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES); String alloyControllerInvokerClassBinaryName = getClassBinaryName(alloyControllerInvokerClassName); String baseAlloyControllerInvokerClassBinaryName = getClassBinaryName( BaseAlloyControllerInvokerImpl.class.getName()); classWriter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, alloyControllerInvokerClassBinaryName, null, baseAlloyControllerInvokerClassBinaryName, null); MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); methodVisitor.visitCode();/*www . j a va2 s.co m*/ methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, baseAlloyControllerInvokerClassBinaryName, "<init>", "()V"); methodVisitor.visitInsn(Opcodes.RETURN); methodVisitor.visitMaxs(1, 1); methodVisitor.visitEnd(); Method[] methods = controllerClass.getDeclaredMethods(); for (Method method : methods) { if (!Modifier.isPublic(method.getModifiers())) { continue; } JSONWebServiceMethod jsonWebServiceMethod = method.getAnnotation(JSONWebServiceMethod.class); if (jsonWebServiceMethod == null) { continue; } jsonWebServiceMethodsPresent = true; Class<?>[] parameterTypes = jsonWebServiceMethod.parameterTypes(); StringBundler sb = new StringBundler(parameterTypes.length + 3); sb.append(StringPool.OPEN_PARENTHESIS); for (Class<?> parameterType : parameterTypes) { sb.append(Type.getDescriptor(parameterType)); } sb.append(StringPool.CLOSE_PARENTHESIS); sb.append(Type.getDescriptor(JSONSerializable.class)); String methodDescriptor = sb.toString(); methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methodDescriptor, null, new String[] { getClassBinaryName(Exception.class.getName()) }); methodVisitor.visitCode(); for (int i = 0; i < parameterTypes.length; i++) { String parameterName = jsonWebServiceMethod.parameterNames()[i]; Class<?> parameterType = parameterTypes[i]; methodVisitor.visitLocalVariable(parameterName, Type.getDescriptor(parameterType), null, new Label(), new Label(), i + 1); } methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitLdcInsn(jsonWebServiceMethod.lifecycle()); methodVisitor.visitIntInsn(Opcodes.BIPUSH, parameterTypes.length * 2 + 2); methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, getClassBinaryName(Object.class.getName())); methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitInsn(Opcodes.ICONST_0); methodVisitor.visitLdcInsn("action"); methodVisitor.visitInsn(Opcodes.AASTORE); methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitInsn(Opcodes.ICONST_1); methodVisitor.visitLdcInsn(method.getName()); methodVisitor.visitInsn(Opcodes.AASTORE); for (int i = 0; i < parameterTypes.length; i++) { String parameterName = jsonWebServiceMethod.parameterNames()[i]; methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitIntInsn(Opcodes.BIPUSH, (i + 1) * 2); methodVisitor.visitLdcInsn(parameterName); methodVisitor.visitInsn(Opcodes.AASTORE); methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitIntInsn(Opcodes.BIPUSH, (i + 1) * 2 + 1); methodVisitor.visitVarInsn(Opcodes.ALOAD, i + 1); methodVisitor.visitInsn(Opcodes.AASTORE); } sb = new StringBundler(5); sb.append(StringPool.OPEN_PARENTHESIS); sb.append(Type.getDescriptor(String.class)); sb.append(Type.getDescriptor(Object[].class)); sb.append(StringPool.CLOSE_PARENTHESIS); sb.append(Type.getDescriptor(JSONSerializable.class)); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, alloyControllerInvokerClassBinaryName, "invokeAlloyController", sb.toString()); methodVisitor.visitInsn(Opcodes.ARETURN); methodVisitor.visitMaxs(-1, -1); methodVisitor.visitEnd(); } classWriter.visitEnd(); if (!jsonWebServiceMethodsPresent) { throw new NoClassNecessaryException(); } return classWriter.toByteArray(); }
From source file:com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtilTest.java
License:Open Source License
private void _doTestCreateProxyMethodNode(Method method, int index, String skeletonId, String stubInternalName) { MethodNode proxyMethodNode = IntrabandProxyUtil.createProxyMethodNode(method, index, skeletonId, Type.getType(stubInternalName)); _assertMethodNodeSignature(proxyMethodNode, method.getModifiers() & ~Modifier.ABSTRACT, method.getName(), Type.getMethodDescriptor(method), method.getExceptionTypes()); InsnList insnList = proxyMethodNode.instructions; Iterator<AbstractInsnNode> iterator = insnList.iterator(); // NEW com/liferay/portal/kernel/io/Serializer _assertTypeInsnNode(iterator.next(), Opcodes.NEW, Serializer.class); // DUP/*from w w w.ja va 2s.co m*/ _assertInsnNode(iterator.next(), Opcodes.DUP); // INVOKESPECIAL com/liferay/portal/kernel/io/Serializer <init> ()V _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, Type.getInternalName(Serializer.class), "<init>", Type.VOID_TYPE); // ASTORE argumentsSize Type methodType = Type.getType(method); int argumentsAndReturnSizes = methodType.getArgumentsAndReturnSizes(); int argumentsSize = argumentsAndReturnSizes >> 2; _assertVarInsnNode(iterator.next(), Opcodes.ASTORE, argumentsSize); // ALOAD argumentsSize _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize); // LDC skeletonId _assertLdcInsnNode(iterator.next(), Opcodes.LDC, skeletonId); // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeString // (Ljava/lang/String;)V _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class), "writeString", Type.VOID_TYPE, Type.getType(String.class)); // ALOAD argumentsSize _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize); // ALOAD 0 _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, 0); // GETFIELD stubInternalName _id Ljava/lang/String; _assertFieldInsnNode(iterator.next(), Opcodes.GETFIELD, stubInternalName, "_id", String.class); // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeString // (Ljava/lang/String;)V _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class), "writeString", Type.VOID_TYPE, Type.getType(String.class)); // ALOAD argumentsSize _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize); if (index <= 5) { // ICONST_index _assertInsnNode(iterator.next(), Opcodes.ICONST_0 + index); } else { // BIPUSH index _assertIntInsnNode(iterator.next(), Opcodes.BIPUSH, index); } // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeInt (I)V _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class), "writeInt", Type.VOID_TYPE, Type.INT_TYPE); Class<?>[] parameterTypes = method.getParameterTypes(); int offset = 1; for (int i = 0; i < parameterTypes.length; i++) { Class<?> parameterClass = parameterTypes[i]; // ALOAD argumentsSize _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize); // xLOAD i Type parameterType = Type.getType(parameterClass); _assertVarInsnNode(iterator.next(), parameterType.getOpcode(Opcodes.ILOAD), offset); offset += parameterType.getSize(); if (parameterClass.isPrimitive() || (parameterClass == String.class)) { String name = TextFormatter.format(parameterClass.getSimpleName(), TextFormatter.G); _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class), "write".concat(name), Type.VOID_TYPE, parameterType); } else { _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class), "writeObject", Type.VOID_TYPE, Type.getType(Serializable.class)); } } // ALOAD 0 _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, 0); // ALOAD argumentsSize _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize); Class<?> returnClass = method.getReturnType(); Type returnType = Type.getType(returnClass); if (returnClass == void.class) { // INVOKESPECIAL stubInternalName _send // (Lcom/liferay/portal/kernel/io/Serializer;)V _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, stubInternalName, "_send", Type.VOID_TYPE, Type.getType(Serializer.class)); _assertInsnNode(iterator.next(), Opcodes.RETURN); } else { // INVOKESPECIAL stubInternalName _syncSend // (Lcom/liferay/portal/kernel/io/Serializer;)Ljava/io/Serializable; _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, stubInternalName, "_syncSend", Type.getType(Serializable.class), Type.getType(Serializer.class)); if (returnClass.isPrimitive()) { // ASTORE argumentsSize + 1 _assertVarInsnNode(iterator.next(), Opcodes.ASTORE, argumentsSize + 1); // ALOAD argumentsSize + 1 _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize + 1); // IFNULL nullCheckLabel LabelNode nullCheckLabelNode = _assertJumpInsnNode(iterator.next(), Opcodes.IFNULL); // ALOAD argumentsSize + 1 _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize + 1); // CHECKCAST returnType _assertTypeInsnNode(iterator.next(), Opcodes.CHECKCAST, _autoboxingMap.get(returnClass)); if (returnClass == boolean.class) { // INVOKEVIRTUAL java/lang/Boolean booleanValue ()Z _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Boolean.class), "booleanValue", Type.BOOLEAN_TYPE); } else if (returnClass == byte.class) { // INVOKEVIRTUAL java/lang/Number intValue ()I _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Number.class), "intValue", Type.INT_TYPE); } else if (returnClass == char.class) { // INVOKEVIRTUAL java/lang/Character charValue ()C _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Character.class), "charValue", Type.CHAR_TYPE); } else if (returnClass == double.class) { // INVOKEVIRTUAL java/lang/Number doubleValue ()D _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Number.class), "doubleValue", Type.DOUBLE_TYPE); } else if (returnClass == float.class) { // INVOKEVIRTUAL java/lang/Number floatValue ()F _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Number.class), "floatValue", Type.FLOAT_TYPE); } else if (returnClass == int.class) { // INVOKEVIRTUAL java/lang/Number intValue ()I _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Number.class), "intValue", Type.INT_TYPE); } else if (returnClass == long.class) { // INVOKEVIRTUAL java/lang/Number longValue ()J _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Number.class), "longValue", Type.LONG_TYPE); } else if (returnClass == short.class) { // INVOKEVIRTUAL java/lang/Number intValue ()I _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Number.class), "intValue", Type.INT_TYPE); } // xRETURN _assertInsnNode(iterator.next(), returnType.getOpcode(Opcodes.IRETURN)); // nullCheckLabel Assert.assertSame(nullCheckLabelNode, iterator.next()); // xRETURN null/0 if (!returnClass.isPrimitive()) { _assertInsnNode(iterator.next(), Opcodes.ACONST_NULL); _assertInsnNode(iterator.next(), Opcodes.ARETURN); } else if (returnClass == void.class) { _assertInsnNode(iterator.next(), Opcodes.RETURN); } else if (returnClass == float.class) { _assertInsnNode(iterator.next(), Opcodes.FCONST_0); _assertInsnNode(iterator.next(), Opcodes.FRETURN); } else if (returnClass == double.class) { _assertInsnNode(iterator.next(), Opcodes.DCONST_0); _assertInsnNode(iterator.next(), Opcodes.DRETURN); } else if (returnClass == long.class) { _assertInsnNode(iterator.next(), Opcodes.LCONST_0); _assertInsnNode(iterator.next(), Opcodes.LRETURN); } else { _assertInsnNode(iterator.next(), Opcodes.ICONST_0); _assertInsnNode(iterator.next(), Opcodes.IRETURN); } } else { if (returnClass != Object.class) { // CHECKCAST _assertTypeInsnNode(iterator.next(), Opcodes.CHECKCAST, returnClass); } // ARETURN _assertInsnNode(iterator.next(), Opcodes.ARETURN); } } Assert.assertFalse(iterator.hasNext()); }