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.github.jknack.handlebars.internal.BaseTemplate.java

/**
 * Creates a new {@link TypeSafeTemplate}.
 *
 * @param rootType The target type.//from w w w. j  a  va  2s .c om
 * @param template The target template.
 * @return A new {@link TypeSafeTemplate}.
 */
private static Object newTypeSafeTemplate(final Class<?> rootType, final Template template) {
    return Proxy.newProxyInstance(template.getClass().getClassLoader(), new Class[] { rootType },
            new InvocationHandler() {
                private Map<String, Object> attributes = new HashMap<String, Object>();

                @Override
                public Object invoke(final Object proxy, final Method method, final Object[] args)
                        throws IOException {
                    String methodName = method.getName();
                    if ("apply".equals(methodName)) {
                        Context context = Context.newBuilder(args[0]).combine(attributes).build();
                        attributes.clear();
                        if (args.length == 2) {
                            template.apply(context, (Writer) args[1]);
                            return null;
                        }
                        return template.apply(context);
                    }

                    if (Modifier.isPublic(method.getModifiers()) && methodName.startsWith("set")) {
                        String attrName = StringUtils.uncapitalize(methodName.substring("set".length()));
                        if (args != null && args.length == 1 && attrName.length() > 0) {
                            attributes.put(attrName, args[0]);
                            if (TypeSafeTemplate.class.isAssignableFrom(method.getReturnType())) {
                                return proxy;
                            }
                            return null;
                        }
                    }
                    String message = String.format(
                            "No handler method for: '%s(%s)', expected method signature is: 'setXxx(value)'",
                            methodName, args == null ? "" : join(args, ", "));
                    throw new UnsupportedOperationException(message);
                }
            });
}

From source file:gov.nih.nci.system.web.struts.action.UpdateAction.java

private void setParameterValue(Class klass, Object instance, String name, String value) throws Exception {
    if (value != null && value.trim().length() == 0)
        return;// w ww . j a  v  a  2  s . c o m

    String paramName = name.substring(0, 1).toUpperCase() + name.substring(1);
    Method[] allMethods = klass.getMethods();
    for (Method m : allMethods) {
        String mname = m.getName();
        if (mname.equals("get" + paramName)) {
            Class type = m.getReturnType();
            Class[] argTypes = new Class[] { type };

            Method method = null;
            while (klass != Object.class) {
                try {
                    Method setMethod = klass.getDeclaredMethod("set" + paramName, argTypes);
                    setMethod.setAccessible(true);
                    setMethod.invoke(instance, convertValue(type, value));
                    break;
                } catch (NoSuchMethodException ex) {
                    klass = klass.getSuperclass();
                }
            }
        }
    }
}

From source file:com.gu.management.spring.ManagementUrlDiscoveryService.java

private boolean isRequestableMethod(Method method) {
    Class<?>[] parameterTypes = method.getParameterTypes();

    boolean isPublic = Modifier.isPublic(method.getModifiers());
    boolean isProtected = Modifier.isProtected(method.getModifiers());
    boolean returnsModelAndView = method.getReturnType().equals(ModelAndView.class);
    boolean takesRequestAndResponse = parameterTypes.length >= 2
            && HttpServletRequest.class.isAssignableFrom(parameterTypes[0])
            && HttpServletResponse.class.isAssignableFrom(parameterTypes[1]);

    return (isPublic || isProtected) && (returnsModelAndView || takesRequestAndResponse);
}

From source file:com.chiralbehaviors.CoRE.phantasm.graphql.schemas.JooqSchema.java

private GraphQLType type(PropertyDescriptor field, PhantasmProcessor processor) {
    Method readMethod = field.getReadMethod();
    return processor.typeFor(readMethod.getReturnType());
}

From source file:com.dinstone.ut.faststub.internal.StubMethodInvocation.java

/**
 * {@inheritDoc}/*from w ww.  j  a  v a 2  s. c o m*/
 * 
 */
public Object invoke(Method method, Object[] args) throws Throwable {
    ApplicationContext stubContext = getStubContext(method);
    Object retObj = stubContext.getBean(method.getName());
    // handle exception
    handleException(retObj);

    // handle array object
    Class<?> retType = method.getReturnType();
    if (retType.isArray() && retObj instanceof List<?>) {
        List<?> list = (List<?>) retObj;
        int len = list.size();
        Object arrObj = Array.newInstance(retType.getComponentType(), len);
        for (int i = 0; i < len; i++) {
            Array.set(arrObj, i, list.get(i));
        }
        return arrObj;
    }

    return retObj;
}

From source file:com.espertech.esper.metrics.jmx.CommonJMXUtil.java

private static ModelMBeanOperationInfo[] extractOperationInfo(Object object) {
    ArrayList<ModelMBeanOperationInfo> infos = new ArrayList<ModelMBeanOperationInfo>();
    for (Method m : object.getClass().getMethods()) {
        JmxOperation jmxOperation = m.getAnnotation(JmxOperation.class);
        JmxGetter jmxGetter = m.getAnnotation(JmxGetter.class);
        JmxSetter jmxSetter = m.getAnnotation(JmxSetter.class);
        if (jmxOperation != null || jmxGetter != null || jmxSetter != null) {
            String description = "";
            int visibility = 1;
            int impact = MBeanOperationInfo.UNKNOWN;
            if (jmxOperation != null) {
                description = jmxOperation.description();
                impact = jmxOperation.impact();
            } else if (jmxGetter != null) {
                description = jmxGetter.description();
                impact = MBeanOperationInfo.INFO;
                visibility = 4;/*  ww w. ja va  2 s  .  c  o m*/
            } else if (jmxSetter != null) {
                description = jmxSetter.description();
                impact = MBeanOperationInfo.ACTION;
                visibility = 4;
            }
            ModelMBeanOperationInfo info = new ModelMBeanOperationInfo(m.getName(), description,
                    extractParameterInfo(m), m.getReturnType().getName(), impact);
            info.getDescriptor().setField("visibility", Integer.toString(visibility));
            infos.add(info);
        }
    }

    return infos.toArray(new ModelMBeanOperationInfo[infos.size()]);
}

From source file:io.github.moosbusch.lumpi.application.spi.AbstractLumpiApplicationContext.java

protected String getBeanCreationMethodName(Class<?> type) {
    Collection<Class<?>> configClasses = getApplication().getBeanConfigurationClasses();

    if (configClasses != null) {
        if (!configClasses.isEmpty()) {
            for (Class<?> configClass : configClasses) {
                Method[] methods = configClass.getMethods();

                for (Method method : methods) {
                    Class<?> returnType = method.getReturnType();
                    if (returnType == type) {
                        return method.getName();
                    }/*w ww. j  ava2 s .co m*/
                }
            }
        }
    }

    throw new BeanCreationException("No qualifying bean of type '" + type.getName() + "' is defined!");
}

From source file:com.aw.swing.mvp.binding.component.support.ValidatorBuilder.java

protected void buildValidatorFor(BindingComponent bindingComponent) {
    String attribute = bindingComponent.getFieldName();
    logger.debug("Attribute <" + attribute + ">");
    Method method = domainBuilder.getMethodFor(attribute);
    if (method == null) {
        return;/*from   w  w w  .jav  a  2  s .  c o m*/
    }

    boolean isDate = method.getReturnType().equals(java.util.Date.class);
    if (isDate) {
        if (bindingComponent instanceof BndIJTextField) {
            BndIJTextField bndIJTextField = (BndIJTextField) bindingComponent;
            if (bndIJTextField.getPropertyBinding().getFormatter() == null) {
                bndIJTextField.formatAsDate();
            }
        } else if (bindingComponent instanceof BndIJLabelField) {
            ((BndIJLabelField) bindingComponent).registerAsDateFormatted();
        }
    }

    PropertyValidator propertyValidator = buildPropertyValidator(method);

    if (method.isAnnotationPresent(Label.class)) {
        Label label = method.getAnnotation(Label.class);
        bindingComponent.setLabelToBeUsedOnError(label.value());
    }

    if (method.isAnnotationPresent(FillFormat.class)) {
        FillFormat fillFormat = method.getAnnotation(FillFormat.class);
        Formatter fillerFormatter = new FillerFormat(fillFormat.character(), fillFormat.lenght());
        ((BndIJTextField) bindingComponent).formatUsingCustomFormatter(fillerFormatter);
        ((BndIJTextField) bindingComponent).setLimitTextSize(fillFormat.lenght());
    }
    if (method.isAnnotationPresent(NotChangeCase.class)) {
        if (bindingComponent instanceof BndIJTextField) {
            ((BndIJTextField) bindingComponent).setAsNotChangeCase();
        }
    }
    if (method.isAnnotationPresent(NumberFormat.class)) {
        NumberFormat numberFormat = method.getAnnotation(NumberFormat.class);
        if (bindingComponent instanceof BndFormattedComponent) {
            if (numberFormat.value().length() != 0) {
                ((BndIJTextField) bindingComponent)
                        .formatUsingCustomFormatter(new NumberFormatter(numberFormat.value()));
            } else {
                ((BndIJTextField) bindingComponent).formatAsNumber();
            }
        }
    }

    if (method.isAnnotationPresent(MoneyFormat.class)) {
        MoneyFormat numberFormat = method.getAnnotation(MoneyFormat.class);
        if (bindingComponent instanceof BndFormattedComponent) {
            if (numberFormat.value().length() != 0) {
                ((BndIJTextField) bindingComponent)
                        .formatUsingCustomFormatter(new NumberFormatter(numberFormat.value()));
            } else {
                ((BndIJTextField) bindingComponent).formatAsMoney();
            }
            propertyValidator.setMoneyFormat(true);
        }
    }

    if (method.isAnnotationPresent(PercentFormat.class)) {
        if (bindingComponent instanceof BndFormattedComponent) {
            ((BndIJTextField) bindingComponent).formatAsPercentage();
        }
    }
    propertyValidator.setDateFormat(isDate);

    if (propertyValidator == null || propertyValidator.equals(Validator.EMPTY_VALIDATOR))
        return;

    presenter.getValidatorMgr().registerBasicRule((BndIJTextField) bindingComponent, propertyValidator);
}

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

@Test
public void shouldCreateStringGetterWithoutEncoding() throws SecurityException, NoSuchMethodException {
    Method getter = classWithMediaProperties.getDeclaredMethod("getUnencoded");

    assertThat("unencoded getter has return type String", getter.getReturnType(), equalToType(String.class));
}

From source file:bammerbom.ultimatecore.bukkit.configuration.ConfigurationSerialization.java

protected Method getMethod(String name, boolean isStatic) {
    try {/*w ww .  jav  a2s.c o  m*/
        Method method = clazz.getDeclaredMethod(name, Map.class);

        if (!ConfigurationSerializable.class.isAssignableFrom(method.getReturnType())) {
            return null;
        }
        if (Modifier.isStatic(method.getModifiers()) != isStatic) {
            return null;
        }

        return method;
    } catch (NoSuchMethodException ex) {
        return null;
    } catch (SecurityException ex) {
        return null;
    }
}