KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > model > VariableElementImpl


1 /*******************************************************************************
2  * Copyright (c) 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * wharley@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12
13 package org.eclipse.jdt.internal.compiler.apt.model;
14
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import javax.lang.model.element.Element;
20 import javax.lang.model.element.ElementKind;
21 import javax.lang.model.element.ElementVisitor;
22 import javax.lang.model.element.Modifier;
23 import javax.lang.model.element.Name;
24 import javax.lang.model.element.PackageElement;
25 import javax.lang.model.element.VariableElement;
26
27 import org.eclipse.jdt.core.compiler.CharOperation;
28 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
29 import org.eclipse.jdt.internal.compiler.impl.Constant;
30 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
31 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
32 import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
33 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
34 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
35 import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
36 import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
37
38 /**
39  * Implementation of VariableElement, which represents a a field, enum constant,
40  * method or constructor parameter, local variable, or exception parameter.
41  */

42 public class VariableElementImpl extends ElementImpl implements VariableElement {
43
44     /**
45      * @param binding might be a FieldBinding (for a field) or a LocalVariableBinding (for a method param)
46      */

47     VariableElementImpl(BaseProcessingEnvImpl env, VariableBinding binding) {
48         super(env, binding);
49     }
50     
51     @Override JavaDoc
52     public <R, P> R accept(ElementVisitor<R, P> v, P p)
53     {
54         return v.visitVariable(this, p);
55     }
56
57     @Override JavaDoc
58     protected AnnotationBinding[] getAnnotationBindings()
59     {
60         return ((VariableBinding)_binding).getAnnotations();
61     }
62
63     @Override JavaDoc
64     public Object JavaDoc getConstantValue() {
65         VariableBinding variableBinding = (VariableBinding) _binding;
66         Constant constant = variableBinding.constant();
67         if (constant == null || constant == Constant.NotAConstant) return null;
68         TypeBinding type = variableBinding.type;
69         switch (type.id) {
70             case TypeIds.T_boolean:
71                 return constant.booleanValue();
72             case TypeIds.T_byte:
73                 return constant.byteValue();
74             case TypeIds.T_char:
75                 return constant.charValue();
76             case TypeIds.T_double:
77                 return constant.doubleValue();
78             case TypeIds.T_float:
79                 return constant.floatValue();
80             case TypeIds.T_int:
81                 return constant.intValue();
82             case TypeIds.T_JavaLangString:
83                 return constant.stringValue();
84             case TypeIds.T_long:
85                 return constant.longValue();
86             case TypeIds.T_short:
87                 return constant.shortValue();
88         }
89         return null;
90     }
91     
92     @Override JavaDoc
93     public List JavaDoc<? extends Element> getEnclosedElements() {
94         return Collections.emptyList();
95     }
96
97     @Override JavaDoc
98     public Element getEnclosingElement() {
99         if (_binding instanceof FieldBinding) {
100             return _env.getFactory().newElement(((FieldBinding)_binding).declaringClass);
101         }
102         else if (_binding instanceof LocalVariableBinding){
103             //TODO: return enclosing method binding
104
throw new UnsupportedOperationException JavaDoc("NYI: VariableElementImpl.getEnclosingElement()"); //$NON-NLS-1$
105
}
106         return null;
107     }
108
109     @Override JavaDoc
110     public ElementKind getKind() {
111         if (_binding instanceof FieldBinding) {
112             if (((FieldBinding)_binding).declaringClass.isEnum()) {
113                 return ElementKind.ENUM_CONSTANT;
114             }
115             else {
116                 return ElementKind.FIELD;
117             }
118         }
119         else {
120             return ElementKind.PARAMETER;
121         }
122     }
123
124     @Override JavaDoc
125     public Set JavaDoc<Modifier> getModifiers()
126     {
127         if (_binding instanceof VariableBinding) {
128             return Factory.getModifiers(((VariableBinding)_binding).modifiers, getKind());
129         }
130         return Collections.emptySet();
131     }
132
133     @Override JavaDoc
134     PackageElement getPackage()
135     {
136         if (_binding instanceof FieldBinding) {
137             PackageBinding pkgBinding = ((FieldBinding)_binding).declaringClass.fPackage;
138             return _env.getFactory().newPackageElement(pkgBinding);
139         }
140         else {
141             // TODO: what is the package of a method parameter?
142
throw new UnsupportedOperationException JavaDoc("NYI: VariableElmentImpl.getPackage() for method parameter"); //$NON-NLS-1$
143
}
144     }
145     
146     @Override JavaDoc
147     public Name getSimpleName() {
148         return new NameImpl(((VariableBinding)_binding).name);
149     }
150
151     @Override JavaDoc
152     public boolean hides(Element hiddenElement)
153     {
154         if (_binding instanceof FieldBinding) {
155             if (!(((ElementImpl)hiddenElement)._binding instanceof FieldBinding)) {
156                 return false;
157             }
158             FieldBinding hidden = (FieldBinding)((ElementImpl)hiddenElement)._binding;
159             if (hidden.isPrivate()) {
160                 return false;
161             }
162             FieldBinding hider = (FieldBinding)_binding;
163             if (hidden == hider) {
164                 return false;
165             }
166             if (!CharOperation.equals(hider.name, hidden.name)) {
167                 return false;
168             }
169             return null != hider.declaringClass.findSuperTypeWithSameErasure(hidden.declaringClass);
170         }
171         // TODO: should we implement hides() for method parameters?
172
return false;
173     }
174
175     @Override JavaDoc
176     public String JavaDoc toString() {
177         return new String JavaDoc(((VariableBinding) _binding).name);
178     }
179 }
180
Popular Tags