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:org.spongepowered.mod.asm.transformers.MixinTransformer.java
License:MIT License
/** * Handles appending instructions from the source method to the target method * // w ww .j a v a 2 s . c om * @param targetClass * @param targetMethodName * @param sourceMethod */ private void appendInsns(ClassNode targetClass, String targetMethodName, MethodNode sourceMethod) { if (Type.getReturnType(sourceMethod.desc) != Type.VOID_TYPE) { throw new IllegalArgumentException("Attempted to merge insns into a method which does not return void"); } if (targetMethodName == null || targetMethodName.length() == 0) { targetMethodName = sourceMethod.name; } for (MethodNode method : targetClass.methods) { if ((targetMethodName.equals(method.name)) && sourceMethod.desc.equals(method.desc)) { AbstractInsnNode returnNode = null; Iterator<AbstractInsnNode> findReturnIter = method.instructions.iterator(); while (findReturnIter.hasNext()) { AbstractInsnNode insn = findReturnIter.next(); if (insn.getOpcode() == Opcodes.RETURN) { returnNode = insn; break; } } Iterator<AbstractInsnNode> injectIter = sourceMethod.instructions.iterator(); while (injectIter.hasNext()) { AbstractInsnNode insn = injectIter.next(); if (!(insn instanceof LineNumberNode) && insn.getOpcode() != Opcodes.RETURN) { method.instructions.insertBefore(returnNode, insn); } } } } }
From source file:org.spongepowered.mod.asm.util.ASMEventListenerFactory.java
License:MIT License
@SuppressWarnings("unchecked") private static <T> Class<T> createClass(Class<T> interf, Method input, Method output) { String className = getClassName(interf, input, output); ClassWriter cwBase = new ClassWriter(0); CheckClassAdapter cw = new CheckClassAdapter(cwBase); MethodVisitor mv;/*from w ww . j a v a 2 s .co m*/ String classNameDesc = className.replace('.', '/'); String interfaceInternalName = Type.getInternalName(interf); String inputName = input.getName(); String inputMethodDescriptor = Type.getMethodDescriptor(input); String outputParameterTypeIntName = Type.getInternalName(output.getParameterTypes()[0]); String outputTargetTypeIntName = Type.getInternalName(output.getDeclaringClass()); String outputMethodDescriptor = Type.getMethodDescriptor(output); String outputName = output.getName(); boolean isOutputInterface = output.getDeclaringClass().isInterface(); // A new class of the following form is created, with a unique name // // package org.spongepowered.mod.asm; // public class <className> extends java.lang.Object implements <interf> // // private final Object target // // public <className> (java.lang.Object target) { // super(); // this.target = target; // return; // } // // public void <inputMethod> (<inputMethodType event) { // ((outputTargetType) this.target).outputMethod((outputParameteType) event); // return // } // } // package org.spongepowered.mod.asm; // public class <className> extends java.lang.Object implements <interf> cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, classNameDesc, null, "java/lang/Object", new String[] { interfaceInternalName }); // private final Object target cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "target", "Ljava/lang/Object;", null, null); // Constructor // public UniqueClass (java.lang.Object target) { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null); mv.visitCode(); // super(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); // this.target = target; mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads target (from input) mv.visitFieldInsn(Opcodes.PUTFIELD, classNameDesc, "target", "Ljava/lang/Object;"); // return; mv.visitInsn(Opcodes.RETURN); // } // 2 localvars due to inputs: this, target // 2 items on stack after double ALOAD mv.visitMaxs(2, 2); mv.visitEnd(); // Callback method // public void <inputMethod> (<inputMethodType event) { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, inputName, inputMethodDescriptor, null, null); mv.visitCode(); // push((casted) this.target) mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this mv.visitFieldInsn(Opcodes.GETFIELD, classNameDesc, "target", "Ljava/lang/Object;"); mv.visitTypeInsn(Opcodes.CHECKCAST, outputTargetTypeIntName); // push((casted) event) mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads method parameter 0 mv.visitTypeInsn(Opcodes.CHECKCAST, outputParameterTypeIntName); // ((outputTargetType) this.target).outputMethod((outputParameteType) event); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, outputTargetTypeIntName, outputName, outputMethodDescriptor, isOutputInterface); // return mv.visitInsn(Opcodes.RETURN); // } mv.visitMaxs(2, 2); mv.visitEnd(); cw.visitEnd(); byte[] bytes = cwBase.toByteArray(); return (Class<T>) loader.defineClass(className, bytes); }
From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java
License:Apache License
private void buildConstructor(ClassVisitor cv, String className) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode();/* www. ja v a 2 s .com*/ mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ICONST_0); mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ICONST_M1); mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ICONST_0); mv.visitFieldInsn(Opcodes.PUTFIELD, className, "index", "I"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); }
From source file:org.testeoa.estatica.AdapterDUG.java
License:Open Source License
@Override public void visitInsn(int opcode) { addInstrucao(getInstrucao(opcode));//from ww w.ja v a 2s.co m if (opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) { adjacente = false; saida = true; inserirVerticeAtual(); } else if (opcode == Opcodes.ATHROW) { adjacente = false; saida = true; inserirVerticeAtual(); } super.visitInsn(opcode); }
From source file:org.testeoa.estatica.AdapterInstrum.java
License:Open Source License
@Override public void visitInsn(int opcode) { if (opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) { inserirRetorno();//from w ww . j a v a 2 s. c om } if (opcode == Opcodes.ATHROW) { //TODO O ATHROW S PODE RETORNAR SE ESTIVER FORA DO TRY-CATCH //inserirRetorno(); } super.visitInsn(opcode); }
From source file:org.zoeey.ztpl.compiler.ByteCodeHelper.java
License:LGPL
/** * template //from w w w . ja v a 2 s . c o m */ public void newClass(String className) { cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); /** * */ cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, // ZtplConstant.CLASS_URI + className, null, "java/lang/Object", // new String[] { ZtplConstant.TEMPLATE_INTERFACE }); /** * construct */ { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); // mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } /** * TemplateAble#publish */ { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "publish", // "(Ljava/io/Writer;Ljava/util/Map;Lorg/zoeey/ztpl/Ztpl;)V", // null, new String[] { "java/io/IOException" }); mv.visitCode(); tracker = new CompileTracker(); } }
From source file:org.zoeey.ztpl.compiler.ByteCodeHelper.java
License:LGPL
/** * template */ public void endClass() { mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(tracker.next(), tracker.next()); mv.visitEnd(); cw.visitEnd(); }
From source file:pl.asie.foamfix.coremod.patches.ReturnIfBooleanTruePatch.java
License:Open Source License
public ReturnIfBooleanTruePatch(String optionName, String... methods) { this.optionName = optionName; this.methods = ImmutableSet.copyOf(methods); list = new InsnList(); Label l = new Label(); LabelNode ln = new LabelNode(l); list.add(new FieldInsnNode(Opcodes.GETSTATIC, "pl/asie/foamfix/shared/FoamFixShared", "config", "Lpl/asie/foamfix/shared/FoamFixConfig;")); list.add(new FieldInsnNode(Opcodes.GETFIELD, "pl/asie/foamfix/shared/FoamFixConfig", optionName, "Z")); list.add(new JumpInsnNode(Opcodes.IFEQ, ln)); list.add(new InsnNode(Opcodes.RETURN)); list.add(ln);/*from ww w .j a v a2 s . co m*/ list.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null)); }
From source file:pl.clareo.coroutines.core.ClassTransformer.java
License:Apache License
@SuppressWarnings("unchecked") void transform() { for (MethodNode coroutine : coroutines) { if (log.isLoggable(Level.FINEST)) { log.finest("Generating method for coroutine " + coroutine.name + coroutine.desc); }/*from w w w . ja v a2 s . c om*/ String coroutineName = getCoroutineName(coroutine); MethodTransformer methodTransformer = new MethodTransformer(coroutine, thisType); MethodNode coroutineImpl = methodTransformer.transform(coroutineName, generateDebugCode); thisNode.methods.add(coroutineImpl); /* * generate co iterators and method stubs */ log.finest("Generating CoIterator implementation and method stubs"); String baseCoIteratorName; Map<String, Object> annotation = getCoroutineAnnotationValues(coroutine); if (getBoolean(annotation, "threadLocal")) { baseCoIteratorName = Type.getInternalName(ThreadLocalCoIterator.class); } else { baseCoIteratorName = Type.getInternalName(SingleThreadedCoIterator.class); } String coIteratorClassName = "pl/clareo/coroutines/core/CoIterator" + num; ClassNode coIteratorClass = new ClassNode(); coIteratorClass.version = Opcodes.V1_6; coIteratorClass.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER; coIteratorClass.name = coIteratorClassName; coIteratorClass.superName = baseCoIteratorName; if (generateDebugCode) { /* * If debugging code is emitted create field keeping JDK logger */ FieldNode loggerField = new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "logger", "Ljava/util/logging/Logger;", null, null); coIteratorClass.fields.add(loggerField); MethodNode clinit = new MethodNode(); clinit.access = Opcodes.ACC_STATIC; clinit.name = "<clinit>"; clinit.desc = "()V"; clinit.exceptions = Collections.EMPTY_LIST; String loggerName = thisType.getClassName(); InsnList clinitCode = clinit.instructions; clinitCode.add(new LdcInsnNode(loggerName)); clinitCode.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/logging/Logger", "getLogger", "(Ljava/lang/String;)Ljava/util/logging/Logger;")); clinitCode.add(new FieldInsnNode(Opcodes.PUTSTATIC, coIteratorClassName, "logger", "Ljava/util/logging/Logger;")); clinitCode.add(new InsnNode(Opcodes.RETURN)); clinit.maxStack = 1; clinit.maxLocals = 0; coIteratorClass.methods.add(clinit); } /* * Generate constructor */ MethodNode init = new MethodNode(); init.access = Opcodes.ACC_PUBLIC; init.name = "<init>"; init.desc = CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR; init.exceptions = Collections.EMPTY_LIST; InsnList initCode = init.instructions; initCode.add(new VarInsnNode(Opcodes.ALOAD, 0)); initCode.add(new VarInsnNode(Opcodes.ALOAD, 1)); initCode.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, baseCoIteratorName, "<init>", CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR)); initCode.add(new InsnNode(Opcodes.RETURN)); init.maxStack = 2; init.maxLocals = 2; coIteratorClass.methods.add(init); /* * Generate overriden call to coroutine */ MethodNode call = new MethodNode(); call.access = Opcodes.ACC_PROTECTED; call.name = "call"; call.desc = CALL_METHOD_DESCRIPTOR; call.exceptions = Collections.EMPTY_LIST; InsnList callCode = call.instructions; /* * if debug needed generate call details */ if (generateDebugCode) { String coroutineId = "Coroutine " + coroutine.name; callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER, coroutineId + " call. Caller sent: ", 2)); callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK)); callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINEST, coroutineId + " state ", 1)); callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK)); } /* * push call arguments: this (if not static), frame, input, output */ boolean isStatic = (coroutine.access & Opcodes.ACC_STATIC) != 0; if (!isStatic) { callCode.add(new VarInsnNode(Opcodes.ALOAD, 1)); callCode.add( new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getThis", "()Ljava/lang/Object;")); callCode.add(new TypeInsnNode(Opcodes.CHECKCAST, thisType.getInternalName())); } callCode.add(new VarInsnNode(Opcodes.ALOAD, 1)); callCode.add(new InsnNode(Opcodes.ACONST_NULL)); callCode.add(new VarInsnNode(Opcodes.ALOAD, 2)); callCode.add(new MethodInsnNode(isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, thisType.getInternalName(), coroutineName, COROUTINE_METHOD_DESCRIPTOR)); // stack: * if (!generateDebugCode) { callCode.add(new InsnNode(Opcodes.ARETURN)); } else { // save result display suspension point (two more locals // needed) callCode.add(new VarInsnNode(Opcodes.ASTORE, 3)); callCode.add(new VarInsnNode(Opcodes.ALOAD, 1)); callCode.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLineOfCode", "()I")); callCode.add(box_int(Type.INT)); callCode.add(new VarInsnNode(Opcodes.ASTORE, 4)); callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER, "Coroutine suspended at line ", 4, ". Yielded:", 3)); callCode.add(new FrameNode(Opcodes.F_APPEND, 2, new Object[] { "java/lang/Object", "java/lang/Integer" }, 0, EMPTY_STACK)); callCode.add(new VarInsnNode(Opcodes.ALOAD, 3)); callCode.add(new InsnNode(Opcodes.ARETURN)); } coIteratorClass.methods.add(call); // if debugging code is emitted it needs space for two // additional locals and 5 stack operand if (generateDebugCode) { call.maxStack = 5; call.maxLocals = 5; } else { if (isStatic) { call.maxStack = 3; } else { call.maxStack = 4; } call.maxLocals = 3; } /* * CoIterator created - define it in the runtime and verify if * needed */ if (log.isLoggable(Level.FINEST)) { log.finest("Generated class " + coIteratorClassName); } ClassWriter cw = new ClassWriter(0); coIteratorClass.accept(cw); byte[] classBytes = cw.toByteArray(); try { CoroutineInstrumentator.dumpClass(coIteratorClassName, classBytes); } catch (IOException e) { throw new CoroutineGenerationException("Unable to write class " + coIteratorClassName, e); } /* * start generating method - new method is named as the method in * user code, it: returns instance of appropriate CoIterator (see * above), saves arguments of call */ if (log.isLoggable(Level.FINEST)) { log.finest("Instrumenting method " + coroutine.name); } InsnList code = coroutine.instructions; code.clear(); /* * create new Frame */ boolean isDebugFramePossible = generateDebugCode && coroutine.localVariables != null; if (isDebugFramePossible) { code.add(createDebugFrame(coroutine)); } else { code.add(createFrame(coroutine)); } /* * save frame in the first, and locals array in the second local * variable */ int argsSize = Type.getArgumentsAndReturnSizes(coroutine.desc) >> 2; if (isStatic) { argsSize -= 1; } code.add(new VarInsnNode(Opcodes.ASTORE, argsSize)); code.add(new VarInsnNode(Opcodes.ALOAD, argsSize)); code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLocals", "()[Ljava/lang/Object;")); int localsArrayIndex = argsSize + 1; code.add(new VarInsnNode(Opcodes.ASTORE, localsArrayIndex)); /* * save all call arguments (along with this if this method is not * static) into locals array */ Type[] argsTypes = Type.getArgumentTypes(coroutine.desc); if (!isStatic) { code.add(saveloc(localsArrayIndex, 0, 0, JAVA_LANG_OBJECT)); code.add(savelocs(localsArrayIndex, 1, 1, argsTypes)); } else { code.add(savelocs(localsArrayIndex, 0, 0, argsTypes)); } /* * create CoIterator instance with saved frame, make initial call to * next if needed and return to caller */ code.add(new TypeInsnNode(Opcodes.NEW, coIteratorClassName)); code.add(new InsnNode(Opcodes.DUP)); code.add(new VarInsnNode(Opcodes.ALOAD, argsSize)); code.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, coIteratorClassName, "<init>", CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR)); if (!getBoolean(annotation, "generator", true)) { code.add(new InsnNode(Opcodes.DUP)); code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, coIteratorClassName, "next", "()Ljava/lang/Object;")); code.add(new InsnNode(Opcodes.POP)); } code.add(new InsnNode(Opcodes.ARETURN)); /* * end method generation; maxs can be statically determined 3 * operands on stack (call to frame setLocals and CoIterator * constructor) + 1 if any argument is long or double (debug frame * needs 7 operands for variable names creation); locals = argsSize * + 1 reference to frame + 1 array of locals */ if (isDebugFramePossible) { coroutine.maxStack = 7; } else { boolean isCategory2ArgumentPresent = false; for (Type argType : argsTypes) { int sort = argType.getSort(); if (sort == Type.LONG || sort == Type.DOUBLE) { isCategory2ArgumentPresent = true; break; } } coroutine.maxStack = isCategory2ArgumentPresent ? 4 : 3; } coroutine.maxLocals = localsArrayIndex + 1; coroutine.localVariables.clear(); coroutine.tryCatchBlocks.clear(); num++; } }
From source file:pt.minha.kernel.instrument.SyncToMonitorClassVisitor.java
License:Open Source License
private void mkClinit() { MethodVisitor mv = visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode();/*from w ww .j ava 2 s .c o m*/ mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }