Main { {{ public static ClassGraph cg = new ClassGraph(true,false); public static final String FIDENT = " edu.neu.ccs.demeter.Ident "; public static void main(String args[]) throws Exception { Cd_graph m = Cd_graph.parse(System.in); System.out.println("Input parsed!"); m.initialize(); /* System.out.print("Defined classes: "); System.out.println(m.definedClasses); System.out.print("Imported Classes: "); System.out.println(ClassFinder.predefined); System.out.print("Imported Packages: "); System.out.println(ClassFinder.packageNames); */ System.out.println("Checking to ensure single inheritance..."); if (!m.resolveSuperClassStructure()) { System.out.println("===Single Inheritance Violation Detected!==="); printMultiInheritClasses(m); System.out.println("Java does not support multiple inheritance.\nPlease fix your class dictionary."); System.out.println("halting..."); System.exit(1); } System.out.println("Checking for inheritance class cycles..."); if (m.hasSuperCycle()) { System.out.println("===Class inheritance cycle detected!==="); printAllClassCycles(m); System.out.println("Java does not support inheritance cycles.\nPlease fix your class dictionary."); System.out.println("halting..."); System.exit(1); } System.out.println("Checking to ensure unique part names..."); m.flatten(); System.out.println("Checking for undefined parts..."); if (m.hasUndefinedParts()) { System.out.println(m.undefinedParts); } Iterator iter = Cd_graph.adjs.keySet().iterator(); Adjacency temp; for (; iter.hasNext(); ) { temp = (Adjacency)Cd_graph.adjs.get(iter.next()); if (temp.tbrV) System.out.println("WARNING: " + temp.getName() + " violates the terminal buffer rule!"); } // m.display(); System.out.println(); //System.out.println(cg); } public static final boolean checkClass(String packageName, String className) { try { Class.forName(packageName + "." + className, false, null); } catch (ClassNotFoundException cnfe) { return false; } return true; } public static final void printMultiInheritClasses(Cd_graph cdg) { Visitor v = new Visitor() { void before(Adjacency host) { if (host.isMultiInherit()) { System.out.print(host.getName() + " inherits from: "); host.printSupers(); System.out.println(); } } }; new TraversalGraph("from Cd_graph to Adjacency", Main.cg).traverse(cdg, v); } public static final void printAllClassCycles(Cd_graph cdg) { List cycles = cdg.getCycleErrors(); Iterator iter = cycles.iterator(); List cycle; Iterator innerIter; for (; iter.hasNext(); ) { cycle = (List)iter.next(); for (innerIter = cycle.iterator(); innerIter.hasNext(); ) { System.out.print(innerIter.next().toString() + " -> "); } System.out.println(cycle.get(0).toString()); } } }} } Cd_graph { {{ static Map adjs = new HashMap(); static Set definedClasses = new HashSet(); static List cErrors; public static final String bypassed = " bypassing {Strategy, TraversalGraph, ClassGraph, String, Map} "; public static final Strategy allAdjsStrategy = new Strategy("{Cd_graph -> Adjacency}"); public static final Strategy importStrategy = new Strategy("{GlobalImports -> Import}"); public static final Strategy superClassResolutionStrategy = new Strategy("{Cd_graph -> Alternat_ns}" + bypassed); public static final Strategy superClassChainResolutionStrategy = new Strategy("{Cd_graph -> Adjacency}"); public static final Strategy flatteningStrategy = new Strategy("from Cd_graph through Adjacency" + bypassed + "to Neighbors_wc"); public final TraversalGraph flatteningGraph = new TraversalGraph(flatteningStrategy, Main.cg); void initialize() { TraversalGraph tgA = new TraversalGraph(allAdjsStrategy, Main.cg); Iterator iter = tgA.asList(this).iterator(); Adjacency adj; for(;iter.hasNext();) { adj = (Adjacency)iter.next(); definedClasses.add(adj.getName().intern()); adjs.put(adj.getName(), adj); } TraversalGraph tgI = new TraversalGraph(importStrategy, Main.cg); iter = tgI.gather(get_globalimports()).iterator(); String s; for (;iter.hasNext();) { s = iter.next().toString(); if (s.endsWith(".*")) { ClassFinder.addPackage(s.substring(0, s.length()-2)); } else { ClassFinder.addClass(s.substring(s.lastIndexOf(".")+1)); } } } static Set undefinedParts = new HashSet(); boolean hasUndefinedParts() { TraversalGraph tgA = new TraversalGraph(allAdjsStrategy, Main.cg); Iterator iter = tgA.asList(this).iterator(); Adjacency adj; for (;iter.hasNext();) { adj = (Adjacency) iter.next(); adj.hasUndefinedParts(); } return undefinedParts.size()>0; } boolean resolveSuperClassStructure() { TraversalGraph tg = new TraversalGraph(superClassResolutionStrategy, Main.cg); SuperClassResolutionVisitor scrv = new SuperClassResolutionVisitor(); tg.traverse(this, scrv); return ((Boolean)scrv.getReturnValue()).booleanValue(); } boolean hasSuperCycle() { TraversalGraph tg = new TraversalGraph(superClassChainResolutionStrategy, Main.cg); SuperClassChainResolutionVisitor sccrv = new SuperClassChainResolutionVisitor(); tg.traverse(this, sccrv); cErrors = (List)sccrv.getReturnValue(); return cErrors.size()>0; } List getCycleErrors() { return cErrors; } void flatten() { flatteningGraph.traverse(this, new FlatteningVisitor()); } }} } Import { {{ Strategy toStringStrategy = new Strategy("from Import to *"); TraversalGraph toStringTraversal = new TraversalGraph(toStringStrategy, Main.cg); public String toString() { return toStringTraversal.traverse(this, new ImportVisitor()).toString(); } }} } Adjacency { {{ public static final String bypassed = " bypassing {Strategy, TraversalGraph, ClassGraph, String} "; public static final Strategy nameStrategy = new Strategy("from Vertex to" + Main.FIDENT + bypassed); public static final Strategy allNormalStrategy = new Strategy("{Adjacency -> Normal}" + bypassed); public static final Strategy onlyPartsStrategy = new Strategy("from Adjacency through Any_vertex_List to Normal"); public final TraversalGraph nameGraph = new TraversalGraph(nameStrategy, Main.cg); List immediateSuperClasses = new ArrayList(); List inheritedParts = new ArrayList(); String getName() { return nameGraph.fetch(get_source()).toString(); } void addPart(String partName, String partClassName) { // System.out.println("Adding " + partClassName + "..." + getName()); inheritedParts.add(partClassName); ns.addPart(partName); } void addSuperClass(String superClassName) { immediateSuperClasses.add(superClassName); } boolean isMultiInherit() { return immediateSuperClasses.size()>1; } String getSuperClass() { int size = immediateSuperClasses.size(); switch (size) { case 0: return null; case 1: return (String)immediateSuperClasses.get(0); default: throw new RuntimeException("getSuperClass():Multiple Inheritance Failure"); } } void printSupers() { Iterator iter = immediateSuperClasses.iterator(); for (;iter.hasNext();) { System.out.print(iter.next()); if (iter.hasNext()) System.out.print(", "); } } List checkCycle() { String superName = getSuperClass(); List supers = new ArrayList(); Adjacency adj; if (superName == null) { return null; } else { while (superName != null && !supers.contains(superName)) { supers.add(superName); adj = (Adjacency)Cd_graph.adjs.get(superName); superName = adj.getSuperClass(); } if (supers.contains(superName)) { return supers; } return null; } } static final String paramStrategy = "from Vertex_Comma_list to "+ Main.FIDENT; boolean tbrV = false; boolean hasUndefinedParts() { TraversalGraph tgp; List params = null; if (parameters != null) { tgp = new TraversalGraph(paramStrategy, Main.cg); params = tgp.gather(parameters); Iterator pIter = params.iterator(); for (;pIter.hasNext();) { Cd_graph.definedClasses.add(pIter.next().toString()); } } TraversalGraph tg = new TraversalGraph(onlyPartsStrategy, Main.cg); List parts = tg.gather(this); Iterator iter = parts.iterator(); boolean terminalEncountered = false; String s; // System.out.print("["); for (; iter.hasNext();) { s = ((Normal)iter.next()).get_vertex().get_vertex_name().toString(); // System.out.print(" " + s + " "); if (!Cd_graph.definedClasses.contains(s)) { if (!ClassFinder.classExist(s)) { Cd_graph.undefinedParts.add(s); } else { terminalEncountered = true; if (parts.size()>1) tbrV = true; } } } // System.out.println("]"); if (inheritedParts.size()>0) { Iterator ipIter = inheritedParts.iterator(); for(;(!tbrV) && ipIter.hasNext() ;) { s = ipIter.next().toString(); if ((!Cd_graph.definedClasses.contains(s)) && ClassFinder.classExist(s)) { if (terminalEncountered) tbrV = true; else terminalEncountered = true; } } } if (parameters != null) { Iterator pIter = params.iterator(); for (;pIter.hasNext();) { Cd_graph.definedClasses.remove(pIter.next().toString()); } } if (tbrV) propagateTBRV(); // if (getSuperClass() != null) // tbrV = tbrV || ((Adjacency)(Cd_graph.adjs.get(getSuperClass()))).tbrV; // if (tbrV) System.out.println("WARNING: " + getName() + " violates the terminal buffer rule!"); return Cd_graph.undefinedParts.size()>0; } void propagateTBRV() { tbrV = true; get_ns().propagateTBRV(); } }} }