KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.source.tree.ClassTree;
22 import com.sun.source.tree.MethodTree;
23 import com.sun.source.tree.Tree;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.lang.model.element.Element;
29 import javax.lang.model.element.ElementKind;
30 import javax.lang.model.element.ExecutableElement;
31 import javax.lang.model.element.Modifier;
32 import javax.lang.model.element.TypeElement;
33 import javax.swing.text.Document JavaDoc;
34 import org.netbeans.api.java.source.CompilationInfo;
35 import org.netbeans.api.java.source.ElementHandle;
36 import org.netbeans.api.java.source.support.CancellableTreePathScanner;
37 import org.netbeans.api.java.source.ClassIndex;
38
39 /**
40  *
41  * @author Jan Lahoda
42  */

43 class IsOverriddenVisitor extends CancellableTreePathScanner<Void JavaDoc, Tree> {
44     
45     private CompilationInfo info;
46     private ClassIndex uq;
47     private Document JavaDoc doc;
48     
49     Map JavaDoc<ElementHandle<TypeElement>, List JavaDoc<ElementHandle<ExecutableElement>>> type2Declaration;
50     Map JavaDoc<ElementHandle<ExecutableElement>, MethodTree> declaration2Tree;
51     Map JavaDoc<ElementHandle<TypeElement>, ClassTree> declaration2Class;
52     
53     private Map JavaDoc<TypeElement, ElementHandle<TypeElement>> type2Handle;
54     
55     IsOverriddenVisitor(Document JavaDoc doc, CompilationInfo info) {
56         this.doc = doc;
57         this.info = info;
58         this.uq = info.getJavaSource().getClasspathInfo().getClassIndex();
59         
60         type2Declaration = new HashMap JavaDoc<ElementHandle<TypeElement>, List JavaDoc<ElementHandle<ExecutableElement>>>();
61         declaration2Tree = new HashMap JavaDoc<ElementHandle<ExecutableElement>, MethodTree>();
62         declaration2Class = new HashMap JavaDoc<ElementHandle<TypeElement>, ClassTree>();
63         
64         type2Handle = new HashMap JavaDoc<TypeElement, ElementHandle<TypeElement>>();
65     }
66     
67     private ElementHandle<TypeElement> getHandle(TypeElement type) {
68         ElementHandle<TypeElement> result = type2Handle.get(type);
69         
70         if (result == null) {
71             type2Handle.put(type, result = ElementHandle.create(type));
72         }
73         
74         return result;
75     }
76     
77     @Override JavaDoc
78     public Void JavaDoc visitMethod(MethodTree tree, Tree d) {
79         if (currentClass != null) {
80             Element el = info.getTrees().getElement(getCurrentPath());
81             
82             if (el != null && el.getKind() == ElementKind.METHOD) {
83                 if (!el.getModifiers().contains(Modifier.PRIVATE) && !el.getModifiers().contains(Modifier.STATIC)) {
84                     ExecutableElement overridee = (ExecutableElement) el;
85                     List JavaDoc<ElementHandle<ExecutableElement>> methods = type2Declaration.get(currentClass);
86                     
87                     if (methods == null) {
88                         type2Declaration.put(currentClass, methods = new ArrayList JavaDoc<ElementHandle<ExecutableElement>>());
89                     }
90                     
91                     ElementHandle<ExecutableElement> methodHandle = ElementHandle.create(overridee);
92                     
93                     methods.add(methodHandle);
94                     declaration2Tree.put(methodHandle, tree);
95                 }
96             }
97         }
98         
99         super.visitMethod(tree, tree);
100         return null;
101     }
102     
103     @Override JavaDoc
104     public Void JavaDoc visitClass(ClassTree tree, Tree d) {
105         Element decl = info.getTrees().getElement(getCurrentPath());
106         
107         if (decl != null && (decl.getKind().isClass() || decl.getKind().isInterface())) {
108             ElementHandle<TypeElement> oldCurrentClass = currentClass;
109             
110             currentClass = getHandle((TypeElement) decl);
111             declaration2Class.put(currentClass, tree);
112             super.visitClass(tree, d);
113             currentClass = oldCurrentClass;
114         } else {
115             super.visitClass(tree, d);
116         }
117         
118         return null;
119     }
120     
121     private ElementHandle<TypeElement> currentClass;
122     
123 }
Popular Tags