1 19 20 package org.netbeans.modules.apisupport.refactoring; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.lang.reflect.Modifier ; 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.jar.Attributes ; 31 import java.util.jar.Manifest ; 32 import java.util.regex.Matcher ; 33 import java.util.regex.Pattern ; 34 import org.netbeans.api.project.FileOwnerQuery; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.jmi.javamodel.Constructor; 37 import org.netbeans.jmi.javamodel.JavaClass; 38 import org.netbeans.jmi.javamodel.Method; 39 import org.netbeans.jmi.javamodel.Parameter; 40 import org.netbeans.jmi.javamodel.Resource; 41 import org.netbeans.modules.apisupport.project.NbModuleProject; 42 import org.netbeans.modules.apisupport.project.layers.LayerUtils; 43 import org.netbeans.modules.javacore.api.JavaModel; 44 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 45 import org.netbeans.modules.refactoring.api.Problem; 46 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 47 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 48 import org.netbeans.modules.refactoring.spi.RefactoringPlugin; 49 import org.openide.ErrorManager; 50 import org.openide.filesystems.FileObject; 51 import org.openide.filesystems.FileSystem; 52 53 57 public abstract class AbstractRefactoringPlugin implements RefactoringPlugin { 58 protected static ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.apisupport.refactoring"); 60 protected AbstractRefactoring refactoring; 61 protected Pattern orderingLayerAttrPattern = Pattern.compile("([\\S]+)/([\\S]+)"); 64 public AbstractRefactoringPlugin(AbstractRefactoring refactoring) { 65 this.refactoring = refactoring; 66 } 67 68 71 public Problem preCheck() { 72 return null; 73 } 74 75 78 public Problem checkParameters() { 79 return null; 80 } 81 82 85 protected final int checkContentOfFile(FileObject fo, String classToLookFor) { 86 BufferedReader reader = null; 87 try { 88 reader = new BufferedReader (new InputStreamReader (fo.getInputStream(), "UTF-8")); String line = reader.readLine(); 90 int counter = 0; 91 while (line != null) { 92 if (line.indexOf(classToLookFor) != -1) { 93 return counter; 94 } 95 counter = counter + 1; 96 line = reader.readLine(); 97 } 98 } catch (IOException exc) { 99 } finally { 101 if (reader != null) { 102 try { 103 reader.close(); 104 } catch (IOException x) { 105 } 107 } 108 } 109 return -1; 110 } 111 112 protected final void checkManifest(NbModuleProject project, JavaClass clzz, RefactoringElementsBag refactoringElements) { 113 String name = clzz.getName(); 114 String pathName = name.replace('.', '/') + ".class"; Manifest mf = project.getManifest(); 116 if (mf == null) { 117 return; 118 } 119 Attributes attrs = mf.getMainAttributes(); 120 Iterator it = attrs.entrySet().iterator(); 121 while (it.hasNext()) { 122 Map.Entry entry = (Map.Entry )it.next(); 123 String val = (String )entry.getValue(); 124 if (val.indexOf(name) != -1 || val.indexOf(pathName) != -1) { 125 RefactoringElementImplementation elem = 126 createManifestRefactoring(clzz, project.getManifestFile(), ((Attributes.Name )entry.getKey()).toString(), val, null); 127 if (elem != null) { 128 refactoringElements.add(refactoring, elem); 129 } 130 } 131 } 132 Map entries = mf.getEntries(); 133 if (entries != null) { 134 it = entries.entrySet().iterator(); 135 while (it.hasNext()) { 136 Map.Entry secEnt = (Map.Entry )it.next(); 137 attrs = (Attributes )secEnt.getValue(); 138 String val = (String )secEnt.getKey(); 139 if (val.indexOf(name) != -1 || val.indexOf(pathName) != -1) { 140 String section = attrs.getValue("OpenIDE-Module-Class"); RefactoringElementImplementation elem = 142 createManifestRefactoring(clzz, project.getManifestFile(), null, val, section); 143 if (elem != null) { 144 refactoringElements.add(refactoring, elem); 145 } 146 } 147 } 148 } 149 } 150 151 protected final void checkMetaInfServices(Project project, JavaClass clzz, RefactoringElementsBag refactoringElements) { 152 FileObject services = Utility.findMetaInfServices(project); 153 if (services == null) { 154 return; 155 } 156 String name = clzz.getName(); 157 FileObject[] files = services.getChildren(); 159 for (int i = 0; i < files.length; i++) { 160 int line = checkContentOfFile(files[i], name); 161 if (line != -1) { 162 RefactoringElementImplementation elem = 163 createMetaInfServicesRefactoring(clzz, files[i], line); 164 if (elem != null) { 165 refactoringElements.add(refactoring, elem); 166 } 167 } 168 } 169 } 170 171 protected final void checkLayer(NbModuleProject project, JavaClass clzz, RefactoringElementsBag refactoringElements) { 172 LayerUtils.LayerHandle handle = LayerUtils.layerForProject(project); 173 FileSystem fs = handle.layer(false); 174 if (fs != null) { 175 checkFileObject(fs.getRoot(), clzz, refactoringElements, handle); 176 } 177 } 178 179 180 private void checkFileObject(FileObject fo, JavaClass clzz, RefactoringElementsBag refactoringElements, LayerUtils.LayerHandle handle) { 181 if (fo.isFolder()) { 182 FileObject[] childs = fo.getChildren(); 183 for (int i =0; i < childs.length; i++) { 184 checkFileObject(childs[i], clzz, refactoringElements, handle); 185 } 186 Enumeration en = fo.getAttributes(); 187 while (en.hasMoreElements()) { 189 String attrKey = (String )en.nextElement(); 190 Matcher match = orderingLayerAttrPattern.matcher(attrKey); 191 if (match.matches()) { 192 String first = match.group(1); 193 if (first.endsWith(".instance")) { String name = first.substring(0, first.length() - ".instance".length()).replace('-', '.'); if (name.equals(clzz.getName())) { 196 RefactoringElementImplementation elem = createLayerRefactoring(clzz, handle, fo, attrKey); 197 if (elem != null) { 198 refactoringElements.add(refactoring, elem); 199 } 200 } 201 } 202 String second = match.group(2); 203 if (second.endsWith(".instance")) { String name = second.substring(0, second.length() - ".instance".length()).replace('-', '.'); if (name.equals(clzz.getName())) { 206 RefactoringElementImplementation elem = createLayerRefactoring(clzz, handle, fo, attrKey); 207 if (elem != null) { 208 refactoringElements.add(refactoring, elem); 209 } 210 } 211 } 212 } 213 } 214 } else if (fo.isData()) { 215 216 Enumeration en = fo.getAttributes(); 217 while (en.hasMoreElements()) { 219 String attrKey = (String )en.nextElement(); 220 Object val = fo.getAttribute("literal:" + attrKey); if (val instanceof String ) { 222 String attrValue = (String )val; 223 String value = attrValue; 224 if (attrValue.startsWith("new:")) { value = attrValue.substring("new:".length()); } 227 if (attrValue.startsWith("method:")) { value = attrValue.substring("method:".length()); int index = value.lastIndexOf('.'); 230 if (index > 0) { 231 value = value.substring(0, index); 232 } 233 } 234 String pattern1 = clzz.getName().replaceAll("\\.", "\\."); String pattern2 = "[a-zA-Z0-9/-]*" + clzz.getName().replaceAll("\\.", "-") + "\\.instance"; 237 if (value.matches(pattern1) || value.matches(pattern2)) { 238 RefactoringElementImplementation elem = createLayerRefactoring(clzz, handle, fo, attrKey); 239 if (elem != null) { 240 refactoringElements.add(refactoring, elem); 241 } 242 } 243 } 244 } 245 if ("instance".equals(fo.getExt())) { String name = fo.getName().replace('-', '.'); 248 if (name.equals(clzz.getName())) { 249 RefactoringElementImplementation elem = createLayerRefactoring(clzz, handle, fo, null); 250 if (elem != null) { 251 refactoringElements.add(refactoring, elem); 252 } 253 } 254 } 255 if ("settings".equals(fo.getExt())) { } 258 259 } 260 261 } 262 263 protected final Problem checkLayer(Method method, RefactoringElementsBag refactoringElements) { 264 Problem problem = null; 265 if (! Modifier.isPublic(method.getModifiers()) || !Modifier.isStatic(method.getModifiers())) { 267 return problem; 268 } 269 List params = method.getParameters(); 271 if (params.size() > 1) { 272 return problem; 273 } 274 Iterator it = params.iterator(); 275 while (it.hasNext()) { 276 Parameter param =(Parameter)it.next(); 277 if (! "org.openide.filesystems.FileObject".equals(param.getType().getName())) { return problem; 279 } 280 } 281 Resource res = method.getResource(); 282 FileObject fo = JavaModel.getFileObject(res); 283 Project project = FileOwnerQuery.getOwner(fo); 284 if (project != null && project instanceof NbModuleProject) { 285 LayerUtils.LayerHandle handle = LayerUtils.layerForProject((NbModuleProject)project); 286 FileSystem fs = handle.layer(false); 287 if (fs != null) { 288 checkFileObject(fs.getRoot(), method, null, refactoringElements, handle); 289 } 290 } 291 return problem; 292 } 293 294 protected final Problem checkLayer(Constructor constructor, RefactoringElementsBag refactoringElements) { 295 Problem problem = null; 296 if (!Modifier.isPublic(constructor.getModifiers())) { 298 return problem; 299 } 300 List params = constructor.getParameters(); 301 if (params.size() > 0) { 302 return problem; 303 } 304 Resource res = constructor.getResource(); 305 FileObject fo = JavaModel.getFileObject(res); 306 Project project = FileOwnerQuery.getOwner(fo); 307 if (project != null && project instanceof NbModuleProject) { 308 LayerUtils.LayerHandle handle = LayerUtils.layerForProject((NbModuleProject)project); 309 FileSystem fs = handle.layer(false); 310 if (fs != null) { 311 checkFileObject(fs.getRoot(), null, constructor, refactoringElements, handle); 312 } 313 } 314 return problem; 315 } 316 317 private void checkFileObject(FileObject fo, Method method, Constructor constructor, 318 RefactoringElementsBag refactoringElements, LayerUtils.LayerHandle handle) { 319 if (fo.isFolder()) { 320 FileObject[] childs = fo.getChildren(); 321 for (int i =0; i < childs.length; i++) { 322 checkFileObject(childs[i], method, constructor, refactoringElements, handle); 323 } 324 } else if (fo.isData()) { 325 if ("settings".equals(fo.getExt())) { } 328 Enumeration en = fo.getAttributes(); 329 while (en.hasMoreElements()) { 331 String attrKey = (String )en.nextElement(); 332 Object val = fo.getAttribute("literal:" + attrKey); if (val instanceof String ) { 334 String attrValue = (String )val; 335 if (method != null && attrValue.startsWith("method:") && attrValue.endsWith(method.getName())) { String clazz = attrValue.substring("method:".length()); String methodString = null; 338 int index = clazz.lastIndexOf('.'); 339 if (index > 0) { 340 methodString = clazz.substring(index + 1); 341 clazz = clazz.substring(0, index); 342 } 343 if (methodString != null && methodString.equals(method.getName()) && 344 clazz.equals(method.getDeclaringClass().getName())) { 345 RefactoringElementImplementation elem = createLayerRefactoring(method, handle, fo, attrKey); 346 if (elem != null) { 347 refactoringElements.add(refactoring, elem); 348 } 349 } 350 } 351 if (constructor != null && attrValue.startsWith("new:")) { String clazz = attrValue.substring("new:".length()); if (clazz.equals(constructor.getDeclaringClass().getName())) { 354 RefactoringElementImplementation elem = createLayerRefactoring(constructor, handle, fo, attrKey); 355 if (elem != null) { 356 refactoringElements.add(refactoring, elem); 357 } 358 } 359 } 360 } 361 } 362 } 363 364 } 365 366 protected abstract RefactoringElementImplementation createMetaInfServicesRefactoring(JavaClass clazz, 367 FileObject serviceFile, int line); 368 369 protected abstract RefactoringElementImplementation createManifestRefactoring(JavaClass clazz, 370 FileObject manifestFile, 371 String attributeKey, 372 String attributeValue, 373 String section); 374 375 protected RefactoringElementImplementation createLayerRefactoring(JavaClass clazz, 376 LayerUtils.LayerHandle handle, 377 FileObject layerFileObject, 378 String layerAttribute) { 379 throw new AssertionError ("if you call checkLayer(), you need to implement this method"); 380 } 381 382 protected RefactoringElementImplementation createLayerRefactoring(Method method, 383 LayerUtils.LayerHandle handle, 384 FileObject layerFileObject, 385 String layerAttribute) { 386 throw new AssertionError ("if you call checkLayer(), you need to implement this method"); 387 } 388 389 protected RefactoringElementImplementation createLayerRefactoring(Constructor constructor, 390 LayerUtils.LayerHandle handle, 391 FileObject layerFileObject, 392 String layerAttribute) { 393 throw new AssertionError ("if you call checkLayer(), you need to implement this method"); 394 } 395 396 } 397 | Popular Tags |