1 11 package org.eclipse.jdt.apt.core.internal.declaration; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv; 15 import org.eclipse.jdt.apt.core.internal.util.Factory; 16 import org.eclipse.jdt.core.dom.ASTNode; 17 import org.eclipse.jdt.core.dom.Expression; 18 import org.eclipse.jdt.core.dom.ITypeBinding; 19 import org.eclipse.jdt.core.dom.SimpleName; 20 import org.eclipse.jdt.core.dom.Type; 21 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 22 23 import com.sun.mirror.declaration.FieldDeclaration; 24 import com.sun.mirror.type.TypeMirror; 25 import com.sun.mirror.util.DeclarationVisitor; 26 27 35 public class ASTBasedFieldDeclarationImpl 36 extends ASTBasedMemberDeclarationImpl 37 implements FieldDeclaration { 38 39 public ASTBasedFieldDeclarationImpl( 40 final VariableDeclarationFragment astNode, 41 final IFile file, 42 final BaseProcessorEnv env) 43 { 44 super(astNode, file, env); 45 assert astNode.getParent() != null && 46 astNode.getParent().getNodeType() == ASTNode.FIELD_DECLARATION : 47 "parent isn't a field declaration"; } 49 50 public void accept(DeclarationVisitor visitor) 51 { 52 visitor.visitFieldDeclaration(this); 53 } 54 55 public String getConstantExpression() 56 { 57 final Object constant = getConstantValue(); 58 if( constant == null ) return null; 59 return constant.toString(); 60 } 61 62 public Object getConstantValue() 63 { 64 final VariableDeclarationFragment fragment = getAstNode(); 65 final Expression initializer = fragment.getInitializer(); 66 if( initializer == null ) return null; 67 return initializer.resolveConstantExpressionValue(); 68 } 69 70 public String getSimpleName() { 71 final VariableDeclarationFragment fragment = getAstNode(); 72 final SimpleName nameNode = fragment.getName(); 73 return nameNode == null ? EMPTY_STRING : nameNode.getIdentifier(); 74 } 75 76 public TypeMirror getType() 77 { 78 final org.eclipse.jdt.core.dom.FieldDeclaration fieldASTNode = getFieldDeclarationAstNode(); 79 final Type type = fieldASTNode.getType(); 80 if( type == null ) 81 return null; 82 final ITypeBinding typeBinding = type.resolveBinding(); 83 if( typeBinding == null ) 85 return Factory.createErrorClassType(type.toString()); 86 else{ 87 TypeMirror typeMirror = Factory.createTypeMirror( typeBinding, _env ); 88 if( typeMirror == null ) 89 typeMirror = Factory.createErrorClassType(typeBinding); 90 return typeMirror; 91 } 92 } 93 94 public String toString() 95 { 96 108 return getSimpleName(); 109 } 110 111 public MirrorKind kind(){ return MirrorKind.FIELD; } 112 113 VariableDeclarationFragment getAstNode() 114 { 115 return (VariableDeclarationFragment)_astNode; 116 } 117 118 org.eclipse.jdt.core.dom.FieldDeclaration getFieldDeclarationAstNode() 119 { 120 return (org.eclipse.jdt.core.dom.FieldDeclaration)_astNode.getParent(); 121 } 122 } 123 | Popular Tags |