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-2007 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.api.support.java;
043:
044: import com.sun.source.tree.ClassTree;
045: import com.sun.source.util.TreePath;
046: import java.util.List;
047: import javax.lang.model.element.Element;
048: import javax.lang.model.element.ElementKind;
049: import javax.lang.model.element.ExecutableElement;
050: import javax.lang.model.element.Modifier;
051: import javax.lang.model.element.TypeElement;
052: import javax.lang.model.type.TypeMirror;
053: import org.netbeans.api.java.source.CompilationController;
054: import org.openide.filesystems.FileObject;
055: import org.openide.util.Parameters;
056:
057: /**
058: * Miscellaneous utilities for working with Java sources.
059: *
060: * @author Andrei Badea
061: */
062: public final class SourceUtils {
063:
064: private SourceUtils() {
065: }
066:
067: /**
068: * Finds the first public top-level type in the compilation unit given by the
069: * given <code>CompilationController</code>.
070: *
071: * This method assumes the restriction that there is at most a public
072: * top-level type declaration in a compilation unit, as described in the
073: * section 7.6 of the JLS.
074: *
075: * @param controller a {@link CompilationController}.
076: * @return the <code>TypeElement</code> encapsulating the public top-level type
077: * in the compilation unit given by <code>controller</code> or null
078: * if no such type is found.
079: * @throws IllegalStateException when the controller was created with no file
080: * objects.
081: */
082: public static TypeElement getPublicTopLevelElement(
083: CompilationController controller) {
084: Parameters.notNull("controller", controller); // NOI18N
085:
086: FileObject mainFileObject = controller.getFileObject();
087: if (mainFileObject == null) {
088: throw new IllegalStateException();
089: }
090: String mainElementName = mainFileObject.getName();
091: List<? extends TypeElement> elements = controller
092: .getTopLevelElements();
093: if (elements != null) {
094: for (TypeElement element : elements) {
095: if (element.getModifiers().contains(Modifier.PUBLIC)
096: && element.getSimpleName().contentEquals(
097: mainElementName)) {
098: return element;
099: }
100: }
101: }
102: return null;
103: }
104:
105: /**
106: * Finds the first public top-level type in the compilation unit given by the
107: * given <code>CompilationController</code>.
108: *
109: * This method assumes the restriction that there is at most a public
110: * top-level type declaration in a compilation unit, as described in the
111: * section 7.6 of the JLS.
112: *
113: * @param controller a {@link CompilationController}.
114: * @return the <code>TypeElement</code> encapsulating the public top-level type
115: * in the compilation unit given by <code>controller</code> or null
116: * if no such type is found.
117: * @throws IllegalStateException when the controller was created with no file
118: * objects.
119: */
120: public static ClassTree getPublicTopLevelTree(
121: CompilationController controller) {
122: Parameters.notNull("controller", controller); // NOI18N
123:
124: TypeElement typeElement = getPublicTopLevelElement(controller);
125: if (typeElement != null) {
126: return controller.getTrees().getTree(typeElement);
127: }
128: return null;
129: }
130:
131: /**
132: * Finds whether the given <code>TypeElement</code> is the subtype of a
133: * given supertype. This is a convenience method for
134: * {@link javax.lang.model.util.Types#isSubtype}.
135: *
136: * @param controller a <code>CompilationController</code>.
137: * @param subtype the presumed subtype.
138: * @param supertype the presumed supertype.
139: * @return true if <code>subtype</code> if a subtype of </code>supertype</code>,
140: * false otherwise.
141: */
142: public static boolean isSubtype(CompilationController controller,
143: TypeElement subtype, String super type) {
144: Parameters.notNull("controller", controller); // NOI18N
145: Parameters.notNull("subtype", subtype); // NOI18N
146: Parameters.notNull("supertype", super type); // NOI18N
147:
148: TypeMirror typeMirror = controller.getTreeUtilities()
149: .parseType(super type, subtype);
150: if (typeMirror != null) {
151: return controller.getTypes().isSubtype(subtype.asType(),
152: typeMirror);
153: }
154: return false;
155: }
156:
157: /**
158: * A convenience method for converting a <code>ClassTree</code> to the
159: * corresponding <code>TypeElement</code>, if any.
160: */
161: static TypeElement classTree2TypeElement(
162: CompilationController controller, ClassTree classTree) {
163: assert controller != null;
164: assert classTree != null;
165:
166: TreePath classTreePath = controller.getTrees().getPath(
167: controller.getCompilationUnit(), classTree);
168: return (TypeElement) controller.getTrees().getElement(
169: classTreePath);
170: }
171:
172: /**
173: * Finds the no-argument non-synthetic constructor in the specified class.
174: */
175: static ExecutableElement getNoArgConstructor(
176: CompilationController controller, TypeElement typeElement) {
177: assert controller != null;
178: assert typeElement != null;
179:
180: for (Element element : typeElement.getEnclosedElements()) {
181: if (element.getKind() == ElementKind.CONSTRUCTOR) {
182: ExecutableElement constructor = (ExecutableElement) element;
183: if (constructor.getParameters().size() == 0
184: && !controller.getElementUtilities()
185: .isSynthetic(constructor)) {
186: return constructor;
187: }
188: }
189: }
190: return null;
191: }
192: }
|