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.editor.java;
043:
044: import java.util.Iterator;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047: import javax.lang.model.element.Element;
048: import javax.lang.model.element.ElementKind;
049: import javax.lang.model.element.TypeElement;
050: import javax.lang.model.type.ArrayType;
051: import javax.lang.model.type.DeclaredType;
052: import javax.lang.model.type.ErrorType;
053: import javax.lang.model.type.TypeKind;
054: import javax.lang.model.type.TypeMirror;
055: import javax.lang.model.type.TypeVariable;
056: import javax.lang.model.type.WildcardType;
057: import javax.lang.model.util.SimpleTypeVisitor6;
058:
059: import com.sun.source.util.TreePath;
060:
061: import org.netbeans.api.java.source.CompilationInfo;
062: import org.netbeans.api.java.source.SourceUtils;
063:
064: /**
065: *
066: * @author Dusan Balek
067: */
068: public class AutoImport extends SimpleTypeVisitor6<Void, Void> {
069:
070: private static final String CAPTURED_WILDCARD = "<captured wildcard>"; //NOI18N
071:
072: private CompilationInfo info;
073: private StringBuilder builder;
074: private TreePath path;
075:
076: private AutoImport(CompilationInfo info) {
077: this .info = info;
078: }
079:
080: public static AutoImport get(CompilationInfo info) {
081: return new AutoImport(info);
082: }
083:
084: public static CharSequence resolveImport(CompilationInfo info,
085: TreePath treePath, TypeMirror type) {
086: AutoImport imp = new AutoImport(info);
087: return imp.resolveImport(treePath, type);
088: }
089:
090: public CharSequence resolveImport(TreePath treePath, TypeMirror type) {
091: this .builder = new StringBuilder();
092: this .path = treePath;
093: visit(type, null);
094: return builder;
095: }
096:
097: @Override
098: public Void defaultAction(TypeMirror type, Void p) {
099: builder.append(type);
100: return null;
101: }
102:
103: @Override
104: public Void visitArray(ArrayType type, Void p) {
105: visit(type.getComponentType());
106: builder.append("[]"); //NOI18N
107: return null;
108: }
109:
110: @Override
111: public Void visitDeclared(DeclaredType type, Void p) {
112: TypeElement element = (TypeElement) type.asElement();
113: String name = element.getQualifiedName().toString();
114: ElementKind kind = element.getEnclosingElement().getKind();
115: if (kind.isClass() || kind.isInterface()
116: || kind == ElementKind.PACKAGE) {
117: try {
118: name = SourceUtils.resolveImport(info, path, name);
119: } catch (Exception e) {
120: Logger.getLogger("global").log(Level.INFO, null, e); //NOI18N
121: }
122: }
123: builder.append(name);
124: Iterator<? extends TypeMirror> it = type.getTypeArguments()
125: .iterator();
126: if (it.hasNext()) {
127: builder.append('<'); //NOI18N
128: while (it.hasNext()) {
129: visit(it.next());
130: if (it.hasNext())
131: builder.append(", "); //NOI18N
132: }
133: builder.append('>'); //NOI18N
134: }
135: return null;
136: }
137:
138: @Override
139: public Void visitTypeVariable(TypeVariable type, Void p) {
140: Element e = type.asElement();
141: if (e != null) {
142: CharSequence name = e.getSimpleName();
143: if (!CAPTURED_WILDCARD.contentEquals(name)) {
144: builder.append(name);
145: return null;
146: }
147: }
148: builder.append("?"); //NOI18N
149: TypeMirror bound = type.getLowerBound();
150: if (bound != null && bound.getKind() != TypeKind.NULL) {
151: builder.append(" super "); //NOI18N
152: visit(bound);
153: } else {
154: bound = type.getUpperBound();
155: if (bound != null && bound.getKind() != TypeKind.NULL) {
156: builder.append(" extends "); //NOI18N
157: if (bound.getKind() == TypeKind.TYPEVAR)
158: bound = ((TypeVariable) bound).getLowerBound();
159: visit(bound);
160: }
161: }
162: return null;
163: }
164:
165: @Override
166: public Void visitWildcard(WildcardType type, Void p) {
167: builder.append("?"); //NOI18N
168: TypeMirror bound = type.getSuperBound();
169: if (bound == null) {
170: bound = type.getExtendsBound();
171: if (bound != null) {
172: builder.append(" extends "); //NOI18N
173: if (bound.getKind() == TypeKind.WILDCARD)
174: bound = ((WildcardType) bound).getSuperBound();
175: visit(bound);
176: }
177: } else {
178: builder.append(" super "); //NOI18N
179: visit(bound);
180: }
181: return null;
182: }
183:
184: @Override
185: public Void visitError(ErrorType type, Void p) {
186: Element e = type.asElement();
187: if (e instanceof TypeElement) {
188: TypeElement te = (TypeElement) e;
189: builder.append(te.getSimpleName());
190: }
191: return null;
192: }
193: }
|