List of usage examples for org.objectweb.asm Opcodes GETFIELD
int GETFIELD
To view the source code for org.objectweb.asm Opcodes GETFIELD.
Click Source Link
From source file:com.lucidtechnics.blackboard.TargetConstructor.java
License:Apache License
private static final byte[] createWrapperObjectByteArray(String _targetName, Class _class) { ClassWriter classWriter = new ClassWriter(true); FieldVisitor fieldVisitor;// w ww . j ava2 s . c om MethodVisitor methodVisitor; String superClassName = _class.getCanonicalName().replaceAll("\\.", "/"); String[] superClassNameParts = superClassName.split("/"); String simpleClassName = superClassNameParts[superClassNameParts.length - 1]; String blackboardSubClassName = "com/lucidtechnics/blackboard/wrapper/" + superClassName + "Wrapper"; classWriter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, blackboardSubClassName, null, superClassName, new String[] { "com/lucidtechnics/blackboard/Target" }); classWriter.visitSource(simpleClassName, null); { fieldVisitor = classWriter.visitField(Opcodes.ACC_PRIVATE, "blackboardObject", "Ljava/lang/Object;", null, null); fieldVisitor.visitEnd(); } { fieldVisitor = classWriter.visitField(Opcodes.ACC_PRIVATE, "intercepter", "Lcom/lucidtechnics/blackboard/Intercepter;", null, null); fieldVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "getBlackboardObject", "()Ljava/lang/Object;", null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitFieldInsn(Opcodes.GETFIELD, blackboardSubClassName, "blackboardObject", "Ljava/lang/Object;"); methodVisitor.visitInsn(Opcodes.ARETURN); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "getIntercepter", "()Lcom/lucidtechnics/blackboard/Intercepter;", null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitFieldInsn(Opcodes.GETFIELD, blackboardSubClassName, "intercepter", "Lcom/lucidtechnics/blackboard/Intercepter;"); methodVisitor.visitInsn(Opcodes.ARETURN); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "setBlackboardObject", "(Ljava/lang/Object;)V", null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, blackboardSubClassName, "blackboardObject", "Ljava/lang/Object;"); methodVisitor.visitInsn(Opcodes.RETURN); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "setIntercepter", "(Lcom/lucidtechnics/blackboard/Intercepter;)V", null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, blackboardSubClassName, "intercepter", "Lcom/lucidtechnics/blackboard/Intercepter;"); methodVisitor.visitInsn(Opcodes.RETURN); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superClassName, "<init>", "()V"); methodVisitor.visitInsn(Opcodes.RETURN); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } List mutatorMethodList = findMutatorMethods(_class); Iterator mutatorMethods = mutatorMethodList.iterator(); while (mutatorMethods.hasNext() == true) { Method method = (Method) mutatorMethods.next(); MethodInfo methodInfo = (MethodInfo) MethodInfoTransformer.getInstance().transform(method); Signature signature = methodInfo.getSignature(); String methodName = signature.getName(); String parameterType = signature.getArgumentTypes()[0].getDescriptor(); String returnType = signature.getReturnType().getDescriptor(); String[] exceptionTypeArray = createExceptionTypeArray(methodInfo.getExceptionTypes()); { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(" + parameterType + ")" + returnType, null, exceptionTypeArray); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getIntercepter", "()Lcom/lucidtechnics/blackboard/Intercepter;"); Label l1 = new Label(); methodVisitor.visitJumpInsn(Opcodes.IFNULL, l1); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getIntercepter", "()Lcom/lucidtechnics/blackboard/Intercepter;"); methodVisitor.visitLdcInsn(superClassName); methodVisitor.visitLdcInsn(blackboardSubClassName); methodVisitor.visitLdcInsn(methodName); methodVisitor.visitLdcInsn(_targetName); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); int tempOpCode = getLoadOpcode(parameterType); methodVisitor.visitVarInsn(tempOpCode, 1); if (tempOpCode == Opcodes.ALOAD) { //this is an object. methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/lucidtechnics/blackboard/Intercepter", "monitor", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V"); } else { //it is a primitive. methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/lucidtechnics/blackboard/Intercepter", "monitor", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;" + parameterType + ")V"); } methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getBlackboardObject", "()Ljava/lang/Object;"); methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, superClassName); methodVisitor.visitVarInsn(getLoadOpcode(parameterType), 1); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClassName, methodName, "(" + parameterType + ")" + returnType); methodVisitor.visitLabel(l1); methodVisitor.visitInsn(getReturnOpcode(returnType)); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } } List otherPublicMethodList = findOtherPublicMethods(_class); Iterator otherPublicMethods = otherPublicMethodList.iterator(); while (otherPublicMethods.hasNext() == true) { Method method = (Method) otherPublicMethods.next(); MethodInfo methodInfo = (MethodInfo) MethodInfoTransformer.getInstance().transform(method); Signature signature = methodInfo.getSignature(); String methodName = signature.getName(); String parameterTypes = constructParameterTypes(signature.getArgumentTypes()); String returnType = signature.getReturnType().getDescriptor(); String[] exceptionTypeArray = createExceptionTypeArray(methodInfo.getExceptionTypes()); if (logger.isDebugEnabled() == true) { logger.debug("Processing method: " + methodName); } if (logger.isDebugEnabled() == true) { logger.debug("Parameter types are: " + parameterTypes); } if ("toString".equalsIgnoreCase(methodName) == false) { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(" + parameterTypes + ")" + returnType, null, exceptionTypeArray); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getBlackboardObject", "()Ljava/lang/Object;"); methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, superClassName); methodVisitor.visitVarInsn(Opcodes.ASTORE, signature.getArgumentTypes().length + 1); methodVisitor.visitVarInsn(Opcodes.ALOAD, signature.getArgumentTypes().length + 1); loadParameters(methodVisitor, signature.getArgumentTypes()); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClassName, methodName, "(" + parameterTypes + ")" + returnType); methodVisitor.visitInsn(getReturnOpcode(returnType)); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } } List protectedMethodList = findProtectedMethods(_class); Iterator protectedMethods = protectedMethodList.iterator(); while (protectedMethods.hasNext() == true) { Method method = (Method) protectedMethods.next(); MethodInfo methodInfo = (MethodInfo) MethodInfoTransformer.getInstance().transform(method); Signature signature = methodInfo.getSignature(); String methodName = signature.getName(); String parameterTypes = constructParameterTypes(signature.getArgumentTypes()); String returnType = signature.getReturnType().getDescriptor(); String[] exceptionTypeArray = createExceptionTypeArray(methodInfo.getExceptionTypes()); if (logger.isDebugEnabled() == true) { logger.debug("Processing method: " + methodName + " and parameter types are: " + parameterTypes); } { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + parameterTypes + ")" + returnType, null, exceptionTypeArray); methodVisitor.visitCode(); methodVisitor.visitTypeInsn(Opcodes.NEW, "com/lucidtechnics/blackboard/BlackboardException"); methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitLdcInsn("Unable to access protected methods on Blackboard object"); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/lucidtechnics/blackboard/BlackboardException", "<init>", "(Ljava/lang/String;)V"); methodVisitor.visitInsn(Opcodes.ATHROW); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } } { methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getBlackboardObject", "()Ljava/lang/Object;"); methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/lucidtechnics/blackboard/util/Utility", "toString", "(Ljava/lang/Object;)Ljava/lang/String;"); methodVisitor.visitInsn(getReturnOpcode("Ljava/lang/String;")); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } classWriter.visitEnd(); if (logger.isDebugEnabled() == true) { logger.debug("Finished creating new class: " + blackboardSubClassName + " for target class: " + superClassName + "."); } return classWriter.toByteArray(); }
From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java
License:Apache License
public void performFieldInsn(int opcode, String owner, String name, String desc) { switch (opcode) { case Opcodes.GETSTATIC: case Opcodes.GETFIELD: { Operand op = fields.get(owner + ":" + name); if (op == null) { op = new Operand(owner + ":" + name, desc); }/* w w w . j av a 2 s. c o m*/ push(op); } break; case Opcodes.PUTSTATIC: case Opcodes.PUTFIELD: if (!stack.isEmpty()) { Operand op = stack.remove(stack.size() - 1); fields.put(owner + ":" + name, op); } break; } }
From source file:com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter.java
License:Apache License
public void addGetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) { if (methodName == null || fieldNode == null) { throw new IllegalArgumentException("method name or fieldNode annotation must not be null."); }//from ww w. ja v a2 s . co m // no argument is (). final String desc = "()" + fieldNode.getDesc(); final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null); if (methodNode.instructions == null) { methodNode.instructions = new InsnList(); } final InsnList instructions = methodNode.instructions; // load this. instructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); // get fieldNode. instructions .add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc())); // return of type. final Type type = Type.getType(fieldNode.getDesc()); instructions.add(new InsnNode(type.getOpcode(Opcodes.IRETURN))); if (this.classNode.methods == null) { this.classNode.methods = new ArrayList<MethodNode>(); } this.classNode.methods.add(methodNode); }
From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java
License:Open Source License
private static void getRawBaseCase(MethodVisitor mv, String className, Relation<String, Integer> hashCodeRelation, EnvironmentClass environmentClass, int code, Label returnNull) {//ww w.ja v a2 s .com PredicateSet<String> strings = hashCodeRelation.matchSecond(code); for (String testString : strings) { mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitLdcInsn(testString); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z"); Label afterReturn = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, afterReturn); mv.visitVarInsn(Opcodes.ALOAD, 0); String idString = testString + environmentClass.namespace(); mv.visitFieldInsn(Opcodes.GETFIELD, className, Naming.mangleIdentifier(idString), environmentClass.descriptor()); mv.visitInsn(Opcodes.ARETURN); mv.visitLabel(afterReturn); } mv.visitJumpInsn(Opcodes.GOTO, returnNull); }
From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java
License:Open Source License
private static void writeDumpMethod(ClassWriter cw, String className, EnvSymbolNames symbolNames) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "dump", "(Ljava/lang/Appendable;)Ljava/lang/Appendable;", null, new String[] { "java/io/IOException" }); mv.visitCode();/*from w ww . j a va 2 s.com*/ Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, className, "within", Type.getType(HasAt.class).getDescriptor()); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, l1); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, className, "within", Type.getType(HasAt.class).getDescriptor()); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(HasAt.class).getInternalName(), "at", "()Ljava/lang/String;"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); Label l3 = new Label(); mv.visitLabel(l3); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitLdcInsn("\n"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); Label l4 = new Label(); mv.visitJumpInsn(Opcodes.GOTO, l4); mv.visitLabel(l1); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitLdcInsn("Not within anything.\n"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); mv.visitLabel(l4); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, className, "verboseDump", "Z"); Label l5 = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, l5); int linebreaks = dumpFields(mv, className, EnvironmentClass.FVALUE, symbolNames, 0); dumpFields(mv, className, EnvironmentClass.FTYPE, symbolNames, linebreaks); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitLdcInsn("\n"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); mv.visitLabel(l5); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitInsn(Opcodes.ARETURN); Label l9 = new Label(); mv.visitLabel(l9); mv.visitLocalVariable("this", Naming.internalToDesc(className), null, l0, l9, 0); mv.visitLocalVariable("a", "Ljava/lang/Appendable;", null, l0, l9, 1); // See comment above on ClassWriter.COMPUTE_FRAMES mv.visitMaxs(2, 2); mv.visitEnd(); }
From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java
License:Open Source License
private static int dumpFields(MethodVisitor mv, String className, EnvironmentClass eClass, EnvSymbolNames symbolNames, int linebreaks) { for (String fieldName : symbolNames.getSymbolNames(eClass)) { Label l6 = new Label(); mv.visitLabel(l6);/*from w ww. java 2 s .co m*/ mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitLdcInsn("(" + fieldName + " = "); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); Label l7 = new Label(); mv.visitLabel(l7); mv.visitVarInsn(Opcodes.ALOAD, 0); String idString = fieldName + eClass.namespace(); mv.visitFieldInsn(Opcodes.GETFIELD, className, Naming.mangleIdentifier(idString), eClass.descriptor()); Label l8 = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, l8); Label l9 = new Label(); mv.visitLabel(l9); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, className, Naming.mangleIdentifier(idString), eClass.descriptor()); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, eClass.internalName(), "toString", "()Ljava/lang/String;"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); Label afterNull = new Label(); mv.visitJumpInsn(Opcodes.GOTO, afterNull); mv.visitLabel(l8); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitLdcInsn("null"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); mv.visitLabel(afterNull); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitLdcInsn(") "); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); linebreaks = (linebreaks + 1) % 5; if (linebreaks == 0) { mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitLdcInsn("\n"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Appendable", "append", "(Ljava/lang/CharSequence;)Ljava/lang/Appendable;"); mv.visitInsn(Opcodes.POP); } } return linebreaks; }
From source file:com.trigersoft.jaque.expression.ExpressionMethodVisitor.java
License:Apache License
@Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { Expression e;//from w w w . j a v a2 s .c o m switch (opcode) { case Opcodes.GETFIELD: try { e = Expression.get(_exprStack.pop(), name); } catch (NoSuchFieldException nsfe) { throw new RuntimeException(nsfe); } break; case Opcodes.GETSTATIC: try { e = Expression.get(_classVisitor.getClass(Type.getObjectType(owner)), name); } catch (NoSuchFieldException nsfe) { throw new RuntimeException(nsfe); } break; case Opcodes.PUTFIELD: case Opcodes.PUTSTATIC: default: throw notLambda(opcode); } _exprStack.push(e); }
From source file:com.yahoo.yqlplus.engine.internal.bytecode.ASMClassSourceTest.java
@Test public void requireInjectedConstant() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { ASMClassSource source = createASMClassSource(); final ToyClass toy = new ToyClass("toy", source); toy.createField("a", source.constant(source.adaptInternal(Hat.class), new Hat())); MethodGenerator method = toy.createMethod("compute"); method.setReturnType(BaseTypeAdapter.INT32); method.add(new BytecodeSequence() { @Override/*from w ww . jav a2 s.c o m*/ public void generate(CodeEmitter code) { toy.getField("a").get(code.getLocal("this").read()).read().generate(code); code.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(Hat.class), "VALUE", Type.INT_TYPE.getDescriptor()); code.getMethodVisitor().visitInsn(Opcodes.IRETURN); } }); source.build(); Class<? extends Computor> clazz = (Class<? extends Computor>) toy.getGeneratedClass(); try { Computor foo = clazz.newInstance(); Assert.assertEquals(foo.compute(), 1); } catch (VerifyError e) { toy.trace(System.err); throw e; } }
From source file:com.yahoo.yqlplus.engine.internal.bytecode.ASMClassSourceTest.java
@Test public void requireInjector() throws Exception { final ASMClassSource source = createASMClassSource(); final UnitGenerator toy = new UnitGenerator("toy", source); toy.addInterface(Computor.class); FieldDefinition field = toy.createField(source.adaptInternal(Hat.class), "hat"); field.addModifier(Modifier.FINAL); ConstructorGenerator gen = toy.createConstructor(); gen.annotate(Inject.class); AssignableValue thisValue = gen.getLocal("this"); AssignableValue hatArg = gen.addArgument("hat", source.adaptInternal(Hat.class)); gen.add(field.get(thisValue.read()).write(hatArg.read())); MethodGenerator method = toy.createMethod("compute"); method.setReturnType(BaseTypeAdapter.INT32); method.add(new BytecodeSequence() { @Override/*from w w w . j a v a 2 s .co m*/ public void generate(CodeEmitter code) { toy.getField("hat").get(code.getLocal("this").read()).read().generate(code); code.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(Hat.class), "VALUE", Type.INT_TYPE.getDescriptor()); code.getMethodVisitor().visitInsn(Opcodes.IRETURN); } }); source.build(); Class<? extends Computor> clazz = (Class<? extends Computor>) toy.getGeneratedClass(); Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Hat.class).toInstance(new Hat()); } }); try { Computor foo = injector.getInstance(clazz); Assert.assertEquals(foo.compute(), 1); } catch (VerifyError e) { toy.trace(System.err); throw e; } }
From source file:com.yahoo.yqlplus.engine.internal.bytecode.ASMClassSourceTest.java
@Test public void requireInjectorField() throws Exception { final ASMClassSource source = createASMClassSource(); final UnitGenerator toy = new UnitGenerator("toy", source); toy.addInterface(Computor.class); FieldDefinition field = toy.createField(source.adaptInternal(Hat.class), "hat"); field.addModifier(Modifier.FINAL); field.annotate(Inject.class); MethodGenerator method = toy.createMethod("compute"); method.setReturnType(BaseTypeAdapter.INT32); method.add(new BytecodeSequence() { @Override//from w ww .j a va 2 s.c o m public void generate(CodeEmitter code) { toy.getField("hat").get(code.getLocal("this").read()).read().generate(code); code.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(Hat.class), "VALUE", Type.INT_TYPE.getDescriptor()); code.getMethodVisitor().visitInsn(Opcodes.IRETURN); } }); source.build(); Class<? extends Computor> clazz = (Class<? extends Computor>) toy.getGeneratedClass(); Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Hat.class).toInstance(new Hat()); } }); try { Computor foo = injector.getInstance(clazz); Assert.assertEquals(foo.compute(), 1); } catch (VerifyError e) { toy.trace(System.err); throw e; } }