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:se.trillian.goodies.spring.DomainObjectFactoryFactoryBean.java

@Override
public void afterPropertiesSet() throws Exception {
    Assert.notNull(interfaceClass, "interfaceClass");
    Assert.notNull(implementationClass, "implementationClass");
    if (factoryClass == null) {
        factoryClass = interfaceClass.getClassLoader().loadClass(interfaceClass.getName() + "$Factory");
    }/*w ww  .  jav  a  2 s .  com*/
    Assert.isTrue(factoryClass.isInterface(), "Factory class '" + factoryClass + "' is not an interface");

    for (Method m : factoryClass.getMethods()) {
        Assert.isTrue("create".equals(m.getName()),
                "Domain object factory method '" + m + "' is invalid. All methods must be named 'create'.");
        Assert.isTrue(interfaceClass.equals(m.getReturnType()),
                "Factory method '" + m + "' does not return instances of '" + interfaceClass + "'");
        findMatchingConstructor(implementationClass, m);
    }
    Assert.isTrue(factoryClass.getMethods().length > 0,
            "Factory class '" + factoryClass + "' does not define any methods");

    super.afterPropertiesSet();
}

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

@Test
public void shouldCreateByteArraySetterWithAnyEncoding() throws SecurityException, NoSuchMethodException {
    Method setter = classWithMediaProperties.getDeclaredMethod("setAnyBinaryEncoding", BYTE_ARRAY);

    assertThat("any binary encoding setter has return type void", setter.getReturnType(),
            equalToType(Void.TYPE));
}

From source file:com.mcapanel.plugin.PluginConnector.java

@SuppressWarnings("unchecked")
private void doMethodAndRespond(String method, String paramStr) {
    JSONObject out = new JSONObject();

    out.put("plugin", "McAdminPanel");
    out.put("type", "response");
    out.put("method", method);

    String[] params = paramStr.length() != 0 ? paramStr.split(", ") : new String[0];

    try {/* w w w  .  ja va  2  s  .co m*/
        Class<?>[] paramClasses = new Class<?>[params.length];

        for (int i = 0; i < params.length; i++)
            paramClasses[i] = Class.forName("java.lang.String");

        Method m = methodHandler.getClass().getDeclaredMethod(method, paramClasses);

        String ret = (String) m.invoke(methodHandler, (Object[]) params);

        if (m.getReturnType().equals(Void.TYPE))
            return;

        out.put("response", ret);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

    if (!out.containsKey("response"))
        out.put("response", "No such method");

    String cmd = "mcadminpanelplugincmd " + out.toJSONString();

    OutputStream writer = server.getWriter();

    try {
        writer.write((cmd + "\n").getBytes());
        writer.flush();
    } catch (IOException e) {
    }
}

From source file:com.github.jknack.handlebars.helper.DefaultHelperRegistry.java

/**
 * <p>//from   w  w  w  .j  a va2 s.co m
 * Register all the helper methods for the given helper source.
 * </p>
 *
 * @param source The helper source.
 * @param clazz The helper source class.
 */
private void registerDynamicHelper(final Object source, final Class<?> clazz) {
    int size = helpers.size();
    int replaced = 0;
    if (clazz != Object.class) {
        Set<String> overloaded = new HashSet<String>();
        // Keep backing up the inheritance hierarchy.
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            boolean isPublic = Modifier.isPublic(method.getModifiers());
            String helperName = method.getName();
            if (isPublic && CharSequence.class.isAssignableFrom(method.getReturnType())) {
                boolean isStatic = Modifier.isStatic(method.getModifiers());
                if (source != null || isStatic) {
                    if (helpers.containsKey(helperName)) {
                        replaced++;
                    }
                    isTrue(overloaded.add(helperName), "name conflict found: " + helperName);
                    registerHelper(helperName, new MethodHelper(method, source));
                }
            }
        }
    }
    isTrue((size + replaced) != helpers.size(), "No helper method was found in: " + clazz.getName());
}

From source file:com.haulmont.cuba.core.config.AppPropertiesLocator.java

private void setDataType(Method method, AppPropertyEntity entity) {
    Class<?> returnType = method.getReturnType();
    if (returnType.isPrimitive()) {
        if (returnType == boolean.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Boolean.class));
        if (returnType == int.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Integer.class));
        if (returnType == long.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Long.class));
        if (returnType == double.class || returnType == float.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Double.class));
    } else if (returnType.isEnum()) {
        entity.setDataTypeName("enum");
        EnumStore enumStoreAnn = method.getAnnotation(EnumStore.class);
        if (enumStoreAnn != null && enumStoreAnn.value() == EnumStoreMode.ID) {
            //noinspection unchecked
            Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
            entity.setEnumValues(Arrays.asList(enumeration.getEnumConstants()).stream()
                    .map(ec -> String.valueOf(ec.getId())).collect(Collectors.joining(",")));
        } else {//from w  w  w . j  av  a2 s  . c  om
            entity.setEnumValues(Arrays.asList(returnType.getEnumConstants()).stream().map(Object::toString)
                    .collect(Collectors.joining(",")));
        }
    } else {
        Datatype<?> datatype = datatypes.get(returnType);
        if (datatype != null)
            entity.setDataTypeName(datatypes.getId(datatype));
        else
            entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
    }

    String dataTypeName = entity.getDataTypeName();
    if (!dataTypeName.equals("enum")) {
        Datatype datatype = Datatypes.get(dataTypeName);
        String v = null;
        try {
            v = entity.getDefaultValue();
            datatype.parse(v);
            v = entity.getCurrentValue();
            datatype.parse(v);
        } catch (ParseException e) {
            log.debug("Cannot parse '{}' with {} datatype, using StringDatatype for property {}", v, datatype,
                    entity.getName());
            entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
        }
    }
}

From source file:com.px100systems.data.core.CompoundIndexDescriptor.java

public CompoundIndexDescriptor(Class<?> entityClass, String name, String[] fields) {
    if (name.trim().isEmpty())
        throw new RuntimeException("Empty compound index name in " + entityClass.getSimpleName());

    this.name = NAME_PREFIX + entityClass.getSimpleName() + "_" + name + "_idx";

    for (String field : fields) {
        String[] s = field.split(" ");
        if (s.length != 2)
            throw new RuntimeException("Invalid compound index " + name + " in " + entityClass.getSimpleName()
                    + ": couldn't parse field '" + field + "'");

        Method getter = ReflectionUtils.findMethod(entityClass, PropertyAccessor.methodName("get", s[0]));
        if (getter == null)
            throw new RuntimeException("Invalid compound index " + name + " in " + entityClass.getSimpleName()
                    + ": couldn't find getter for '" + field + "'");
        Class<?> returnType = getter.getReturnType();
        if (!returnType.equals(Integer.class) && !returnType.equals(Long.class)
                && !returnType.equals(Double.class) && !returnType.equals(Date.class)
                && !returnType.equals(String.class) && !returnType.equals(Boolean.class))
            throw new RuntimeException("Invalid compound index " + name + " in " + entityClass.getSimpleName()
                    + ": field '" + field
                    + "' - only Integer, Long, Double, Date, String, and Boolean are supported");

        this.fields.add(new Field(s[0], returnType, s[1].equalsIgnoreCase("DESC")));
    }//w ww  .j  av  a2  s. co  m
}

From source file:com.nabla.project.application.tool.runner.ServiceInvoker.java

/**
 * DOCUMENT ME!//from   w  ww  .  java 2 s  .  c om
 */
public void afterPropertiesSet() {

    if ((serviceInterface != null) && !serviceInterface.isInterface()) {

        throw new IllegalArgumentException("serviceInterface must be an interface");

    }

    Method methods[] = serviceInterface.getDeclaredMethods();

    for (Method method : methods) {

        if (isAsynchronousMethod(method) && !method.getReturnType().equals(Void.TYPE)) {

            throw new IllegalArgumentException(
                    "serviceInterface for ServiceRunner must containt only method returning void : " + method);

        }

    }

}

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

private List<CSVColumn> auto(Object object) {
    List<CSVColumn> columns = new ArrayList<CSVColumn>();
    Method[] declaredMethods = object.getClass().getDeclaredMethods();
    for (Method method : declaredMethods) {
        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;
        boolean isGetter = method.getName().startsWith("get");
        if (isGetter && noArguments && isPublic && isNotStatic && !isVoid) {
            columns.add(new CSVColumnMethodReflected(method.getName(), method.getName()));
        }/*from   w  w  w .  j  av  a  2 s.c o  m*/
    }
    return columns;
}

From source file:org.openmrs.module.metadatasharing.reflection.OpenmrsClassScanner.java

protected void initServiceSaveMethodsCache() throws IOException {
    if (serviceSaveMethodsCache != null && serviceSaveMethodsCacheTimeout.after(new Date())) {
        return;//from ww  w .j ava  2  s  . c  om
    }
    log.debug("Refreshing OpenmrsService classes cache");
    long future = new Date().getTime();
    future += CACHE_TIMEOUT_IN_MS;
    serviceSaveMethodsCacheTimeout = new Date(future);
    serviceSaveMethodsCache = new HashMap<Class<?>, ClassMethod<OpenmrsService>>();

    List<Class<OpenmrsService>> services = getClasses(OpenmrsService.class, false);
    for (Class<OpenmrsService> service : services) {
        Method[] methods = service.getMethods();
        for (Method method : methods) {
            if (method.getName().startsWith("save")) {
                if (Arrays.equals(method.getParameterTypes(), new Class<?>[] { method.getReturnType() })) {
                    if (canBeSaved(method.getReturnType())) {
                        serviceSaveMethodsCache.put(method.getReturnType(),
                                new ClassMethod<OpenmrsService>(service, method));
                    }
                }
            }
        }
    }
    //META-114 HACK
    try {
        serviceSaveMethodsCache.put(PersonAttributeType.class, new ClassMethod(PersonService.class,
                PersonService.class.getMethod("savePersonAttributeType", PersonAttributeType.class)));
        serviceSaveMethodsCache.put(RelationshipType.class, new ClassMethod(PersonService.class,
                PersonService.class.getMethod("saveRelationshipType", RelationshipType.class)));
    } catch (Exception ex) {
    }
}

From source file:com.home.ln_spring.ch4.mi.FormatMessageReplacer.java

private boolean isFormatMessageMethod(Method method) {
    //  ? ? /*from  w  w  w  . jav  a2 s  . co m*/
    if (method.getParameterTypes().length != 1) {
        return false;
    }

    //  ? 
    if (!("formatMessage".equals(method.getName()))) {
        return false;
    }

    //   
    if (method.getReturnType() != String.class) {
        return false;
    }

    //   
    if (method.getParameterTypes()[0] != String.class) {
        return false;
    }

    return true;
}