1 19 20 package org.netbeans.modules.editor.java; 21 22 import java.util.Iterator ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 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 46 public class AutoImport extends SimpleTypeVisitor6<Void , Void > { 47 48 private static final String CAPTURED_WILDCARD = "<captured wildcard>"; 50 private CompilationInfo info; 51 private StringBuilder 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 resolveImport(CompilationInfo info, TreePath treePath, TypeMirror type) { 63 AutoImport imp = new AutoImport(info); 64 return imp.resolveImport(treePath, type); 65 } 66 67 public CharSequence resolveImport(TreePath treePath, TypeMirror type) { 68 this.builder = new StringBuilder (); 69 this.path = treePath; 70 visit(type, null); 71 return builder; 72 } 73 74 @Override 75 public Void defaultAction(TypeMirror type, Void p) { 76 builder.append(type); 77 return null; 78 } 79 80 @Override 81 public Void visitArray(ArrayType type, Void p) { 82 visit(type.getComponentType()); 83 builder.append("[]"); return null; 85 } 86 87 @Override 88 public Void visitDeclared(DeclaredType type, Void p) { 89 TypeElement element = (TypeElement)type.asElement(); 90 String 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 e) { 96 Logger.getLogger("global").log(Level.INFO, null, e); } 98 } 99 builder.append(name); 100 Iterator <? extends TypeMirror> it = type.getTypeArguments().iterator(); 101 if (it.hasNext()) { 102 builder.append('<'); while(it.hasNext()) { 104 visit(it.next()); 105 if (it.hasNext()) 106 builder.append(", "); } 108 builder.append('>'); } 110 return null; 111 } 112 113 @Override 114 public Void visitTypeVariable(TypeVariable type, Void p) { 115 Element e = type.asElement(); 116 if (e != null) { 117 CharSequence name = e.getSimpleName(); 118 if (!CAPTURED_WILDCARD.contentEquals(name)) { 119 builder.append(name); 120 return null; 121 } 122 } 123 builder.append("?"); TypeMirror bound = type.getLowerBound(); 125 if (bound != null && bound.getKind() != TypeKind.NULL) { 126 builder.append(" super "); visit(bound); 128 } else { 129 bound = type.getUpperBound(); 130 if (bound != null && bound.getKind() != TypeKind.NULL) { 131 builder.append(" extends "); if (bound.getKind() == TypeKind.TYPEVAR) 133 bound = ((TypeVariable)bound).getLowerBound(); 134 visit(bound); 135 } 136 } 137 return null; 138 } 139 140 @Override 141 public Void visitWildcard(WildcardType type, Void p) { 142 builder.append("?"); TypeMirror bound = type.getSuperBound(); 144 if (bound == null) { 145 bound = type.getExtendsBound(); 146 if (bound != null) { 147 builder.append(" extends "); if (bound.getKind() == TypeKind.WILDCARD) 149 bound = ((WildcardType)bound).getSuperBound(); 150 visit(bound); 151 } 152 } else { 153 builder.append(" super "); visit(bound); 155 } 156 return null; 157 } 158 159 @Override 160 public Void visitError(ErrorType type, Void 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 |