List of usage examples for org.objectweb.asm MethodVisitor visitEnd
public void visitEnd()
From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java
License:Open Source License
public static byte[] compileFunc(TokenSource<RPNToken> source, String variable) throws ParsingError { // initialize class ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/github/rgcjonas/kuemmelgtr/jit/BogusName", null, CompiledClassBase.class.getName().replace('.', '/'), new String[] { Evaluator.Function.class.getName().replace('.', '/') }); // create constructor MethodVisitor construct = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); construct.visitCode();//from www .jav a 2 s.c o m construct.visitVarInsn(Opcodes.ALOAD, 0); construct.visitMethodInsn(Opcodes.INVOKESPECIAL, CompiledClassBase.class.getName().replace('.', '/'), "<init>", "()V"); construct.visitInsn(Opcodes.RETURN); construct.visitMaxs(0, 0); construct.visitEnd(); MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "evaluate", "(D)D", null, new String[] { "com/github/rgcjonas/kuemmelgtr/core/RuntimeError" }); compileCommon(source, method, variable); writer.visitEnd(); return writer.toByteArray(); }
From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java
License:Open Source License
public static byte[] compileBasic(TokenSource<RPNToken> source) throws ParsingError { // initialize class ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/github/rgcjonas/kuemmelgtr/jit/BogusName", null, CompiledClassBase.class.getName().replace('.', '/'), new String[] { Evaluator.Basic.class.getName().replace('.', '/') }); // create constructor MethodVisitor construct = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); construct.visitCode();/*from ww w . j av a 2s . com*/ construct.visitVarInsn(Opcodes.ALOAD, 0); construct.visitMethodInsn(Opcodes.INVOKESPECIAL, CompiledClassBase.class.getName().replace('.', '/'), "<init>", "()V"); construct.visitInsn(Opcodes.RETURN); construct.visitMaxs(0, 0); construct.visitEnd(); MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "evaluate", "()D", null, new String[] { "com/github/rgcjonas/kuemmelgtr/core/ParsingError", "com/github/rgcjonas/kuemmelgtr/core/RuntimeError" }); compileCommon(source, method, null); writer.visitEnd(); return writer.toByteArray(); }
From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java
License:Open Source License
private static void compileCommon(TokenSource<RPNToken> source, MethodVisitor method, String variable) throws ParsingError { method.visitCode();//ww w. ja v a 2 s. c o m // we now can compile our token stream int currentStackSize = 0; // we keep track of the current stack size to throw errors if we encounter an invalid instruction RPNToken t; while ((t = source.nextToken()) != null) { if (t instanceof RPNToken.Operand) { // we push it onto the stack method.visitLdcInsn(((RPNToken.Operand) t).getValue()); currentStackSize++; } else if (t instanceof RPNToken.Operator) { RPNToken.Operator op = (RPNToken.Operator) t; if (currentStackSize < op.getNumOperands()) throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Missing operand(s)"); switch (op.getName()) { case "_add": method.visitInsn(Opcodes.DADD); currentStackSize--; break; case "_mult": method.visitInsn(Opcodes.DMUL); currentStackSize--; break; case "_sub": method.visitInsn(Opcodes.DSUB); currentStackSize--; break; case "_div": method.visitInsn(Opcodes.DDIV); currentStackSize--; break; case "_pow": method.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", "pow", "(DD)D"); currentStackSize--; break; default: //HACK: support every function in java/lang/Math. Will be way more performant than any lookup-and-evaluate //TODO: implement more functions inline if (currentStackSize < 1) throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Missing operand"); try { Method meth = Math.class.getDeclaredMethod(op.getName(), double.class); // just check if it's available and hope it returns a double if (meth.getReturnType() != double.class) throw new NoSuchMethodException(); // we don't want to blow up at runtime, do we? method.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", op.getName(), "(D)D"); } catch (NoSuchMethodException e) { // we do not give up. The method may be available at runtime. method.visitVarInsn(Opcodes.ALOAD, 0); method.visitInsn(Opcodes.DUP_X2); // swap the this pointer and double method.visitInsn(Opcodes.POP); method.visitLdcInsn(op.getName()); method.visitLdcInsn((int) op.getSrcLine()); method.visitLdcInsn((int) op.getSrcColumn()); method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CompiledClassBase.class.getName().replace('.', '/'), "resolveAndEvaluateFunction", "(DLjava/lang/String;II)D"); } } } else if (t instanceof RPNToken.VariableRecall) { // maybe, maybe the variable is an argument we can load if (variable != null && ((RPNToken.VariableRecall) t).getName().equalsIgnoreCase(variable)) { method.visitVarInsn(Opcodes.DLOAD, 1); } else { // we let our parent class do the hard work method.visitVarInsn(Opcodes.ALOAD, 0); method.visitLdcInsn(((RPNToken.VariableRecall) t).getName()); method.visitLdcInsn(t.getSrcLine()); method.visitLdcInsn(t.getSrcColumn()); method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CompiledClassBase.class.getName().replace('.', '/'), "getVariable", "(Ljava/lang/String;II)D"); } currentStackSize++; } else if (t instanceof RPNToken.VariableAssignment) { // also defer to our parent class // we do not give up. The method may be available at runtime. method.visitVarInsn(Opcodes.ALOAD, 0); method.visitInsn(Opcodes.DUP_X2); // swap the this pointer and double method.visitInsn(Opcodes.POP); method.visitLdcInsn(((RPNToken.VariableAssignment) t).getVariableName()); method.visitLdcInsn(t.getSrcLine()); method.visitLdcInsn(t.getSrcColumn()); method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CompiledClassBase.class.getName().replace('.', '/'), "setVariable", "(DLjava/lang/String;II)D"); } else { throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Unknown instruction: " + t); } } if (currentStackSize != 1) throw new ParsingError(0, 0, "Expected stack to be one value, found " + currentStackSize); method.visitInsn(Opcodes.DRETURN); method.visitMaxs(0, 0); method.visitEnd(); }
From source file:com.github.stokito.gag.agent.AnswerToLifeGenerator.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) { MethodVisitor mv = writer().visitMethod(access, name, desc, sig, exceptions); mv.visitCode();//ww w . j a v a 2 s . c o m MethodInfo method = classInfo().getMethod(name, desc); for (LocalVarInfo param : method.getLocalVars()) { AnnoInfo anno = param.getAnnoFor(ANSWER_TYPE); if (anno == null) { continue; } Type paramType = param.getType(); switch (paramType.getSort()) { case Type.INT: case Type.BYTE: case Type.CHAR: case Type.SHORT: mv.visitLdcInsn(FORTY_TWO); mv.visitVarInsn(ISTORE, param.getIndex()); break; case Type.LONG: mv.visitLdcInsn((long) FORTY_TWO); mv.visitVarInsn(LSTORE, param.getIndex()); break; case Type.DOUBLE: mv.visitLdcInsn((double) FORTY_TWO); mv.visitVarInsn(DSTORE, param.getIndex()); break; case Type.FLOAT: mv.visitLdcInsn((float) FORTY_TWO); mv.visitVarInsn(FSTORE, param.getIndex()); break; case Type.OBJECT: visitObject(mv, param); break; default: throwUnsupportedException(param); } setInstrumented(true); } mv.visitEnd(); return mv; }
From source file:com.github.stokito.gag.agent.NoopGenerator.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) { boolean isNoopForAll = classInfo().getAnnoFor(NOOP_TYPE) != null; MethodVisitor mv = writer().visitMethod(access, name, desc, sig, exceptions); mv.visitCode();//from w w w . java 2 s .com MethodInfo method = classInfo().getMethod(name, desc); boolean isConstructor = "<init>".equals(method.getName()); boolean isNoopForMethod = method.getAnnoFor(NOOP_TYPE) != null; if (!isConstructor && (isNoopForAll || isNoopForMethod)) { Type returnType = Type.getReturnType(desc); switch (returnType.getSort()) { case Type.VOID: mv.visitInsn(RETURN); break; case Type.OBJECT: case Type.ARRAY: mv.visitInsn(ACONST_NULL); mv.visitInsn(ARETURN); break; case Type.DOUBLE: mv.visitInsn(DCONST_0); mv.visitInsn(DRETURN); break; case Type.FLOAT: mv.visitInsn(FCONST_0); mv.visitInsn(FRETURN); break; case Type.LONG: mv.visitInsn(LCONST_0); mv.visitInsn(LRETURN); break; case Type.INT: case Type.SHORT: case Type.CHAR: case Type.BYTE: case Type.BOOLEAN: mv.visitInsn(ICONST_0); mv.visitInsn(IRETURN); break; } setInstrumented(true); } mv.visitEnd(); return mv; }
From source file:com.github.stokito.gag.agent.RouletteGenerator.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) { MethodVisitor mv = writer().visitMethod(access, name, desc, sig, exceptions); mv.visitCode();// w w w . j a va2s .c o m MethodInfo method = classInfo().getMethod(name, desc); AnnoInfo anno = method.getAnnoFor(ROULETTE_TYPE); if (anno != null) { Double probability = (Double) anno.getValue("probability"); if (probability < 0.0 || probability > 1.0) { throw new AnnotationStateError("Probability (" + probability + ") needs to be between 0 and 1"); } Type exception = (Type) anno.getValue("exception"); // TODO: Figure out how to get the default value from the annotation itself. if (exception == null) { exception = Type.getType(RuntimeException.class); } String message = (String) anno.getValue("message"); Label okay = new Label(); mv.visitTypeInsn(NEW, "java/util/Random"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/Random", "<init>", "()V"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Random", "nextDouble", "()D"); mv.visitLdcInsn(probability); mv.visitInsn(DCMPG); mv.visitJumpInsn(IFGT, okay); mv.visitTypeInsn(NEW, exception.getInternalName()); mv.visitInsn(DUP); mv.visitLdcInsn(message); mv.visitMethodInsn(INVOKESPECIAL, exception.getInternalName(), "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); mv.visitLabel(okay); setInstrumented(true); } mv.visitEnd(); return mv; }
From source file:com.github.stokito.gag.agent.ThisHadBetterGenerator.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) { MethodVisitor mv = writer().visitMethod(access, name, desc, sig, exceptions); mv.visitCode();/*www. j a v a2s . c om*/ MethodInfo method = classInfo().getMethod(name, desc); for (LocalVarInfo param : method.getLocalVars()) { AnnoInfo anno = param.getAnnoFor(BETTER_BE_TYPE); if (anno != null) { visitThisHadBetter(mv, param, anno, true); setInstrumented(true); } anno = param.getAnnoFor(BETTER_NOT_BE_TYPE); if (anno != null) { visitThisHadBetter(mv, param, anno, false); setInstrumented(true); } } mv.visitEnd(); return mv; }
From source file:com.github.wolf480pl.mias4j.MakeTestBuryUninitialized.java
License:Open Source License
public static byte[] dump() throws Exception { ClassWriter cw = new ClassWriter(0); FieldVisitor fv;//from w w w. j a va2 s. com MethodVisitor mv; AnnotationVisitor av0; cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "Test3", null, "java/lang/Object", null); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(I)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn("hey"); mv.visitVarInsn(ILOAD, 1); mv.visitInsn(ICONST_1); mv.visitInsn(IADD); mv.visitMethodInsn(INVOKESTATIC, "Test3", "say", "(Ljava/lang/Object;I)I", false); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false); mv.visitTypeInsn(NEW, "java/util/LinkedList"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/util/LinkedList", "<init>", "()V", false); mv.visitMethodInsn(INVOKESPECIAL, "Test3", "<init>", "(Ljava/lang/Integer;Ljava/util/List;)V", false); mv.visitLdcInsn("hey1"); mv.visitInsn(ICONST_1); mv.visitTypeInsn(NEW, "java/util/Date"); mv.visitInsn(DUP_X2); mv.visitMethodInsn(INVOKESPECIAL, "java/util/Date", "<init>", "()V", false); mv.visitInsn(POP2); mv.visitInsn(ICONST_0); mv.visitMethodInsn(INVOKESTATIC, "Test3", "say", "(Ljava/lang/Object;I)I", false); mv.visitInsn(POP); mv.visitInsn(RETURN); mv.visitMaxs(4, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/Integer;Ljava/util/List;)V", "(Ljava/lang/Integer;Ljava/util/List<Ljava/lang/Integer;>;)V", null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true); mv.visitInsn(POP); mv.visitInsn(RETURN); mv.visitMaxs(2, 3); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, "Test3"); mv.visitIntInsn(BIPUSH, 10); mv.visitMethodInsn(INVOKESPECIAL, "Test3", "<init>", "(I)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("byebye"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); mv.visitInsn(RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "say", "(Ljava/lang/Object;I)I", null, null); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V", false); mv.visitVarInsn(ILOAD, 1); mv.visitInsn(IRETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:com.github.wolf480pl.mias4j.MakeTestDynamic.java
License:Open Source License
public static byte[] dump() throws Exception { ClassWriter cw = new ClassWriter(0); FieldVisitor fv;//from w ww . j a va 2s.co m MethodVisitor mv; AnnotationVisitor av0; cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "TestDynamic", null, "java/lang/Object", null); cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", ACC_PUBLIC + ACC_FINAL + ACC_STATIC); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "bootstrap", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;", null, new String[] { "java/lang/NoSuchMethodException", "java/lang/IllegalAccessException" }); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(Type.getType("LTestDynamic;")); mv.visitLdcInsn("sayHi"); mv.visitVarInsn(ALOAD, 3); mv.visitLdcInsn(Type.getType("LTestDynamic;")); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findSpecial", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/Class;)Ljava/lang/invoke/MethodHandle;", false); mv.visitVarInsn(ASTORE, 4); mv.visitTypeInsn(NEW, "java/lang/invoke/ConstantCallSite"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/invoke/ConstantCallSite", "<init>", "(Ljava/lang/invoke/MethodHandle;)V", false); mv.visitInsn(ARETURN); mv.visitMaxs(5, 5); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, "TestDynamic"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "TestDynamic", "<init>", "()V", false); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); // mv.visitMethodInsn(INVOKESPECIAL, "TestDynamic", "sayHi", "()V", false); mv.visitInvokeDynamicInsn("sayHi", "(LTestDynamic;)V", new Handle(H_INVOKESTATIC, "TestDynamic", "bootstrap", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"), Type.getMethodType("()V")); mv.visitInsn(RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PRIVATE, "sayHi", "()V", null, null); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("hi"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); mv.visitInsn(RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:com.github.wolf480pl.mias4j.MakeTestMH.java
License:Open Source License
public static byte[] dump() throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); MethodVisitor mv; cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "TestMH", null, "java/lang/Object", null); cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", ACC_PUBLIC + ACC_FINAL + ACC_STATIC); {//from ww w . j a va2s. com mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable"); mv.visitLabel(l0); /*mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodHandles", "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", false); mv.visitLdcInsn(Type.getType("Ljava/io/PrintStream;")); mv.visitLdcInsn("println"); mv.visitFieldInsn(GETSTATIC, "java/lang/Void", "TYPE", "Ljava/lang/Class;"); mv.visitLdcInsn(Type.getType("Ljava/lang/String;")); mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "methodType", "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/invoke/MethodType;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findVirtual", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);*/ mv.visitLdcInsn(new Handle(H_INVOKEVIRTUAL, "java/io/PrintStream", "println", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)))); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("hey"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke", "(Ljava/io/PrintStream;Ljava/lang/String;)V", false); mv.visitLabel(l1); Label l3 = new Label(); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V", false); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(5, 2); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }