KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > overridden > ElementDescription


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.overridden;
20
21 import java.util.Collection JavaDoc;
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 JavaDoc;
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 /**
40  *
41  * @author Jan Lahoda
42  */

43 public class ElementDescription {
44     
45     private ClasspathInfo originalCPInfo;
46     
47     private ElementHandle<Element> handle;
48     private ElementHandle<TypeElement> outtermostElement;
49     private Collection JavaDoc<Modifier> modifiers;
50     private String JavaDoc 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 JavaDoc getIcon() {
73         return UiUtils.getElementIcon(handle.getKind(), modifiers);
74     }
75     
76     public String JavaDoc getDisplayName() {
77         return displayName;
78     }
79     
80     public Collection JavaDoc<Modifier> getModifiers() {
81         return modifiers;
82     }
83     
84     private static class ElementNameVisitor extends SimpleElementVisitor6<String JavaDoc,Boolean JavaDoc> {
85         
86     @Override JavaDoc
87         public String JavaDoc visitPackage(PackageElement e, Boolean JavaDoc p) {
88             return p ? e.getQualifiedName().toString() : e.getSimpleName().toString();
89         }
90
91     @Override JavaDoc
92         public String JavaDoc visitType(TypeElement e, Boolean JavaDoc 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 JavaDoc
101         public String JavaDoc visitExecutable(ExecutableElement e, Boolean JavaDoc p) {
102             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc
126         public String JavaDoc visitVariable(VariableElement ve, Boolean JavaDoc p) {
127             return Utilities.getTypeName(ve.asType(), false) + " " + ve.getSimpleName();
128         }
129         
130         @Override JavaDoc
131         public String JavaDoc visitTypeParameter(TypeParameterElement tpe, Boolean JavaDoc p) {
132             return tpe.getSimpleName().toString();
133         }
134     }
135
136 }
137
Popular Tags