List of usage examples for org.objectweb.asm MethodVisitor visitLocalVariable
public void visitLocalVariable(final String name, final String descriptor, final String signature, final Label start, final Label end, final int index)
From source file:com.khubla.jvmbasic.jvmbasicc.JVMBasicCompiler.java
License:Open Source License
/** * Generate program/*from 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();/*from w w w . j a v a2 s. c o 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.mto.asm.helloworld.ASMCodeGenerator.java
License:Open Source License
/** * Generate the byte code of a simple HelloWorld program * * public class HelloWorld//from w w w . java 2 s . c om * { * public void sayHello() * { * System.out.println("Hello World"); * } * } * @return */ public byte[] generateHelloWorld() { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "com/mto/asm/helloworld/HelloWorld", null, "java/lang/Object", null); cw.visitSource("HelloWorld.java", null); mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(25, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "Lcom/mto/asm/helloworld/HelloWorld;", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); mv = cw.visitMethod(ACC_PUBLIC, "sayHello", "()V", null, null); mv.visitCode(); l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(29, l0); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Hello World"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(30, l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "Lcom/mto/asm/helloworld/HelloWorld;", null, l0, l2, 0); mv.visitMaxs(2, 1); mv.visitEnd(); cw.visitEnd(); return cw.toByteArray(); }
From source file:com.mulberry.athena.asm.ASMTest.java
License:Open Source License
@Test public void generateClass() throws Exception { ClassWriter classWriter = new ClassWriter(0); ClassVisitor cv = new TraceClassVisitor(classWriter, new PrintWriter(System.out)); //FieldVisitor fv; MethodVisitor mv; //AnnotationVisitor av0; cv.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Testing", null, "java/lang/Object", null); cv.visitSource("Testing.java", null); {/*from w ww . j a v a 2 s . c o m*/ mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(1, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "LTesting;", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cv.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(3, l0); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Works!"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(4, l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l2, 0); mv.visitMaxs(2, 1); mv.visitEnd(); } cv.visitEnd(); Class<?> clazz = new DynamicClassLoader().defineClass("Testing", classWriter.toByteArray()); java.lang.reflect.Method method = clazz.getMethod("main", String[].class); final String[] arguments = new String[] { "hello", "world" }; method.invoke(clazz, (Object) arguments); }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * //from ww w .j a v a 2s . com * * @param cw * @param mv * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl * @param beanName com/nway/commons/dbutils/test/User * @return [0]:bean[1]createBean */ private Object[] prepScript(ClassWriter cw, MethodVisitor mv, String processorName, String beanName) { Object[] lab = new Object[2]; cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, processorName, null, "com/nway/spring/jdbc/bean/DbBeanFactory", null); cw.visitSource(processorName.substring(processorName.lastIndexOf('/') + 1) + ".java", null); { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(6, l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nway/spring/jdbc/bean/DbBeanFactory", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + processorName + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createBean", "(Ljava/sql/ResultSet;Ljava/lang/Class;)Ljava/lang/Object;", "<T:Ljava/lang/Object;>(Ljava/sql/ResultSet;Ljava/lang/Class<TT;>;)TT;", new String[] { "java/sql/SQLException" }); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(10, l0); mv.visitTypeInsn(Opcodes.NEW, beanName); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, beanName, "<init>", "()V", false); mv.visitVarInsn(Opcodes.ASTORE, 3); lab[0] = l0; lab[1] = mv; } return lab; }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * /*from ww w . java2s . c o m*/ * * @param mv MethodVisitor * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl * @param beanName com/nway/commons/dbutils/test/User */ private void endScript(MethodVisitor mv, Label processorLabel, Label beanStart, int lineNumber, String processorName, String beanName) { Label l10 = new Label(); mv.visitLabel(l10); mv.visitLineNumber(lineNumber, l10); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitInsn(Opcodes.ARETURN); Label l11 = new Label(); mv.visitLabel(l11); mv.visitLocalVariable("this", "L" + processorName + ";", null, processorLabel, l11, 0); mv.visitLocalVariable("rs", "Ljava/sql/ResultSet;", null, processorLabel, l11, 1); mv.visitLocalVariable("type", "Ljava/lang/Class;", "Ljava/lang/Class<TT;>;", processorLabel, l11, 2); mv.visitLocalVariable("bean", "L" + beanName + ";", null, beanStart, l11, 3); mv.visitMaxs(5, 4); mv.visitEnd(); }
From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.LocalVariable.java
License:Open Source License
public void toAsm(MethodVisitor mv) { mv.visitLocalVariable(_name, desc, sig, start, end, _index); }
From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java
License:Open Source License
/** * Implementing "static reflection" for the method getFooRaw so the * interpreter uses a switch instruction for ***GetRaw * based on the hash values of String names in this namespace. *//*w w w . j a v a 2 s . co m*/ private static void writeMethodGetRaw(ClassWriter cw, String className, String methodName, EnvironmentClass environmentClass, EnvSymbolNames symbolNames) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(Ljava/lang/String;)" + environmentClass.descriptor(), null, null); mv.visitCode(); Label beginFunction = new Label(); mv.visitLabel(beginFunction); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I"); mv.visitVarInsn(Opcodes.ISTORE, 2); Label beginLoop = new Label(); mv.visitLabel(beginLoop); Relation<String, Integer> hashCodeRelation = symbolNames.makeHashCodeRelation(environmentClass); ArrayList<Integer> sortedCodes = new ArrayList<Integer>(hashCodeRelation.secondSet()); Collections.sort(sortedCodes); Label returnNull = new Label(); getRawHelper(mv, className, hashCodeRelation, environmentClass, sortedCodes, returnNull); mv.visitLabel(returnNull); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitInsn(Opcodes.ARETURN); Label endFunction = new Label(); mv.visitLabel(endFunction); mv.visitLocalVariable("this", Naming.internalToDesc(className), null, beginFunction, endFunction, 0); mv.visitLocalVariable("queryString", "Ljava/lang/String;", null, beginFunction, endFunction, 1); mv.visitLocalVariable("queryHashCode", "I", null, beginLoop, endFunction, 2); // See comment above on ClassWriter.COMPUTE_FRAMES mv.visitMaxs(2, 3); mv.visitEnd(); }
From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java
License:Open Source License
/** * Implementing "static reflection" for the method putRaw so the * interpreter uses a switch instruction for ***PutRaw * based on the hash values of String names in this namespace. */// ww w . jav a2s . c o m private static void writeMethodPutRaw(ClassWriter cw, String className, String methodName, EnvironmentClass environmentClass, EnvSymbolNames symbolNames) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(" + STRING_DESCRIPTOR + environmentClass.descriptor() + ")V", null, null); mv.visitCode(); Label beginFunction = new Label(); mv.visitLabel(beginFunction); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_INTERNALNAME, "hashCode", "()I"); mv.visitVarInsn(Opcodes.ISTORE, 3); Label beginLoop = new Label(); mv.visitLabel(beginLoop); Relation<String, Integer> hashCodeRelation = symbolNames.makeHashCodeRelation(environmentClass); ArrayList<Integer> sortedCodes = new ArrayList<Integer>(hashCodeRelation.secondSet()); Collections.sort(sortedCodes); Label notFound = new Label(); putRawHelper(mv, className, environmentClass, hashCodeRelation, sortedCodes, notFound); mv.visitLabel(notFound); mv.visitInsn(Opcodes.RETURN); Label endFunction = new Label(); mv.visitLabel(endFunction); mv.visitLocalVariable("this", Naming.internalToDesc(className), null, beginFunction, endFunction, 0); mv.visitLocalVariable("queryString", STRING_DESCRIPTOR, null, beginFunction, endFunction, 1); mv.visitLocalVariable("value", environmentClass.descriptor(), null, beginFunction, endFunction, 2); mv.visitLocalVariable("queryHashCode", "I", null, beginLoop, endFunction, 3); // See comment above on ClassWriter.COMPUTE_FRAMES mv.visitMaxs(2, 4); mv.visitEnd(); }
From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java
License:Open Source License
private static void writeNullGetter(ClassWriter cw, String className, String methodName, String signature) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, signature, null, null); mv.visitCode();// w w w . j a v a 2s. co m Label l0 = new Label(); mv.visitLabel(l0); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", Naming.internalToDesc(className), null, l0, l1, 0); mv.visitLocalVariable("str", "Ljava/lang/String;", null, l0, l1, 1); // See comment above on ClassWriter.COMPUTE_FRAMES mv.visitMaxs(1, 2); mv.visitEnd(); }