KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > APIIsSelfContainedTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.java.source;
20
21 import java.beans.PropertyVetoException JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
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 /**
52  *
53  * @author Jan Lahoda
54  */

55 public class APIIsSelfContainedTest extends NbTestCase {
56
57     private List JavaDoc<String JavaDoc> violations = new ArrayList JavaDoc<String JavaDoc>();
58     
59     /**
60      * Creates a new instance of APIIsSelfContained
61      */

62
63     public APIIsSelfContainedTest(String JavaDoc name) {
64         super(name);
65     }
66     
67     protected void setUp() throws Exception JavaDoc {
68         SourceUtilsTestUtil.prepareTest(new String JavaDoc[0], new Object JavaDoc[0]);
69     }
70     
71     private boolean shouldCheck(Set JavaDoc<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 JavaDoc what) throws Exception JavaDoc {
81         FileLock lock = file.lock();
82         OutputStream JavaDoc 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 JavaDoc<String JavaDoc> API_PACKAGES = Arrays.asList(new String JavaDoc[] {
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 JavaDoc nameS = /*!!!!*/clazz.toString();
103         
104         for (String JavaDoc 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; //do not check non-public things
127

128         verifyAPIClass(ve.asType(), in);
129     }
130     
131     private void verifySelfContainedAPI(ExecutableElement ee, TypeElement in) {
132         if (!shouldCheck(ee.getModifiers()))
133             return; //do not check non-public things
134

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; //do not check non-public things
144

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 JavaDoc {
195         FileObject root = makeScratchDir(this);
196         
197         final ClassPath bootPath = ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL JavaDoc[0]));//ClassPath.getClassPath(source, ClassPath.BOOT);
198
final ClassPath compilePath = ClassPathSupport.createClassPath(prepareClasspath());
199         final ClassPath srcPath = ClassPathSupport.createClassPath(new URL JavaDoc[0]);
200         
201         JavaSource js = JavaSource.create(ClasspathInfo.create(bootPath, compilePath, srcPath)/*, source*/);
202         
203         js.runUserActionTask(new CancellableTask<CompilationController>() {
204             public void cancel() {}
205             public void run(CompilationController copy) throws Exception JavaDoc {
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     /**Copied from org.netbeans.api.project.
218      * Create a scratch directory for tests.
219      * Will be in /tmp or whatever, and will be empty.
220      * If you just need a java.io.File use clearWorkDir + getWorkDir.
221      */

222     public static FileObject makeScratchDir(NbTestCase test) throws IOException JavaDoc {
223         test.clearWorkDir();
224         File JavaDoc root = test.getWorkDir();
225         assert root.isDirectory() && root.list().length == 0;
226         FileObject fo = FileUtil.toFileObject(root);
227         if (fo != null) {
228             // Presumably using masterfs.
229
return fo;
230         } else {
231             // For the benefit of those not using masterfs.
232
LocalFileSystem lfs = new LocalFileSystem();
233             try {
234                 lfs.setRootDirectory(root);
235             } catch (PropertyVetoException JavaDoc e) {
236                 assert false : e;
237             }
238             Repository.getDefault().addFileSystem(lfs);
239             return lfs.getRoot();
240         }
241     }
242     
243 }
244
Popular Tags