List of usage examples for java.lang Class getDeclaredMethods
@CallerSensitive public Method[] getDeclaredMethods() throws SecurityException
From source file:Main.java
public static boolean hasSdCard() { boolean sResult = false; String stateString = null;/*from ww w . ja va 2s . com*/ try { Class c = Class.forName("android.os.SystemProperties"); Method m[] = c.getDeclaredMethods(); try { try { stateString = (String) m[1].invoke(null, "EXTERNAL_STORAGE_STATE", "unmounted"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (stateString != null) { if (stateString.equals(Environment.MEDIA_MOUNTED)) { sResult = true; } } return sResult; }
From source file:Main.java
public static Method findMethodByNameAndReturnType(Class<?> targetObject, String methodName, String returnType, Class<?>... params) { for (Method method : targetObject.getDeclaredMethods()) { if (method.getReturnType().getName().equals(returnType) && method.getName().equals(methodName)) { Class[] parameterTypes = method.getParameterTypes(); if (params.length != parameterTypes.length) { continue; }/*from w w w. j a va2 s . c om*/ for (int i = 0; i < params.length; i++) { if (params[i] != parameterTypes[i]) { break; } } method.setAccessible(true); return method; } } throw new NoSuchMethodError(); }
From source file:koper.util.ReflectUtil.java
public static Optional<Method> getMethod(Class<?> clazz, String eventName, Predicate<Method> methodPredicate) { final Method[] methods = clazz.getDeclaredMethods(); return Arrays.stream(methods).filter(method -> method.getName().equals(eventName)).filter(methodPredicate) .findAny();/* w w w .java 2 s. c o m*/ }
From source file:ReflectApp.java
static void describeClassOrInterface(Class className, String name) { displayModifiers(className.getModifiers()); displayFields(className.getDeclaredFields()); displayMethods(className.getDeclaredMethods()); if (className.isInterface()) { System.out.println("Interface: " + name); } else {//from w w w .j a v a 2 s .c om System.out.println("Class: " + name); displayInterfaces(className.getInterfaces()); displayConstructors(className.getDeclaredConstructors()); } }
From source file:com.netflix.astyanax.util.StringUtils.java
public static <T> String joinClassGettersValues(final T object, String name, Class<T> clazz) { Method[] methods = clazz.getDeclaredMethods(); StringBuilder sb = new StringBuilder(); sb.append(name).append("["); sb.append(org.apache.commons.lang.StringUtils.join(Collections2.transform( // Filter any field that does not start with lower case // (we expect constants to start with upper case) Collections2.filter(Arrays.asList(methods), new Predicate<Method>() { @Override/*from ww w. ja v a 2s .c om*/ public boolean apply(Method method) { if ((method.getModifiers() & Modifier.STATIC) == Modifier.STATIC) return false; return org.apache.commons.lang.StringUtils.startsWith(method.getName(), "get"); } }), // Convert field to "name=value". value=*** on error new Function<Method, String>() { @Override public String apply(Method method) { Object value; try { value = method.invoke(object); } catch (Exception e) { value = "***"; } return org.apache.commons.lang.StringUtils.uncapitalize( org.apache.commons.lang.StringUtils.substring(method.getName(), 3)) + "=" + value; } }), ",")); sb.append("]"); return sb.toString(); }
From source file:Main.java
@SuppressWarnings("rawtypes") private static List<Method> getMothds(Class clazz, boolean includeParentClass) { List<Method> list = new ArrayList<Method>(); Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { list.add(method);//w w w . j av a 2s . c om } if (includeParentClass) { getParentClassMothds(list, clazz.getSuperclass()); } return list; }
From source file:org.apache.sling.models.impl.ReflectionUtil.java
private static void addAnnotatedMethodsFromInterfaces(Class<?> type, List<Method> result) { for (Class<?> iface : type.getInterfaces()) { Method[] methods = iface.getDeclaredMethods(); addAnnotated(methods, result);/* w w w . j a va 2 s .co m*/ addAnnotatedMethodsFromInterfaces(iface, result); } }
From source file:Main.java
public static <T extends Object, Result> Result callMethodCast(T receiver, Class<Result> resultClass, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (receiver == null || methodName == null) { return null; }//from w w w .j a va 2s . com Class<?> cls = receiver.getClass(); Method toInvoke = null; do { Method[] methods = cls.getDeclaredMethods(); methodLoop: for (Method method : methods) { if (!methodName.equals(method.getName())) { continue; } Class<?>[] paramTypes = method.getParameterTypes(); if (args == null && paramTypes == null) { toInvoke = method; break; } else if (args == null || paramTypes == null || paramTypes.length != args.length) { continue; } for (int i = 0; i < args.length; ++i) { if (!paramTypes[i].isAssignableFrom(args[i].getClass())) { continue methodLoop; } } toInvoke = method; } } while (toInvoke == null && (cls = cls.getSuperclass()) != null); Result t; if (toInvoke != null) { boolean accessible = toInvoke.isAccessible(); toInvoke.setAccessible(true); if (resultClass != null) { t = resultClass.cast(toInvoke.invoke(receiver, args)); } else { toInvoke.invoke(receiver, args); t = null; } toInvoke.setAccessible(accessible); return t; } else { throw new NoSuchMethodException("Method " + methodName + " not found"); } }
From source file:cz.muni.fi.editor.database.test.helpers.AbstractDAOTest.java
protected static void checkMethods(Class testClass, Class testingInterface) { Set<String> testMethods = new HashSet<>(); for (Method m : testClass.getDeclaredMethods()) { if (m.isAnnotationPresent(Test.class)) { testMethods.add(m.getName()); }// w w w . jav a 2 s.c o m } List<String> targetMethods = new ArrayList<>( Arrays.asList("create", "update", "getById", "delete", "getAll", "getClassType")); targetMethods.addAll(new ArrayList<>(Arrays.asList(testingInterface.getDeclaredMethods())).stream() .map(m -> m.getName()).collect(Collectors.toList())); testMethods.forEach(targetMethods::remove); Assert.assertEquals("Following method(s) are missing in DAO test :" + targetMethods.toString(), 0, targetMethods.size()); }
From source file:Main.java
public static List<Method> findAnnotatedMethods(final Class<?> type, final Class<? extends Annotation> annotation) { final List<Method> methods = new ArrayList<>(); Method[] ms = type.getDeclaredMethods(); for (Method method : ms) { // Must not static if (Modifier.isStatic(method.getModifiers())) { continue; }//from www. j a v a 2 s . c o m // Must be public if (Modifier.isPublic(method.getModifiers())) { continue; } // Must has only one parameter if (method.getParameterTypes().length != 1) { continue; } // Must has annotation if (!method.isAnnotationPresent(annotation)) { continue; } methods.add(method); } return methods; }