List of usage examples for org.objectweb.asm Opcodes IRETURN
int IRETURN
To view the source code for org.objectweb.asm Opcodes IRETURN.
Click Source Link
From source file:asmlib.Type.java
License:Open Source License
/** Mtodo que retorna o opcode certo para o tipo de varivel que se quer fazer return. * Opcodes disponveis:/*from www. j a va 2 s .c o m*/ * - IRETURN para boolean, byte, char, short, int * - LRETURN para long * - FRETURN para float * - DRETURN para double * - ARETURN para referncia (objecto ou array) * - RETURN para mtodos void **/ public int getReturnInsn() { char c = bytecodeName().charAt(0); switch (c) { case 'Z': // boolean case 'B': // byte case 'C': // char case 'S': // short case 'I': // int return Opcodes.IRETURN; case 'J': // long return Opcodes.LRETURN; case 'F': // float return Opcodes.FRETURN; case 'D': // double return Opcodes.DRETURN; case '[': // Algum tipo de array case 'L': // objecto case 'T': // objecto (generics) return Opcodes.ARETURN; case 'V': // void return Opcodes.RETURN; } throw new InstrumentationException("Unknown fieldType in getReturnInsn"); }
From source file:ataspectj.UnweavableTest.java
License:Open Source License
ISome getJit() { ClassWriter cw = new ClassWriter(true, true); cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/ISomeGen", null, "java/lang/Object", new String[] { "ataspectj/UnweavableTest$ISome" }); AnnotationVisitor av = cw.visitAnnotation("Lataspectj/UnweavableTest$ASome;", true); av.visitEnd();/* w ww . ja v a 2 s . c om*/ MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "giveOne", "()I", null, new String[0]); mv.visitInsn(Opcodes.ICONST_2); mv.visitInsn(Opcodes.IRETURN); mv.visitMaxs(0, 0); cw.visitEnd(); try { ClassLoader loader = this.getClass().getClassLoader(); Method def = ClassLoader.class.getDeclaredMethod("defineClass", new Class[] { String.class, byte[].class, int.class, int.class }); def.setAccessible(true); Class gen = (Class) def.invoke(loader, "ataspectj.ISomeGen", cw.toByteArray(), 0, cw.toByteArray().length); return (ISome) gen.newInstance(); } catch (Throwable t) { fail(t.toString()); return null; } }
From source file:br.usp.each.saeg.badua.core.internal.instr.CoverageMethodTransformer.java
License:Open Source License
private boolean isReturn(final int opcode) { if (opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) return true; return opcode == Opcodes.ATHROW; }
From source file:br.usp.each.saeg.badua.test.validation.MaxTest.java
License:Open Source License
@Override @Before// w ww . j a va2 s . co m public void setUp() throws Exception { super.setUp(); final int classVersion = Opcodes.V1_6; final int classAccessor = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER; final String className = "Max"; final String superName = "java/lang/Object"; final int methodAccessor = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC; final String methodName = "max"; final String methodDesc = "([II)I"; final ClassWriter cw = new ClassWriter(0); final MethodVisitor mw; cw.visit(classVersion, classAccessor, className, null, superName, null); mw = cw.visitMethod(methodAccessor, methodName, methodDesc, null, null); mw.visitCode(); // block 0 (definitions {0, 1, 2, 3}) mw.visitInsn(Opcodes.ICONST_0); mw.visitVarInsn(Opcodes.ISTORE, 2); mw.visitVarInsn(Opcodes.ALOAD, 0); mw.visitVarInsn(Opcodes.ILOAD, 2); mw.visitIincInsn(2, 1); mw.visitInsn(Opcodes.IALOAD); mw.visitVarInsn(Opcodes.ISTORE, 3); // block 1 (p-uses {1, 2}) final Label backLoop = new Label(); mw.visitLabel(backLoop); mw.visitVarInsn(Opcodes.ILOAD, 2); mw.visitVarInsn(Opcodes.ILOAD, 1); final Label breakLoop = new Label(); mw.visitJumpInsn(Opcodes.IF_ICMPGE, breakLoop); // block 3 (p-uses {0, 2, 3}) mw.visitVarInsn(Opcodes.ALOAD, 0); mw.visitVarInsn(Opcodes.ILOAD, 2); mw.visitInsn(Opcodes.IALOAD); mw.visitVarInsn(Opcodes.ILOAD, 3); final Label jump = new Label(); mw.visitJumpInsn(Opcodes.IF_ICMPLE, jump); // block 5 (definitions {3}, uses {0, 2}) mw.visitVarInsn(Opcodes.ALOAD, 0); mw.visitVarInsn(Opcodes.ILOAD, 2); mw.visitInsn(Opcodes.IALOAD); mw.visitVarInsn(Opcodes.ISTORE, 3); // block 4 (definitions {2}, uses {2}) mw.visitLabel(jump); mw.visitIincInsn(2, 1); mw.visitJumpInsn(Opcodes.GOTO, backLoop); // block 2 ( uses {3}) mw.visitLabel(breakLoop); mw.visitVarInsn(Opcodes.ILOAD, 3); mw.visitInsn(Opcodes.IRETURN); mw.visitMaxs(2, 4); mw.visitEnd(); cw.visitEnd(); final byte[] bytes = cw.toByteArray(); klass = addClass(className, bytes); method = klass.getMethod(methodName, int[].class, int.class); classId = CRC64.checksum(bytes); RT.init(new RuntimeData()); }
From source file:bytecode.InstructionExporter.java
License:Apache License
/** * Outputs a return instruction, selecting the correct type variant. * * @param instruction Return instruction. * @return <code>null</code> *//*from w w w . j a va 2 s . c o m*/ @Override public Void visit(ValueReturn instruction) { switch (instruction.getType().getSort()) { case LONG: mv.visitInsn(Opcodes.LRETURN); break; case FLOAT: mv.visitInsn(Opcodes.FRETURN); break; case DOUBLE: mv.visitInsn(Opcodes.DRETURN); break; case REF: mv.visitInsn(Opcodes.ARETURN); break; default: mv.visitInsn(Opcodes.IRETURN); break; } return null; }
From source file:bytecode.MethodImporter.java
License:Apache License
/** * Imports instructions with no immediate operands. * * @param opcode Opcode./*from ww w .j av a 2 s .com*/ */ @Override public void visitInsn(final int opcode) { Producer a, b, c, d; Type top; switch (opcode) { // Constants case Opcodes.ACONST_NULL: createConstant(null); break; case Opcodes.ICONST_M1: createConstant(new Integer(-1)); break; case Opcodes.ICONST_0: createConstant(new Integer(0)); break; case Opcodes.ICONST_1: createConstant(new Integer(1)); break; case Opcodes.ICONST_2: createConstant(new Integer(2)); break; case Opcodes.ICONST_3: createConstant(new Integer(3)); break; case Opcodes.ICONST_4: createConstant(new Integer(4)); break; case Opcodes.ICONST_5: createConstant(new Integer(5)); break; case Opcodes.LCONST_0: createConstant(new Long(0)); break; case Opcodes.LCONST_1: createConstant(new Long(1)); break; case Opcodes.FCONST_0: createConstant(new Float(0.0f)); break; case Opcodes.FCONST_1: createConstant(new Float(1.0f)); break; case Opcodes.FCONST_2: createConstant(new Float(2.0f)); break; case Opcodes.DCONST_0: createConstant(new Double(0.0f)); break; case Opcodes.DCONST_1: createConstant(new Double(1.0f)); break; // Binary Operations case Opcodes.IADD: createArithmetic(Arithmetic.Operator.ADD, Type.INT); break; case Opcodes.LADD: createArithmetic(Arithmetic.Operator.ADD, Type.LONG); break; case Opcodes.FADD: createArithmetic(Arithmetic.Operator.ADD, Type.FLOAT); break; case Opcodes.DADD: createArithmetic(Arithmetic.Operator.ADD, Type.DOUBLE); break; case Opcodes.ISUB: createArithmetic(Arithmetic.Operator.SUB, Type.INT); break; case Opcodes.LSUB: createArithmetic(Arithmetic.Operator.SUB, Type.LONG); break; case Opcodes.FSUB: createArithmetic(Arithmetic.Operator.SUB, Type.FLOAT); break; case Opcodes.DSUB: createArithmetic(Arithmetic.Operator.SUB, Type.DOUBLE); break; case Opcodes.IMUL: createArithmetic(Arithmetic.Operator.MUL, Type.INT); break; case Opcodes.LMUL: createArithmetic(Arithmetic.Operator.MUL, Type.LONG); break; case Opcodes.FMUL: createArithmetic(Arithmetic.Operator.MUL, Type.FLOAT); break; case Opcodes.DMUL: createArithmetic(Arithmetic.Operator.MUL, Type.DOUBLE); break; case Opcodes.IDIV: createArithmetic(Arithmetic.Operator.DIV, Type.INT); break; case Opcodes.LDIV: createArithmetic(Arithmetic.Operator.DIV, Type.LONG); break; case Opcodes.FDIV: createArithmetic(Arithmetic.Operator.DIV, Type.FLOAT); break; case Opcodes.DDIV: createArithmetic(Arithmetic.Operator.DIV, Type.DOUBLE); break; case Opcodes.IREM: createArithmetic(Arithmetic.Operator.REM, Type.INT); break; case Opcodes.LREM: createArithmetic(Arithmetic.Operator.REM, Type.LONG); break; case Opcodes.FREM: createArithmetic(Arithmetic.Operator.REM, Type.FLOAT); break; case Opcodes.DREM: createArithmetic(Arithmetic.Operator.REM, Type.DOUBLE); break; case Opcodes.IAND: createArithmetic(Arithmetic.Operator.AND, Type.INT); break; case Opcodes.LAND: createArithmetic(Arithmetic.Operator.AND, Type.LONG); break; case Opcodes.IOR: createArithmetic(Arithmetic.Operator.OR, Type.INT); break; case Opcodes.LOR: createArithmetic(Arithmetic.Operator.OR, Type.LONG); break; case Opcodes.IXOR: createArithmetic(Arithmetic.Operator.XOR, Type.INT); break; case Opcodes.LXOR: createArithmetic(Arithmetic.Operator.XOR, Type.LONG); break; case Opcodes.ISHL: createArithmetic(Arithmetic.Operator.SHL, Type.INT); break; case Opcodes.LSHL: createArithmetic(Arithmetic.Operator.SHL, Type.LONG); break; case Opcodes.ISHR: createArithmetic(Arithmetic.Operator.SHR, Type.INT); break; case Opcodes.LSHR: createArithmetic(Arithmetic.Operator.SHR, Type.LONG); break; case Opcodes.IUSHR: createArithmetic(Arithmetic.Operator.USHR, Type.INT); break; case Opcodes.LUSHR: createArithmetic(Arithmetic.Operator.USHR, Type.LONG); break; case Opcodes.LCMP: createCompare(false, Type.LONG); break; case Opcodes.FCMPL: createCompare(false, Type.FLOAT); break; case Opcodes.FCMPG: createCompare(true, Type.FLOAT); break; case Opcodes.DCMPL: createCompare(false, Type.DOUBLE); break; case Opcodes.DCMPG: createCompare(true, Type.DOUBLE); break; case Opcodes.INEG: createNegate(); break; case Opcodes.LNEG: createNegate(); break; case Opcodes.FNEG: createNegate(); break; case Opcodes.DNEG: createNegate(); break; case Opcodes.I2L: createConvert(Type.LONG); break; case Opcodes.I2F: createConvert(Type.FLOAT); break; case Opcodes.I2D: createConvert(Type.DOUBLE); break; case Opcodes.I2B: createConvert(Type.BYTE); break; case Opcodes.I2C: createConvert(Type.CHAR); break; case Opcodes.I2S: createConvert(Type.SHORT); break; case Opcodes.L2I: createConvert(Type.INT); break; case Opcodes.L2F: createConvert(Type.FLOAT); break; case Opcodes.L2D: createConvert(Type.DOUBLE); break; case Opcodes.F2I: createConvert(Type.INT); break; case Opcodes.F2L: createConvert(Type.LONG); break; case Opcodes.F2D: createConvert(Type.DOUBLE); break; case Opcodes.D2I: createConvert(Type.INT); break; case Opcodes.D2F: createConvert(Type.FLOAT); break; case Opcodes.D2L: createConvert(Type.LONG); break; case Opcodes.IALOAD: createArrayRead(Type.INT); break; case Opcodes.LALOAD: createArrayRead(Type.LONG); break; case Opcodes.FALOAD: createArrayRead(Type.FLOAT); break; case Opcodes.DALOAD: createArrayRead(Type.DOUBLE); break; case Opcodes.AALOAD: createArrayRead(Type.getFreshRef()); break; case Opcodes.BALOAD: createArrayRead(Type.BYTE); break; case Opcodes.CALOAD: createArrayRead(Type.CHAR); break; case Opcodes.SALOAD: createArrayRead(Type.SHORT); break; case Opcodes.IASTORE: createArrayWrite(Type.INT); break; case Opcodes.LASTORE: createArrayWrite(Type.LONG); break; case Opcodes.FASTORE: createArrayWrite(Type.FLOAT); break; case Opcodes.DASTORE: createArrayWrite(Type.DOUBLE); break; case Opcodes.AASTORE: createArrayWrite(Type.getFreshRef()); break; case Opcodes.BASTORE: createArrayWrite(Type.BYTE); break; case Opcodes.CASTORE: createArrayWrite(Type.CHAR); break; case Opcodes.SASTORE: createArrayWrite(Type.SHORT); break; case Opcodes.IRETURN: createReturn(Type.INT); break; case Opcodes.LRETURN: createReturn(Type.LONG); break; case Opcodes.FRETURN: createReturn(Type.FLOAT); break; case Opcodes.DRETURN: createReturn(Type.DOUBLE); break; case Opcodes.ARETURN: createReturn(Type.REF); break; case Opcodes.RETURN: createReturn(null); break; case Opcodes.ATHROW: createThrow(); break; // Array Length case Opcodes.ARRAYLENGTH: ordered.add(stack.push(new ArrayLength(stack.pop()))); break; // Swap case Opcodes.SWAP: a = stack.pop(); b = stack.pop(); stack.push(a); stack.push(b); ordered.add(new StackOperation(StackOperation.Sort.SWAP)); break; // Duplicates case Opcodes.DUP: stack.push(stack.peek()); ordered.add(new StackOperation(StackOperation.Sort.DUP)); break; case Opcodes.DUP2: top = stack.peek().getType(); // Type 2 Values if (top.getSize() == 2) { stack.push(stack.peek()); // Type 1 Values } else { b = stack.pop(); a = stack.pop(); stack.push(a); stack.push(b); stack.push(a); stack.push(b); } ordered.add(new StackOperation(StackOperation.Sort.DUP2)); break; case Opcodes.DUP_X1: b = stack.pop(); a = stack.pop(); stack.push(b); stack.push(a); stack.push(b); ordered.add(new StackOperation(StackOperation.Sort.DUP_X1)); break; case Opcodes.DUP_X2: top = stack.peek().getType(); // Type 2 Values if (top.getSize() == 2) { b = stack.pop(); a = stack.pop(); stack.push(b); stack.push(a); stack.push(b); // Type 1 Values } else { c = stack.pop(); b = stack.pop(); a = stack.pop(); stack.push(c); stack.push(a); stack.push(b); stack.push(c); } ordered.add(new StackOperation(StackOperation.Sort.DUP_X2)); break; // Pops case Opcodes.POP: stack.pop(); ordered.add(new StackOperation(StackOperation.Sort.POP)); break; case Opcodes.POP2: top = stack.peek().getType(); // Type 2 Values if (top.getSize() == 2) { stack.pop(); // Type 1 Values } else { stack.pop(); stack.pop(); } ordered.add(new StackOperation(StackOperation.Sort.POP2)); break; // TODO: DUP2_X1, DUP2_X2, MONITORENTER, MONITOREXIT case Opcodes.MONITORENTER: throw new RuntimeException("visitInsn: MONITORENTER"); case Opcodes.MONITOREXIT: throw new RuntimeException("visitInsn: MONITOREXIT"); case Opcodes.DUP2_X1: throw new RuntimeException("visitInsn: DUP2_X1"); case Opcodes.DUP2_X2: throw new RuntimeException("visitInsn: DUP2_X2"); default: throw new RuntimeException("visitInsn: " + opcode); } }
From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java
License:Open Source License
private void dumpCodeBlock(MethodVisitor mv, int idx, int skip) { int start = codeBlocks[idx].endInstruction; int end = codeBlocks[idx + 1].endInstruction; for (int i = start + skip; i < end; i++) { AbstractInsnNode ins = mn.instructions.get(i); switch (ins.getOpcode()) { case Opcodes.RETURN: case Opcodes.ARETURN: case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: emitPopMethod(mv);/*from ww w . j av a 2s .c om*/ break; case Opcodes.MONITORENTER: case Opcodes.MONITOREXIT: if (!db.isAllowMonitors()) { if (!className.equals("clojure/lang/LazySeq")) throw new UnableToInstrumentException("synchronization", className, mn.name, mn.desc); } else if (!warnedAboutMonitors) { warnedAboutMonitors = true; db.log(LogLevel.WARNING, "Method %s#%s%s contains synchronization", className, mn.name, mn.desc); } break; case Opcodes.INVOKESPECIAL: MethodInsnNode min = (MethodInsnNode) ins; if ("<init>".equals(min.name)) { int argSize = TypeAnalyzer.getNumArguments(min.desc); Frame frame = frames[i]; int stackIndex = frame.getStackSize() - argSize - 1; Value thisValue = frame.getStack(stackIndex); if (stackIndex >= 1 && isNewValue(thisValue, true) && isNewValue(frame.getStack(stackIndex - 1), false)) { NewValue newValue = (NewValue) thisValue; if (newValue.omitted) { emitNewAndDup(mv, frame, stackIndex, min); } } else { db.log(LogLevel.WARNING, "Expected to find a NewValue on stack index %d: %s", stackIndex, frame); } } break; } ins.accept(mv); } }
From source file:com.android.builder.testing.MockableJarGenerator.java
License:Apache License
/** * Rewrites the method bytecode to remove the "Stub!" exception. *//*from w w w .j a va 2s . c o m*/ private void fixMethodBody(MethodNode methodNode, ClassNode classNode) { if ((methodNode.access & Opcodes.ACC_NATIVE) != 0 || (methodNode.access & Opcodes.ACC_ABSTRACT) != 0) { // Abstract and native method don't have bodies to rewrite. return; } if ((classNode.access & Opcodes.ACC_ENUM) != 0 && ENUM_METHODS.contains(methodNode.name)) { // Don't break enum classes. return; } Type returnType = Type.getReturnType(methodNode.desc); InsnList instructions = methodNode.instructions; if (methodNode.name.equals(CONSTRUCTOR)) { // Keep the call to parent constructor, delete the exception after that. boolean deadCode = false; for (AbstractInsnNode instruction : instructions.toArray()) { if (!deadCode) { if (instruction.getOpcode() == Opcodes.INVOKESPECIAL) { instructions.insert(instruction, new InsnNode(Opcodes.RETURN)); // Start removing all following instructions. deadCode = true; } } else { instructions.remove(instruction); } } } else { instructions.clear(); if (returnDefaultValues || methodNode.name.equals(CLASS_CONSTRUCTOR)) { if (INTEGER_LIKE_TYPES.contains(returnType)) { instructions.add(new InsnNode(Opcodes.ICONST_0)); } else if (returnType.equals(Type.LONG_TYPE)) { instructions.add(new InsnNode(Opcodes.LCONST_0)); } else if (returnType.equals(Type.FLOAT_TYPE)) { instructions.add(new InsnNode(Opcodes.FCONST_0)); } else if (returnType.equals(Type.DOUBLE_TYPE)) { instructions.add(new InsnNode(Opcodes.DCONST_0)); } else { instructions.add(new InsnNode(Opcodes.ACONST_NULL)); } instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN))); } else { instructions.insert(throwExceptionsList(methodNode, classNode)); } } }
From source file:com.android.ide.eclipse.apt.internal.analysis.InternalGetSetAnalyzer.java
License:Apache License
/** * Checks if a method is a getter//from w ww.j av a 2 s. c om * @param methodTest The method to test * @return True if the method is a getter, false otherwise */ private boolean isGetter(final MethodNode methodTest) { boolean getter = false; final String desc = methodTest.desc; final Type[] arguments = Type.getArgumentTypes(desc); final Type returnType = Type.getReturnType(desc); if (arguments.length == 0 && returnType.getSort() != Type.VOID) { final InsnList instructions = methodTest.instructions; //three next to skip label and line number instructions final AbstractInsnNode first = instructions.getFirst().getNext().getNext(); final int returnOp = returnType.getOpcode(Opcodes.IRETURN); final int firstOp = first.getOpcode(); //check for static getter if ((Opcodes.ACC_STATIC & methodTest.access) == 0) { if (firstOp == Opcodes.ALOAD) { final AbstractInsnNode second = first.getNext(); if (second.getOpcode() == Opcodes.GETFIELD) { final AbstractInsnNode third = second.getNext(); if (third.getOpcode() == returnOp) { getter = true; } } } } else { if (firstOp == Opcodes.GETSTATIC) { final AbstractInsnNode second = first.getNext(); if (second.getOpcode() == returnOp) { getter = true; } } } } return getter; }
From source file:com.android.tools.layoutlib.create.DelegateMethodAdapter.java
License:Apache License
/** * Generates the new code for the method. * <p/>// w w w . jav a2 s .c o m * For native methods, this must be invoked directly by {@link DelegateClassAdapter} * (since they have no code to visit). * <p/> * Otherwise for non-native methods the {@link DelegateClassAdapter} simply needs to * return this instance of {@link DelegateMethodAdapter} and let the normal visitor pattern * invoke it as part of the {@link ClassReader#accept(ClassVisitor, int)} workflow and then * this method will be invoked from {@link MethodVisitor#visitEnd()}. */ public void generateCode() { /* * The goal is to generate a call to a static delegate method. * If this method is non-static, the first parameter will be 'this'. * All the parameters must be passed and then the eventual return type returned. * * Example, let's say we have a method such as * public void method_1(int a, Object b, ArrayList<String> c) { ... } * * We'll want to create a body that calls a delegate method like this: * TheClass_Delegate.method_1(this, a, b, c); * * If the method is non-static and the class name is an inner class (e.g. has $ in its * last segment), we want to push the 'this' of the outer class first: * OuterClass_InnerClass_Delegate.method_1( * OuterClass.this, * OuterClass$InnerClass.this, * a, b, c); * * Only one level of inner class is supported right now, for simplicity and because * we don't need more. * * The generated class name is the current class name with "_Delegate" appended to it. * One thing to realize is that we don't care about generics -- since generic types * are erased at runtime, they have no influence on the method name being called. */ // Add our annotation AnnotationVisitor aw = mParentVisitor.visitAnnotation( Type.getObjectType(Type.getInternalName(LayoutlibDelegate.class)).toString(), true); // visible at runtime aw.visitEnd(); if (!mVisitCodeCalled) { // If this is a direct call to generateCode() as done by DelegateClassAdapter // for natives, visitCode() hasn't been called yet. mParentVisitor.visitCode(); mVisitCodeCalled = true; } ArrayList<Type> paramTypes = new ArrayList<Type>(); String delegateClassName = mClassName + DELEGATE_SUFFIX; boolean pushedArg0 = false; int maxStack = 0; // For an instance method (e.g. non-static), push the 'this' preceded // by the 'this' of any outer class, if any. if (!mIsStatic) { // Check if the last segment of the class name has inner an class. // Right now we only support one level of inner classes. int slash = mClassName.lastIndexOf('/'); int dol = mClassName.lastIndexOf('$'); if (dol != -1 && dol > slash && dol == mClassName.indexOf('$')) { String outerClass = mClassName.substring(0, dol); Type outerType = Type.getObjectType(outerClass); // Change a delegate class name to "com/foo/Outer_Inner_Delegate" delegateClassName = delegateClassName.replace('$', '_'); // The first-level inner class has a package-protected member called 'this$0' // that points to the outer class. // Push this.getField("this$0") on the call stack. mParentVisitor.visitVarInsn(Opcodes.ALOAD, 0); // var 0 = this mParentVisitor.visitFieldInsn(Opcodes.GETFIELD, mClassName, // class where the field is defined "this$0", // field name outerType.getDescriptor()); // type of the field maxStack++; paramTypes.add(outerType); } // Push "this" for the instance method, which is always ALOAD 0 mParentVisitor.visitVarInsn(Opcodes.ALOAD, 0); maxStack++; pushedArg0 = true; paramTypes.add(Type.getObjectType(mClassName)); } // Push all other arguments. Start at arg 1 if we already pushed 'this' above. Type[] argTypes = Type.getArgumentTypes(mDesc); int maxLocals = pushedArg0 ? 1 : 0; for (Type t : argTypes) { int size = t.getSize(); mParentVisitor.visitVarInsn(t.getOpcode(Opcodes.ILOAD), maxLocals); maxLocals += size; maxStack += size; paramTypes.add(t); } // Construct the descriptor of the delegate based on the parameters // we pushed on the call stack. The return type remains unchanged. String desc = Type.getMethodDescriptor(Type.getReturnType(mDesc), paramTypes.toArray(new Type[paramTypes.size()])); // Invoke the static delegate mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, delegateClassName, mMethodName, desc); Type returnType = Type.getReturnType(mDesc); mParentVisitor.visitInsn(returnType.getOpcode(Opcodes.IRETURN)); mParentVisitor.visitMaxs(maxStack, maxLocals); mParentVisitor.visitEnd(); // For debugging now. Maybe we should collect these and store them in // a text file for helping create the delegates. We could also compare // the text file to a golden and break the build on unsupported changes // or regressions. Even better we could fancy-print something that looks // like the expected Java method declaration. mLog.debug("Delegate: %1$s # %2$s %3$s", delegateClassName, mMethodName, desc); }