List of usage examples for org.objectweb.asm Opcodes PUTSTATIC
int PUTSTATIC
To view the source code for org.objectweb.asm Opcodes PUTSTATIC.
Click Source Link
From source file:erjang.beam.CompilerVisitor.java
License:Apache License
/** * /* w w w . j ava2 s . c o m*/ */ private void generate_classinit() { MethodVisitor mv = cv.visitMethod(ACC_STATIC | ACC_PRIVATE, "<clinit>", "()V", null, null); mv.visitCode(); for (Map.Entry<String, String> ent : funs.entrySet()) { String field = ent.getKey(); String clazz = ent.getValue(); mv.visitTypeInsn(NEW, clazz); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, clazz, "<init>", "()V"); mv.visitFieldInsn(PUTSTATIC, self_type.getInternalName(), field, "L" + funt.get(field) + ";"); } for (Map.Entry<EObject, String> ent : constants.entrySet()) { EObject term = ent.getKey(); term.emit_const(mv); mv.visitFieldInsn(Opcodes.PUTSTATIC, self_type.getInternalName(), ent.getValue(), Type.getType(term.getClass()).getDescriptor()); } cv.visitField(ACC_STATIC | ACC_PRIVATE, "attributes", ESEQ_TYPE.getDescriptor(), null, null); atts.emit_const(mv); mv.visitFieldInsn(Opcodes.PUTSTATIC, self_type.getInternalName(), "attributes", ESEQ_TYPE.getDescriptor()); if (this.module_md5 != null) { cv.visitField(ACC_STATIC, "module_md5", EBINARY_TYPE.getDescriptor(), null, null); module_md5.emit_const(mv); mv.visitFieldInsn(PUTSTATIC, self_type.getInternalName(), "module_md5", EBINARY_TYPE.getDescriptor()); } mv.visitInsn(RETURN); mv.visitMaxs(200, 10); mv.visitEnd(); // make the method module_name mv = cv.visitMethod(ACC_PROTECTED, "module_name", "()Ljava/lang/String;", null, null); mv.visitCode(); mv.visitLdcInsn(this.module_name.getName()); mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); // make the method attributes mv = cv.visitMethod(ACC_PROTECTED, "attributes", "()" + ESEQ_TYPE.getDescriptor(), null, null); mv.visitCode(); mv.visitFieldInsn(Opcodes.GETSTATIC, self_type.getInternalName(), "attributes", ESEQ_TYPE.getDescriptor()); mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); // make default constructor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, ECOMPILEDMODULE_NAME, "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); mv = cv.visitMethod(ACC_PUBLIC, "registerImportsAndExports", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, ECOMPILEDMODULE_NAME, "registerImportsAndExports", "()V"); for (Lambda l : lambdas_xx.values()) { mv.visitTypeInsn(NEW, Type.getInternalName(LocalFunID.class)); mv.visitInsn(DUP); module_name.emit_const(mv); l.fun.emit_const(mv); push_int(mv, l.arity); push_int(mv, l.old_index); push_int(mv, l.index); push_int(mv, l.old_uniq); mv.visitFieldInsn(GETSTATIC, self_type.getInternalName(), "module_md5", EBINARY_TYPE.getDescriptor()); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(LocalFunID.class), "<init>", "(" + EATOM_DESC + EATOM_DESC + "IIII" + EBINARY_TYPE.getDescriptor() + ")V"); mv.visitInsn(DUP); cv.visitField(ACC_STATIC, anon_fun_name(l), Type.getDescriptor(LocalFunID.class), null, null) .visitEnd(); mv.visitFieldInsn(PUTSTATIC, self_type.getInternalName(), anon_fun_name(l), Type.getDescriptor(LocalFunID.class)); String mname = EUtil.getJavaName(l.fun, l.arity - l.freevars); String outer_name = self_type.getInternalName(); String inner_name = "FN_" + mname; String full_inner_name = outer_name + "$" + inner_name; mv.visitLdcInsn(full_inner_name.replace('/', '.')); mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Class.class), "forName", "(Ljava/lang/String;)Ljava/lang/Class;"); mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(EModuleManager.class), "register_lambda", "(" + Type.getDescriptor(LocalFunID.class) + Type.getDescriptor(Class.class) + ")V"); } mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); }
From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java
License:Open Source License
private void fillOperationArray(MethodVisitor clinit) { // Operations array clinit.visitLdcInsn(new Integer(remotemethods.length)); clinit.visitTypeInsn(Opcodes.ANEWARRAY, typeArg(Operation.class)); clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "operations", Type.getDescriptor(Operation[].class)); for (int i = 0; i < remotemethods.length; i++) { Method m = remotemethods[i].meth; StringBuilder desc = new StringBuilder(); desc.append(getPrettyName(m.getReturnType()) + " "); desc.append(m.getName() + "("); // signature Class[] sig = m.getParameterTypes(); for (int j = 0; j < sig.length; j++) { desc.append(getPrettyName(sig[j])); if (j + 1 < sig.length) desc.append(", "); }// w ww . j av a2 s . c om // push operations array clinit.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "operations", Type.getDescriptor(Operation[].class)); // push array index clinit.visitLdcInsn(new Integer(i)); // instantiate operation and leave a copy on the stack clinit.visitTypeInsn(Opcodes.NEW, typeArg(Operation.class)); clinit.visitInsn(Opcodes.DUP); clinit.visitLdcInsn(desc.toString()); clinit.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Operation.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) })); // store in operations array clinit.visitInsn(Opcodes.AASTORE); } }
From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java
License:Open Source License
private void generateStaticMethodObjs(MethodVisitor clinit) { for (int i = 0; i < remotemethods.length; i++) { Method m = remotemethods[i].meth; /*/*from w w w. j av a2 s . co m*/ * $method_<i>m.getName()</i>_<i>i</i> = * <i>m.getDeclaringClass()</i>.class.getMethod * (m.getName(), m.getParameterType()) */ String methodVar = "$method_" + m.getName() + "_" + i; generateClassConstant(clinit, m.getDeclaringClass()); clinit.visitLdcInsn(m.getName()); generateClassArray(clinit, m.getParameterTypes()); clinit.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Class.class), "getMethod", Type.getMethodDescriptor(Type.getType(Method.class), new Type[] { Type.getType(String.class), Type.getType(Class[].class) })); clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, methodVar, Type.getDescriptor(Method.class)); } }
From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java
License:Open Source License
private void generateStub() throws IOException { stubname = fullclassname + "_Stub"; String stubclassname = classname + "_Stub"; File file = new File((destination == null ? "." : destination) + File.separator + stubname.replace('.', File.separatorChar) + ".class"); if (verbose)/*from www . j ava 2s .c om*/ System.out.println("[Generating class " + stubname + "]"); final ClassWriter stub = new ClassWriter(true); classInternalName = stubname.replace('.', '/'); final String superInternalName = Type.getType(RemoteStub.class).getInternalName(); String[] remoteInternalNames = internalNameArray((Class[]) mRemoteInterfaces.toArray(new Class[] {})); stub.visit(Opcodes.V1_2, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, classInternalName, null, superInternalName, remoteInternalNames); if (need12Stubs) { stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "serialVersionUID", Type.LONG_TYPE.getDescriptor(), null, new Long(2L)); } if (need11Stubs) { stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "interfaceHash", Type.LONG_TYPE.getDescriptor(), null, new Long(RMIHashes.getInterfaceHash(clazz))); if (need12Stubs) { stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, "useNewInvoke", Type.BOOLEAN_TYPE.getDescriptor(), null, null); } stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "operations", Type.getDescriptor(Operation[].class), null, null); } // Set of method references. if (need12Stubs) { for (int i = 0; i < remotemethods.length; i++) { Method m = remotemethods[i].meth; String slotName = "$method_" + m.getName() + "_" + i; stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, slotName, Type.getDescriptor(Method.class), null, null); } } MethodVisitor clinit = stub.visitMethod(Opcodes.ACC_STATIC, "<clinit>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null); if (need11Stubs) { fillOperationArray(clinit); if (!need12Stubs) clinit.visitInsn(Opcodes.RETURN); } if (need12Stubs) { // begin of try Label begin = new Label(); // beginning of catch Label handler = new Label(); clinit.visitLabel(begin); // Initialize the methods references. if (need11Stubs) { /* * RemoteRef.class.getMethod("invoke", new Class[] { * Remote.class, Method.class, Object[].class, long.class }) */ generateClassConstant(clinit, RemoteRef.class); clinit.visitLdcInsn("invoke"); generateClassArray(clinit, new Class[] { Remote.class, Method.class, Object[].class, long.class }); clinit.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Class.class), "getMethod", Type.getMethodDescriptor(Type.getType(Method.class), new Type[] { Type.getType(String.class), Type.getType(Class[].class) })); // useNewInvoke = true clinit.visitInsn(Opcodes.ICONST_1); clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "useNewInvoke", Type.BOOLEAN_TYPE.getDescriptor()); } generateStaticMethodObjs(clinit); // jump past handler clinit.visitInsn(Opcodes.RETURN); clinit.visitLabel(handler); if (need11Stubs) { // useNewInvoke = false clinit.visitInsn(Opcodes.ICONST_0); clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "useNewInvoke", Type.BOOLEAN_TYPE.getDescriptor()); clinit.visitInsn(Opcodes.RETURN); } else { // throw NoSuchMethodError clinit.visitTypeInsn(Opcodes.NEW, typeArg(NoSuchMethodError.class)); clinit.visitInsn(Opcodes.DUP); clinit.visitLdcInsn("stub class initialization failed"); clinit.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(NoSuchMethodError.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) })); clinit.visitInsn(Opcodes.ATHROW); } clinit.visitTryCatchBlock(begin, handler, handler, Type.getInternalName(NoSuchMethodException.class)); } clinit.visitMaxs(-1, -1); generateClassForNamer(stub); // Constructors if (need11Stubs) { // no arg public constructor MethodVisitor code = stub.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null); code.visitVarInsn(Opcodes.ALOAD, 0); code.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {})); code.visitInsn(Opcodes.RETURN); code.visitMaxs(-1, -1); } // public RemoteRef constructor MethodVisitor constructor = stub.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteRef.class) }), null, null); constructor.visitVarInsn(Opcodes.ALOAD, 0); constructor.visitVarInsn(Opcodes.ALOAD, 1); constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteRef.class) })); constructor.visitInsn(Opcodes.RETURN); constructor.visitMaxs(-1, -1); // Method implementations for (int i = 0; i < remotemethods.length; i++) { Method m = remotemethods[i].meth; Class[] sig = m.getParameterTypes(); Class returntype = m.getReturnType(); Class[] except = sortExceptions((Class[]) remotemethods[i].exceptions.toArray(new Class[0])); MethodVisitor code = stub.visitMethod(Opcodes.ACC_PUBLIC, m.getName(), Type.getMethodDescriptor(Type.getType(returntype), typeArray(sig)), null, internalNameArray(typeArray(except))); final Variables var = new Variables(); // this and parameters are the declared vars var.declare("this"); for (int j = 0; j < sig.length; j++) var.declare(param(m, j), size(sig[j])); Label methodTryBegin = new Label(); code.visitLabel(methodTryBegin); if (need12Stubs) { Label oldInvoke = new Label(); if (need11Stubs) { // if not useNewInvoke jump to old invoke code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "useNewInvoke", Type.getDescriptor(boolean.class)); code.visitJumpInsn(Opcodes.IFEQ, oldInvoke); } // this.ref code.visitVarInsn(Opcodes.ALOAD, var.get("this")); code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref", Type.getDescriptor(RemoteRef.class)); // "this" is first arg to invoke code.visitVarInsn(Opcodes.ALOAD, var.get("this")); // method object is second arg to invoke String methName = "$method_" + m.getName() + "_" + i; code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, methName, Type.getDescriptor(Method.class)); // args to remote method are third arg to invoke if (sig.length == 0) code.visitInsn(Opcodes.ACONST_NULL); else { // create arg Object[] (with boxed primitives) and push it code.visitLdcInsn(new Integer(sig.length)); code.visitTypeInsn(Opcodes.ANEWARRAY, typeArg(Object.class)); var.allocate("argArray"); code.visitVarInsn(Opcodes.ASTORE, var.get("argArray")); for (int j = 0; j < sig.length; j++) { int size = size(sig[j]); int insn = loadOpcode(sig[j]); Class box = sig[j].isPrimitive() ? box(sig[j]) : null; code.visitVarInsn(Opcodes.ALOAD, var.get("argArray")); code.visitLdcInsn(new Integer(j)); // put argument on stack if (box != null) { code.visitTypeInsn(Opcodes.NEW, typeArg(box)); code.visitInsn(Opcodes.DUP); code.visitVarInsn(insn, var.get(param(m, j))); code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(box), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(sig[j]) })); } else code.visitVarInsn(insn, var.get(param(m, j))); code.visitInsn(Opcodes.AASTORE); } code.visitVarInsn(Opcodes.ALOAD, var.deallocate("argArray")); } // push remote operation opcode code.visitLdcInsn(new Long(remotemethods[i].hash)); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "invoke", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Remote.class), Type.getType(Method.class), Type.getType(Object[].class), Type.LONG_TYPE })); if (!returntype.equals(Void.TYPE)) { int retcode = returnOpcode(returntype); Class boxCls = returntype.isPrimitive() ? box(returntype) : null; code.visitTypeInsn(Opcodes.CHECKCAST, typeArg(boxCls == null ? returntype : boxCls)); if (returntype.isPrimitive()) { // unbox code.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(boxCls).getInternalName(), unboxMethod(returntype), Type.getMethodDescriptor(Type.getType(returntype), new Type[] {})); } code.visitInsn(retcode); } else code.visitInsn(Opcodes.RETURN); if (need11Stubs) code.visitLabel(oldInvoke); } if (need11Stubs) { // this.ref.newCall(this, operations, index, interfaceHash) code.visitVarInsn(Opcodes.ALOAD, var.get("this")); code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref", Type.getDescriptor(RemoteRef.class)); // "this" is first arg to newCall code.visitVarInsn(Opcodes.ALOAD, var.get("this")); // operations is second arg to newCall code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "operations", Type.getDescriptor(Operation[].class)); // method index is third arg code.visitLdcInsn(new Integer(i)); // interface hash is fourth arg code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "interfaceHash", Type.LONG_TYPE.getDescriptor()); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "newCall", Type.getMethodDescriptor(Type.getType(RemoteCall.class), new Type[] { Type.getType(RemoteObject.class), Type.getType(Operation[].class), Type.INT_TYPE, Type.LONG_TYPE })); // store call object on stack and leave copy on stack var.allocate("call"); code.visitInsn(Opcodes.DUP); code.visitVarInsn(Opcodes.ASTORE, var.get("call")); Label beginArgumentTryBlock = new Label(); code.visitLabel(beginArgumentTryBlock); // ObjectOutput out = call.getOutputStream(); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getOutputStream", Type.getMethodDescriptor(Type.getType(ObjectOutput.class), new Type[] {})); for (int j = 0; j < sig.length; j++) { // dup the ObjectOutput code.visitInsn(Opcodes.DUP); // get j'th arg to remote method code.visitVarInsn(loadOpcode(sig[j]), var.get(param(m, j))); Class argCls = sig[j].isPrimitive() ? sig[j] : Object.class; // out.writeFoo code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), writeMethod(sig[j]), Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(argCls) })); } // pop ObjectOutput code.visitInsn(Opcodes.POP); Label iohandler = new Label(); Label endArgumentTryBlock = new Label(); code.visitJumpInsn(Opcodes.GOTO, endArgumentTryBlock); code.visitLabel(iohandler); // throw new MarshalException(msg, ioexception); code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); code.visitTypeInsn(Opcodes.NEW, typeArg(MarshalException.class)); code.visitInsn(Opcodes.DUP); code.visitLdcInsn("error marshalling arguments"); code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(MarshalException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); code.visitInsn(Opcodes.ATHROW); code.visitLabel(endArgumentTryBlock); code.visitTryCatchBlock(beginArgumentTryBlock, iohandler, iohandler, Type.getInternalName(IOException.class)); // this.ref.invoke(call) code.visitVarInsn(Opcodes.ALOAD, var.get("this")); code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref", Type.getDescriptor(RemoteRef.class)); code.visitVarInsn(Opcodes.ALOAD, var.get("call")); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "invoke", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteCall.class) })); // handle return value boolean needcastcheck = false; Label beginReturnTryCatch = new Label(); code.visitLabel(beginReturnTryCatch); int returncode = returnOpcode(returntype); if (!returntype.equals(Void.TYPE)) { // call.getInputStream() code.visitVarInsn(Opcodes.ALOAD, var.get("call")); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getInputStream", Type.getMethodDescriptor(Type.getType(ObjectInput.class), new Type[] {})); Class readCls = returntype.isPrimitive() ? returntype : Object.class; code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), readMethod(returntype), Type.getMethodDescriptor(Type.getType(readCls), new Type[] {})); boolean castresult = false; if (!returntype.isPrimitive()) { if (!returntype.equals(Object.class)) castresult = true; else needcastcheck = true; } if (castresult) code.visitTypeInsn(Opcodes.CHECKCAST, typeArg(returntype)); // leave result on stack for return } // this.ref.done(call) code.visitVarInsn(Opcodes.ALOAD, var.get("this")); code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref", Type.getDescriptor(RemoteRef.class)); code.visitVarInsn(Opcodes.ALOAD, var.deallocate("call")); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "done", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteCall.class) })); // return; or return result; code.visitInsn(returncode); // exception handler Label handler = new Label(); code.visitLabel(handler); code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); // throw new UnmarshalException(msg, e) code.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class)); code.visitInsn(Opcodes.DUP); code.visitLdcInsn("error unmarshalling return"); code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); code.visitInsn(Opcodes.ATHROW); Label endReturnTryCatch = new Label(); // catch IOException code.visitTryCatchBlock(beginReturnTryCatch, handler, handler, Type.getInternalName(IOException.class)); if (needcastcheck) { // catch ClassNotFoundException code.visitTryCatchBlock(beginReturnTryCatch, handler, handler, Type.getInternalName(ClassNotFoundException.class)); } } Label rethrowHandler = new Label(); code.visitLabel(rethrowHandler); // rethrow declared exceptions code.visitInsn(Opcodes.ATHROW); boolean needgeneral = true; for (int j = 0; j < except.length; j++) { if (except[j] == Exception.class) needgeneral = false; } for (int j = 0; j < except.length; j++) { code.visitTryCatchBlock(methodTryBegin, rethrowHandler, rethrowHandler, Type.getInternalName(except[j])); } if (needgeneral) { // rethrow unchecked exceptions code.visitTryCatchBlock(methodTryBegin, rethrowHandler, rethrowHandler, Type.getInternalName(RuntimeException.class)); Label generalHandler = new Label(); code.visitLabel(generalHandler); String msg = "undeclared checked exception"; // throw new java.rmi.UnexpectedException(msg, e) code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); code.visitTypeInsn(Opcodes.NEW, typeArg(UnexpectedException.class)); code.visitInsn(Opcodes.DUP); code.visitLdcInsn(msg); code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnexpectedException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); code.visitInsn(Opcodes.ATHROW); code.visitTryCatchBlock(methodTryBegin, rethrowHandler, generalHandler, Type.getInternalName(Exception.class)); } code.visitMaxs(-1, -1); } stub.visitEnd(); byte[] classData = stub.toByteArray(); if (!noWrite) { if (file.exists()) file.delete(); if (file.getParentFile() != null) file.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(file); fos.write(classData); fos.flush(); fos.close(); } }
From source file:io.syncframework.optimizer.OControllerStaticMethodVisitor.java
License:Apache License
public void visitInsn(int opcode) { if (opcode != Opcodes.RETURN) { mv.visitInsn(opcode);//from w ww.ja va 2 s .c o m return; } Label start = new Label(); Label l1 = new Label(); Label l2 = new Label(); Label interceptorsLabel = new Label(); mv.visitCode(); mv.visitTryCatchBlock(start, l1, l2, "java/lang/Throwable"); /* * _asActions = new HashMap<String,Boolean>(); */ { mv.visitLabel(start); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;"); } /* * _asActions.put("main", true); * _asActions.put("action1", true); */ for (String name : reflector.getActions().keySet()) { Label l = new Label(); mv.visitLabel(l); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitInsn(Opcodes.ICONST_1); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } /* * _asInterceptors = new HashMap<String,Class<?>[]>() */ { Label l = new Label(); mv.visitLabel(l); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asInterceptors", "Ljava/util/Map;"); } /* * List<Class<?>> l = new ArrayList<Class<?>>(); */ mv.visitLabel(interceptorsLabel); mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false); mv.visitVarInsn(Opcodes.ASTORE, 0); for (String name : reflector.getActions().keySet()) { Class<?> interceptors[] = reflector.getInterceptors().get(name); if (interceptors == null || interceptors.length == 0) continue; /* * l.clear(); */ Label l01 = new Label(); mv.visitLabel(l01); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "clear", "()V", true); for (Class<?> interceptor : interceptors) { /* * l.add(LoginInterceptor.class); */ Label l02 = new Label(); mv.visitLabel(l02); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitLdcInsn(Type.getType(interceptor)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true); } /* * _asInterceptors.put("upload", l.toArray(new Class[0])); */ Label l03 = new Label(); mv.visitLabel(l03); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asInterceptors", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ICONST_0); mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Class"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;", true); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Class;"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } /* * _asParameters = new HashMap<String,Class<?>>() */ { Label l = new Label(); mv.visitLabel(l); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asParameters", "Ljava/util/Map;"); } /* * _asParameters.put("name", Type.class); */ for (String name : reflector.getParameters().keySet()) { Label l = new Label(); mv.visitLabel(l); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asParameters", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitLdcInsn(Type.getType(reflector.getParameters().get(name))); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } /* * _asConverters = new HashMap<String,Class<?>>() */ { Label l = new Label(); mv.visitLabel(l); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asConverters", "Ljava/util/Map;"); } /* * _asConverters.put("name", Type.class); */ for (String name : reflector.getParameters().keySet()) { if (reflector.getConverters().get(name) != null) { Class<?> converter = reflector.getConverters().get(name); Label l = new Label(); mv.visitLabel(l); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asConverters", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitLdcInsn(Type.getType(converter)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } } /* * } * catch(Throwable t) { * throw t; * } */ Label throwableStart = new Label(); Label throwableEnd = new Label(); mv.visitLabel(l1); mv.visitJumpInsn(Opcodes.GOTO, throwableEnd); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(Opcodes.ASTORE, 0); mv.visitLabel(throwableStart); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(throwableEnd); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.RETURN); mv.visitLocalVariable("l", "Ljava/util/List;", null, interceptorsLabel, l1, 0); mv.visitLocalVariable("t", "Ljava/lang/Throwable;", null, throwableStart, throwableEnd, 0); }
From source file:io.syncframework.optimizer.OInterceptorStaticMethodVisitor.java
License:Apache License
public void visitInsn(int opcode) { if (opcode != Opcodes.RETURN) { mv.visitInsn(opcode);//from w w w . ja v a2 s. c o m return; } Label start = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitCode(); mv.visitTryCatchBlock(start, l1, l2, "java/lang/Throwable"); /* * _asParameters = new HashMap<String,Class<?>>() */ { mv.visitLabel(start); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asParameters", "Ljava/util/Map;"); } /* * _asParameters.put("name", Type.class); */ for (String name : reflector.getParameters().keySet()) { Label l = new Label(); mv.visitLabel(l); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asParameters", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitLdcInsn(Type.getType(reflector.getParameters().get(name))); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } /* * _asConverters = new HashMap<String,Class<?>>() */ { Label l = new Label(); mv.visitLabel(l); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asConverters", "Ljava/util/Map;"); } /* * _asConverters.put("name", Type.class); */ for (String name : reflector.getParameters().keySet()) { if (reflector.getConverters().get(name) != null) { Class<?> converter = reflector.getConverters().get(name); Label l = new Label(); mv.visitLabel(l); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asConverters", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitLdcInsn(Type.getType(converter)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } } /* * } * catch(Throwable t) { * throw t; * } */ Label throwableStart = new Label(); Label throwableEnd = new Label(); mv.visitLabel(l1); mv.visitJumpInsn(Opcodes.GOTO, throwableEnd); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(Opcodes.ASTORE, 0); mv.visitLabel(throwableStart); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(throwableEnd); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.RETURN); mv.visitLocalVariable("t", "Ljava/lang/Throwable;", null, throwableStart, throwableEnd, 0); }
From source file:jerl.semc.MethodEntrySEMCInitLog.java
License:Apache License
@Override public void inject(MethodVisitor mv) { mv.visitLdcInsn(Type.getType("L" + className + ";")); mv.visitMethodInsn(Opcodes.INVOKESTATIC, FieldAddSEMCLog.LOG_CLASS_NAME, "getLogger", "(Ljava/lang/Class;)L" + FieldAddSEMCLog.LOG_CLASS_NAME + ";"); mv.visitFieldInsn(Opcodes.PUTSTATIC, className, FieldAddSEMCLog.LOG_FIELD_NAME, "L" + FieldAddSEMCLog.LOG_CLASS_NAME + ";"); }
From source file:jpcsp.Allegrex.compiler.CodeBlock.java
License:Open Source License
private void addNonStaticMethods(CompilerContext context, ClassVisitor cv) { MethodVisitor mv;//from www.j a v a2 s .c o m // public int exec(int returnAddress, int alternativeReturnAddress, boolean isJump) throws Exception; mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getExecMethodName(), context.getExecMethodDesc(), null, exceptions); mv.visitCode(); mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassName(), context.getStaticExecMethodName(), context.getStaticExecMethodDesc()); mv.visitInsn(Opcodes.IRETURN); mv.visitMaxs(1, 1); mv.visitEnd(); // private static IExecutable e; FieldVisitor fv = cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, context.getReplaceFieldName(), executableDescriptor, null, null); fv.visitEnd(); // public void setExecutable(IExecutable e); mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getReplaceMethodName(), context.getReplaceMethodDesc(), null, exceptions); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassName(), context.getReplaceFieldName(), executableDescriptor); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 2); mv.visitEnd(); // public IExecutable getExecutable(); mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getGetMethodName(), context.getGetMethodDesc(), null, exceptions); mv.visitCode(); mv.visitFieldInsn(Opcodes.GETSTATIC, getClassName(), context.getReplaceFieldName(), executableDescriptor); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); }
From source file:lapin.comp.asm.ASMByteCodeGenerator.java
License:Open Source License
private void generateClassInitializer(Env env) { MethodVisitor mv = _cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); // field for SELF (static field) mv.visitCode();//from w ww .java 2s . c o m mv.visitInsn(Opcodes.ACONST_NULL); mv.visitFieldInsn(Opcodes.PUTSTATIC, toInternalName(super.classInfo.classname()), "SELF", toTypeDescriptor(super.classInfo.classname())); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:lapin.comp.asm.ASMByteCodeGenerator.java
License:Open Source License
private void generateConstructor(Env env) { /*//from w w w . j a va 2 s . c o m * local variables (reserved) * 0: this * 1: env */ MethodVisitor mv = _cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", //"(Llapin/lang/Env;)V", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_ENV }), null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitLdcInsn(super.classInfo.name().pname()); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "lapin/function/CompiledExpr", "<init>", //"(Ljava/lang/String;)V"); Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_STRING })); Iterator it; // fields for constants it = constTable.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); // key: object (const) String val = Data.string(constTable.get(key)); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitLdcInsn(Printer.prin1ToString(key, env)); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "lapin/function/CompiledExpr", "toSexp", Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_STRING, TYPE_ENV })); mv.visitFieldInsn(Opcodes.PUTFIELD, toInternalName(super.classInfo.classname()), val, TYPE_OBJECT.getDescriptor()); } // fields for vars it = varTable.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); // key: symbol (var) String val = Data.string(varTable.get(key)); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitLdcInsn(Printer.prin1ToString(key, env)); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "lapin/function/CompiledExpr", "toSexp", Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_STRING, TYPE_ENV })); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lapin/lang/Data", "symbol", Type.getMethodDescriptor(TYPE_SYMBOL, new Type[] { TYPE_OBJECT })); mv.visitFieldInsn(Opcodes.PUTFIELD, toInternalName(super.classInfo.classname()), val, TYPE_SYMBOL.getDescriptor()); } // fields for lambdaLists it = llTable.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); // key: lambdaList String val = Data.string(llTable.get(key)); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitLdcInsn(Printer.prin1ToString(Data.lambdaList(key).params(), env)); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "lapin/function/CompiledExpr", "toLambdaList", Type.getMethodDescriptor(TYPE_LAMBDA_LIST, new Type[] { TYPE_STRING, TYPE_ENV })); mv.visitFieldInsn(Opcodes.PUTFIELD, toInternalName(super.classInfo.classname()), val, TYPE_LAMBDA_LIST.getDescriptor()); } // field for SELF (static field) mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.PUTSTATIC, toInternalName(super.classInfo.classname()), "SELF", toTypeDescriptor(super.classInfo.classname())); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }