Example usage for java.lang.reflect Method getParameterTypes

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

Introduction

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

Prototype

@Override
public Class<?>[] getParameterTypes() 

Source Link

Usage

From source file:com.softmotions.commons.bean.BeanUtils.java

/**
 * Sets a property at the given bean.//from   w w w.java  2 s .c o  m
 *
 * @param bean         The bean to set a property at.
 * @param propertyName The name of the property to set.
 * @param value        The value to set for the property.
 * @throws BeanException In case the bean access failed.
 */
public static void setProperty(Object bean, String propertyName, Object value) throws BeanException {
    Class valueClass = null;
    try {
        // getting property object from bean using "setNnnn", where nnnn is parameter name
        Method setterMethod = null;
        // first trying form getPropertyNaae for regular value
        String setterName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
        Class paramClass = bean.getClass();
        if (value != null) {
            valueClass = value.getClass();
            Class[] setterArgTypes = new Class[] { valueClass };
            setterMethod = paramClass.getMethod(setterName, setterArgTypes);
        } else {
            Method[] methods = paramClass.getMethods();
            for (int i = 0; i < methods.length; i++) {
                Method m = methods[i];
                if (m.getName().equals(setterName) && (m.getParameterTypes().length == 1)) {
                    setterMethod = m;
                    break;
                }
            }
        }
        if (setterMethod == null) {
            throw new NoSuchMethodException(setterName);
        }
        Object[] setterArgs = new Object[] { value };
        setterMethod.invoke(bean, setterArgs);
    } catch (NoSuchMethodError | NoSuchMethodException ex) {
        throw new BeanException("No setter method found for property '" + propertyName + "' and type "
                + valueClass + " at given bean from class " + bean.getClass().getName() + ".", ex);
    } catch (InvocationTargetException ex) {
        throw new BeanException("Property '" + propertyName + "' could not be set for given bean from class "
                + bean.getClass().getName() + ".", ex);
    } catch (IllegalAccessException ex) {
        throw new BeanException("Property '" + propertyName
                + "' could not be accessed for given bean from class " + bean.getClass().getName() + ".", ex);
    }
}

From source file:nz.co.senanque.validationengine.ValidationUtils.java

public static void setDefaults(ValidationObject object) {
    for (Field field : object.getClass().getDeclaredFields()) {
        XmlElement xmlElement = field.getAnnotation(XmlElement.class);
        if (xmlElement != null && hasText(xmlElement.defaultValue())) {
            String value = xmlElement.defaultValue();
            Method setter = figureSetter(field.getName(), object.getClass());
            Class<?> type = setter.getParameterTypes()[0];
            try {
                setter.invoke(object, ConvertUtils.convertToObject(type, value, null));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }/*  w  w  w  .ja va 2  s  .c om*/
        }
        String n = field.getName();
        if (n.startsWith("m_")) {
            n = n.substring(2);
        }
        try {
            Method getter = figureGetter(n, object.getClass());
            Annotation unknown = getter.getAnnotation(Unknown.class);
            if (unknown != null) {
                object.getMetadata().addUnknown(field.getName());
            }
        } catch (RuntimeException e) {
            // ignore
        }
    }
}

From source file:com.palantir.ptoss.util.Reflections.java

private static List<ObjectFieldMethod> getParameterlessMethods(Object tupleObject, Class<?> klass) {
    List<ObjectFieldMethod> methods = Lists.newArrayList();
    for (Method method : klass.getDeclaredMethods()) {
        if (method.getParameterTypes().length == 0) {
            methods.add(new ObjectFieldMethod(tupleObject, null, method));
        }/*from ww  w .j a  v a 2 s  .co m*/
    }
    return methods;
}

From source file:cop.raml.mocks.MockUtils.java

public static ExecutableElementMock createExecutable(Method method) {
    if (method == null)
        return null;

    boolean isStatic = Modifier.isStatic(method.getModifiers());
    String params = Arrays.stream(method.getParameterTypes()).map(Class::getSimpleName)
            .collect(Collectors.joining(","));
    String name = String.format("(%s)%s", params, method.getReturnType().getSimpleName());

    return new ExecutableElementMock(method.getName() + "()", createMethodElement(name).asType())
            .setStatic(isStatic).setSimpleName(method.getName());
}

From source file:Utils.java

/**
 * Returns true if the method is a JMX attribute setter method
 *///from  w  w  w .  j  a v a  2 s . c  om
public static boolean isAttributeSetter(Method m) {
    if (m == null)
        return false;

    String name = m.getName();
    Class retType = m.getReturnType();
    Class[] params = m.getParameterTypes();
    if (retType == Void.TYPE && params.length == 1 && name.startsWith("set") && name.length() > 3) {
        return true;
    }
    return false;
}

From source file:Utils.java

/**
 * Returns true is the given method is a JMX attribute getter method
 *//*from  ww  w.java2  s . c  o  m*/
public static boolean isAttributeGetter(Method m) {
    if (m == null)
        return false;

    String name = m.getName();
    Class retType = m.getReturnType();
    Class[] params = m.getParameterTypes();
    if (retType != Void.TYPE && params.length == 0) {
        if (name.startsWith("get") && name.length() > 3)
            return true;
        else if (name.startsWith("is") && name.length() > 2 && retType == Boolean.TYPE)
            return true;
    }
    return false;
}

From source file:de.micromata.genome.tpsb.GroovyExceptionInterceptor.java

protected static String methodToString(Method m) {
    StringBuilder sb = new StringBuilder();
    String returnType = m.getReturnType().getName();
    // returnType = "def";
    sb.append(returnType).append(" ").append(m.getName()).append("(");
    int pos = 0;//from  ww  w .  j av  a2 s. c  o  m
    for (Class<?> cls : m.getParameterTypes()) {
        if (pos > 0) {
            sb.append(", ");
        }
        sb.append(cls.getName()).append(" arg" + pos);
        ++pos;
    }
    sb.append(")");
    return sb.toString();
}

From source file:jp.go.nict.langrid.client.soap.io.SoapRequestWriter.java

public static void writeSoapRequest(OutputStream os, String namespace, Iterable<RpcHeader> headers,
        Method method, Object... args) throws IOException, IllegalAccessException, InvocationTargetException {
    Service sa = method.getDeclaringClass().getAnnotation(Service.class);
    Map<String, Object> bindings = new HashMap<String, Object>();
    Class<?>[] paramTypes = method.getParameterTypes();
    Annotation[][] paramAnnots = method.getParameterAnnotations();
    List<Encoder> params = new ArrayList<Encoder>();
    int n = paramTypes.length;
    for (int i = 0; i < n; i++) {
        String name = EncoderUtil.getParameterName(paramAnnots[i]);
        if (name == null)
            name = "in" + i;
        params.add(Encoders.create(3, name, paramTypes[i], args[i]));
    }/*from   w  ww .  j  av  a  2s .co  m*/
    String ns = namespace;
    if (ns == null) {
        if (sa != null && sa.namespace().length() > 0) {
            ns = sa.namespace();
        } else {
            String sn = method.getDeclaringClass().getSimpleName();
            if (sn.endsWith("Service")) {
                sn = sn.substring(0, sn.length() - 7);
            }
            ns = "servicegrid:servicetype:nict.nlp:" + sn;
        }
    }
    bindings.put("namespace", ns);
    bindings.put("headers", escapeValueXml(headers));
    bindings.put("methodName", method.getName());
    bindings.put("parameters", params);

    StreamUtil.writeString(os, jmte.transform(template, bindings), "UTF-8");
}

From source file:edu.mayo.cts2.framework.model.util.ModelUtils.java

/**
 * Sets the entity./* w  w  w. j  av  a  2 s.c  om*/
 *
 * @param wrapper the wrapper
 * @param entityDescription the entity description
 */
public static void setEntity(EntityDescription wrapper, EntityDescriptionBase entityDescription) {
    try {
        for (Method method : EntityDescription.class.getDeclaredMethods()) {
            if (method.getName().startsWith("set") && method.getParameterTypes().length == 1
                    && method.getParameterTypes()[0].equals(entityDescription.getClass())) {
                method.invoke(wrapper, entityDescription);
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static Object invokeMethodByMemoryRow(Object object, Method method, MemoryRow memoryRow,
        Mutable outParameters) {//  ww  w. ja v  a2s . c  o m
    Class<?>[] parameterTypes = method.getParameterTypes();
    method.getTypeParameters();
    Object[] parameterObjects = new Object[parameterTypes.length];
    for (int i = 0; i < parameterTypes.length; i++) {
        Class<?> clazz = parameterTypes[i];
        MemoryField field = memoryRow.getField(i + 1);
        Object value = (field.getType() == MemoryFieldType.Null) ? null : field.getValue();
        parameterObjects[i] = TypeConvertUtil.convert(clazz, value);
    }
    if (outParameters != null) {
        outParameters.setValue(Arrays.asList(parameterObjects));
    }
    try {
        method.setAccessible(true);
        return method.invoke(object, parameterObjects);
    } catch (Exception e) {
        throw ExceptionUtil.wrapToRuntimeException(e);
    }
}