1 19 package org.netbeans.modules.java.editor.overridden; 20 21 import java.util.Collection ; 22 import javax.lang.model.element.Element; 23 import javax.lang.model.element.ExecutableElement; 24 import javax.lang.model.element.Modifier; 25 import javax.lang.model.element.PackageElement; 26 import javax.lang.model.element.TypeElement; 27 import javax.lang.model.element.TypeParameterElement; 28 import javax.lang.model.element.VariableElement; 29 import javax.lang.model.util.SimpleElementVisitor6; 30 import javax.swing.Icon ; 31 import org.netbeans.api.java.source.ClasspathInfo; 32 import org.netbeans.api.java.source.CompilationInfo; 33 import org.netbeans.api.java.source.ElementHandle; 34 import org.netbeans.api.java.source.SourceUtils; 35 import org.netbeans.api.java.source.UiUtils; 36 import org.netbeans.modules.editor.java.Utilities; 37 import org.openide.filesystems.FileObject; 38 39 43 public class ElementDescription { 44 45 private ClasspathInfo originalCPInfo; 46 47 private ElementHandle<Element> handle; 48 private ElementHandle<TypeElement> outtermostElement; 49 private Collection <Modifier> modifiers; 50 private String displayName; 51 52 public ElementDescription(CompilationInfo info, Element element) { 53 this.originalCPInfo = info.getClasspathInfo(); 54 this.handle = ElementHandle.create(element); 55 this.outtermostElement = ElementHandle.create(SourceUtils.getOutermostEnclosingTypeElement(element)); 56 this.modifiers = element.getModifiers(); 57 this.displayName = element.accept(new ElementNameVisitor(), true); 58 } 59 60 public FileObject getSourceFile() { 61 FileObject file = SourceUtils.getFile(outtermostElement, originalCPInfo); 62 if (file != null) 63 return SourceUtils.getFile(outtermostElement, ClasspathInfo.create(file)); 64 else 65 return null; 66 } 67 68 public ElementHandle<Element> getHandle() { 69 return handle; 70 } 71 72 public Icon getIcon() { 73 return UiUtils.getElementIcon(handle.getKind(), modifiers); 74 } 75 76 public String getDisplayName() { 77 return displayName; 78 } 79 80 public Collection <Modifier> getModifiers() { 81 return modifiers; 82 } 83 84 private static class ElementNameVisitor extends SimpleElementVisitor6<String ,Boolean > { 85 86 @Override 87 public String visitPackage(PackageElement e, Boolean p) { 88 return p ? e.getQualifiedName().toString() : e.getSimpleName().toString(); 89 } 90 91 @Override 92 public String visitType(TypeElement e, Boolean p) { 93 if (e.getQualifiedName() == null || e.getSimpleName() == null) { 94 return "annonymous inner"; 95 } 96 97 return p ? e.getQualifiedName().toString() : e.getSimpleName().toString(); 98 } 99 100 @Override 101 public String visitExecutable(ExecutableElement e, Boolean p) { 102 StringBuffer sb = new StringBuffer (); 103 104 sb.append(e.getEnclosingElement().accept(this, p)); 105 sb.append("."); 106 sb.append(e.getSimpleName()); 107 sb.append("("); 108 109 boolean addComma = false; 110 111 for (VariableElement ve : e.getParameters()) { 112 if (addComma) 113 sb.append(", "); 114 115 addComma = true; 116 117 sb.append(ve.accept(this, p)); 118 } 119 120 sb.append(")"); 121 122 return sb.toString(); 123 } 124 125 @Override 126 public String visitVariable(VariableElement ve, Boolean p) { 127 return Utilities.getTypeName(ve.asType(), false) + " " + ve.getSimpleName(); 128 } 129 130 @Override 131 public String visitTypeParameter(TypeParameterElement tpe, Boolean p) { 132 return tpe.getSimpleName().toString(); 133 } 134 } 135 136 } 137 | Popular Tags |