1 18 package org.codehaus.groovy.syntax; 19 20 import org.codehaus.groovy.ast.ModuleNode; 21 import org.codehaus.groovy.control.CompilationFailedException; 22 import org.codehaus.groovy.control.SourceUnit; 23 import org.codehaus.groovy.syntax.Types; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 39 public class ASTHelper { 40 41 private static final String [] EMPTY_STRING_ARRAY = new String [0]; 42 private static final String [] DEFAULT_IMPORTS = {"java.lang.", "java.io.", "java.net.", "java.util.", "groovy.lang.", "groovy.util."}; 43 44 45 private SourceUnit controller; 46 47 48 private ClassLoader classLoader; 49 50 51 private Map imports; 52 protected ModuleNode output; 53 54 55 private String packageName; 57 protected static HashMap resolutions = new HashMap (); 60 private static String NOT_RESOLVED = new String (); 61 62 63 private List newClasses = new ArrayList (); 64 65 public ASTHelper(SourceUnit controller, ClassLoader classLoader) { 66 this(); 67 this.controller = controller; 68 this.classLoader = classLoader; 69 } 70 71 public ASTHelper() { 72 imports = new HashMap (); 73 } 74 75 public String getPackageName() { 76 return packageName; 77 } 78 79 public void setPackageName(String packageName) { 80 this.packageName = packageName; 81 82 output.setPackageName(packageName); 83 } 84 85 86 89 public ClassLoader getClassLoader() { 90 return classLoader; 91 } 92 93 public void setClassLoader(ClassLoader classLoader) { 94 this.classLoader = classLoader; 95 } 96 97 public SourceUnit getController() { 98 return controller; 99 } 100 101 public void setController(SourceUnit controller) { 102 this.controller = controller; 103 } 104 105 109 110 protected String resolveName(String name, boolean safe) { 111 114 String resolution = (String ) resolutions.get(name); 115 if (NOT_RESOLVED.equals(resolution)) { 116 return (safe ? name : null); 117 } 118 else if (resolution != null) { 119 return (String ) resolution; 120 } 121 122 try { 123 getClassLoader().loadClass(name); 124 resolutions.put(name,name); 125 return name; 126 } catch (ClassNotFoundException cnfe){ 127 if (cnfe.getCause() instanceof CompilationFailedException) { 128 resolutions.put(name,name); 129 return name; 130 } 131 } catch (NoClassDefFoundError ncdfe) { 132 } 134 135 do { 136 140 if (name.indexOf(".") >= 0) { 141 resolution = name; 142 break; } 144 145 146 150 String scalar = name, postfix = ""; 151 while (scalar.endsWith("[]")) { 152 scalar = scalar.substring(0, scalar.length() - 2); 153 postfix += "[]"; 154 } 155 156 157 160 if (Types.ofType(Types.lookupKeyword(scalar), Types.PRIMITIVE_TYPE)) { 161 resolution = name; 162 break; } 164 165 166 170 if (this.imports.containsKey(scalar)) { 171 resolution = ((String ) this.imports.get(scalar)) + postfix; 172 break; } 174 175 176 179 if (packageName != null && packageName.length() > 0) { 180 try { 181 getClassLoader().loadClass(dot(packageName, scalar)); 182 resolution = dot(packageName, name); 183 184 break; } catch (ClassNotFoundException cnfe){ 186 if (cnfe.getCause() instanceof CompilationFailedException) { 187 break; 188 } 189 } catch (NoClassDefFoundError ncdfe) { 190 } 192 } 193 194 List packageImports = output.getImportPackages(); 196 for (int i = 0; i < packageImports.size(); i++) { 197 String pack = (String ) packageImports.get(i); 198 String clsName = pack + name; 199 try { 200 getClassLoader().loadClass(clsName); 201 resolution = clsName; 202 break; 203 } catch (ClassNotFoundException cnfe){ 204 if (cnfe.getCause() instanceof CompilationFailedException) { 205 break; 206 } 207 } catch (NoClassDefFoundError ncdfe) { 208 } 210 } 211 if (resolution != null) { 212 break; 213 } 214 215 218 for (int i = 0; i < DEFAULT_IMPORTS.length; i++) { 219 try { 220 String qualified = DEFAULT_IMPORTS[i] + scalar; 221 getClassLoader().loadClass(qualified); 222 223 resolution = qualified + postfix; 224 break; } catch (ClassNotFoundException cnfe){ 226 if (cnfe.getCause() instanceof CompilationFailedException) { 227 break; 228 } 229 } catch (NoClassDefFoundError ncdfee) { 230 } 232 } 233 234 } 235 while (false); 236 237 238 241 if (resolution == null) { 242 resolutions.put(name, NOT_RESOLVED); 243 return (safe ? name : null); 244 } 245 else { 246 resolutions.put(name, resolution); 247 return resolution; 248 } 249 } 250 251 255 256 protected String dot(String base, String name) { 257 if (base != null && base.length() > 0) { 258 return base + "." + name; 259 } 260 261 return name; 262 } 263 264 protected void makeModule() { 265 this.newClasses.clear(); 266 this.output = new ModuleNode(controller); 267 resolutions.clear(); 268 } 269 270 273 274 protected boolean isDatatype(String name) { 275 return resolveName(name, false) != null; 276 } 277 278 281 282 protected String dot(String base) { 283 return dot(base, ""); 284 } 285 286 protected String resolveNewClassOrName(String name, boolean safe) { 287 if (this.newClasses.contains(name)) { 288 return dot(packageName, name); 289 } 290 else { 291 return resolveName(name, safe); 292 } 293 } 294 295 protected void addNewClassName(String name) { 296 this.newClasses.add(name); 297 } 298 299 protected void importClass(String importPackage, String name, String as) { 300 309 if (as==null) as=name; 310 output.addImport( as, name ); 312 name = dot( importPackage, name ); 313 314 imports.put( as, name ); 316 } 317 318 protected void importPackageWithStar(String importPackage) { 319 String [] classes = output.addImportPackage( dot(importPackage) ); 320 for( int i = 0; i < classes.length; i++ ) 321 { 322 imports.put( classes[i], dot(importPackage, classes[i]) ); 323 } 324 } 325 } 326 | Popular Tags |