1 19 20 package org.netbeans.api.java.source; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import javax.lang.model.element.Element; 29 import javax.lang.model.element.ElementKind; 30 import javax.lang.model.element.ExecutableElement; 31 import javax.lang.model.element.TypeElement; 32 import javax.lang.model.element.VariableElement; 33 import javax.lang.model.util.ElementFilter; 34 import javax.swing.event.ChangeListener ; 35 import org.netbeans.api.java.queries.SourceForBinaryQuery.Result; 36 import org.netbeans.api.java.source.ClasspathInfo; 37 import org.netbeans.api.java.source.ClasspathInfo.PathKind; 38 import org.netbeans.junit.NbTestCase; 39 import org.netbeans.modules.java.source.ElementHandleAccessor; 40 import org.netbeans.modules.java.source.TestUtil; 41 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 42 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 43 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileUtil; 46 47 51 public class SourceUtilsTest extends NbTestCase { 52 53 private JavaSource js; 54 private CompilationInfo info; 55 56 public SourceUtilsTest(String testName) { 57 super(testName); 58 } 59 60 protected void setUp() throws Exception { 61 clearWorkDir(); 62 SourceUtilsTestUtil.prepareTest(new String [0], new Object [] {SFBQImpl.getDefault()}); 63 } 64 65 private void prepareTest() throws Exception { 66 File work = TestUtil.createWorkFolder(); 67 FileObject workFO = FileUtil.toFileObject(work); 68 69 assertNotNull(workFO); 70 71 FileObject sourceRoot = workFO.createFolder("src"); 72 FileObject buildRoot = workFO.createFolder("build"); 73 FileObject cache = workFO.createFolder("cache"); 74 FileObject packageRoot = sourceRoot.createFolder("sourceutils"); 75 76 SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache); 77 78 String capitalizedName = "T" + getName().substring(1); 79 80 TestUtil.copyFiles(FileUtil.toFile(sourceRoot), "sourceutils/" + capitalizedName + ".java"); 81 82 packageRoot.refresh(); 83 84 FileObject testSource = packageRoot.getFileObject(capitalizedName + ".java"); 85 86 assertNotNull(testSource); 87 88 SourceUtilsTestUtil.compileRecursively(sourceRoot); 89 90 js = JavaSource.forFileObject(testSource); 91 92 assertNotNull(js); 93 94 info = SourceUtilsTestUtil.getCompilationInfo(js, JavaSource.Phase.RESOLVED); 95 96 assertNotNull(info); 97 } 98 99 public void testGetEnclosingTypeElement() throws Exception { 100 prepareTest(); 102 103 TypeElement test = info.getElements().getTypeElement("sourceutils.TestGetEnclosingTypeElement"); 104 105 assertNotNull(test); 106 107 ExecutableElement testMethod = ElementFilter.methodsIn(test.getEnclosedElements()).get(0); 108 VariableElement testField = ElementFilter.fieldsIn(test.getEnclosedElements()).get(0); 113 TypeElement nestedClass = ElementFilter.typesIn(test.getEnclosedElements()).get(0); 114 ExecutableElement nestedClassMethod = ElementFilter.methodsIn(nestedClass.getEnclosedElements()).get(0); 115 VariableElement nestedClassField = ElementFilter.fieldsIn(nestedClass.getEnclosedElements()).get(0); 116 TypeElement nestedClassNestedClass = ElementFilter.typesIn(nestedClass.getEnclosedElements()).get(0); 117 118 assertEquals("TestGetEnclosingTypeElement", test.getSimpleName().toString()); 119 assertEquals("testMethod", testMethod.getSimpleName().toString()); 120 assertEquals("testField", testField.getSimpleName().toString()); 125 assertEquals("NestedClass", nestedClass.getSimpleName().toString()); 126 assertEquals("nestedClassMethod", nestedClassMethod.getSimpleName().toString()); 127 assertEquals("nestedClassField", nestedClassField.getSimpleName().toString()); 128 assertEquals("NestedClassNestedClass", nestedClassNestedClass.getSimpleName().toString()); 129 130 assertEquals(null, SourceUtils.getEnclosingTypeElement(test)); 131 assertEquals(test, SourceUtils.getEnclosingTypeElement(testMethod)); 132 assertEquals(test, SourceUtils.getEnclosingTypeElement(testField)); 133 assertEquals(test, SourceUtils.getEnclosingTypeElement(nestedClass)); 134 assertEquals(nestedClass, SourceUtils.getEnclosingTypeElement(nestedClassMethod)); 135 assertEquals(nestedClass, SourceUtils.getEnclosingTypeElement(nestedClassField)); 136 assertEquals(nestedClass, SourceUtils.getEnclosingTypeElement(nestedClassNestedClass)); 137 138 try { 139 SourceUtils.getEnclosingTypeElement(test.getEnclosingElement()); 140 fail(); 141 } catch (IllegalArgumentException e) { 142 } 144 } 145 146 public void testIsDeprecated1() throws Exception { 147 prepareTest(); 148 149 TypeElement test = info.getElements().getTypeElement("sourceutils.TestIsDeprecated1"); 150 151 assertNotNull(test); 152 153 ExecutableElement methodDeprecated = findElementBySimpleName("methodDeprecated", ElementFilter.methodsIn(test.getEnclosedElements())); 154 ExecutableElement methodNotDeprecated = findElementBySimpleName("methodNotDeprecated", ElementFilter.methodsIn(test.getEnclosedElements())); 155 VariableElement fieldDeprecated = findElementBySimpleName("fieldDeprecated", ElementFilter.fieldsIn(test.getEnclosedElements())); 156 VariableElement fieldNotDeprecated = findElementBySimpleName("fieldNotDeprecated", ElementFilter.fieldsIn(test.getEnclosedElements())); 157 TypeElement classDeprecated = findElementBySimpleName("classDeprecated", ElementFilter.typesIn(test.getEnclosedElements())); 158 TypeElement classNotDeprecated = findElementBySimpleName("classNotDeprecated", ElementFilter.typesIn(test.getEnclosedElements())); 159 160 assertFalse(info.getElements().isDeprecated(methodNotDeprecated)); 161 assertFalse(info.getElements().isDeprecated(fieldNotDeprecated)); 162 assertFalse(info.getElements().isDeprecated(classNotDeprecated)); 163 164 assertTrue(info.getElements().isDeprecated(methodDeprecated)); 165 assertTrue(info.getElements().isDeprecated(fieldDeprecated)); 166 assertTrue(info.getElements().isDeprecated(classDeprecated)); 167 } 168 169 public void testIsDeprecated2() throws Exception { 170 prepareTest(); 171 172 TypeElement test = info.getElements().getTypeElement("sourceutils.TestIsDeprecated2"); 173 174 assertNotNull(test); 175 176 ExecutableElement methodDeprecated = findElementBySimpleName("methodDeprecated", ElementFilter.methodsIn(test.getEnclosedElements())); 177 ExecutableElement methodNotDeprecated = findElementBySimpleName("methodNotDeprecated", ElementFilter.methodsIn(test.getEnclosedElements())); 178 VariableElement fieldDeprecated = findElementBySimpleName("fieldDeprecated", ElementFilter.fieldsIn(test.getEnclosedElements())); 179 VariableElement fieldNotDeprecated = findElementBySimpleName("fieldNotDeprecated", ElementFilter.fieldsIn(test.getEnclosedElements())); 180 TypeElement classDeprecated = findElementBySimpleName("classDeprecated", ElementFilter.typesIn(test.getEnclosedElements())); 181 TypeElement classNotDeprecated = findElementBySimpleName("classNotDeprecated", ElementFilter.typesIn(test.getEnclosedElements())); 182 183 assertFalse(info.getElements().isDeprecated(methodNotDeprecated)); 184 assertFalse(info.getElements().isDeprecated(fieldNotDeprecated)); 185 assertFalse(info.getElements().isDeprecated(classNotDeprecated)); 186 187 assertTrue(info.getElements().isDeprecated(methodDeprecated)); 188 assertTrue(info.getElements().isDeprecated(fieldDeprecated)); 189 assertTrue(info.getElements().isDeprecated(classDeprecated)); 190 } 191 192 public void testGetOutermostEnclosingTypeElement () throws Exception { 193 prepareTest(); 194 TypeElement test = info.getElements().getTypeElement("sourceutils.TestGetOutermostEnclosingTypeElement"); 195 assertNotNull(test); 196 assertEquals("TestGetOutermostEnclosingTypeElement", test.getSimpleName().toString()); 197 198 ExecutableElement testMethod = ElementFilter.methodsIn(test.getEnclosedElements()).get(0); 199 VariableElement testField = ElementFilter.fieldsIn(test.getEnclosedElements()).get(0); 204 TypeElement nestedClass = ElementFilter.typesIn(test.getEnclosedElements()).get(0); 205 ExecutableElement nestedClassMethod = ElementFilter.methodsIn(nestedClass.getEnclosedElements()).get(0); 206 VariableElement nestedClassField = ElementFilter.fieldsIn(nestedClass.getEnclosedElements()).get(0); 207 TypeElement nestedClassNestedClass = ElementFilter.typesIn(nestedClass.getEnclosedElements()).get(0); 208 209 210 assertEquals("testMethod", testMethod.getSimpleName().toString()); 211 assertEquals("testField", testField.getSimpleName().toString()); 216 assertEquals("NestedClass", nestedClass.getSimpleName().toString()); 217 assertEquals("nestedClassMethod", nestedClassMethod.getSimpleName().toString()); 218 assertEquals("nestedClassField", nestedClassField.getSimpleName().toString()); 219 assertEquals("NestedClassNestedClass", nestedClassNestedClass.getSimpleName().toString()); 220 221 assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(test)); 222 assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(testMethod)); 223 assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(testField)); 224 assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(nestedClass)); 225 assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(nestedClassMethod)); 226 assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(nestedClassField)); 227 assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(nestedClassNestedClass)); 228 229 try { 230 SourceUtils.getOutermostEnclosingTypeElement(test.getEnclosingElement()); 231 fail(); 232 } catch (IllegalArgumentException e) { 233 } 235 } 236 237 private <E extends Element> E findElementBySimpleName(String simpleName, List <E> elements) { 238 for (E e : elements) { 239 if (simpleName.contentEquals(e.getSimpleName())) 240 return e; 241 } 242 243 fail("Not found element with simple name: " + simpleName); 244 245 throw new Error ("Should never be here!"); 246 } 247 248 public void testGetFQNsForSimpleName() throws Exception { 249 282 } 284 285 286 public void testGetFile () throws Exception { 287 File workDir = getWorkDir(); 288 FileObject workFo = FileUtil.toFileObject(workDir); 289 assertNotNull (workFo); 290 FileObject src = workFo.createFolder("src"); 291 FileObject srcInDefPkg = src.createData("Foo","java"); 292 assertNotNull(srcInDefPkg); 293 FileObject sourceFile = src.createFolder("org").createFolder("me").createData("Test", "java"); 294 assertNotNull(sourceFile); 295 ClasspathInfo cpInfo = ClasspathInfo.create(ClassPathSupport.createClassPath(new FileObject[0]), ClassPathSupport.createClassPath(new FileObject[0]), 296 ClassPathSupport.createClassPath(new FileObject[]{src})); 297 FileObject cls = cpInfo.getClassPath(PathKind.OUTPUT).getRoots()[0]; 298 FileObject classInDefPkg = cls.createData("Foo","class"); 299 assertNotNull(classInDefPkg); 300 FileObject classPkg = cls.createFolder("org").createFolder("me"); 301 assertNotNull(classPkg); 302 FileObject classFile = classPkg.createData("Test", "class"); 303 assertNotNull(classFile); 304 FileObject classFileInnder = classPkg.createData("Test$Inner", "class"); 305 assertNotNull(classFileInnder); 306 SFBQImpl.getDefault().register(cls, src); 307 ElementHandle<? extends Element> handle = ElementHandleAccessor.INSTANCE.create(ElementKind.CLASS, new String [] {"org.me.Test"}); 308 assertNotNull (handle); 309 FileObject result = SourceUtils.getFile(handle, cpInfo); 310 assertNotNull(result); 311 handle = ElementHandleAccessor.INSTANCE.create(ElementKind.CLASS, new String [] {"org.me.Test$Inner"}); 312 result = SourceUtils.getFile(handle,cpInfo); 313 assertNotNull(result); 314 handle = ElementHandleAccessor.INSTANCE.create(ElementKind.PACKAGE, new String [] {"org.me"}); 315 result = SourceUtils.getFile(handle,cpInfo); 316 assertNotNull(result); 317 handle = ElementHandleAccessor.INSTANCE.create(ElementKind.CLASS, new String [] {"Foo"}); 318 result = SourceUtils.getFile(handle,cpInfo); 319 assertNotNull(result); 320 } 321 322 323 private static class SFBQImpl implements SourceForBinaryQueryImplementation { 324 325 private static SFBQImpl instance; 326 327 private final Map <URL , FileObject> map = new HashMap <URL , FileObject> (); 328 329 private SFBQImpl () { 330 331 } 332 333 public void register (FileObject bin, FileObject src) throws IOException { 334 map.put(bin.getURL(), src); 335 } 336 337 public Result findSourceRoots(URL binaryRoot) { 338 final FileObject src = map.get (binaryRoot); 339 if (src != null) { 340 return new Result() { 341 342 public FileObject[] getRoots() { 343 return new FileObject[] {src}; 344 } 345 346 public void addChangeListener(ChangeListener l) { 347 } 348 349 public void removeChangeListener(ChangeListener l) { 350 } 351 }; 352 } 353 return null; 354 } 355 356 public static synchronized SFBQImpl getDefault () { 357 if (instance == null) { 358 instance = new SFBQImpl (); 359 } 360 return instance; 361 } 362 } 363 364 } 365 | Popular Tags |