Example usage for java.lang.reflect Method getReturnType

List of usage examples for java.lang.reflect Method getReturnType

Introduction

In this page you can find the example usage for java.lang.reflect Method getReturnType.

Prototype

public Class<?> getReturnType() 

Source Link

Document

Returns a Class object that represents the formal return type of the method represented by this Method object.

Usage

From source file:com.zenesis.qx.remote.ProxyMethod.java

/**
 * @param name/* w ww. j  av a  2  s .c  o m*/
 * @param returnType
 * @param parameters
 */
public ProxyMethod(Method method) {
    super();
    this.method = method;

    Class returnType = method.getReturnType();
    Class keyType = String.class;
    boolean prefetchResult = false;
    boolean cacheResult = false;
    isMap = Map.class.isAssignableFrom(returnType);
    com.zenesis.qx.remote.annotations.Method anno = method
            .getAnnotation(com.zenesis.qx.remote.annotations.Method.class);

    if (returnType.isArray() || Iterable.class.isAssignableFrom(returnType) || isMap) {
        // How to present on the client - only ArrayList by default is wrapped on the client
        Remote.Array array;
        if (returnType.isArray()) {
            returnType = returnType.getComponentType();
            array = Remote.Array.NATIVE;
        } else {
            returnType = Object.class;
            array = Remote.Array.WRAP;
        }

        // Component type
        if (anno != null) {
            if (anno.array() != Remote.Array.DEFAULT)
                array = anno.array();
            if (anno.arrayType() != Object.class)
                returnType = anno.arrayType();
            if (anno.keyType() != Object.class)
                keyType = anno.keyType();
        }
        this.array = array;
        this.arrayType = returnType;
    } else {
        array = null;
        this.arrayType = null;
    }

    if (anno != null) {
        if (method.getParameterTypes().length == 0) {
            prefetchResult = anno.prefetchResult();
            cacheResult = anno.cacheResult() || prefetchResult;
        }
    }

    this.keyType = keyType;
    this.prefetchResult = prefetchResult;
    this.staticMethod = (method.getModifiers() & Modifier.STATIC) != 0;
    if (staticMethod && cacheResult) {
        log.warn("Cannot cacheResult on static method " + method);
        cacheResult = false;
    }
    this.cacheResult = cacheResult;
}

From source file:com.futureplatforms.kirin.internal.attic.ProxyGenerator.java

public <T> T javascriptProxyForResponse(final JSONObject obj, Class<T> baseInterface,
        Class<?>... otherClasses) {
    Class<?>[] allClasses;//from   w w w . j a va 2s .  c o m

    if (otherClasses.length == 0) {
        allClasses = new Class[] { baseInterface };
    } else {
        allClasses = new Class[otherClasses.length + 1];
        allClasses[0] = baseInterface;
        System.arraycopy(otherClasses, 0, allClasses, 1, otherClasses.length);
    }
    InvocationHandler h = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            String methodName = method.getName();
            Class<?> returnType = method.getReturnType();
            if (void.class.equals(returnType) && args.length == 1) {
                String propertyName = findSetter(methodName);
                if (propertyName != null) {
                    handleSetter(obj, propertyName, args[0]);
                }
            }

            String propertyName = findGetter(methodName);
            if (propertyName != null) {
                return handleGetter(obj, returnType, propertyName);
            }

            if ("toString".equals(methodName) && String.class.equals(returnType)) {
                return obj.toString();
            }

            return null;
        }

    };
    Object proxy = Proxy.newProxyInstance(baseInterface.getClassLoader(), allClasses, h);
    return baseInterface.cast(proxy);
}

From source file:com.netflix.hystrix.contrib.javanica.utils.FallbackMethod.java

public FallbackMethod(Method method, boolean extended) {
    this.method = method;
    this.extended = extended;
    if (method != null) {
        this.executionType = ExecutionType.getExecutionType(method.getReturnType());
    }/*w w w. j  a  v  a 2 s. c om*/
}

From source file:com.haulmont.cuba.web.gui.components.table.LinkCellClickListener.java

protected Method findLinkInvokeMethod(Class cls, String methodName) {
    Method exactMethod = MethodUtils.getAccessibleMethod(cls, methodName,
            new Class[] { Entity.class, String.class });
    if (exactMethod != null) {
        return exactMethod;
    }/*from   www  .  j a v  a  2  s .c  om*/

    // search through all methods
    Method[] methods = cls.getMethods();
    for (Method availableMethod : methods) {
        if (availableMethod.getName().equals(methodName)) {
            if (availableMethod.getParameterCount() == 2 && Void.TYPE.equals(availableMethod.getReturnType())) {
                if (Entity.class.isAssignableFrom(availableMethod.getParameterTypes()[0])
                        && String.class == availableMethod.getParameterTypes()[1]) {
                    // get accessible version of method
                    return MethodUtils.getAccessibleMethod(availableMethod);
                }
            }
        }
    }
    return null;
}

From source file:edu.cornell.mannlib.vitro.webapp.web.beanswrappers.ReadOnlyBeansWrapper.java

@SuppressWarnings("rawtypes")
@Override/*from w  ww.  jav  a  2  s.  c  o m*/
protected void finetuneMethodAppearance(Class cls, Method method, MethodAppearanceDecision decision) {

    // How to define a setter? This is a weak approximation: a method whose name
    // starts with "set" or returns void.
    if (method.getName().startsWith("set")) {
        decision.setExposeMethodAs(null);

    } else if (method.getReturnType().getName().equals("void")) {
        decision.setExposeMethodAs(null);

    } else {

        Class<?> declaringClass = method.getDeclaringClass();
        if (declaringClass.equals(java.lang.Object.class)) {
            decision.setExposeMethodAs(null);

        } else {
            Package pkg = declaringClass.getPackage();
            if (pkg.getName().equals("java.util")) {
                decision.setExposeMethodAs(null);
            }
        }
    }
}

From source file:com.futureplatforms.kirin.internal.attic.ProxyGenerator.java

public <T> T javascriptProxyForRequest(final JSONObject obj, Class<T> baseInterface, Class<?>... otherClasses) {
    Class<?>[] allClasses;/*  www  .  j  a v a  2 s . c o m*/

    if (otherClasses.length == 0) {
        allClasses = new Class[] { baseInterface };
    } else {
        allClasses = new Class[otherClasses.length + 1];
        allClasses[0] = baseInterface;
        System.arraycopy(otherClasses, 0, allClasses, 1, otherClasses.length);
    }
    InvocationHandler h = new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            String methodName = method.getName();
            Class<?> returnType = method.getReturnType();

            if (obj.has(methodName)) {
                String id = obj.optString("__id");

                if (id != null) {
                    // so assume it's a callback
                    if (void.class.equals(returnType)) {
                        mKirinHelper.jsCallbackObjectMethod(id, methodName, (Object[]) args);
                    } else {
                        return mKirinHelper.jsSyncCallbackObjectMethod(id, returnType, methodName,
                                (Object[]) args);
                    }
                }
                return null;
            }

            String propertyName = findGetter(methodName);
            if (propertyName != null) {
                return handleGetter(obj, returnType, propertyName);
            }

            if ("toString".equals(methodName) && String.class.equals(returnType)) {
                return obj.toString();
            }

            return null;
        }
    };

    Object proxy = Proxy.newProxyInstance(baseInterface.getClassLoader(), allClasses, h);
    return baseInterface.cast(proxy);
}

From source file:com.emc.ecs.sync.config.ConfigWrapper.java

public ConfigWrapper(Class<C> targetClass) {
    try {//from   w  ww  .  j av a2 s  .c o  m
        this.targetClass = targetClass;
        if (targetClass.isAnnotationPresent(StorageConfig.class))
            this.uriPrefix = targetClass.getAnnotation(StorageConfig.class).uriPrefix();
        if (targetClass.isAnnotationPresent(FilterConfig.class))
            this.cliName = targetClass.getAnnotation(FilterConfig.class).cliName();
        if (targetClass.isAnnotationPresent(Label.class))
            this.label = targetClass.getAnnotation(Label.class).value();
        if (targetClass.isAnnotationPresent(Documentation.class))
            this.documentation = targetClass.getAnnotation(Documentation.class).value();
        BeanInfo beanInfo = Introspector.getBeanInfo(targetClass);
        for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
            if (descriptor.getReadMethod().isAnnotationPresent(Option.class)) {
                propertyMap.put(descriptor.getName(), new ConfigPropertyWrapper(descriptor));
            }
        }
        for (MethodDescriptor descriptor : beanInfo.getMethodDescriptors()) {
            Method method = descriptor.getMethod();
            if (method.isAnnotationPresent(UriParser.class)) {
                if (method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length == 1
                        && method.getParameterTypes()[0].equals(String.class)) {
                    uriParser = method;
                } else {
                    log.warn("illegal signature for @UriParser method {}.{}", targetClass.getSimpleName(),
                            method.getName());
                }
            } else if (method.isAnnotationPresent(UriGenerator.class)) {
                if (method.getReturnType().equals(String.class) && method.getParameterTypes().length == 0) {
                    uriGenerator = method;
                } else {
                    log.warn("illegal signature for @UriGenerator method {}.{}", targetClass.getSimpleName(),
                            method.getName());
                }
            }
        }
        if (propertyMap.isEmpty())
            log.info("no @Option annotations found in {}", targetClass.getSimpleName());
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.jsonschema2pojo.integration.ArrayIT.java

@Test
public void arraysCanBeMultiDimensional() throws NoSuchMethodException {

    Method getterMethod = classWithArrayProperties.getMethod("getMultiDimensionalArray");

    // assert List
    assertThat(getterMethod.getReturnType().getName(), is(List.class.getName()));
    assertThat(getterMethod.getGenericReturnType(), is(instanceOf(ParameterizedType.class)));

    // assert List<List>
    Type genericType = ((ParameterizedType) getterMethod.getGenericReturnType()).getActualTypeArguments()[0];
    assertThat(genericType, is(instanceOf(ParameterizedType.class)));
    assertThat(((Class<?>) ((ParameterizedType) genericType).getRawType()).getName(), is(List.class.getName()));

    // assert List<List<Object>>
    Type itemsType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
    assertThat(itemsType, is(instanceOf(Class.class)));
    assertThat(((Class<?>) itemsType).getName(), is(Object.class.getName()));

}

From source file:ijfx.ui.widgets.PluginInfoPane.java

private Map<String, Object> convert(Method item) {

    return Maps.newHashMap(ImmutableMap.<String, Object>builder().put("name", item.getName())
            .put("inputs", toList(item.getParameters())).put("output", item.getReturnType().getName()).build());
}

From source file:com.sulacosoft.bitcoindconnector4j.BitcoindApiHandler.java

private Object deserializeResult(Method method, Object result) {
    if (result instanceof MorphDynaBean) {
        return JSONObject.toBean(JSONObject.fromObject(JSONSerializer.toJSON((MorphDynaBean) result)),
                method.getReturnType());
    } else if (result instanceof List) {
        List<?> incomingList = (List<?>) result;
        if (incomingList.size() == 0)
            return Collections.EMPTY_LIST;

        ParameterizedType type = (ParameterizedType) method.getGenericReturnType(); // method.getGenericType();
        Class<?> clazz = (Class<?>) type.getActualTypeArguments()[0];

        ArrayList<Object> outcomingList = new ArrayList<>();
        if (incomingList.get(0) instanceof MorphDynaBean) {
            for (Object entity : incomingList) {
                Object ent = JSONObject.toBean(JSONObject.fromObject(entity), clazz);
                outcomingList.add(ent);//ww  w  .j ava 2s  . com
            }
        } else {
            outcomingList.addAll(incomingList);
        }
        return outcomingList;
    } else {
        return result;
    }
}