List of usage examples for java.lang.reflect Method getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:org.kantega.dogmaticmvc.web.DogmaticMVCHandler.java
public static Method findSameMethod(Method method, Class classToTest) { for (Method m : classToTest.getMethods()) { if (m.getName().equals(method.getName()) && Arrays.equals(m.getParameterTypes(), method.getParameterTypes())) { method = m;//from w w w . j av a 2 s . c o m break; } } return method; }
From source file:net.buffalo.protocal.util.ClassUtil.java
public static Object invokeMethod(Object instance, Method method, Object[] arguments) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class[] parameterTypes = method.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { if (!parameterTypes[i].isAssignableFrom(arguments[i].getClass())) { arguments[i] = convertValue(arguments[i], parameterTypes[i]); }/* w w w .j a v a 2 s.c o m*/ } return method.invoke(instance, arguments); }
From source file:com.helpinput.core.MapConvertor.java
/** * map??Javabean//from w w w. j av a2 s . co m * * @param javabean * javaBean * @param values * map? */ public static Object toJavaBean(Object javabean, Map<?, ?> data) { Method[] methods = javabean.getClass().getDeclaredMethods(); for (Method method : methods) { try { if (method.getName().startsWith("set")) { Class<?>[] parmaTypes = method.getParameterTypes(); if (parmaTypes.length == 1) { String fieldName = methodNameGetFieldName(method.getName()); Object value = Utils.convert(data.get(fieldName), parmaTypes[0]); method.invoke(javabean, value); // String field = method.getName(); // field = field.substring(field.indexOf("set") + 3); // field = field.toLowerCase().charAt(0) + field.substring(1); // method.invoke(javabean, new Object[] { data.get(field) }); } } } catch (Exception e) { } } return javabean; }
From source file:com.qmetry.qaf.automation.testng.pro.DataProviderUtil.java
/** * <p>//w ww .j av a2s .c o m * Use as TestNG dataProvider. * <p> * data-provider name: <code>isfw-csv</code> * <p> * csv data provider utility method for testNG<br> * Required property * <ol> * <li>test.<method_name>.datafile= <file URL> * </ol> * * @param method * @return */ @DataProvider(name = "isfw_csv") public static final Object[][] getCSVData(Method method) { Class<?> types[] = method.getParameterTypes(); if ((types.length == 1) && Map.class.isAssignableFrom(types[0])) { // will consider first row as header row return CSVUtil.getCSVDataAsMap(getParameters(method).get(params.DATAFILE.name())) .toArray(new Object[][] {}); } return CSVUtil.getCSVData(getParameters(method).get(params.DATAFILE.name())).toArray(new Object[][] {}); }
From source file:com.qcadoo.view.internal.CustomMethodHolder.java
public static boolean methodExists(final String className, final String methodName, final ApplicationContext applicationContext, final Class<?>[] expectedParameterTypes) { Preconditions.checkArgument(!StringUtils.isBlank(className), "class name attribute is not specified!"); Preconditions.checkArgument(!StringUtils.isBlank(methodName), "method name attribute is not specified!"); Preconditions.checkArgument(expectedParameterTypes != null, "expected parameter types are not specified!"); try {// w w w .j a v a2s. c o m final Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className); for (Method method : clazz.getMethods()) { if (method.getName().equals(methodName) && Arrays.deepEquals(method.getParameterTypes(), expectedParameterTypes)) { return true; } } return false; } catch (ClassNotFoundException e) { return false; } }
From source file:mangotiger.poker.channel.EventChannelImpl.java
static boolean methodMatchesEvent(final Method method, final Class<?> event) { return METHOD_NAME.equals(method.getName()) && Modifier.isPublic(method.getModifiers()) && Void.TYPE.equals(method.getReturnType()) && method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(event); }
From source file:com.newtranx.util.monitoring.MonitorAspect.java
private static Method getMethod(MethodSignature signature, ProceedingJoinPoint pjp) { Method method = signature.getMethod(); if (method.getDeclaringClass().isInterface()) { try {/*from w w w . j av a 2 s . c o m*/ method = pjp.getTarget().getClass().getDeclaredMethod(pjp.getSignature().getName(), method.getParameterTypes()); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } } return method; }
From source file:com.relicum.ipsum.Reflection.ReflectionUtil.java
/** * Gets a {@link Method} in a given {@link Class} object with the specified * arguments.//from w ww.j av a 2 s. c o m * * @param clazz Class object * @param name Method name * @param args Arguments * @return The method, or null if none exists */ public static final Method getMethod(Class<?> clazz, String name, Class<?>... args) { Validate.notNull(clazz, "clazz cannot be null!"); Validate.notNull(name, "name cannot be null!"); if (args == null) args = new Class<?>[0]; for (Method method : clazz.getMethods()) { if (method.getName().equals(name) && Arrays.equals(args, method.getParameterTypes())) return method; } return null; }
From source file:it.unibo.alchemist.language.protelis.util.ReflectionUtils.java
/** * @param method//from w ww . j av a 2 s . co m * the methods to invoke * @param target * the target object. It can be null, if the method which is * being invoked is static * @param args * the arguments for the method * @return the result of the invocation, or an {@link IllegalStateException} * if something goes wrong. */ public static Object invokeMethod(final Method method, final Object target, final Object[] args) { final Class<?>[] params = method.getParameterTypes(); List<Object> list = new ArrayList<Object>(); for (int i = 0; i < args.length; i++) { final Class<?> expected = params[i]; final Object actual = args[i]; if (!expected.isAssignableFrom(actual.getClass()) && PrimitiveUtils.classIsNumber(expected)) { // TODO: Removed .get() list.add(PrimitiveUtils.castIfNeeded(expected, (Number) actual)); // list.add(PrimitiveUtils.castIfNeeded(expected, (Number) actual).get()); } else { list.add(actual); } } Object[] actualArgs = list.toArray(); try { return method.invoke(target, actualArgs); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { L.error(e); throw new IllegalStateException( "Cannot invoke " + method + " with arguments " + Arrays.toString(args) + " on " + target, e); } }
From source file:com.facebook.stetho.inspector.MethodDispatcher.java
/** * Determines if the method is a {@link ChromeDevtoolsMethod}, and validates accordingly * if it is.//from w ww. j ava2 s. c o m * * @throws IllegalArgumentException Thrown if it is a {@link ChromeDevtoolsMethod} but * it otherwise fails to satisfy requirements. */ private static boolean isDevtoolsMethod(Method method) throws IllegalArgumentException { if (!method.isAnnotationPresent(ChromeDevtoolsMethod.class)) { return false; } else { Class<?> args[] = method.getParameterTypes(); String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName(); Util.throwIfNot(args.length == 2, "%s: expected 2 args, got %s", methodName, args.length); Util.throwIfNot(args[0].equals(JsonRpcPeer.class), "%s: expected 1st arg of JsonRpcPeer, got %s", methodName, args[0].getName()); Util.throwIfNot(args[1].equals(JSONObject.class), "%s: expected 2nd arg of JSONObject, got %s", methodName, args[1].getName()); Class<?> returnType = method.getReturnType(); if (!returnType.equals(void.class)) { Util.throwIfNot(JsonRpcResult.class.isAssignableFrom(returnType), "%s: expected JsonRpcResult return type, got %s", methodName, returnType.getName()); } return true; } }