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.mastercard.test.spring.security.SpringSecurityJUnit4ClassRunner.java

/**
 * Locate repeated annoations within a Java 8 annotation container.  If the annotation
 * provided is a container for annotations with the @Repeatable annotation, then the contained
 * annotations are returned./*from ww w.j av  a  2  s  .co m*/
 * @param annotation The annotation to investigate for repeated annotations.
 * @return A list containing repeated annotations if present.
 */
private List<Annotation> findRepeatableAnnotations(Annotation annotation) {
    List<Annotation> retVal = new ArrayList<>();

    for (Method method : annotation.annotationType().getMethods()) {
        if ("value".equals(method.getName())) {
            if (method.getReturnType().isArray()) {
                try {
                    Annotation[] types = (Annotation[]) method.invoke(annotation);
                    retVal.addAll(Arrays.asList(types));
                } catch (IllegalAccessException | InvocationTargetException | ClassCastException e) {
                    //ignore, must not be a container for annotations
                }
            }
            break;
        }
    }

    return retVal;
}

From source file:io.milton.http.annotated.AbstractAnnotationHandler.java

private boolean isReturnTypeMatch(java.lang.reflect.Method method, Class returnType) {
    if (returnType == null) {
        return true;
    } else {//from   w w w  .jav a  2  s  .com
        return returnType.isAssignableFrom(method.getReturnType());
    }
}

From source file:com.wavemaker.tools.javaservice.JavaServiceDefinition.java

private void initOperation(Method method, TypeState typeState) {

    ServiceOperation so = new ServiceOperation();
    so.setName(method.getName());//from w w  w .  j a  v  a  2s  .  c o m

    if (!method.getReturnType().equals(void.class)) {
        so.setReturnType(ReflectTypeUtils.getFieldDefinition(method, typeState, false, null));
        checkAddType(so.getReturnType().getTypeDefinition());
    }

    List<String> paramNames = ServerUtils.getParameterNames(method);
    Type[] types = method.getGenericParameterTypes();
    List<FieldDefinition> params = new ArrayList<FieldDefinition>(types.length);
    so.setParameterTypes(params);
    for (int i = 0; i < types.length; i++) {
        params.add(ReflectTypeUtils.getFieldDefinition(types[i], typeState, false, paramNames.get(i)));
        checkAddType(params.get(i).getTypeDefinition());
    }

    this.operations.add(so);
}

From source file:com.googlecode.jdbcproc.daofactory.impl.block.service.OutputParametersGetterBlockServiceImpl.java

public IOutputParametersGetterBlock create(ParameterConverterService converterService, Method daoMethod,
        StoredProcedureInfo procedureInfo) {

    // List.getAll();
    if (BlockFactoryUtils.isGetAllMethod(daoMethod, procedureInfo)) {
        return null;

        // CREATE ENTITY
    } else if (isCreateEntityMethod(daoMethod, procedureInfo)) {
        int entityParameterIndex = findEntityParameterIndex(daoMethod);
        List<EntityPropertySetter> setters = new LinkedList<>();
        Class entityClass = daoMethod.getParameterTypes()[entityParameterIndex];
        for (StoredProcedureArgumentInfo argumentInfo : procedureInfo.getArguments()) {
            if (argumentInfo.isOutputParameter()) {
                Method getterMethod = BlockFactoryUtils.findGetterMethod(entityClass, argumentInfo);
                Method setterMethod = BlockFactoryUtils.findSetterMethod(entityClass, getterMethod);
                IParameterConverter paramConverter = converterService.getConverter(argumentInfo.getDataType(),
                        getterMethod.getReturnType());
                setters.add(new EntityPropertySetter(setterMethod, paramConverter, argumentInfo.getColumnName(),
                        argumentInfo.getStatementArgument(), argumentInfo.getDataType()));
            }/* w w  w .j av a2  s.  c o m*/
        }
        return setters.size() > 0 ? new OutputParametersGetterBlockEntity(setters, entityParameterIndex) : null;

        // RETURN ONE OUTPUT PARAMETER
    } else if (BlockFactoryUtils.isOneOutputHasReturn(daoMethod, procedureInfo)) {
        return createOutputParameterHasReturn(converterService, daoMethod, procedureInfo);

        // DEFAULT NO     
    } else {
        return null;
    }
}

From source file:org.brushingbits.jnap.struts2.config.RestControllerConfigBuilder.java

/**
 * TODO//from  w  w w  .j  av  a 2  s  . com
 * 
 * @param controllerClass The <code>Controller</code> class to search methods for.
 * @return a map containing the method's name as the key and its configuration as the value.
 * 
 * @see org.apache.struts2.convention.annotation.Action
 * @see javax.ws.rs.Path
 * @see javax.ws.rs.HttpMethod
 * @see javax.ws.rs.GET
 * @see javax.ws.rs.POST
 * @see javax.ws.rs.PUT
 * @see javax.ws.rs.DELETE
 * @see javax.ws.rs.Consumes
 * @see javax.ws.rs.Produces
 */
protected Map<String, ActionMethodConfigBean> findActionMethods(Class<?> controllerClass) {
    Map<String, ActionMethodConfigBean> map = new HashMap<String, ActionMethodConfigBean>();
    Method[] methods = controllerClass.getMethods();
    for (Method method : methods) {

        // first of all, lets validate the candidate...
        Class<?> returnType = method.getReturnType();
        if (returnType == null
                || (!returnType.equals(String.class) && !returnType.isAssignableFrom(Response.class))
                || method.getParameterTypes().length > 0 || Modifier.isStatic(method.getModifiers())) {
            continue;
        }

        ActionMethodConfigBean methodConfig = new ActionMethodConfigBean();
        methodConfig.setName(method.getName());

        // Guessing http method
        String httpMethod = this.guessHttpMethod(method).value();
        methodConfig.setHttpMethod(httpMethod);

        // if a @Action annotation is found then keep it for results, interceptors and so to be configured properly
        final Action actionAnnotation = ReflectionUtils.getAnnotation(Action.class, method);
        if (actionAnnotation != null) {
            methodConfig.setAction(actionAnnotation);
            methodConfig.setUriTemplate(actionAnnotation.value().trim());
        }

        // if a @Path annotation is found then its value overwrites the @Action one (if found too)
        final Path pathAnnotation = ReflectionUtils.getAnnotation(Path.class, method);
        if (pathAnnotation != null) {
            methodConfig.setUriTemplate(pathAnnotation.value().trim());
        }

        // are there any restrinctions for response type
        //         methodConfig.setProduces(method.getAnnotation(Produces.class));
        // TODO @Consumes

        // if no URI template found, then guess based on method's name
        if (StringUtils.isBlank(methodConfig.getUriTemplate())) {
            // TODO filter methods
            //methodConfig.setUriTemplate(actionNameBuilder.build(method.getName()));
        } else {
            map.put(method.getName(), methodConfig);
        }

        //         map.put(method.getName(), methodConfig); TODO remove 'else' above after impl method filtering
    }
    return map;
}

From source file:com.proteanplatform.protean.entity.EntityTableCellMetadata.java

/**
 * @param clazz/*from w  ww.j  a v a 2s.  c  om*/
 * @param anno
 * @throws NoSuchMethodException 
 */
public EntityTableCellMetadata(String name, Class<?> clazz) throws NoSuchMethodException {

    Method method = null;
    Class<?> rtype = clazz;

    String[] tokens = name.split("\\.");
    for (int i = 0; i < tokens.length - 1; ++i) {
        method = rtype.getMethod("get" + StringUtils.capitalize(tokens[i]), new Class[0]);
        methods.add(method);
        rtype = method.getReturnType();
    }

    method = rtype.getMethod("get" + StringUtils.capitalize(tokens[tokens.length - 1]), new Class[0]);
    rtype = method.getReturnType();
    String type = "string"; // determines sortability

    // have to do string, date first, since they can both assign object.
    if (String.class.isAssignableFrom(rtype)) {
        methods.add(method);
    }

    else if (Date.class.isAssignableFrom(rtype)) {
        methods.add(method);
        type = "date";
    }

    else if (rtype == boolean.class || rtype == char.class || rtype == Boolean.class
            || rtype == Character.class) {
        methods.add(method);
    }

    else if (rtype.isPrimitive() || rtype == int.class || rtype == Integer.class || rtype == Long.class
            || rtype == Short.class || rtype == Byte.class || rtype == Double.class || rtype == Float.class) {
        methods.add(method);
        type = "numeric";
    }

    else if (Object.class.isAssignableFrom(rtype)) {
        calculateObjectCell(method);
    }

    column = new TableColumn(name, type, Sorting.BOTH);

}

From source file:com.px100systems.util.PropertyAccessor.java

/**
 * Constructor//from ww w. ja  va  2 s . c  om
 * @param srcClass class
 * @param srcProperty property
 */
public PropertyAccessor(Class<?> srcClass, String srcProperty) {
    accessorMethods = new ArrayList<Method>();
    mutatorMethods = new ArrayList<Method>();

    String[] properties = srcProperty.split("\\.");
    lastClass = srcClass;
    for (int i = 0; i < properties.length; i++) {
        lastField = properties[i];

        Method getter = ReflectionUtils.findMethod(lastClass, methodName("get", lastField));
        if (getter == null)
            throw new RuntimeException("Couldn't find getter for " + lastField + " in " + lastClass.getName());
        accessorMethods.add(getter);

        Class<?> fieldType = getter.getReturnType();

        if (mutatorMethods != null) {
            Method setter = ReflectionUtils.findMethod(lastClass, methodName("set", lastField), fieldType);
            if (setter != null)
                mutatorMethods.add(setter);
            else
                mutatorMethods = null;
        }

        if (i < properties.length - 1)
            lastClass = fieldType;
        else
            lastType = fieldType;
    }
}

From source file:ca.uhn.fhir.rest.method.BaseOutcomeReturningMethodBinding.java

public BaseOutcomeReturningMethodBinding(Method theMethod, FhirContext theContext, Class<?> theMethodAnnotation,
        Object theProvider) {//from   w w w  . j av  a2s  .c om
    super(theMethod, theContext, theProvider);

    if (!theMethod.getReturnType().equals(MethodOutcome.class)) {
        if (!allowVoidReturnType()) {
            throw new ConfigurationException("Method " + theMethod.getName() + " in type "
                    + theMethod.getDeclaringClass().getCanonicalName() + " is a @"
                    + theMethodAnnotation.getSimpleName() + " method but it does not return "
                    + MethodOutcome.class);
        } else if (theMethod.getReturnType() == void.class) {
            myReturnVoid = true;
        }
    }
}

From source file:com.all.app.DefaultContext.java

private void checkInvokePredestroy(Method method, Object ob) {
    boolean annotationPresent = false;
    boolean noArguments = method.getParameterTypes().length == 0;
    boolean isVoid = method.getReturnType().equals(void.class);
    boolean isPublic = (method.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC;
    boolean isNotStatic = (method.getModifiers() & Modifier.STATIC) != Modifier.STATIC;
    Annotation[] annotations = method.getAnnotations();
    for (Annotation annotation : annotations) {
        if (annotation.getClass().getName().contains("PreDestroy")
                || annotation.toString().contains("PreDestroy")) {
            annotationPresent = true;/*www.ja v a 2s  .c  o  m*/
        }
    }
    if (annotationPresent && noArguments && isPublic && isNotStatic && isVoid) {
        try {
            method.invoke(ob);
        } catch (Exception e) {
            log.error(e, e);
        }
    }

}

From source file:com.all.shared.util.JsonConverterPrimitiveCompliance.java

public void checkGetterAndSetter(Method getter, Method setter, Object o)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Class<?> type = getter.getReturnType();
    String primitiveClazz = capitalize(type.getSimpleName());
    for (Object value : Primitives.valueOf(primitiveClazz).testValues()) {
        checkGetterAndSetter(getter, setter, o, value);
    }/*from www .  ja va2  s  . c  o  m*/

}