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:com.yahoo.yqlplus.engine.internal.bytecode.ASMClassSourceTest.java
@Test public void requireFields() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { ASMClassSource source = createASMClassSource(); final ToyClass toy = new ToyClass("toy", source); toy.createField("a", source.constant(BaseTypeAdapter.INT32, 1)); toy.createField("b", source.constant(BaseTypeAdapter.INT32, 2)); MethodGenerator method = toy.createMethod("compute"); method.setReturnType(BaseTypeAdapter.INT32); method.add(new BytecodeSequence() { @Override/*w w w .j av a 2 s . c om*/ public void generate(CodeEmitter code) { toy.getField("a").get(code.getLocal("this").read()) .write(new BaseTypeExpression(BaseTypeAdapter.INT32) { @Override public void generate(CodeEmitter code) { toy.getField("a").get(code.getLocal("this").read()).read().generate(code); code.emitIntConstant(1); code.getMethodVisitor().visitInsn(Opcodes.IADD); } }).generate(code); toy.getField("a").get(code.getLocal("this").read()).read().generate(code); toy.getField("b").get(code.getLocal("this").read()).read().generate(code); code.getMethodVisitor().visitInsn(Opcodes.IADD); 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(), 4); Assert.assertEquals(foo.compute(), 5); } catch (VerifyError e) { toy.trace(System.err); throw e; } }
From source file:com.yahoo.yqlplus.engine.internal.bytecode.ASMClassSourceTest.java
@Test public void requireBoxedConstant() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { final ASMClassSource source = createASMClassSource(); ToyClass toy = new ToyClass("toy", source); MethodGenerator method = toy.createMethod("compute"); method.setReturnType(BaseTypeAdapter.INT32); method.add(new BytecodeSequence() { @Override//from w w w. j a v a 2s . c om public void generate(CodeEmitter code) { source.constant(BaseTypeAdapter.INT32.boxed(), 1).generate(code); code.unbox(BaseTypeAdapter.INT32.boxed()); code.getMethodVisitor().visitInsn(Opcodes.IRETURN); } }); source.build(); Class<? extends Computor> clazz = (Class<? extends Computor>) toy.getGeneratedClass(); Computor foo = clazz.newInstance(); Assert.assertEquals(foo.compute(), 1); }
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/* w w w . j a v a2s. co 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 testStruct() throws Exception { YQLStructType structType = YQLStructType.builder().addField("a", YQLBaseType.INT32).build(); final ASMClassSource source = createASMClassSource(); final TypeWidget struct = source.resolveStruct(structType); final ToyClass toy = new ToyClass("toy", source); MethodGenerator method = toy.createMethod("compute"); method.setReturnType(BaseTypeAdapter.INT32); method.add(new BytecodeSequence() { @Override/*from ww w .j av a2 s .c om*/ public void generate(CodeEmitter code) { AssignableValue local = code.allocate(struct, "s"); local.write(struct.construct()).generate(code); AssignableValue property = struct.getPropertyAdapter().property(local.read(), "a"); property.write(source.constant(BaseTypeAdapter.INT32, 1)).generate(code); property.read().generate(code); 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 av a 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; } }
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/* w w w . j a v a2 s.c om*/ 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 requirePredicate() throws Exception { final ASMClassSource source = createASMClassSource(); final UnitGenerator toy = new UnitGenerator("toy", source); toy.addInterface(Predicate.class); MethodGenerator method = toy.createMethod("apply"); method.setReturnType(BaseTypeAdapter.BOOLEAN); method.addArgument("input", BaseTypeAdapter.ANY); method.add(new BytecodeSequence() { @Override//w ww . j av a 2 s . c o m public void generate(CodeEmitter code) { source.constant(BaseTypeAdapter.BOOLEAN, true).generate(code); code.getMethodVisitor().visitInsn(Opcodes.IRETURN); } }); source.build(); Class<? extends Predicate> clazz = (Class<? extends Predicate>) toy.getGeneratedClass(); try { Predicate<Object> foo = clazz.newInstance(); Assert.assertTrue(foo.apply(1)); } catch (VerifyError e) { toy.trace(System.err); throw e; } }
From source file:com.yahoo.yqlplus.engine.internal.bytecode.ASMClassSourceTest.java
@Test public void requirePredicateBoxed() throws Exception { final ASMClassSource source = createASMClassSource(); final UnitGenerator toy = new UnitGenerator("toy", source); toy.addInterface(Predicate.class); MethodGenerator method = toy.createMethod("apply"); method.setReturnType(BaseTypeAdapter.BOOLEAN); method.addArgument("input", BaseTypeAdapter.ANY); method.add(new BytecodeSequence() { @Override/*from w w w. j a v a 2s .c o m*/ public void generate(CodeEmitter code) { source.constant(BaseTypeAdapter.BOOLEAN.boxed(), true).generate(code); code.unbox(BaseTypeAdapter.BOOLEAN.boxed()); code.getMethodVisitor().visitInsn(Opcodes.IRETURN); } }); source.build(); Class<? extends Predicate> clazz = (Class<? extends Predicate>) toy.getGeneratedClass(); try { Predicate<Object> foo = clazz.newInstance(); Assert.assertTrue(foo.apply(1)); } catch (VerifyError e) { toy.trace(System.err); throw e; } }
From source file:com.yahoo.yqlplus.engine.internal.bytecode.ReturnCode.java
public ReturnCode(TypeWidget type) { this.op = type.getJVMType().getOpcode(Opcodes.IRETURN); }
From source file:com.yahoo.yqlplus.engine.internal.compiler.MethodGenerator.java
public void returnValue(BytecodeExpression result) { setReturnType(result.getType()); add(result); add(new ReturnCode(result.getType().getJVMType().getOpcode(Opcodes.IRETURN))); }