KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > declaration > ASTBasedFieldDeclarationImpl


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

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 /**
28  * This field declaration implementation is based on the variable declaration
29  * fragment ast node in the parse tree.
30  * The most common scenario where such implementation is required is when
31  * the type of the field cannot be resolved. In this case, the jdt will not
32  * create the field binding. Information such as the declaring type as well
33  * as the name of the field will still be captured and make available to clients.
34  */

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"; //$NON-NLS-1$
48
}
49     
50     public void accept(DeclarationVisitor visitor)
51     {
52         visitor.visitFieldDeclaration(this);
53     }
54
55     public String JavaDoc getConstantExpression()
56     {
57         final Object JavaDoc constant = getConstantValue();
58         if( constant == null ) return null;
59         return constant.toString();
60     }
61
62     public Object JavaDoc 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 JavaDoc 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         // This is probably why we end up with an ast based implementation.
84
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 JavaDoc toString()
95     {
96         /*
97         final org.eclipse.jdt.core.dom.FieldDeclaration fieldASTNode = getFieldDeclarationAstNode();
98         StringBuilder buffer = new StringBuilder();
99         final Type type = fieldASTNode.getType();
100         if( type != null ){
101             buffer.append(type);
102             buffer.append(' ');
103         }
104         buffer.append( getSimpleName() );
105         
106         return buffer.toString();
107         */

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