KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > query > ElementReferenceList


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.api.java.source.query;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.lang.model.element.Element;
26 import javax.lang.model.element.ElementKind;
27 import javax.lang.model.element.PackageElement;
28 import javax.lang.model.element.TypeElement;
29
30 /**
31  * A list of references to a specified element, as well as how that reference
32  * uses the element. Usage constants are defined in UseFinder, and describe
33  * whether the access is a read or write, and is a private, package-private, or
34  * public/protected access. Note: the reference list includes the declaration
35  * of the element.
36  *
37  * @author Tom Ball
38  */

39 public class ElementReferenceList<E> {
40     Element var;
41     PackageElement varPackage;
42     TypeElement varClass;
43     int usage;
44     List JavaDoc<E> refList;
45
46     public ElementReferenceList(Element var) {
47         this.var = var;
48         refList = new LinkedList JavaDoc<E>();
49         varPackage = packageElement(var);
50         varClass = enclosingTypeElement(var);
51         usage = 0;
52     }
53
54     /**
55      * Adds a reference to this list, with its usage flags.
56      */

57     protected void addReference(E e, int flags) {
58         if (!refList.contains(e))
59             refList.add(e);
60         usage |= flags;
61     }
62
63     /**
64      * Returns flags indicating how the element is referenced.
65      *
66      * @return the usage flags for the element.
67      * @see org.netbeans.api.java.source.query.UseFinder
68      */

69     public int getUsage() {
70         return usage;
71     }
72     
73     /**
74      * Returns the list of references to the element.
75      */

76     public List JavaDoc<E> getReferences() {
77         return new ArrayList JavaDoc<E>(refList);
78     }
79     
80     /**
81      * Returns the element for which this list's references refer to.
82      */

83     public Element getElement() {
84         return var;
85     }
86     
87     /**
88      * Returns the type of the element for which this list's references refer to.
89      */

90     protected TypeElement getTypeElement() {
91         return varClass;
92     }
93     
94     /**
95      * Returns the package of the element for which this list's references refer to.
96      */

97     protected PackageElement getPackageElement() {
98         return varPackage;
99     }
100
101     public int hashCode() {
102         return var.hashCode();
103     }
104
105     public boolean equals(Object JavaDoc obj) {
106         return obj instanceof ElementReferenceList &&
107             var == ((ElementReferenceList) obj).var;
108     }
109
110     private TypeElement enclosingTypeElement(Element element) {
111         Element e = element;
112         while (e != null && !(e instanceof TypeElement)) {
113             e = e.getEnclosingElement();
114         }
115         return (TypeElement)e;
116     }
117
118     private PackageElement packageElement(Element element) {
119         Element e = element;
120         while (e.getKind() != ElementKind.PACKAGE) {
121             e = e.getEnclosingElement();
122         }
123         return (PackageElement)e;
124     }
125 }
126
127
Popular Tags