1 23 24 29 30 package com.sun.enterprise.tools.verifier.apiscan.classfile; 31 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.util.*; 35 import java.util.logging.Level ; 36 37 43 public class BCELClosureCompilerImpl extends ClosureCompilerImplBase { 44 45 49 50 private Stack<ClassFile> callStack = new Stack<ClassFile>(); 51 52 private HashSet<String > closure = new HashSet<String >(); 53 54 private HashSet<String > nativeMethods = new HashSet<String >(); 55 56 private Map<String , List<String >> failed = new HashMap<String , List<String >>(); 58 59 private static final String myClassName = "BCELClosureCompilerImpl"; 61 65 public BCELClosureCompilerImpl(ClassFileLoader loader) { 66 super(loader); 67 } 68 69 public boolean buildClosure(String className) { 71 logger.entering(myClassName, "buildClosure", className); ClassFile cf; 73 if (!needToBuildClosure(className)) 74 return true; 75 try { 76 cf = loader.load(className); 77 } catch (IOException e) { 78 handleFailure(className); 79 return false; 80 } 81 return buildClosure(cf); 82 } 83 84 88 private boolean buildClosure(ClassFile cf) { 89 boolean result = true; 90 callStack.push(cf); 91 if (needToBuildClosure(cf.getName())) { 92 visitedClasses.add(cf.getName()); 93 Collection<String > names = cf.getAllReferencedClassNames(); 94 closure.addAll(names); 95 for(Method m : cf.getMethods()) { 98 if(m.isNative()) { 99 final String methodDesc = 100 m.getOwningClass().getName()+ "." + m.getName(); nativeMethods.add(methodDesc); 102 } 103 } 104 for (Iterator i = names.iterator(); i.hasNext();) { 105 String nextExternalName = (String ) i.next(); 106 if (!needToBuildClosure(nextExternalName)) continue; 107 ClassFile next; 108 try { 109 next = loader.load(nextExternalName); 110 } catch (IOException e) { 111 result = false; 112 handleFailure(nextExternalName); 113 continue; 114 } 115 boolean newresult = buildClosure(next); result = newresult && result; 117 } 118 } 119 callStack.pop(); 120 return result; 121 } 122 123 private void handleFailure(String referencedClass) { 124 String referencingPath = ""; 125 try { 126 StringBuilder referencingPathBuffer = new StringBuilder (); 127 for (Iterator i = callStack.iterator(); i.hasNext();) { 128 if (referencingPathBuffer.length() != 0) 129 referencingPathBuffer.append(File.separator); 130 referencingPathBuffer.append(((ClassFile) i.next()).getName()); 131 } 132 referencingPath = referencingPathBuffer.toString(); 133 } catch (EmptyStackException e) { 134 } 135 logger.finer( 136 "Could not locate " + referencingPath + File.separator + referencedClass); 138 List<String > failedList = failed.get(referencingPath); 139 if (failedList == null) { 140 failedList = new ArrayList<String >(); 141 failed.put(referencingPath, failedList); 142 } 143 failedList.add(referencedClass); 144 } 145 146 public Collection getClosure() { 148 return Collections.unmodifiableCollection(closure); 149 } 150 151 public Map getFailed() { 153 return Collections.unmodifiableMap(failed); 154 } 155 156 162 public void reset() { 163 closure.clear(); 164 visitedClasses.clear(); 165 failed.clear(); 166 nativeMethods.clear(); 167 } 168 169 public Collection<String > getNativeMethods() { 170 return Collections.unmodifiableCollection(nativeMethods); 171 } 172 173 public String toString() { 174 StringBuilder sb=new StringBuilder (); 175 if(logger.isLoggable(Level.FINER)){ 176 sb.append("\n<Closure>"); 178 sb.append("\n\t<ExcludedClasses>"); for(Iterator i=excludedClasses.iterator(); i.hasNext();) { 180 sb.append("\n\t\t"); sb.append((String )i.next()); 182 } 183 sb.append("\n\t</ExcludedClasses>"); 185 sb.append("\n\t<ExcludedPackages>"); for(Iterator i=excludedPackages.iterator(); i.hasNext();){ 187 sb.append("\n\t\t"); sb.append((String )i.next()); 189 } 190 sb.append("\n\t</ExcludedPackages>"); 192 sb.append("\n\t<ExcludedPatterns>"); for(Iterator i=excludedPatterns.iterator(); i.hasNext();){ 194 sb.append("\n\t\t"); sb.append((String )i.next()); 196 } 197 sb.append("\n\t</ExcludedPatterns>"); 199 sb.append("\n\t<Classes>"); for(Iterator i=closure.iterator(); i.hasNext();){ 201 sb.append("\n\t\t"); sb.append((String )i.next()); 203 } 204 sb.append("\n\t</Classes>"); } 206 sb.append("\n\t<Failed>"); for(Iterator i=failed.entrySet().iterator(); i.hasNext();) { 208 Map.Entry referencingPathToFailedList=(Map.Entry)i.next(); 209 sb.append("\n\t\t"); sb.append("<ReferencingPath>"); sb.append("\n\t\t\t"); sb.append(referencingPathToFailedList.getKey()); 213 sb.append("\n\t\t"); sb.append("</ReferencingPath>"); sb.append("\n\t\t"); sb.append("<Classes>"); for(Iterator iii=((List)referencingPathToFailedList.getValue()).iterator(); iii.hasNext();){ 218 sb.append("\n\t\t\t"); sb.append((String )iii.next()); 220 } 221 sb.append("\n\t\t"); sb.append("</Classes>"); } 224 sb.append("\n\t</Failed>"); 226 sb.append("\n\t<NativeMethods>"); for(String s : nativeMethods) { 228 sb.append("\n\t\t"); sb.append(s); 230 } 231 sb.append("\n\t</NativeMethods>"); 233 if(logger.isLoggable(Level.FINER)){ 234 sb.append("\n</Closure>"); } 236 return sb.toString(); 237 } 238 239 } 240 | Popular Tags |