1 12 13 package org.eclipse.jdt.internal.compiler.apt.model; 14 15 import java.util.Collections ; 16 import java.util.List ; 17 import java.util.Set ; 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 42 public class VariableElementImpl extends ElementImpl implements VariableElement { 43 44 47 VariableElementImpl(BaseProcessingEnvImpl env, VariableBinding binding) { 48 super(env, binding); 49 } 50 51 @Override 52 public <R, P> R accept(ElementVisitor<R, P> v, P p) 53 { 54 return v.visitVariable(this, p); 55 } 56 57 @Override 58 protected AnnotationBinding[] getAnnotationBindings() 59 { 60 return ((VariableBinding)_binding).getAnnotations(); 61 } 62 63 @Override 64 public Object 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 93 public List <? extends Element> getEnclosedElements() { 94 return Collections.emptyList(); 95 } 96 97 @Override 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 throw new UnsupportedOperationException ("NYI: VariableElementImpl.getEnclosingElement()"); } 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 } 115 else { 116 return ElementKind.FIELD; 117 } 118 } 119 else { 120 return ElementKind.PARAMETER; 121 } 122 } 123 124 @Override 125 public Set <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 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 throw new UnsupportedOperationException ("NYI: VariableElmentImpl.getPackage() for method parameter"); } 144 } 145 146 @Override 147 public Name getSimpleName() { 148 return new NameImpl(((VariableBinding)_binding).name); 149 } 150 151 @Override 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 return false; 173 } 174 175 @Override 176 public String toString() { 177 return new String (((VariableBinding) _binding).name); 178 } 179 } 180 | Popular Tags |