001: /*******************************************************************************
002: * Copyright (c) 2007 BEA Systems, Inc.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * wharley@bea.com - initial API and implementation
010: *
011: *******************************************************************************/package org.eclipse.jdt.internal.compiler.apt.model;
012:
013: import java.util.Collections;
014: import java.util.List;
015: import java.util.Set;
016:
017: import javax.lang.model.element.Element;
018: import javax.lang.model.element.ElementKind;
019: import javax.lang.model.element.ElementVisitor;
020: import javax.lang.model.element.Modifier;
021: import javax.lang.model.element.Name;
022: import javax.lang.model.element.PackageElement;
023: import javax.lang.model.element.VariableElement;
024:
025: import org.eclipse.jdt.core.compiler.CharOperation;
026: import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
027: import org.eclipse.jdt.internal.compiler.impl.Constant;
028: import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
029: import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
030: import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
031: import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
032: import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
033: import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
034: import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
035:
036: /**
037: * Implementation of VariableElement, which represents a a field, enum constant,
038: * method or constructor parameter, local variable, or exception parameter.
039: */
040: public class VariableElementImpl extends ElementImpl implements
041: VariableElement {
042:
043: /**
044: * @param binding might be a FieldBinding (for a field) or a LocalVariableBinding (for a method param)
045: */
046: VariableElementImpl(BaseProcessingEnvImpl env,
047: VariableBinding binding) {
048: super (env, binding);
049: }
050:
051: @Override
052: public <R, P> R accept(ElementVisitor<R, P> v, P p) {
053: return v.visitVariable(this , p);
054: }
055:
056: @Override
057: protected AnnotationBinding[] getAnnotationBindings() {
058: return ((VariableBinding) _binding).getAnnotations();
059: }
060:
061: @Override
062: public Object getConstantValue() {
063: VariableBinding variableBinding = (VariableBinding) _binding;
064: Constant constant = variableBinding.constant();
065: if (constant == null || constant == Constant.NotAConstant)
066: return null;
067: TypeBinding type = variableBinding.type;
068: switch (type.id) {
069: case TypeIds.T_boolean:
070: return constant.booleanValue();
071: case TypeIds.T_byte:
072: return constant.byteValue();
073: case TypeIds.T_char:
074: return constant.charValue();
075: case TypeIds.T_double:
076: return constant.doubleValue();
077: case TypeIds.T_float:
078: return constant.floatValue();
079: case TypeIds.T_int:
080: return constant.intValue();
081: case TypeIds.T_JavaLangString:
082: return constant.stringValue();
083: case TypeIds.T_long:
084: return constant.longValue();
085: case TypeIds.T_short:
086: return constant.shortValue();
087: }
088: return null;
089: }
090:
091: @Override
092: public List<? extends Element> getEnclosedElements() {
093: return Collections.emptyList();
094: }
095:
096: @Override
097: public Element getEnclosingElement() {
098: if (_binding instanceof FieldBinding) {
099: return _env.getFactory().newElement(
100: ((FieldBinding) _binding).declaringClass);
101: } else if (_binding instanceof LocalVariableBinding) {
102: //TODO: return enclosing method binding
103: throw new UnsupportedOperationException(
104: "NYI: VariableElementImpl.getEnclosingElement()"); //$NON-NLS-1$
105: }
106: return null;
107: }
108:
109: @Override
110: public ElementKind getKind() {
111: if (_binding instanceof FieldBinding) {
112: if (((FieldBinding) _binding).declaringClass.isEnum()) {
113: return ElementKind.ENUM_CONSTANT;
114: } else {
115: return ElementKind.FIELD;
116: }
117: } else {
118: return ElementKind.PARAMETER;
119: }
120: }
121:
122: @Override
123: public Set<Modifier> getModifiers() {
124: if (_binding instanceof VariableBinding) {
125: return Factory.getModifiers(
126: ((VariableBinding) _binding).modifiers, getKind());
127: }
128: return Collections.emptySet();
129: }
130:
131: @Override
132: PackageElement getPackage() {
133: if (_binding instanceof FieldBinding) {
134: PackageBinding pkgBinding = ((FieldBinding) _binding).declaringClass.fPackage;
135: return _env.getFactory().newPackageElement(pkgBinding);
136: } else {
137: // TODO: what is the package of a method parameter?
138: throw new UnsupportedOperationException(
139: "NYI: VariableElmentImpl.getPackage() for method parameter"); //$NON-NLS-1$
140: }
141: }
142:
143: @Override
144: public Name getSimpleName() {
145: return new NameImpl(((VariableBinding) _binding).name);
146: }
147:
148: @Override
149: public boolean hides(Element hiddenElement) {
150: if (_binding instanceof FieldBinding) {
151: if (!(((ElementImpl) hiddenElement)._binding instanceof FieldBinding)) {
152: return false;
153: }
154: FieldBinding hidden = (FieldBinding) ((ElementImpl) hiddenElement)._binding;
155: if (hidden.isPrivate()) {
156: return false;
157: }
158: FieldBinding hider = (FieldBinding) _binding;
159: if (hidden == hider) {
160: return false;
161: }
162: if (!CharOperation.equals(hider.name, hidden.name)) {
163: return false;
164: }
165: return null != hider.declaringClass
166: .findSuperTypeWithSameErasure(hidden.declaringClass);
167: }
168: // TODO: should we implement hides() for method parameters?
169: return false;
170: }
171:
172: @Override
173: public String toString() {
174: return new String(((VariableBinding) _binding).name);
175: }
176: }
|