@Test void testFindClassesWithAnnotatedMethods() throws Exception { List<Class<?>> ret = PluginsViaAnnotations.findClassesWithAnnotatedMethods("reflection", MyTestAnnotation.class); assertEquals(1, ret.size()); assertSame(PluginsViaAnnotationsTest.class, ret.get(0)); }
public static List<Class<?>> findClassesWithAnnotatedMethods(String packageName, Class<? extends Annotation> methodAnnotationClass) throws Exception { List<Class<?>> ret = new ArrayList<>(); String[] clazzNames = ClassesInPackage.getPackageContent(packageName); for (String clazzName : clazzNames) { if (!clazzName.endsWith(".class")) { continue; } clazzName = clazzName.replace('/', '.').replace(".class", ""); Class<?> c = null; try { c = Class.forName(clazzName); } catch (ClassNotFoundException ex) { System.err.println("Weird: class " + clazzName + " reported in package but gave CNFE: " + ex); continue; } for (Method m : c.getDeclaredMethods()) { if (m.isAnnotationPresent(methodAnnotationClass) && !ret.contains(c)) { ret.add(c); } } } return ret; }
PluginsViaAnnotations { public static List<Class<?>> findClassesWithAnnotatedMethods(String packageName, Class<? extends Annotation> methodAnnotationClass) throws Exception { List<Class<?>> ret = new ArrayList<>(); String[] clazzNames = ClassesInPackage.getPackageContent(packageName); for (String clazzName : clazzNames) { if (!clazzName.endsWith(".class")) { continue; } clazzName = clazzName.replace('/', '.').replace(".class", ""); Class<?> c = null; try { c = Class.forName(clazzName); } catch (ClassNotFoundException ex) { System.err.println("Weird: class " + clazzName + " reported in package but gave CNFE: " + ex); continue; } for (Method m : c.getDeclaredMethods()) { if (m.isAnnotationPresent(methodAnnotationClass) && !ret.contains(c)) { ret.add(c); } } } return ret; } }
PluginsViaAnnotations { public static List<Class<?>> findClassesWithAnnotatedMethods(String packageName, Class<? extends Annotation> methodAnnotationClass) throws Exception { List<Class<?>> ret = new ArrayList<>(); String[] clazzNames = ClassesInPackage.getPackageContent(packageName); for (String clazzName : clazzNames) { if (!clazzName.endsWith(".class")) { continue; } clazzName = clazzName.replace('/', '.').replace(".class", ""); Class<?> c = null; try { c = Class.forName(clazzName); } catch (ClassNotFoundException ex) { System.err.println("Weird: class " + clazzName + " reported in package but gave CNFE: " + ex); continue; } for (Method m : c.getDeclaredMethods()) { if (m.isAnnotationPresent(methodAnnotationClass) && !ret.contains(c)) { ret.add(c); } } } return ret; }  }
PluginsViaAnnotations { public static List<Class<?>> findClassesWithAnnotatedMethods(String packageName, Class<? extends Annotation> methodAnnotationClass) throws Exception { List<Class<?>> ret = new ArrayList<>(); String[] clazzNames = ClassesInPackage.getPackageContent(packageName); for (String clazzName : clazzNames) { if (!clazzName.endsWith(".class")) { continue; } clazzName = clazzName.replace('/', '.').replace(".class", ""); Class<?> c = null; try { c = Class.forName(clazzName); } catch (ClassNotFoundException ex) { System.err.println("Weird: class " + clazzName + " reported in package but gave CNFE: " + ex); continue; } for (Method m : c.getDeclaredMethods()) { if (m.isAnnotationPresent(methodAnnotationClass) && !ret.contains(c)) { ret.add(c); } } } return ret; }  static List<Class<?>> findAnnotatedClasses(String packageName,
		Class<? extends Annotation> annotationClass); static List<Class<?>> findClassesWithAnnotatedMethods(String packageName, 
			Class<? extends Annotation> methodAnnotationClass); }
PluginsViaAnnotations { public static List<Class<?>> findClassesWithAnnotatedMethods(String packageName, Class<? extends Annotation> methodAnnotationClass) throws Exception { List<Class<?>> ret = new ArrayList<>(); String[] clazzNames = ClassesInPackage.getPackageContent(packageName); for (String clazzName : clazzNames) { if (!clazzName.endsWith(".class")) { continue; } clazzName = clazzName.replace('/', '.').replace(".class", ""); Class<?> c = null; try { c = Class.forName(clazzName); } catch (ClassNotFoundException ex) { System.err.println("Weird: class " + clazzName + " reported in package but gave CNFE: " + ex); continue; } for (Method m : c.getDeclaredMethods()) { if (m.isAnnotationPresent(methodAnnotationClass) && !ret.contains(c)) { ret.add(c); } } } return ret; }  static List<Class<?>> findAnnotatedClasses(String packageName,
		Class<? extends Annotation> annotationClass); static List<Class<?>> findClassesWithAnnotatedMethods(String packageName, 
			Class<? extends Annotation> methodAnnotationClass);  }