KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > util > SourceUtils


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
20 package org.netbeans.modules.j2ee.persistence.util;
21
22 import com.sun.source.tree.*;
23 import com.sun.source.util.*;
24 import java.io.IOException JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.lang.model.element.*;
28 import javax.lang.model.type.*;
29 import org.netbeans.api.java.source.CompilationController;
30 import org.netbeans.api.java.source.ElementUtilities;
31 import org.netbeans.api.java.source.JavaSource.Phase;
32 import org.openide.util.Parameters;
33
34 /**
35  * <b><i>This class is a temporary copy of the
36  * <code>org.netbeans.modules.j2ee.common.source.SourceUtils</code>, will be
37  * removed when the equivalent functionality is provided by java/source, which
38  * should happen during M8</i></b>.
39  *
40  * @author Andrei Badea, Martin Adamek
41  */

42 public class SourceUtils {
43
44     // TODO we could probably also have a SourceUtils(CompilationController, TypeElement) factory method
45

46     /**
47      * The compilation controller this instance works with.
48      */

49     private final CompilationController controller;
50
51     /**
52      * The type element this instance works with. Do not use directly, use
53      * {@link #getTypeElement} instead.
54      */

55     private TypeElement typeElement;
56
57     /**
58      * The class tree corresponding to {@link #typeElement}. Do not use directly,
59      * use {@link #getClassTree} instead.
60      */

61     private ClassTree classTree;
62
63     // <editor-fold defaultstate="collapsed" desc="Constructors and factory methods">
64

65     SourceUtils(CompilationController controller, TypeElement typeElement) {
66         this.controller = controller;
67         this.typeElement = typeElement;
68     }
69
70     SourceUtils(CompilationController controller, ClassTree classTree) {
71         this.controller = controller;
72         this.classTree = classTree;
73     }
74
75     public static SourceUtils newInstance(CompilationController controller, TypeElement typeElement) {
76         Parameters.notNull("controller", controller); // NOI18N
77
Parameters.notNull("typeElement", typeElement); // NOI18N
78

79         return new SourceUtils(controller, typeElement);
80     }
81
82     public static SourceUtils newInstance(CompilationController controller, ClassTree classTree) {
83         Parameters.notNull("controller", controller); // NOI18N
84
Parameters.notNull("classTree", classTree); // NOI18N
85

86         return new SourceUtils(controller, classTree);
87     }
88
89     public static SourceUtils newInstance(CompilationController controller) throws IOException JavaDoc {
90         Parameters.notNull("controller", controller); // NOI18N
91

92         ClassTree classTree = findPublicTopLevelClass(controller);
93         if (classTree != null) {
94             return newInstance(controller, classTree);
95         }
96         return null;
97     }
98
99     // </editor-fold>
100

101     // <editor-fold defaultstate="collapsed" desc="Non-public static methods">
102

103     /**
104      * Finds the first public top-level type in the compilation unit given by the
105      * given <code>CompilationController</code>.
106      *
107      * This method assumes the restriction that there is at most a public
108      * top-level type declaration in a compilation unit, as described in the
109      * section 7.6 of the JLS.
110      */

111     static ClassTree findPublicTopLevelClass(CompilationController controller) throws IOException JavaDoc {
112         controller.toPhase(Phase.ELEMENTS_RESOLVED);
113
114         final String JavaDoc mainElementName = controller.getFileObject().getName();
115         for (Tree tree : controller.getCompilationUnit().getTypeDecls()) {
116             if (tree.getKind() != Tree.Kind.CLASS) {
117                 continue;
118             }
119             ClassTree classTree = (ClassTree)tree;
120             if (!classTree.getSimpleName().contentEquals(mainElementName)) {
121                 continue;
122             }
123             if (!classTree.getModifiers().getFlags().contains(Modifier.PUBLIC)) {
124                 continue;
125             }
126             return classTree;
127         }
128         return null;
129     }
130
131     // </editor-fold>
132

133     // <editor-fold desc="Public methods">
134

135     /**
136      * Returns the type element that this instance works with
137      * (corresponding to {@link #getClassTree}.
138      *
139      * @return the type element that this instance works with; never null.
140      */

141     public TypeElement getTypeElement() {
142         if (typeElement == null) {
143             assert classTree != null;
144             TreePath classTreePath = controller.getTrees().getPath(getCompilationController().getCompilationUnit(), classTree);
145             typeElement = (TypeElement)controller.getTrees().getElement(classTreePath);
146         }
147         return typeElement;
148     }
149
150     /**
151      * Returns the class tree that this instance works with
152      * (corresponding to {@link #getTypeElement}.
153      *
154      * @return the class tree that this instance works with; never null.
155      */

156     public ClassTree getClassTree() {
157         if (classTree == null) {
158             assert typeElement != null;
159             classTree = controller.getTrees().getTree(typeElement);
160         }
161         return classTree;
162     }
163
164     /**
165      * Returns true if {@link #getTypeElement} is a subtype of the given type.
166      *
167      * @param type the string representation of a type. The type will be parsed
168      * in the context of {@link #getTypeElement}.
169      * @return true {@link #getTypeElement} is a subtype of the given type,
170      * false otherwise.
171      */

172     public boolean isSubtype(String JavaDoc type) {
173         Parameters.notNull("type", type); // NOI18N
174

175         TypeMirror typeMirror = getCompilationController().getTreeUtilities().parseType(type, getTypeElement());
176         if (typeMirror != null) {
177             return getCompilationController().getTypes().isSubtype(getTypeElement().asType(), typeMirror);
178         }
179         return false;
180     }
181
182     // </editor-fold>
183

184     // <editor-fold defaultstate="collapsed" desc="Non-public methods">
185

186     /**
187      * Returns the <code>CompilationController</code> that this instance
188      * works with.
189      */

190     CompilationController getCompilationController() {
191         return controller;
192     }
193
194     /**
195      * Returns the non-synthetic no-arg constructor of the main type element.
196      */

197     ExecutableElement getNoArgConstructor() throws IOException JavaDoc {
198         controller.toPhase(Phase.ELEMENTS_RESOLVED);
199
200         ElementUtilities elementUtils = controller.getElementUtilities();
201         for (Element element : getTypeElement().getEnclosedElements()) {
202             if (element.getKind() == ElementKind.CONSTRUCTOR) {
203                 ExecutableElement constructor = (ExecutableElement)element;
204                 if (constructor.getParameters().size() == 0 && !elementUtils.isSynthetic(constructor)) {
205                     return constructor;
206                 }
207             }
208         }
209         return null;
210     }
211
212     /**
213      * Returns true if the given method is a main method.
214      */

215     private boolean isMainMethod(ExecutableElement method) {
216         // check method name
217
if (!method.getSimpleName().contentEquals("main")) { // NOI18N
218
return false;
219         }
220         // check modifiers
221
Set JavaDoc<Modifier> modifiers = method.getModifiers();
222         if (!modifiers.contains(Modifier.PUBLIC) || !modifiers.contains(Modifier.STATIC)) {
223             return false;
224         }
225         // check return type
226
if (TypeKind.VOID != method.getReturnType().getKind()) {
227             return false;
228         }
229         // check parameters
230
// there must be just one parameter
231
List JavaDoc<? extends VariableElement> params = method.getParameters();
232         if (params.size() != 1) {
233             return false;
234         }
235         VariableElement param = params.get(0); // it is ok to take first item, it was tested before
236
TypeMirror paramType = param.asType();
237         // parameter must be an array
238
if (TypeKind.ARRAY != paramType.getKind()) {
239             return false;
240         }
241         ArrayType arrayType = (ArrayType) paramType;
242         TypeElement stringTypeElement = controller.getElements().getTypeElement(String JavaDoc.class.getName());
243         // array must be array of Strings
244
if (!controller.getTypes().isSameType(stringTypeElement.asType(), arrayType.getComponentType())) {
245             return false;
246         }
247         return true;
248     }
249
250     // </editor-fold>
251
}
252
Popular Tags