List of usage examples for java.lang.reflect Method getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:bammerbom.ultimatecore.bukkit.resources.utils.BossbarUtil.java
public static Method getMethod(Class<?> cl, String method, Class<?>[] args) { for (Method m : cl.getMethods()) { if ((m.getName().equals(method)) && (ClassListEqual(args, m.getParameterTypes()))) { return m; }//from ww w. j a va2s . c om } return null; }
From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java
/** * Returns a {@link Method} with a certain name and parameter types declared in the given class or any subclass * (except {@link Object})./* ww w.java2s . com*/ * <p/> * If no parameter types are specified i.e., {@code paramTypes} is {@code null} the parameter types are ignored * for signature comparison.<br/> * If a parameter type is not known i.e., it is {@code null}, all declared methods are checked whether their * parameter types conform to the known parameter types i.e., if every known type is assignable to the parameter * type of the method and if exactly one was found, it is returned.<br/> * Otherwise a {@link NoSuchMethodException} is thrown indicating that no or several methods were found. * * @param type the class * @param methodName the name of the method to find * @param clazzes the full-qualified class names of the parameters * @return the accessible method resolved * @throws ClassNotFoundException if a class cannot be located by the specified class loader */ public static Method findMethod(final Class<?> type, String methodName, Class<?>... clazzes) throws ClassNotFoundException { Method method = null; // If all parameter types are known, find the method that exactly matches the signature if (clazzes != null && !ArrayUtils.contains(clazzes, null)) { for (Class<?> clazz = type; clazz != Object.class; clazz = clazz.getSuperclass()) { try { method = type.getDeclaredMethod(methodName, clazzes); break; } catch (NoSuchMethodException e) { // Ignore } } } // If no method was found, find all possible candidates if (method == null) { List<Method> candidates = new ArrayList<>(); for (Class<?> clazz = type; clazz != null; clazz = clazz.getSuperclass()) { for (Method declaredMethod : clazz.getDeclaredMethods()) { if (declaredMethod.getName().equals(methodName) && (clazzes == null || ClassUtils.isAssignable(clazzes, declaredMethod.getParameterTypes()))) { // Check if there is already a overridden method with the same signature for (Method candidate : candidates) { if (candidate.getName().equals(declaredMethod.getName())) { /** * If there is at least one parameters in the method of the super type, which is a * sub type of the corresponding parameter of the sub type, remove the method declared * in the sub type. */ if (!Arrays.equals(declaredMethod.getParameterTypes(), candidate.getParameterTypes()) && ClassUtils.isAssignable(declaredMethod.getParameterTypes(), candidate.getParameterTypes())) { candidates.remove(candidate); } else { declaredMethod = null; } break; } } // If the method has a different signature matching the given types, add it to the candidates if (declaredMethod != null) { candidates.add(declaredMethod); } } } } if (candidates.size() != 1) { throw new JCloudScaleException( String.format("Cannot find distinct method '%s.%s()' with parameter types %s", type, methodName, Arrays.toString(clazzes))); } method = candidates.get(0); } //do we really need this dependency? //ReflectionUtils.makeAccessible(method); if (method != null && !method.isAccessible()) method.setAccessible(true); return method; }
From source file:bammerbom.ultimatecore.bukkit.resources.utils.BossbarUtil.java
public static Method getMethod(Class<?> cl, String method, Integer args) { for (Method m : cl.getMethods()) { if ((m.getName().equals(method)) && (args.equals(new Integer(m.getParameterTypes().length)))) { return m; }//from w w w. ja v a2 s. co m } return null; }
From source file:com.snaplogic.snaps.firstdata.Create.java
static boolean isSetter(Method method) { return Modifier.isPublic(method.getModifiers()) && method.getReturnType().equals(void.class) && method.getParameterTypes().length == 1 && method.getName().matches(REGEX_SET); }
From source file:com.smart.utils.ReflectionUtils.java
/** * ?????,danfo//from w w w . j a va2 s. c om * * @param bean * @param methodName * @param args * @return */ public static Method getMethodOfBeanByName(Object bean, String methodName, Object[] args) { if (bean == null || methodName == null) { return null; } if (args == null) { Object[] paras = {}; args = paras; } Method method = null; Class beanClass = bean.getClass(); Method[] methods = beanClass.getMethods(); for (int i = methods.length - 1; i >= 0; i--) { Method methodTemp = (Method) methods[i]; String methodNameTemp = methodTemp.getName(); // ??,?? if (methodName.equals(methodNameTemp)) { Class[] paras = methodTemp.getParameterTypes(); // ?? if (paras.length != args.length) { continue; } // ,??? boolean isParaTheSame = true; for (int j = paras.length - 1; j >= 0; j--) { Class paraNeededClass = paras[j]; Class paraGivenClass = args[j].getClass(); if (!paraNeededClass.getName().equals(paraGivenClass.getName())) { isParaTheSame = false; break; } } if (isParaTheSame) { method = methodTemp; } } } return method; // end of methodNameargs }
From source file:io.stallion.reflection.PropertyUtils.java
private static Class getPropertyType(Object target, String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { return method.getReturnType(); }/*from ww w.j a v a 2 s. com*/ } throw new PropertyException( "no property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
From source file:ReflectUtil.java
/** * <p>Attempts to find an accessible version of the method passed in, where accessible * is defined as the method itself being public and the declaring class being public. * Mostly useful as a workaround to the situation when * {@link PropertyDescriptor#getReadMethod()} and/or * {@link java.beans.PropertyDescriptor#getWriteMethod()} returns methods that are not * accessible (usually due to public implementations of interface methods in private * classes).</p>//from w ww . ja v a 2s. co m * * <p>Checks the method passed in and if it already meets these criteria it is returned * immediately. In general this leads to very little performance overhead</p> * * <p>If the method does not meet the criteria then the class' interfaces are scanned * for a matching method. If one is not found, then the class' superclass hierarchy * is searched. Finally, if no matching method can be found the original method is * returned.</p> * * @param m a method that may or may not be accessible * @return either an accessible version of the same method, or the method passed in if * an accessible version cannot be found */ public static Method findAccessibleMethod(final Method m) { // If the passed in method is accessible, then just give it back. if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers())) return m; if (m.isAccessible()) return m; final Class<?> clazz = m.getDeclaringClass(); final String name = m.getName(); final Class<?>[] ptypes = m.getParameterTypes(); // Else, loop through the interfaces for the declaring class, looking for a // public version of the method that we can call for (Class<?> iface : clazz.getInterfaces()) { try { Method m2 = iface.getMethod(name, ptypes); if (m2.isAccessible()) return m2; if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers())) return m2; } catch (NoSuchMethodException nsme) { /* Not Unexpected. */ } } // Else loop through the superclasses looking for a public method Class<?> c = clazz.getSuperclass(); while (c != null) { try { Method m2 = c.getMethod(name, ptypes); if (m2.isAccessible()) return m2; if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers())) return m2; } catch (NoSuchMethodException nsme) { /* Not Unexpected. */ } c = c.getSuperclass(); } // If we haven't found anything at this point, just give up! return m; }
From source file:io.stallion.reflection.PropertyUtils.java
public static Method getGetter(Object target, String propertyName) { String cacheKey = "getGetter" + "|" + target.getClass().getCanonicalName() + "|" + propertyName; if (target instanceof BaseJavascriptModel) { cacheKey = "getGetter" + "|jsModel" + ((BaseJavascriptModel) target).getBucketName() + "|" + propertyName;/*from w w w.ja v a 2 s. c o m*/ } if (lookupCache.containsKey(cacheKey)) { return (Method) lookupCache.get(cacheKey); } String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { lookupCache.put(cacheKey, method); return method; } } lookupCache.put(cacheKey, null); throw new PropertyException( "no readable property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java
/** * Determine whether the given method is an "equals" method. * @see java.lang.Object#equals(Object)//from w w w . j av a 2s.c o m */ public static boolean isEqualsMethod(Method method) { if (method == null || !method.getName().equals("equals")) { return false; } Class<?>[] paramTypes = method.getParameterTypes(); return (paramTypes.length == 1 && paramTypes[0] == Object.class); }
From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java
/** * Determine whether the given method is originally declared by {@link java.lang.Object}. *//*from w ww . j a v a 2s. c o m*/ public static boolean isObjectMethod(Method method) { if (method == null) { return false; } try { Object.class.getDeclaredMethod(method.getName(), method.getParameterTypes()); return true; } catch (Exception ex) { return false; } }