Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package net.sf.alchim.codeplus.spoonchecker;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.Stack;
  import java.util.TreeSet;
 
Finds circular dependencies between packages and report warnings when found.
 
 
 
     @Override
     public void init() {
         
                 .add(getFactory().Type().createReference(Environment.class));
         .add(getFactory().Type().createReference(Factory.class));
                 FactoryAccessor.class));
     }
 
 
     @Override
     @SuppressWarnings("unchecked")
     public void analyze(CtPackage element) {
         CtPackageReference pack = element.getReference();
         Set<CtPackageReferencerefs = new TreeSet<CtPackageReference>();
         for (CtSimpleType t : element.getTypes()) {
             for (CtTypeReference tref : Query.getReferences(t,
                     new ReferenceTypeFilter<CtTypeReference>(
                             CtTypeReference.class))) {
                 if ((tref.getPackage() != null)
                         && !tref.getPackage().equals(pack)) {
                     if (.contains(tref)) {
                         continue;
                     }
                     refs.add(tref.getPackage());
                 }
             }
         }
         if (refs.size() > 0) {
             .put(packrefs);
         // if (getFactory().getEnvironment().isVerbose())
         // System.out.println(refs);
         }
     }
 
     @Override
     public void processingDone() {
         // getFactory().getEnvironment().reportMessage(
         // "package dependencies: " + packRefs);
         // getFactory().getEnvironment().reportMessage(
         // "looking for circular package dependencies...");
         // looking for circular dependecies
         for (CtPackageReference p : .keySet()) {
             Stack<CtPackageReferencepath = new Stack<CtPackageReference>();
             path.push(p);
             scanDependencies(path);
         }
     }
 
 
     void scanDependencies(Stack<CtPackageReferencepath) {
         // getFactory().getEnvironment().reportMessage("scanning " + path);
         CtPackageReference ref = path.peek();
         // return if already scanned
         if (.contains(ref)) {
             return;
         }
         .add(ref);
         Set<CtPackageReferencerefs = .get(ref);
         if (refs != null) {
             for (CtPackageReference p : refs) {
                 if (path.contains(p)) {
                     List<CtPackageReferencecircularPath = new ArrayList<CtPackageReference>(
                             path.subList(path.indexOf(p), path.size()));
                     circularPath.add(p);
                     report(null"circular dependency " + circularPath);
                     break;
                 } else {
                     path.push(p);
                     scanDependencies(path);
                    path.pop();
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X