List of usage examples for java.lang.reflect Method getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:Main.java
/** * find the setter method and set the value. *//*from www . j ava 2s. c om*/ public static void setProperties(Object bean, Map<String, Object> properties) { for (Method method : bean.getClass().getMethods()) { String name = method.getName(); if (name.length() > 3 && name.startsWith("set") && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 1 && method.getDeclaringClass() != Object.class) { String key = name.substring(3, 4).toLowerCase() + name.substring(4); try { Object value = properties.get(key); if (value != null) { method.invoke(bean, new Object[] { convertCompatibleType(value, method.getParameterTypes()[0]) }); } } catch (Exception e) { } } } }
From source file:net.mindengine.blogix.BlogixMain.java
private static void invokeCommandWithArguments(Method method, String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes[0].equals(String.class)) { method.invoke(NO_INSTANCE, args[0]); } else {/*from w w w. j ava 2 s .co m*/ method.invoke(NO_INSTANCE, new Object[] { args }); } }
From source file:com.espertech.esper.event.bean.PropertyHelper.java
private static void addIntrospectPropertiesWritable(Class clazz, Set<WriteablePropertyDescriptor> result) { PropertyDescriptor properties[] = introspect(clazz); for (int i = 0; i < properties.length; i++) { PropertyDescriptor property = properties[i]; String propertyName = property.getName(); Method writeMethod = property.getWriteMethod(); if (writeMethod == null) { continue; }//from w w w .j a va2 s . co m result.add( new WriteablePropertyDescriptor(propertyName, writeMethod.getParameterTypes()[0], writeMethod)); } }
From source file:at.ac.tuwien.infosys.jcloudscale.api.CloudObjects.java
/** * Invokes a desired method on remote object using the provided arguments. * /*w w w . j ava 2 s . co m*/ * @param obj the remote object * @param method the method to invoke * @param args the arguments * @return the result of the method invocation */ public static Object invoke(Object obj, Method method, Object... args) { UUID id = getId(obj); if (!isCloudObject(id)) { throw new JCloudScaleException(id == null ? "Invocation target is no CloudObject!" : "Cannot invoke method on destroyed CloudObject!"); } return CloudManager.getInstance().invokeCloudObject(id, method, args, method.getParameterTypes()); }
From source file:com.tealeaf.plugin.PluginEvent.java
private static String invokeMethod(Object targetObject, Object[] parameters, String methodName, String className) {/*from w w w .j a v a 2s. c o m*/ String retStr = "{}"; if (targetObject == null) { logger.log("{plugins} WARNING: Event could not be delivered for missing plugin:", className); return retStr; } boolean found = false; for (Method method : targetObject.getClass().getMethods()) { if (!method.getName().equals(methodName)) { continue; } Class<?>[] parameterTypes = method.getParameterTypes(); boolean match = true; for (int i = 0; i < parameterTypes.length; i++) { if (parameters[i] == null) { continue; } if (!parameterTypes[i].isAssignableFrom(parameters[i].getClass())) { match = false; break; } } if (!match && parameters.length != 0) { try { // try and coerce string argument in the first place to a JSONObject if (parameters[0] instanceof String) { JSONObject arg = new JSONObject((String) parameters[0]); parameters[0] = arg; if (parameterTypes[0].isAssignableFrom(parameters[0].getClass())) { match = true; } } } catch (JSONException e) { // first param is not valid JSON. Pass. } } if (match) { try { if (method.getReturnType().equals(Void.TYPE)) { method.invoke(targetObject, parameters); } else if (method.getReturnType().equals(String.class)) { retStr = (String) method.invoke(targetObject, parameters); } else { retStr = "" + method.invoke(targetObject, parameters); } found = true; } catch (IllegalArgumentException e) { logger.log(e); } catch (IllegalAccessException e) { logger.log(e); } catch (InvocationTargetException e) { logger.log(e); } } } if (!found) { logger.log("{plugins} WARNING: Unknown event could not be delivered for plugin:", className, ", method:", methodName); } if (retStr == null) { retStr = "{}"; } return retStr; }
From source file:com.springframework.beans.BeanUtils.java
/** * Find a method with the given method name and minimal parameters (best case: none) * in the given list of methods.//from w ww. ja v a 2s . c o m * @param methods the methods to check * @param methodName the name of the method to find * @return the Method object, or {@code null} if not found * @throws IllegalArgumentException if methods of the given name were found but * could not be resolved to a unique method with minimal parameters */ public static Method findMethodWithMinimalParameters(Method[] methods, String methodName) throws IllegalArgumentException { Method targetMethod = null; int numMethodsFoundWithCurrentMinimumArgs = 0; for (Method method : methods) { if (method.getName().equals(methodName)) { int numParams = method.getParameterTypes().length; if (targetMethod == null || numParams < targetMethod.getParameterTypes().length) { targetMethod = method; numMethodsFoundWithCurrentMinimumArgs = 1; } else { if (targetMethod.getParameterTypes().length == numParams) { // Additional candidate with same length numMethodsFoundWithCurrentMinimumArgs++; } } } } if (numMethodsFoundWithCurrentMinimumArgs > 1) { throw new IllegalArgumentException("Cannot resolve method '" + methodName + "' to a unique method. Attempted to resolve to overloaded method with " + "the least number of parameters, but there were " + numMethodsFoundWithCurrentMinimumArgs + " candidates."); } return targetMethod; }
From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java
/** * Retrieve the given annotation's attributes as an {@code org.springframework.core.annotation.AnnotationAttributes} * map structure. Implemented in Spring 3.1.1 to provide fully recursive annotation * reading capabilities on par with that of the reflection-based * {@code org.springframework.core.type.StandardAnnotationMetadata}. * @param annotation the annotation to retrieve the attributes for * @param classValuesAsString whether to turn Class references into Strings (for * compatibility with {@code org.springframework.core.type.AnnotationMetadata} or to * preserve them as Class references//w w w . j a va 2 s.c om * @param nestedAnnotationsAsMap whether to turn nested Annotation instances into * {@code org.springframework.core.annotation.AnnotationAttributes} maps (for compatibility with * {@code org.springframework.core.type.AnnotationMetadata} or to preserve them as * Annotation instances * @return the annotation attributes (a specialized Map) with attribute names as keys * and corresponding attribute values as values * @since 3.1.1 */ public static AnnotationAttributes getAnnotationAttributes(Annotation annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap) { AnnotationAttributes attrs = new AnnotationAttributes(); Method[] methods = annotation.annotationType().getDeclaredMethods(); for (Method method : methods) { if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) { try { Object value = method.invoke(annotation); if (classValuesAsString) { if (value instanceof Class) { value = ((Class<?>) value).getName(); } else if (value instanceof Class[]) { Class<?>[] clazzArray = (Class[]) value; String[] newValue = new String[clazzArray.length]; for (int i = 0; i < clazzArray.length; i++) { newValue[i] = clazzArray[i].getName(); } value = newValue; } } if (nestedAnnotationsAsMap && value instanceof Annotation) { attrs.put(method.getName(), getAnnotationAttributes((Annotation) value, classValuesAsString, nestedAnnotationsAsMap)); } else if (nestedAnnotationsAsMap && value instanceof Annotation[]) { Annotation[] realAnnotations = (Annotation[]) value; AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length]; for (int i = 0; i < realAnnotations.length; i++) { mappedAnnotations[i] = getAnnotationAttributes(realAnnotations[i], classValuesAsString, nestedAnnotationsAsMap); } attrs.put(method.getName(), mappedAnnotations); } else { attrs.put(method.getName(), value); } } catch (Exception ex) { throw new IllegalStateException("Could not obtain annotation attribute values", ex); } } } return attrs; }
From source file:com.wavemaker.runtime.server.ServerUtils.java
public static Object invokeMethod(Object serviceObject, Method method, Object[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class<?>[] paramTypes = method.getParameterTypes(); Object[] argsToSend = null;// w w w.ja v a 2 s . c o m if (args != null && paramTypes != null && args.length > 0) { argsToSend = new Object[args.length]; int i = 0; for (Class<?> p : paramTypes) { Object arg = args[i]; if (arg != null && !p.isAssignableFrom(arg.getClass()) && String.class.isAssignableFrom(arg.getClass())) { arg = convert(arg.toString(), p); } argsToSend[i] = arg; i++; } } return method.invoke(serviceObject, argsToSend); }
From source file:net.mindengine.blogix.BlogixMain.java
private static void invokeCommand(String name, String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Method method = findCommandMethod(name); if (method == null) { info("Unknown command \"" + name + "\""); System.exit(1);/*from w w w. ja v a2s .co m*/ } if (method.getParameterTypes().length == 0) { method.invoke(NO_INSTANCE); } else { invokeCommandWithArguments(method, args); } }
From source file:RssAtomGenerationTest.java
public static <T> T convertObject(final T destination, final Object source, FeedType type, Object... context) { Map<Class, Object> contentMap = new HashMap<Class, Object>(); for (Object o : context) { contentMap.put(o.getClass(), o); }//from w ww. j av a 2s. c o m final Method[] methods = source.getClass().getMethods(); for (Method method : methods) { final boolean refPresent = method.isAnnotationPresent(SyndicationRefs.class); if (refPresent || (method.isAnnotationPresent(SyndicationElement.class) && method.getAnnotation(SyndicationElement.class).type().equals(type))) { SyndicationElement annotation = null; if (refPresent) { final SyndicationElement[] value = method.getAnnotation(SyndicationRefs.class).value(); for (SyndicationElement element : value) { if (element.type().equals(type)) { annotation = element; break; } } if (annotation == null) { continue; } } else { annotation = method.getAnnotation(SyndicationElement.class); } //final SyndicationElement annotation = final String name = annotation.name(); try { final Object initValue = method.invoke(source); Object value = null; if (!(annotation.converter().isAssignableFrom(NoopConverter.class))) { final Converter converter = annotation.converter().newInstance(); value = converter.convert(initValue); } if (!(annotation.transformer().isAssignableFrom(NoopTransformer.class))) { contentMap.put(initValue.getClass(), initValue); final Class<?> transformer = annotation.transformer(); final Method[] transformerMethods = transformer.getMethods(); for (Method transformerMethod : transformerMethods) { if (transformerMethod.isAnnotationPresent(ContextTransformable.class)) { final Class<?>[] parameterTypes = transformerMethod.getParameterTypes(); List<Object> parameters = new ArrayList<Object>(); for (Class clazz : parameterTypes) { if (contentMap.containsKey(clazz)) { parameters.add(contentMap.get(clazz)); } else { boolean found = false; for (Object obj : contentMap.values()) { if (clazz.isInstance(obj)) { parameters.add(obj); found = true; break; } } if (!found) { parameters.add(null); } } } if (Modifier.isStatic(transformerMethod.getModifiers())) { value = transformerMethod.invoke(null, parameters.toArray()); } else { value = transformerMethod.invoke(transformer.newInstance(), parameters.toArray()); } break; } } } BeanUtils.setProperty(destination, name, value); } catch (Exception e) { log.error("test", e); e.printStackTrace(); } } } return destination; }