List of usage examples for org.objectweb.asm MethodVisitor visitInsn
public void visitInsn(final int opcode)
From source file:com.github.fge.grappa.transform.process.GroupClassGenerator.java
License:Apache License
private void generateConstructor(ClassWriter cw) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", CodegenUtils.sig(void.class, String.class), null, null);/*from w w w .j a va2 s .c om*/ mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, getBaseType().getInternalName(), "<init>", CodegenUtils.sig(void.class, String.class), false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); // trigger automatic computing }
From source file:com.github.jasmo.obfuscate.ScrambleStrings.java
License:Open Source License
private void createUnscramble() { MethodVisitor mv = unscrambleClass.visitMethod(ACC_PUBLIC | ACC_STATIC, CALL_NAME, CALL_DESC, null, null); mv.visitCode();//from w w w. j ava2 s . c om mv.visitTypeInsn(NEW, "java/lang/String"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESTATIC, "java/util/Base64", "getDecoder", "()Ljava/util/Base64$Decoder;", false); mv.visitFieldInsn(GETSTATIC, unscrambleClass.name, FIELD_NAME, "[Ljava/lang/String;"); mv.visitVarInsn(ILOAD, 0); mv.visitInsn(AALOAD); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Base64$Decoder", "decode", "(Ljava/lang/String;)[B", false); mv.visitLdcInsn("UTF-8"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/String", "<init>", "([BLjava/lang/String;)V", false); mv.visitInsn(ARETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:com.github.javalbert.bytecode.utils.AsmUtils.java
License:Apache License
public static void visitDefaultConstructor(ClassWriter cw, String classTypeDescriptor) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode();// w w w . ja va 2s . co m Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", classTypeDescriptor, null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); }
From source file:com.github.javalbert.bytecode.utils.AsmUtils.java
License:Apache License
/** * //from w w w .j a v a 2 s . c om * @param mv * @param i the int value */ public static void visitZeroOperandInt(MethodVisitor mv, int i) { if (i > 5) { mv.visitIntInsn(BIPUSH, i); } else { int opcode = NOP; switch (i) { case 0: opcode = ICONST_0; break; case 1: opcode = ICONST_1; break; case 2: opcode = ICONST_2; break; case 3: opcode = ICONST_3; break; case 4: opcode = ICONST_4; break; case 5: opcode = ICONST_5; break; } mv.visitInsn(opcode); } }
From source file:com.github.megatronking.stringfog.plugin.StringFogClassVisitor.java
License:Apache License
@Override public void visitEnd() { if (!mIgnoreClass && !isClInitExists && !mStaticFinalFields.isEmpty()) { MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode();//w ww . ja v a 2 s .c o m // Here init static final fields. for (ClassStringField field : mStaticFinalFields) { if (!canEncrypted(field.value)) { continue; } String originValue = field.value; String encryptValue = mStringFogImpl.encrypt(originValue, mKey); mMappingPrinter.output(getJavaClassName(), originValue, encryptValue); mv.visitLdcInsn(encryptValue); mv.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt", "(Ljava/lang/String;)Ljava/lang/String;", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, mClassName, field.name, ClassStringField.STRING_DESC); } mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 0); mv.visitEnd(); } super.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 w w w . ja va 2 s . 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)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();/* w w w.j ava 2 s. co 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", 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();//from w w w. j a 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.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();/* w ww. java 2s .c o m*/ 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 ww . j a v a 2 s. c om*/ 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; }