List of usage examples for java.lang Class getMethods
@CallerSensitive public Method[] getMethods() throws SecurityException
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
public static List<Method> getAllPublicMethods(List<Method> methods, Class<?> c) { methods.addAll(Arrays.asList(c.getMethods())); if (c.getSuperclass() != null) { methods = getAllPublicMethods(methods, c.getSuperclass()); }/*from ww w . ja v a 2s. co m*/ return methods; }
From source file:com.googlecode.jsonplugin.JSONUtil.java
/** * List visible methods carrying the @SMDMethod annotation * * @param ignoreInterfaces if true, only the methods of the class are examined. If false, annotations on * every interfaces' methods are examined. *///w w w .ja va 2 s . c om @SuppressWarnings("unchecked") public static Method[] listSMDMethods(Class clazz, boolean ignoreInterfaces) { final List<Method> methods = new LinkedList<Method>(); if (ignoreInterfaces) { for (Method method : clazz.getMethods()) { SMDMethod smdMethodAnnotation = method.getAnnotation(SMDMethod.class); if (smdMethodAnnotation != null) { methods.add(method); } } } else { // recurse the entire superclass/interface hierarchy and add in order encountered JSONUtil.visitInterfaces(clazz, new JSONUtil.ClassVisitor() { public boolean visit(Class aClass) { for (Method method : aClass.getMethods()) { SMDMethod smdMethodAnnotation = method.getAnnotation(SMDMethod.class); if (smdMethodAnnotation != null && !methods.contains(method)) { methods.add(method); } } return true; } }); } Method[] methodResult = new Method[methods.size()]; return methods.toArray(methodResult); }
From source file:com.springframework.beans.BeanUtils.java
/** * Find a method with the given method name and minimal parameters (best case: none), * declared on the given class or one of its superclasses. Prefers public methods, * but will return a protected, package access, or private method too. * <p>Checks {@code Class.getMethods} first, falling back to * {@code findDeclaredMethodWithMinimalParameters}. This allows for finding public * methods without issues even in environments with restricted Java security settings. * @param clazz the class to check/*from w ww . j av a2 s . co m*/ * @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 * @see Class#getMethods * @see #findDeclaredMethodWithMinimalParameters */ public static Method findMethodWithMinimalParameters(Class<?> clazz, String methodName) throws IllegalArgumentException { Method targetMethod = findMethodWithMinimalParameters(clazz.getMethods(), methodName); if (targetMethod == null) { targetMethod = findDeclaredMethodWithMinimalParameters(clazz, methodName); } return targetMethod; }
From source file:com.github.dozermapper.core.util.ReflectionUtils.java
private static Method findPreferablyNonSyntheticMethod(String methodName, Class<?> clazz) { Method[] methods = clazz.getMethods(); Method syntheticMethod = null; for (Method method : methods) { if (method.getName().equals(methodName)) { if (!method.isBridge() && !method.isSynthetic()) { return method; } else { syntheticMethod = method; }/* ww w . java 2 s .c o m*/ } } return syntheticMethod; }
From source file:Main.java
/** * search the method and return the defined method. * it will {@link Class#getMethod(String, Class[])}, if exception occurs, * it will search for all methods, and find the most fit method. */// w w w . j a v a 2s. c om public static Method searchMethod(Class<?> currentClass, String name, Class<?>[] parameterTypes, boolean boxed) throws NoSuchMethodException { if (currentClass == null) { throw new NoSuchMethodException("class == null"); } try { return currentClass.getMethod(name, parameterTypes); } catch (NoSuchMethodException e) { Method likeMethod = null; for (Method method : currentClass.getMethods()) { if (method.getName().equals(name) && parameterTypes.length == method.getParameterTypes().length && Modifier.isPublic(method.getModifiers())) { if (parameterTypes.length > 0) { Class<?>[] types = method.getParameterTypes(); boolean eq = true; boolean like = true; for (int i = 0; i < parameterTypes.length; i++) { Class<?> type = types[i]; Class<?> parameterType = parameterTypes[i]; if (type != null && parameterType != null && !type.equals(parameterType)) { eq = false; if (boxed) { type = getBoxedClass(type); parameterType = getBoxedClass(parameterType); } if (!type.isAssignableFrom(parameterType)) { eq = false; like = false; break; } } } if (!eq) { if (like && (likeMethod == null || likeMethod.getParameterTypes()[0] .isAssignableFrom(method.getParameterTypes()[0]))) { likeMethod = method; } continue; } } return method; } } if (likeMethod != null) { return likeMethod; } throw e; } }
From source file:net.kamhon.ieagle.util.VoUtil.java
/** * To upperCase object String field/*from w ww. j a va 2s . com*/ * * @param all * default is false. true means toUpperCase all String field, false toUpperCase the fields have * net.kamhon.ieagle.vo.core.annotation.ToUpperCase. * * @param objects */ public static void toUpperCaseProperties(boolean all, Object... objects) { for (Object object : objects) { if (object == null) { continue; } // getter for String field only Map<String, Method> getterMap = new HashMap<String, Method>(); // setter for String field only Map<String, Method> setterMap = new HashMap<String, Method>(); Class<?> clazz = object.getClass(); Method[] methods = clazz.getMethods(); for (Method method : methods) { /* * log.debug("method = " + method.getName()); * log.debug("method.getParameterTypes().length = " + * method.getParameterTypes().length); if * (method.getParameterTypes().length == 1) * log.debug("method.getParameterTypes()[0] = " + * method.getParameterTypes()[0]); * log.debug("method.getReturnType() = " + * method.getReturnType()); * log.debug("=================================================" * ); */ if (method.getName().startsWith("get") && method.getParameterTypes().length == 0 && method.getReturnType().equals(String.class)) { // if method name is getXxx String fieldName = method.getName().substring(3); fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1); getterMap.put(fieldName, method); } else if (method.getName().startsWith("set") && method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == String.class && method.getReturnType().equals(void.class)) { // log.debug("setter = " + method.getName()); // if method name is setXxx String fieldName = method.getName().substring(3); fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1); setterMap.put(fieldName, method); } } // if the key exists in both getter & setter for (String key : getterMap.keySet()) { if (setterMap.containsKey(key)) { try { Method getterMethod = getterMap.get(key); Method setterMethod = setterMap.get(key); // if not all, check on Field if (!all) { Field field = null; Class<?> tmpClazz = clazz; // looping up to superclass to get decleared field do { try { field = tmpClazz.getDeclaredField(key); } catch (Exception ex) { // do nothing } if (field != null) { break; } tmpClazz = tmpClazz.getSuperclass(); } while (tmpClazz != null); ToUpperCase toUpperCase = field.getAnnotation(ToUpperCase.class); if (toUpperCase == null || toUpperCase.upperCase() == false) { continue; } } String value = (String) getterMethod.invoke(object, new Object[] {}); if (StringUtils.isNotBlank(value)) setterMethod.invoke(object, value.toUpperCase()); } catch (Exception ex) { // log.error("Getter Setter for " + key + " has error ", ex); } } } } }
From source file:com.iisigroup.cap.utils.CapBeanUtil.java
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) { Assert.notNull(clazz, "Class must not be null"); Assert.notNull(name, "Method name must not be null"); Class<?> searchType = clazz; while (searchType != null) { Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods()); for (Method method : methods) { if (name.equals(method.getName()) && (paramTypes == null || paramTypes.length == 0 || paramTypes[0] == null || Arrays.equals(paramTypes, method.getParameterTypes()))) { return method; }//from ww w. ja v a 2s .com } searchType = searchType.getSuperclass(); } return null; }
From source file:de.micromata.genome.util.runtime.ClassUtils.java
/** * Returns all 'visible' methods for the given class. Visible methods are: * * - own methods (clazz.getDeclaredMethods()) - all public and protected methods from it's inheritance hierarchy * * @param clazz the Class/*from www.ja va 2s . c o m*/ * @return set of visible methods for that class */ public static Set<Method> getAllVisibleMethods(final Class<?> clazz) { Set<Method> allMethods = new HashSet<>(); allMethods.addAll(Arrays.asList(clazz.getMethods())); allMethods.addAll(Arrays.asList(clazz.getDeclaredMethods())); for (Object obj : ClassUtils.getAllSuperclasses(clazz)) { Class aClass = (Class) obj; for (Method method : aClass.getDeclaredMethods()) { if (Modifier.isProtected(method.getModifiers())) { allMethods.add(method); } } } return allMethods; }
From source file:com.github.dozermapper.core.util.ReflectionUtils.java
private static Method findMethod(Class<?> clazz, String methodName) { Method[] methods = clazz.getMethods(); Method result = null;//from ww w .j av a2 s . c o m for (Method method : methods) { if (method.getName().equals(methodName)) { result = method; } } return result; }
From source file:com.sun.socialsite.util.DebugBeanJsonConverter.java
private static Map<String, Method> getSetters(Object pojo) { Class<?> clazz = pojo.getClass(); Map<String, Method> methods = setters.get(clazz); if (methods != null) { return methods; }//w w w. j a v a 2 s . c o m // Ensure consistent method ordering by using a linked hash map. methods = Maps.newHashMap(); for (Method method : clazz.getMethods()) { String name = getPropertyName(method); if (name != null) { methods.put(name, method); } } setters.put(clazz, methods); return methods; }