1 19 package org.netbeans.api.java.source; 20 21 import java.beans.PropertyVetoException ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 import java.util.Set ; 30 import javax.lang.model.element.Element; 31 import javax.lang.model.element.ElementKind; 32 import javax.lang.model.element.ExecutableElement; 33 import javax.lang.model.element.Modifier; 34 import javax.lang.model.element.PackageElement; 35 import javax.lang.model.element.TypeElement; 36 import javax.lang.model.element.VariableElement; 37 import javax.lang.model.type.DeclaredType; 38 import javax.lang.model.type.TypeKind; 39 import javax.lang.model.type.TypeMirror; 40 import org.netbeans.api.java.classpath.ClassPath; 41 import org.netbeans.api.java.source.JavaSource.Phase; 42 import org.netbeans.junit.NbTestCase; 43 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 44 import org.openide.filesystems.FileLock; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.filesystems.LocalFileSystem; 48 import org.openide.filesystems.Repository; 49 import org.openide.filesystems.URLMapper; 50 51 55 public class APIIsSelfContainedTest extends NbTestCase { 56 57 private List <String > violations = new ArrayList <String >(); 58 59 62 63 public APIIsSelfContainedTest(String name) { 64 super(name); 65 } 66 67 protected void setUp() throws Exception { 68 SourceUtilsTestUtil.prepareTest(new String [0], new Object [0]); 69 } 70 71 private boolean shouldCheck(Set <Modifier> mods) { 72 if (mods.contains(Modifier.PUBLIC)) 73 return true; 74 if (mods.contains(Modifier.PROTECTED)) 75 return true; 76 77 return false; 78 } 79 80 private void writeIntoFile(FileObject file, String what) throws Exception { 81 FileLock lock = file.lock(); 82 OutputStream out = file.getOutputStream(lock); 83 84 try { 85 out.write(what.getBytes()); 86 } finally { 87 out.close(); 88 lock.releaseLock(); 89 } 90 } 91 92 private static final List <String > API_PACKAGES = Arrays.asList(new String [] { 93 "java", 94 "org.netbeans.api", 95 "org.openide", 96 "com.sun.source.tree", 97 "com.sun.source.util", 98 "com.sun.javadoc", 99 }); 100 101 private boolean isAPIClass(TypeElement clazz) { 102 String nameS = clazz.toString(); 103 104 for (String s : API_PACKAGES) { 105 if (nameS.startsWith(s)) 106 return true; 107 } 108 109 return false; 110 } 111 112 private void verifyAPIClass(TypeElement clazz, TypeElement in) { 113 if (!isAPIClass(clazz)) { 114 violations.add("Use of non-API class: " + clazz + " in " + in.toString()); 115 } 116 } 117 118 private void verifyAPIClass(TypeMirror tm, TypeElement in) { 119 if (tm.getKind() == TypeKind.DECLARED) { 120 verifyAPIClass((TypeElement) ((DeclaredType) tm).asElement(), in); 121 } 122 } 123 124 private void verifySelfContainedAPI(VariableElement ve, TypeElement in) { 125 if (!shouldCheck(ve.getModifiers())) 126 return; 128 verifyAPIClass(ve.asType(), in); 129 } 130 131 private void verifySelfContainedAPI(ExecutableElement ee, TypeElement in) { 132 if (!shouldCheck(ee.getModifiers())) 133 return; 135 verifyAPIClass(ee.getReturnType(), in); 136 for (VariableElement ve : ee.getParameters()) { 137 verifyAPIClass(ve.asType(), in); 138 } 139 } 140 141 private void verifySelfContainedAPI(TypeElement tel) { 142 if (!shouldCheck(tel.getModifiers())) 143 return; 145 verifyAPIClass(tel.getSuperclass(), tel); 146 147 for (TypeMirror intf : tel.getInterfaces()) { 148 verifyAPIClass(intf, tel); 149 } 150 151 for (Element e : tel.getEnclosedElements()) { 152 verifySelfContainedAPI(e, tel); 153 } 154 } 155 156 private void verifySelfContainedAPI(PackageElement pel) { 157 for (Element e : pel.getEnclosedElements()) { 158 verifySelfContainedAPI(e, null); 159 } 160 } 161 162 private void verifySelfContainedAPI(Element e, TypeElement in) { 163 switch (e.getKind()) { 164 case ANNOTATION_TYPE: 165 case CLASS: 166 case INTERFACE: 167 case ENUM: 168 verifySelfContainedAPI((TypeElement) e); 169 break; 170 case CONSTRUCTOR: 171 case METHOD: 172 verifySelfContainedAPI((ExecutableElement) e, in); 173 break; 174 case FIELD: 175 verifySelfContainedAPI((VariableElement) e, in); 176 break; 177 case PACKAGE: 178 verifySelfContainedAPI((PackageElement) e); 179 break; 180 } 181 } 182 183 private FileObject[] prepareClasspath() { 184 FileObject javaSourceJar = URLMapper.findFileObject(JavaSource.class.getProtectionDomain().getCodeSource().getLocation()); 185 FileObject root = javaSourceJar.getParent().getParent().getParent(); 186 187 return new FileObject[] { 188 FileUtil.getArchiveRoot(root.getFileObject("ide8/modules/org-netbeans-modules-java-source.jar")), 189 FileUtil.getArchiveRoot(root.getFileObject("ide8/modules/ext/javac-api.jar")), 190 FileUtil.getArchiveRoot(root.getFileObject("ide8/modules/ext/javac-impl.jar")), 191 }; 192 } 193 194 public void testAPIIsSelfContained() throws Exception { 195 FileObject root = makeScratchDir(this); 196 197 final ClassPath bootPath = ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL [0])); final ClassPath compilePath = ClassPathSupport.createClassPath(prepareClasspath()); 199 final ClassPath srcPath = ClassPathSupport.createClassPath(new URL [0]); 200 201 JavaSource js = JavaSource.create(ClasspathInfo.create(bootPath, compilePath, srcPath)); 202 203 js.runUserActionTask(new CancellableTask<CompilationController>() { 204 public void cancel() {} 205 public void run(CompilationController copy) throws Exception { 206 PackageElement apiPackage = copy.getElements().getPackageElement("org.netbeans.api.java.source"); 207 verifySelfContainedAPI(apiPackage); 208 209 apiPackage = copy.getElements().getPackageElement("org.netbeans.api.java.source.support"); 210 verifySelfContainedAPI(apiPackage); 211 } 212 },true); 213 214 assertTrue(violations.toString(), violations.isEmpty()); 215 } 216 217 222 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 223 test.clearWorkDir(); 224 File root = test.getWorkDir(); 225 assert root.isDirectory() && root.list().length == 0; 226 FileObject fo = FileUtil.toFileObject(root); 227 if (fo != null) { 228 return fo; 230 } else { 231 LocalFileSystem lfs = new LocalFileSystem(); 233 try { 234 lfs.setRootDirectory(root); 235 } catch (PropertyVetoException e) { 236 assert false : e; 237 } 238 Repository.getDefault().addFileSystem(lfs); 239 return lfs.getRoot(); 240 } 241 } 242 243 } 244 | Popular Tags |