001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.wsitconf.util;
043:
044: import com.sun.source.tree.*;
045: import com.sun.source.util.*;
046: import java.io.IOException;
047: import javax.lang.model.element.*;
048: import javax.lang.model.type.*;
049: import org.netbeans.api.java.source.CompilationController;
050: import org.netbeans.api.java.source.ElementUtilities;
051: import org.netbeans.api.java.source.JavaSource.Phase;
052: import org.openide.util.Parameters;
053:
054: /**
055: *
056: * @author Andrei Badea, Martin Adamek
057: */
058: public class SourceUtils {
059:
060: // TODO we could probably also have a SourceUtils(CompilationController, TypeElement) factory method
061:
062: /**
063: * The compilation controller this instance works with.
064: */
065: private final CompilationController controller;
066:
067: /**
068: * The type element this instance works with. Do not use directly, use
069: * {@link #getTypeElement} instead.
070: */
071: private TypeElement typeElement;
072:
073: /**
074: * The class tree corresponding to {@link #typeElement}. Do not use directly,
075: * use {@link #getClassTree} instead.
076: */
077: private ClassTree classTree;
078:
079: // <editor-fold defaultstate="collapsed" desc="Constructors and factory methods">
080:
081: SourceUtils(CompilationController controller,
082: TypeElement typeElement) {
083: this .controller = controller;
084: this .typeElement = typeElement;
085: }
086:
087: SourceUtils(CompilationController controller, ClassTree classTree) {
088: this .controller = controller;
089: this .classTree = classTree;
090: }
091:
092: public static SourceUtils newInstance(
093: CompilationController controller, TypeElement typeElement) {
094: Parameters.notNull("controller", controller); // NOI18N
095: Parameters.notNull("typeElement", typeElement); // NOI18N
096:
097: return new SourceUtils(controller, typeElement);
098: }
099:
100: public static SourceUtils newInstance(
101: CompilationController controller, ClassTree classTree) {
102: Parameters.notNull("controller", controller); // NOI18N
103: Parameters.notNull("classTree", classTree); // NOI18N
104:
105: return new SourceUtils(controller, classTree);
106: }
107:
108: public static SourceUtils newInstance(
109: CompilationController controller) throws IOException {
110: Parameters.notNull("controller", controller); // NOI18N
111:
112: ClassTree classTree = findPublicTopLevelClass(controller);
113: if (classTree != null) {
114: return newInstance(controller, classTree);
115: }
116: return null;
117: }
118:
119: // </editor-fold>
120:
121: // <editor-fold defaultstate="collapsed" desc="Non-public static methods">
122:
123: /**
124: * Finds the first public top-level type in the compilation unit given by the
125: * given <code>CompilationController</code>.
126: *
127: * This method assumes the restriction that there is at most a public
128: * top-level type declaration in a compilation unit, as described in the
129: * section 7.6 of the JLS.
130: */
131: static ClassTree findPublicTopLevelClass(
132: CompilationController controller) throws IOException {
133: controller.toPhase(Phase.ELEMENTS_RESOLVED);
134:
135: final String mainElementName = controller.getFileObject()
136: .getName();
137: for (Tree tree : controller.getCompilationUnit().getTypeDecls()) {
138: if (tree.getKind() != Tree.Kind.CLASS) {
139: continue;
140: }
141: ClassTree classTree = (ClassTree) tree;
142: if (!classTree.getSimpleName().contentEquals(
143: mainElementName)) {
144: continue;
145: }
146: if (!classTree.getModifiers().getFlags().contains(
147: Modifier.PUBLIC)) {
148: continue;
149: }
150: return classTree;
151: }
152: return null;
153: }
154:
155: // </editor-fold>
156:
157: // <editor-fold desc="Public methods">
158:
159: /**
160: * Returns the type element that this instance works with
161: * (corresponding to {@link #getClassTree}.
162: *
163: * @return the type element that this instance works with; never null.
164: */
165: public TypeElement getTypeElement() {
166: if (typeElement == null) {
167: assert classTree != null;
168: TreePath classTreePath = controller.getTrees().getPath(
169: getCompilationController().getCompilationUnit(),
170: classTree);
171: typeElement = (TypeElement) controller.getTrees()
172: .getElement(classTreePath);
173: }
174: return typeElement;
175: }
176:
177: /**
178: * Returns the class tree that this instance works with
179: * (corresponding to {@link #getTypeElement}.
180: *
181: * @return the class tree that this instance works with; never null.
182: */
183: public ClassTree getClassTree() {
184: if (classTree == null) {
185: assert typeElement != null;
186: classTree = controller.getTrees().getTree(typeElement);
187: }
188: return classTree;
189: }
190:
191: /**
192: * Returns true if {@link #getTypeElement} is a subtype of the given type.
193: *
194: * @param type the string representation of a type. The type will be parsed
195: * in the context of {@link #getTypeElement}.
196: * @return true {@link #getTypeElement} is a subtype of the given type,
197: * false otherwise.
198: */
199: public boolean isSubtype(String type) {
200: Parameters.notNull("type", type); // NOI18N
201:
202: TypeMirror typeMirror = getCompilationController()
203: .getTreeUtilities().parseType(type, getTypeElement());
204: if (typeMirror != null) {
205: return getCompilationController().getTypes().isSubtype(
206: getTypeElement().asType(), typeMirror);
207: }
208: return false;
209: }
210:
211: // </editor-fold>
212:
213: // <editor-fold defaultstate="collapsed" desc="Non-public methods">
214:
215: /**
216: * Returns the <code>CompilationController</code> that this instance
217: * works with.
218: */
219: CompilationController getCompilationController() {
220: return controller;
221: }
222:
223: /**
224: * Returns the non-synthetic no-arg constructor of the main type element.
225: */
226: ExecutableElement getNoArgConstructor() throws IOException {
227: controller.toPhase(Phase.ELEMENTS_RESOLVED);
228:
229: ElementUtilities elementUtils = controller
230: .getElementUtilities();
231: for (Element element : getTypeElement().getEnclosedElements()) {
232: if (element.getKind() == ElementKind.CONSTRUCTOR) {
233: ExecutableElement constructor = (ExecutableElement) element;
234: if (constructor.getParameters().size() == 0
235: && !elementUtils.isSynthetic(constructor)) {
236: return constructor;
237: }
238: }
239: }
240: return null;
241: }
242:
243: // </editor-fold>
244: }
|