List of usage examples for java.lang.reflect Method getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java
/** * retrieve candidate setter method//from ww w .j a v a 2s. co m * * @param clazz * @param name * @return */ @SuppressWarnings({ "rawtypes" }) private static Method getCandidateMethod(Class clazz, String name) { for (Method method : clazz.getMethods()) { if (name.equals(method.getName()) && method.getParameterTypes().length == 1) return method; } return null; }
From source file:Main.java
/** * Converts a given object to a form encoded map * @param objName Name of the object/*from www . j a v a 2 s.c o m*/ * @param obj The object to convert into a map * @param objectMap The object map to populate * @param processed List of objects hashCodes that are already parsed * @throws InvalidObjectException */ private static void objectToMap(String objName, Object obj, Map<String, Object> objectMap, HashSet<Integer> processed) throws InvalidObjectException { //null values need not to be processed if (obj == null) return; //wrapper types are autoboxed, so reference checking is not needed if (!isWrapperType(obj.getClass())) { //avoid infinite recursion if (processed.contains(obj.hashCode())) return; processed.add(obj.hashCode()); } //process arrays if (obj instanceof Collection<?>) { //process array if ((objName == null) || (objName.isEmpty())) throw new InvalidObjectException("Object name cannot be empty"); Collection<?> array = (Collection<?>) obj; //append all elements in the array into a string int index = 0; for (Object element : array) { //load key value pair String key = String.format("%s[%d]", objName, index++); loadKeyValuePairForEncoding(key, element, objectMap, processed); } } else if (obj instanceof Map) { //process map Map<?, ?> map = (Map<?, ?>) obj; //append all elements in the array into a string for (Map.Entry<?, ?> pair : map.entrySet()) { String attribName = pair.getKey().toString(); String key = attribName; if ((objName != null) && (!objName.isEmpty())) { key = String.format("%s[%s]", objName, attribName); } loadKeyValuePairForEncoding(key, pair.getValue(), objectMap, processed); } } else { //process objects // invoke getter methods Method[] methods = obj.getClass().getMethods(); for (Method method : methods) { //is a getter? if ((method.getParameterTypes().length != 0) || (!method.getName().startsWith("get"))) continue; //get json attribute name Annotation getterAnnotation = method.getAnnotation(JsonGetter.class); if (getterAnnotation == null) continue; //load key name String attribName = ((JsonGetter) getterAnnotation).value(); String key = attribName; if ((objName != null) && (!objName.isEmpty())) { key = String.format("%s[%s]", objName, attribName); } try { //load key value pair Object value = method.invoke(obj); loadKeyValuePairForEncoding(key, value, objectMap, processed); } catch (Exception ex) { } } // load fields Field[] fields = obj.getClass().getFields(); for (Field field : fields) { //load key name String attribName = field.getName(); String key = attribName; if ((objName != null) && (!objName.isEmpty())) { key = String.format("%s[%s]", objName, attribName); } try { //load key value pair Object value = field.get(obj); loadKeyValuePairForEncoding(key, value, objectMap, processed); } catch (Exception ex) { } } } }
From source file:Main.java
public static void dumpMethod(final Method method) { final StringBuilder builder = new StringBuilder(); builder.append("------------------------------\n"); builder.append("MethodName: ").append(method.getName()).append("\n"); builder.append("ParameterTypes:{"); for (Class<?> cls : method.getParameterTypes()) { builder.append(cls.getName()).append(", "); }//from www . j a va 2 s.c o m builder.append("}\n"); builder.append("GenericParameterTypes:{"); for (Type cls : method.getGenericParameterTypes()) { builder.append(cls.getClass()).append(", "); } builder.append("}\n"); builder.append("TypeParameters:{"); for (TypeVariable<Method> cls : method.getTypeParameters()) { builder.append(cls.getName()).append(", "); } builder.append("}\n"); builder.append("DeclaredAnnotations:{"); for (Annotation cls : method.getDeclaredAnnotations()) { builder.append(cls).append(", "); } builder.append("}\n"); builder.append("Annotations:{"); for (Annotation cls : method.getAnnotations()) { builder.append(cls).append(", "); } builder.append("}\n"); builder.append("ExceptionTypes:{"); for (Class<?> cls : method.getExceptionTypes()) { builder.append(cls.getName()).append(", "); ; } builder.append("}\n"); builder.append("ReturnType: ").append(method.getReturnType()); builder.append("\nGenericReturnType: ").append(method.getGenericReturnType()); builder.append("\nDeclaringClass: ").append(method.getDeclaringClass()); builder.append("\n"); System.out.println(builder.toString()); }
From source file:com.helpinput.propertyeditors.PropertyEditorRegister.java
private static boolean addProperty(Class<? extends PropertyEditor> propertyEditorType, Property propertyAnn, Map<Method, Object> setMethodAndValues) { final String methodName = Commons.getSetterName(propertyAnn.name()); if (!Utils.hasLength(methodName)) return false; Method method = Utils.findMethod(propertyEditorType, methodName); if (method == null || !method.getReturnType().equals(Void.TYPE)) return false; Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes == null || parameterTypes.length != 1) return false; try {/*from ww w . j av a 2 s . co m*/ Class<?> parameterType = parameterTypes[0]; Object realValue = ConvertUtils.convert(propertyAnn.value(), parameterType); setMethodAndValues.put(method, realValue); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:com.evolveum.midpoint.util.ReflectionUtil.java
/** * Rough lookup of a compatible method. It takes first method with matching name, number of parameters and compatible * parameter values. It is not perfect, e.g. it cannot select foo(String) instead of foo(Object). But it is better than * nothing. And stock Java reflection has really nothing. *//*w ww .java 2 s . c om*/ private static Method findMethodCompatible(Object object, String methodName, List<?> argList) throws SecurityException { for (Method method : object.getClass().getMethods()) { if (method.getName().equals(methodName)) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == argList.size()) { boolean wrong = false; for (int i = 0; i < parameterTypes.length; i++) { Object arg = argList.get(i); if (arg == null) { // null argument matches any parameter type } else { if (!parameterTypes[i].isAssignableFrom(arg.getClass())) { wrong = true; break; } } } if (!wrong) { // We got it. We have compatible signature here. return method; } } } } return null; }
From source file:cat.albirar.framework.dynabean.impl.DynaBeanImplementationUtils.java
/** * Comprova que el mtode representa una propietat correcta. Un propietat correcta s: * <ul>/* w ww . ja v a 2 s .co m*/ * <li>Un {@link #isSetter(String) mtode set} amb arguments i sense retorn</li> * <li>Un {@link #isGetter(String) mtode get} sense arguments i amb retorn diferent de 'void'.</li> * <li>Un {@link #isGetterBoolean(String) mtode 'is'} sense arguments i amb retorn 'boolean'.</li> * </ul> * * @param method El mtode * @return true si s correcta i false en cas contrari */ public static final boolean isCorrectProperty(Method method) { if (isGetter(method.getName())) { if (isGetterBoolean(method.getName())) { return (method.getParameterTypes().length == 0 && method.getReturnType().equals(boolean.class)); } return (!method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0); } else { return (method.getParameterTypes().length == 1 && method.getReturnType().equals(void.class)); } }
From source file:edu.vt.middleware.ldap.props.AbstractPropertyInvoker.java
/** * Invokes the supplied method on the supplied object with the supplied * argument.//from w ww . j a v a 2s. c o m * * @param method <code>Method</code> to invoke * @param object <code>Object</code> to invoke method on * @param arg <code>Object</code> to invoke method with * * @return <code>Object</code> produced by the invocation * * @throws IllegalArgumentException if an error occurs invoking the method */ public static Object invokeMethod(final Method method, final Object object, final Object arg) { try { Object[] params = new Object[] { arg }; if (arg == null && method.getParameterTypes().length == 0) { params = (Object[]) null; } return method.invoke(object, params); } catch (InvocationTargetException e) { throw new IllegalArgumentException(e); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } }
From source file:bdi4jade.util.ReflectionUtils.java
private static boolean isGetter(Method method) { if (!method.getName().startsWith(GETTER_PREFIX)) return false; if (method.getParameterTypes().length != 0) return false; if (void.class.equals(method.getReturnType())) return false; return true;/*from www . j a v a2 s.co m*/ }
From source file:bdi4jade.util.ReflectionUtils.java
private static boolean isSetter(Method method) { if (!method.getName().startsWith(SETTER_PREFIX)) return false; if (method.getParameterTypes().length != 1) return false; if (!void.class.equals(method.getReturnType())) return false; return true;/*from ww w . jav a 2 s .c om*/ }
From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java
private static Method findSetterMethod(Class<?> clazz, String name) { // Build the method name. name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Method[] methods = clazz.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; Class<?> params[] = method.getParameterTypes(); if (method.getName().equals(name) && params.length == 1) { return method; }//from w ww.j ava 2s. c om } return null; }