1 package net.sourceforge.importscrubber; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 10 public class ImportStatements 11 { 12 public static final String MARKER = "import"; 13 private ArrayList _stmts = new ArrayList (); 14 15 public void add(String candidateString) 16 { 17 ImportStatement candidate = new ImportStatement(candidateString); 18 if(candidate.getPackage() == null || _stmts.contains(candidate)) { 19 if (ImportScrubber.DEBUG) 20 System.out.println("not adding " + candidate.getFullyQualifiedClassName()); 21 return; 22 } 23 24 _stmts.add(candidate); 25 } 26 27 public StringBuffer getOutput(StatementFormat format) 28 { 29 return format.applyFormat(_stmts); 30 } 31 32 public int getCount() 33 { 34 return _stmts.size(); 35 } 36 37 40 public void removeLocalToPackage(PackageStmt packageStmt) 41 { 42 for (Iterator i = _stmts.iterator(); i.hasNext();) { 43 ImportStatement stmt = (ImportStatement)i.next(); 44 if (packageStmt.isInSamePackageAs(stmt)) { 45 if (ImportScrubber.DEBUG) 46 System.out.println("Removing local import:" + stmt.getClassName()); 47 i.remove(); 48 } 49 } 50 } 51 52 public void removeInnerClasses(String className) { 53 if (ImportScrubber.DEBUG) 54 System.out.println("Looking for inner classes of " + className); 55 for (Iterator i = _stmts.iterator(); i.hasNext();) { 56 ImportStatement stmt = (ImportStatement)i.next(); 57 if (stmt.getFullyQualifiedClassName().startsWith(className + ".")) { 58 if (ImportScrubber.DEBUG) 59 System.out.println("Removing inner class import:" + stmt.getClassName()); 60 i.remove(); 61 } 62 } 63 } 64 65 70 public String removeUnreferenced(String classBody) 71 { 72 for (Iterator i = _stmts.iterator(); i.hasNext();) { 73 ImportStatement stmt = (ImportStatement)i.next(); 74 if (classBody.indexOf(stmt.getClassName()) == -1) { 76 if (ImportScrubber.DEBUG) 78 System.out.println("Removing unreferenced import:" + stmt.getClassName()); 79 i.remove(); 80 continue; 81 } 82 83 int j = classBody.indexOf(stmt.getFullyQualifiedClassName()); 85 if (j != -1) { 86 if (ImportScrubber.DEBUG) 87 System.out.println("FQ class found:" + stmt.getClassName()); 88 boolean bareClass = false; 89 while (j != -1) { 90 if (classBody.charAt(j - 1) != '.') { 91 bareClass = true; 92 break; 93 } 94 j = classBody.indexOf(stmt.getFullyQualifiedClassName(), j + 1); 95 } 96 if (bareClass) { 104 if (ImportScrubber.DEBUG) 105 System.out.println("Bare class also found"); 106 for (Iterator k = _stmts.iterator(); k.hasNext(); ) { 107 ImportStatement s2 = (ImportStatement)k.next(); 108 if (s2 != stmt && stmt.getClassName().compareTo(s2.getClassName()) == 0) { 109 return "ambiguous use of " + stmt.getClassName() + "." 110 + "\n\t(" + stmt.getFullyQualifiedClassName() + " and " + s2.getFullyQualifiedClassName() + ")" 111 + "\n\tTo scrub, make all references fully qualified, or none."; 112 } 113 } 114 } else { 116 if (ImportScrubber.DEBUG) 117 System.out.println("No bare class found; removing"); 118 i.remove(); 119 } 120 } 121 } 122 123 return null; } 125 } 126 127 | Popular Tags |