KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > java > AutoImport


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.editor.java;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import javax.lang.model.element.Element;
26 import javax.lang.model.element.ElementKind;
27 import javax.lang.model.element.TypeElement;
28 import javax.lang.model.type.ArrayType;
29 import javax.lang.model.type.DeclaredType;
30 import javax.lang.model.type.ErrorType;
31 import javax.lang.model.type.TypeKind;
32 import javax.lang.model.type.TypeMirror;
33 import javax.lang.model.type.TypeVariable;
34 import javax.lang.model.type.WildcardType;
35 import javax.lang.model.util.SimpleTypeVisitor6;
36
37 import com.sun.source.util.TreePath;
38
39 import org.netbeans.api.java.source.CompilationInfo;
40 import org.netbeans.api.java.source.SourceUtils;
41
42 /**
43  *
44  * @author Dusan Balek
45  */

46 public class AutoImport extends SimpleTypeVisitor6<Void JavaDoc, Void JavaDoc> {
47
48     private static final String JavaDoc CAPTURED_WILDCARD = "<captured wildcard>"; //NOI18N
49

50     private CompilationInfo info;
51     private StringBuilder JavaDoc builder;
52     private TreePath path;
53
54     private AutoImport(CompilationInfo info) {
55         this.info = info;
56     }
57
58     public static AutoImport get(CompilationInfo info) {
59         return new AutoImport(info);
60     }
61     
62     public static CharSequence JavaDoc resolveImport(CompilationInfo info, TreePath treePath, TypeMirror type) {
63         AutoImport imp = new AutoImport(info);
64         return imp.resolveImport(treePath, type);
65     }
66     
67     public CharSequence JavaDoc resolveImport(TreePath treePath, TypeMirror type) {
68         this.builder = new StringBuilder JavaDoc();
69         this.path = treePath;
70         visit(type, null);
71         return builder;
72     }
73     
74     @Override JavaDoc
75     public Void JavaDoc defaultAction(TypeMirror type, Void JavaDoc p) {
76         builder.append(type);
77         return null;
78     }
79         
80     @Override JavaDoc
81     public Void JavaDoc visitArray(ArrayType type, Void JavaDoc p) {
82         visit(type.getComponentType());
83         builder.append("[]"); //NOI18N
84
return null;
85     }
86     
87     @Override JavaDoc
88     public Void JavaDoc visitDeclared(DeclaredType type, Void JavaDoc p) {
89         TypeElement element = (TypeElement)type.asElement();
90         String JavaDoc name = element.getQualifiedName().toString();
91         ElementKind kind = element.getEnclosingElement().getKind();
92         if (kind.isClass() || kind.isInterface() || kind == ElementKind.PACKAGE) {
93             try {
94                 name = SourceUtils.resolveImport(info, path, name);
95             } catch (Exception JavaDoc e) {
96                 Logger.getLogger("global").log(Level.INFO, null, e); //NOI18N
97
}
98         }
99         builder.append(name);
100         Iterator JavaDoc<? extends TypeMirror> it = type.getTypeArguments().iterator();
101         if (it.hasNext()) {
102             builder.append('<'); //NOI18N
103
while(it.hasNext()) {
104                 visit(it.next());
105                 if (it.hasNext())
106                     builder.append(", "); //NOI18N
107
}
108             builder.append('>'); //NOI18N
109
}
110         return null;
111     }
112     
113     @Override JavaDoc
114     public Void JavaDoc visitTypeVariable(TypeVariable type, Void JavaDoc p) {
115         Element e = type.asElement();
116         if (e != null) {
117             CharSequence JavaDoc name = e.getSimpleName();
118             if (!CAPTURED_WILDCARD.contentEquals(name)) {
119                 builder.append(name);
120                 return null;
121             }
122         }
123         builder.append("?"); //NOI18N
124
TypeMirror bound = type.getLowerBound();
125         if (bound != null && bound.getKind() != TypeKind.NULL) {
126             builder.append(" super "); //NOI18N
127
visit(bound);
128         } else {
129             bound = type.getUpperBound();
130             if (bound != null && bound.getKind() != TypeKind.NULL) {
131                 builder.append(" extends "); //NOI18N
132
if (bound.getKind() == TypeKind.TYPEVAR)
133                     bound = ((TypeVariable)bound).getLowerBound();
134                 visit(bound);
135             }
136         }
137         return null;
138     }
139
140     @Override JavaDoc
141     public Void JavaDoc visitWildcard(WildcardType type, Void JavaDoc p) {
142         builder.append("?"); //NOI18N
143
TypeMirror bound = type.getSuperBound();
144         if (bound == null) {
145             bound = type.getExtendsBound();
146             if (bound != null) {
147                 builder.append(" extends "); //NOI18N
148
if (bound.getKind() == TypeKind.WILDCARD)
149                     bound = ((WildcardType)bound).getSuperBound();
150                 visit(bound);
151             }
152         } else {
153             builder.append(" super "); //NOI18N
154
visit(bound);
155         }
156         return null;
157     }
158
159     @Override JavaDoc
160     public Void JavaDoc visitError(ErrorType type, Void JavaDoc p) {
161         Element e = type.asElement();
162         if (e instanceof TypeElement) {
163             TypeElement te = (TypeElement)e;
164             builder.append(te.getSimpleName());
165         }
166         return null;
167     }
168 }
169
Popular Tags