List of usage examples for org.objectweb.asm.commons Method getReturnType
public Type getReturnType()
From source file:com.google.code.nanorm.internal.introspect.asm.AccessorBuilder.java
License:Apache License
/** * {@inheritDoc}//from w w w . j a v a 2 s. c o m */ public Class<?> visitProperty(int pos, String property, java.lang.reflect.Method getter, boolean hasNext, Class<?> beanClass) { checkNull(pos); // If expected class is not equal to the type on top of the stack, do // the cast if (declaredClass != beanClass) { accessormg.checkCast(Type.getType(beanClass)); } if (!hasNext && isSetter) { java.lang.reflect.Method setter = IntrospectUtils.findSetter(beanClass, property); Type type = Type.getType(setter.getParameterTypes()[0]); // Cast parameter to required type accessormg.loadArg(1); accessormg.unbox(type); Method method = new Method(setter.getName(), Type.VOID_TYPE, new Type[] { type }); accessormg.invokeVirtual(Type.getType(beanClass), method); } else { Type type = Type.getType(getter.getReturnType()); Method method = new Method(getter.getName(), type, new Type[0]); accessormg.invokeVirtual(Type.getType(beanClass), method); // If this is last property in the path, box the result to match // Getter#getValue return value if (!hasNext) { accessormg.box(type); } // Remember type on top of the stack declaredClass = getter.getReturnType(); } return null; }
From source file:com.google.code.nanorm.internal.introspect.asm.MapperBuilder.java
License:Apache License
/** * Generate mapper method.// w w w .j a v a 2 s .c om * * @param owner self type * @param cw class writer * @param config method configuration */ private static void visitMethod(Type owner, ClassWriter cw, MethodConfig config) { java.lang.reflect.Method ifaceMethod = config.getMethod(); Type returnType = Type.getType(ifaceMethod.getReturnType()); Type[] args = new Type[ifaceMethod.getParameterTypes().length]; for (int i = 0; i < args.length; ++i) { args[i] = Type.getType(ifaceMethod.getParameterTypes()[i]); } Method method = new Method(ifaceMethod.getName(), returnType, args); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, null, null, cw); // Factory mg.loadThis(); mg.getField(owner, "delegate", QUERY_DELEGATE_TYPE); // Statement config mg.loadThis(); mg.getField(owner, "configs", STATEMENT_CONFIGS_ARR_TYPE); mg.push(config.getIndex()); mg.arrayLoad(Type.getType(StatementConfig.class)); // Arguments mg.loadArgArray(); mg.invokeInterface(QUERY_DELEGATE_TYPE, QUERY_METHOD); mg.unbox(returnType); mg.returnValue(); mg.endMethod(); // Copy the annotations copyAnnotations(ifaceMethod, null, mg); }
From source file:com.google.gwt.dev.shell.rewrite.RewriteSingleJsoImplDispatches.java
License:Apache License
/** * For regular Java objects that implement a SingleJsoImpl interface, write * instance trampoline dispatchers for mangled method names to the * implementing method.// w w w . j av a2 s .c o m */ private void writeTrampoline(String stubIntr) { /* * This is almost the same kind of trampoline as the ones generated in * WriteJsoImpl, however there are enough small differences between the * semantics of the dispatches that would make a common implementation far * more awkward than the duplication of code. */ for (String mangledName : toImplement(stubIntr)) { for (Method method : jsoData.getDeclarations(mangledName)) { Method toCall = new Method(method.getName(), method.getDescriptor()); // Must not be final MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, mangledName, method.getDescriptor(), null, null); if (mv != null) { mv.visitCode(); /* * It just so happens that the stack and local variable sizes are the * same, but they're kept distinct to aid in clarity should the * dispatch logic change. * * These start at 1 because we need to load "this" onto the stack */ int var = 1; int size = 1; // load this mv.visitVarInsn(Opcodes.ALOAD, 0); // then the rest of the arguments for (Type t : toCall.getArgumentTypes()) { size += t.getSize(); mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), var); var += t.getSize(); } // Make sure there's enough room for the return value size = Math.max(size, toCall.getReturnType().getSize()); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, currentTypeName, toCall.getName(), toCall.getDescriptor(), false); mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN)); mv.visitMaxs(size, var); mv.visitEnd(); } } } }
From source file:com.google.template.soy.jbcsrc.ConstructorRef.java
License:Apache License
/** * Returns a new {@link ConstructorRef} that refers to a constructor on the given type with the * given parameter types.//from w w w . ja v a2 s. com */ static ConstructorRef create(TypeInfo type, Method init) { checkArgument(init.getName().equals("<init>") && init.getReturnType().equals(Type.VOID_TYPE), "'%s' is not a valid constructor", init); return new AutoValue_ConstructorRef(type, init, ImmutableList.copyOf(init.getArgumentTypes())); }
From source file:com.google.template.soy.jbcsrc.ExpressionTester.java
License:Apache License
private static ClassData createClass(Class<? extends Invoker> targetInterface, Expression expr) { java.lang.reflect.Method invokeMethod; try {//from ww w . j a va2 s .c o m invokeMethod = targetInterface.getMethod("invoke"); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } Class<?> returnType = invokeMethod.getReturnType(); if (!Type.getType(returnType).equals(expr.resultType())) { if (!returnType.equals(Object.class) || expr.resultType().getSort() != Type.OBJECT) { throw new IllegalArgumentException(targetInterface + " is not appropriate for this expression"); } } TypeInfo generatedType = TypeInfo.create( ExpressionTester.class.getPackage().getName() + "." + targetInterface.getSimpleName() + "Impl"); SoyClassWriter cw = SoyClassWriter.builder(generatedType) .setAccess(Opcodes.ACC_FINAL | Opcodes.ACC_SUPER | Opcodes.ACC_PUBLIC) .implementing(TypeInfo.create(targetInterface)).build(); BytecodeUtils.defineDefaultConstructor(cw, generatedType); Method invoke = Method.getMethod(invokeMethod); Statement.returnExpression(expr).writeMethod(Opcodes.ACC_PUBLIC, invoke, cw); Method voidInvoke; try { voidInvoke = Method.getMethod(Invoker.class.getMethod("voidInvoke")); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); // this method definitely exists } Statement.concat(LocalVariable.createThisVar(generatedType, new Label(), new Label()) .invoke(MethodRef.create(invokeMethod)).toStatement(), new Statement() { @Override void doGen(CodeBuilder adapter) { adapter.visitInsn(Opcodes.RETURN); } }).writeMethod(Opcodes.ACC_PUBLIC, voidInvoke, cw); ClassData data = cw.toClassData(); checkClassData(data); return data; }
From source file:com.google.template.soy.jbcsrc.MethodRef.java
License:Apache License
static MethodRef create(java.lang.reflect.Method method) { Class<?> clazz = method.getDeclaringClass(); TypeInfo ownerType = TypeInfo.create(method.getDeclaringClass()); boolean isStatic = Modifier.isStatic(method.getModifiers()); ImmutableList<Type> argTypes; if (isStatic) { argTypes = ImmutableList.copyOf(Type.getArgumentTypes(method)); } else {//from w w w . j a v a 2s. c om // for instance methods the first 'argument' is always an instance of the class. argTypes = ImmutableList.<Type>builder().add(ownerType.type()).add(Type.getArgumentTypes(method)) .build(); } return new AutoValue_MethodRef( clazz.isInterface() ? Opcodes.INVOKEINTERFACE : isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, ownerType, org.objectweb.asm.commons.Method.getMethod(method), Type.getType(method.getReturnType()), argTypes, Features.of()); }
From source file:com.google.template.soy.jbcsrc.MethodRef.java
License:Apache License
static MethodRef createInstanceMethod(TypeInfo owner, Method method) { return new AutoValue_MethodRef(Opcodes.INVOKEVIRTUAL, owner, method, method.getReturnType(), ImmutableList.<Type>builder().add(owner.type()).add(method.getArgumentTypes()).build(), Features.of());/*from www . j a v a2 s . c om*/ }
From source file:com.google.template.soy.jbcsrc.restricted.ConstructorRef.java
License:Apache License
/** * Returns a new {@link ConstructorRef} that refers to a constructor on the given type with the * given parameter types./*w w w. java2 s . com*/ */ public static ConstructorRef create(TypeInfo type, Method init) { checkArgument(init.getName().equals("<init>") && init.getReturnType().equals(Type.VOID_TYPE), "'%s' is not a valid constructor", init); return new AutoValue_ConstructorRef(type, init, ImmutableList.copyOf(init.getArgumentTypes())); }
From source file:com.google.template.soy.jbcsrc.restricted.MethodRef.java
License:Apache License
public static MethodRef create(java.lang.reflect.Method method) { Class<?> clazz = method.getDeclaringClass(); TypeInfo ownerType = TypeInfo.create(method.getDeclaringClass()); boolean isStatic = Modifier.isStatic(method.getModifiers()); ImmutableList<Type> argTypes; if (isStatic) { argTypes = ImmutableList.copyOf(Type.getArgumentTypes(method)); } else {//from w w w.j a v a 2 s .c om // for instance methods the first 'argument' is always an instance of the class. argTypes = ImmutableList.<Type>builder().add(ownerType.type()).add(Type.getArgumentTypes(method)) .build(); } return new AutoValue_MethodRef( clazz.isInterface() ? Opcodes.INVOKEINTERFACE : isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, ownerType, Method.getMethod(method), Type.getType(method.getReturnType()), argTypes, Features.of()); }
From source file:com.google.template.soy.jbcsrc.restricted.MethodRef.java
License:Apache License
public static MethodRef createInstanceMethod(TypeInfo owner, Method method) { return new AutoValue_MethodRef(Opcodes.INVOKEVIRTUAL, owner, method, method.getReturnType(), ImmutableList.<Type>builder().add(owner.type()).add(method.getArgumentTypes()).build(), Features.of());/* w w w .j a v a 2 s. c o m*/ }