package SBST2015; import java.lang.reflect.*; import java.net.URL; import java.util.*; import org.reflections.Reflections ; import org.reflections.scanners.SubTypesScanner; import org.reflections.util.ClasspathHelper; import Sequenic.T3.*; public class Inspection { /** * Check if C has non-private instance method or constructor that it declares itself. */ public static boolean has_NonPrivateDeclared_IMethod_OrConstructor(Class C) { Method[] methods = C.getDeclaredMethods() ; for (Method m : methods) { int flag = m.getModifiers() ; if (!Modifier.isStatic(flag) && !Modifier.isPrivate(flag)) return true ; } Constructor[] constructors = C.getDeclaredConstructors() ; for (Constructor c : constructors) { int flag = c.getModifiers() ; if(!Modifier.isStatic(flag) && !Modifier.isPrivate(flag)) return true ; } return false ; } /** * Check if C has non-private static method that it declares itself. */ public static boolean has_NonPrivateDeclared_SMethod(Class C) { Method[] methods = C.getDeclaredMethods() ; for (Method m : methods) { int flag = m.getModifiers() ; if (Modifier.isStatic(flag) && !Modifier.isPrivate(flag)) return true ; } return false ; } /** * Return static inner class. */ public static List getStaticInnerClasses(Class C) { Class[] innerclasses = C.getDeclaredClasses() ; List R = new LinkedList() ; for (Class ic : innerclasses) { if (ic.isInterface()) continue ; int flag = ic.getModifiers() ; //if (Modifier.isStatic(flag) && !Modifier.isPrivate(flag)) R.add(ic) ; if (Modifier.isStatic(flag)) R.add(ic) ; } return R ; } public static List getPrivateConstructors(Class C) { List cons = new LinkedList() ; Constructor[] constructors = C.getDeclaredConstructors() ; for (Constructor c : constructors) { int flag = c.getModifiers() ; if(Modifier.isPrivate(flag)) cons.add(c) ; } return cons ; } /** * Get all concrete classes that implement an interface in a package. */ public static List getAllConcreteClasses(String pckgName) { Reflections reflections = new Reflections(pckgName, new SubTypesScanner(false)); Set allclasses_ = reflections.getSubTypesOf(Object.class) ; Set allclasses = (Set) allclasses_ ; List R = new LinkedList() ; for (Class C : allclasses) { System.err.println("** seeing " + C + " ...") ; if (C.isInterface()) { for (Class D : allclasses) { int mod = D.getModifiers() ; if (!D.isInterface() && !Modifier.isAbstract(mod) && Modifier.isPublic(mod) && C.isAssignableFrom(D)) { R.add(D) ; } } } } return R ; } public static Class[] getAllConcreteClassesUnderParentPackage(Class CUT) { List R = new LinkedList() ; try { String pckgName = CUT.getPackage().getName() ; String pckgParentName = pckgName ; System.err.println("** trying to scan " + pckgParentName) ; if (pckgName.contains(".")) pckgParentName = pckgName.substring(0, pckgName.lastIndexOf(".")) ; R = getAllConcreteClasses(pckgParentName) ; } catch (Throwable e) { System.err.println("** fail to scan package for concrete classes.") ; System.err.println("** " + e) ; } Class[] R_ = new Class[R.size()] ; for (int k=0; k cs = getAllConcreteClasses("Sequenic.T3") ; //for (Class C : cs) System.out.println(">> " + C) ; } }