List of usage examples for java.lang Class getDeclaredMethods
@CallerSensitive public Method[] getDeclaredMethods() throws SecurityException
From source file:com.mawujun.util.AnnotationUtils.java
/** * <p>Checks if two annotations are equal using the criteria for equality * presented in the {@link Annotation#equals(Object)} API docs.</p> * * @param a1 the first Annotation to compare, {@code null} returns * {@code false} unless both are {@code null} * @param a2 the second Annotation to compare, {@code null} returns * {@code false} unless both are {@code null} * @return {@code true} if the two annotations are {@code equal} or both * {@code null}// w ww . ja va2 s . com */ public static boolean equals(Annotation a1, Annotation a2) { if (a1 == a2) { return true; } if (a1 == null || a2 == null) { return false; } Class<? extends Annotation> type = a1.annotationType(); Class<? extends Annotation> type2 = a2.annotationType(); Validate.notNull(type, "Annotation %s with null annotationType()", a1); Validate.notNull(type2, "Annotation %s with null annotationType()", a2); if (!type.equals(type2)) { return false; } try { for (Method m : type.getDeclaredMethods()) { if (m.getParameterTypes().length == 0 && isValidAnnotationMemberType(m.getReturnType())) { Object v1 = m.invoke(a1); Object v2 = m.invoke(a2); if (!memberEquals(m.getReturnType(), v1, v2)) { return false; } } } } catch (IllegalAccessException ex) { return false; } catch (InvocationTargetException ex) { return false; } return true; }
From source file:org.apache.sling.testing.mock.osgi.OsgiServiceUtil.java
private static Method getMethodWithAssignableTypes(Class clazz, String methodName, Class<?>[] types) { Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (StringUtils.equals(method.getName(), methodName) && method.getParameterTypes().length == types.length) { boolean foundMismatch = false; for (int i = 0; i < types.length; i++) { if (!method.getParameterTypes()[i].isAssignableFrom(types[i])) { foundMismatch = true; break; }/*from w ww . j a va 2 s .co m*/ } if (!foundMismatch) { return method; } } } // not found? check super classes Class<?> superClass = clazz.getSuperclass(); if (superClass != null && superClass != Object.class) { return getMethodWithAssignableTypes(superClass, methodName, types); } return null; }
From source file:org.apache.sling.testing.mock.osgi.OsgiServiceUtil.java
private static Method getMethod(Class clazz, String methodName, Class<?>[] types) { Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (StringUtils.equals(method.getName(), methodName) && method.getParameterTypes().length == types.length) { boolean foundMismatch = false; for (int i = 0; i < types.length; i++) { if (!((method.getParameterTypes()[i] == types[i]) || (types[i] == Annotation.class && method.getParameterTypes()[i].isAnnotation()))) { foundMismatch = true; break; }/*from w ww .jav a 2 s. c o m*/ } if (!foundMismatch) { return method; } } } // not found? check super classes Class<?> superClass = clazz.getSuperclass(); if (superClass != null && superClass != Object.class) { return getMethod(superClass, methodName, types); } return null; }
From source file:org.apache.sling.testing.mock.osgi.OsgiServiceUtil.java
private static Method getMethodWithAnyCombinationArgs(Class clazz, String methodName, Class<?>[] types) { Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (StringUtils.equals(method.getName(), methodName) && method.getParameterTypes().length > 1) { boolean foundMismatch = false; for (Class<?> parameterType : method.getParameterTypes()) { boolean foundAnyMatch = false; for (int i = 0; i < types.length; i++) { if ((parameterType == types[i]) || (types[i] == Annotation.class && parameterType.isAnnotation())) { foundAnyMatch = true; break; }/* www .j a va2s . c om*/ } if (!foundAnyMatch) { foundMismatch = true; break; } } if (!foundMismatch) { return method; } } } // not found? check super classes Class<?> superClass = clazz.getSuperclass(); if (superClass != null && superClass != Object.class) { return getMethodWithAnyCombinationArgs(superClass, methodName, types); } return null; }
From source file:com.snaplogic.snaps.firstdata.Create.java
/** * finds the declared getter methods in the given classtype * * @param c/*from www . j ava 2 s . c o m*/ * @return ArrayList<Method> */ public static ArrayList<Method> findGetters(Class<?> c) { ArrayList<Method> list = new ArrayList<Method>(); Method[] methods = c.getDeclaredMethods(); for (Method method : methods) { if (isGetter(method)) { list.add(method); } } return list; }
From source file:com.snaplogic.snaps.firstdata.Create.java
static ArrayList<Method> findSetters(Class<?> classType) { ArrayList<Method> list = new ArrayList<Method>(); Method[] methods = classType.getDeclaredMethods(); for (Method method : methods) { if (isSetter(method)) { list.add(method);//w w w .j a v a 2 s .co m } } return list; }
From source file:org.thoughtland.xlocation.Util.java
public static Method getMethod(Class<?> clazz, String name, Object[] args) { Util.log(null, Log.DEBUG, "Looking for " + name); for (Method m : clazz.getDeclaredMethods()) { Util.log(null, Log.DEBUG, "Got method " + clazz.getSimpleName() + "." + m.getName() + "( " + TextUtils.join(", ", m.getParameterTypes()) + " )"); if (m.getName().equals(name)) { Util.log(null, Log.DEBUG, " names match!"); if (args == null) return m; Class<?>[] params = m.getParameterTypes(); if (params.length == args.length) { Util.log(null, Log.DEBUG, " params length match!"); boolean matches = true; for (int i = 0; i < params.length; i++) { if (args[i] != null && !params[i].isInstance(args[i])) { Util.log(null, Log.DEBUG, " param[" + i + "] " + args[i].getClass().getName() + " does not match type " + params[i].getSimpleName()); matches = false; break; }//from ww w . j a v a 2s . c om } if (matches) { Util.log(null, Log.DEBUG, "Found match for " + name); return m; } } } } return null; }
From source file:gov.nih.nci.caarray.util.CaArrayUtils.java
/** * For given class, returns a ReflectionHelper instance with property accessors for the class. * /*w ww. j a va 2s.co m*/ * @param clazz the class * @return the ReflectionHelper */ public static ReflectionHelper createReflectionHelper(Class<?> clazz) { final List<PropertyAccessor> accessors = new ArrayList<PropertyAccessor>(); Class<?> currentClass = clazz; while (currentClass != null) { final Method[] methods = currentClass.getDeclaredMethods(); for (final Method getter : methods) { if (getter.getName().startsWith("get") && getter.getParameterTypes().length == 0) { for (final Method setter : methods) { if (setter.getName().equals('s' + getter.getName().substring(1)) && setter.getParameterTypes().length == 1 && Void.TYPE.equals(setter.getReturnType()) && getter.getReturnType().equals(setter.getParameterTypes()[0])) { getter.setAccessible(true); setter.setAccessible(true); accessors.add(new PropertyAccessor(getter, setter)); } } } } currentClass = currentClass.getSuperclass(); } return new ReflectionHelper(accessors.toArray(new PropertyAccessor[accessors.size()])); }
From source file:com.liferay.cli.support.util.ReflectionUtils.java
/** * Attempt to find a {@link Method} on the supplied class with the supplied * name and parameter types. Searches all superclasses up to * <code>Object</code>.//from w w w . java 2 s. c om * <p> * Returns <code>null</code> if no {@link Method} can be found. * * @param clazz the class to introspect * @param name the name of the method * @param parameterTypes the parameter types of the method (may be * <code>null</code> to indicate any signature) * @return the Method object, or <code>null</code> if none found */ public static Method findMethod(final Class<?> clazz, final String name, final Class<?>[] parameterTypes) { Validate.notNull(clazz, "Class must not be null"); Validate.notNull(name, "Method name must not be null"); Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { final Method[] methods = searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods(); for (final Method method : methods) { if (name.equals(method.getName()) && (parameterTypes == null || Arrays.equals(parameterTypes, method.getParameterTypes()))) { return method; } } searchType = searchType.getSuperclass(); } return null; }
From source file:com.liferay.cli.support.util.ReflectionUtils.java
/** * Perform the given callback operation on all matching methods of the given * class and superclasses./*from w w w.ja v a 2 s .c o m*/ * <p> * The same named method occurring on subclass and superclass will appear * twice, unless excluded by the specified {@link MethodFilter}. * * @param targetClass class to start looking at * @param mc the callback to invoke for each method * @param mf the filter that determines the methods to apply the callback to */ public static void doWithMethods(Class<?> targetClass, final MethodCallback mc, final MethodFilter mf) throws IllegalArgumentException { // Keep backing up the inheritance hierarchy. do { final Method[] methods = targetClass.getDeclaredMethods(); for (final Method method : methods) { if (mf != null && !mf.matches(method)) { continue; } try { mc.doWith(method); } catch (final IllegalAccessException ex) { throw new IllegalStateException( "Shouldn't be illegal to access method '" + method.getName() + "': " + ex); } } targetClass = targetClass.getSuperclass(); } while (targetClass != null); }