KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.apt.core.internal.env.BaseProcessorEnv;
14 import org.eclipse.jdt.apt.core.internal.util.Factory;
15 import org.eclipse.jdt.core.dom.ITypeBinding;
16 import org.eclipse.jdt.core.dom.IVariableBinding;
17
18 import com.sun.mirror.declaration.FieldDeclaration;
19 import com.sun.mirror.declaration.TypeDeclaration;
20 import com.sun.mirror.type.TypeMirror;
21 import com.sun.mirror.util.DeclarationVisitor;
22
23 public class FieldDeclarationImpl extends MemberDeclarationImpl implements FieldDeclaration
24 {
25     public FieldDeclarationImpl(final IVariableBinding binding, final BaseProcessorEnv env)
26     {
27         super(binding, env);
28         assert binding.isField() : "binding doesn't represent a field"; //$NON-NLS-1$
29
}
30     
31     public void accept(DeclarationVisitor visitor)
32     {
33         visitor.visitFieldDeclaration(this);
34     }
35
36     public String JavaDoc getConstantExpression()
37     {
38         final IVariableBinding field = getDeclarationBinding();
39         final Object JavaDoc constant = field.getConstantValue();
40         if( constant == null ) return null;
41         return constant.toString();
42     }
43
44     public Object JavaDoc getConstantValue()
45     {
46         final IVariableBinding field = getDeclarationBinding();
47         return field.getConstantValue();
48     }
49
50     public TypeDeclaration getDeclaringType()
51     {
52         final IVariableBinding field = getDeclarationBinding();
53         final ITypeBinding outer = field.getDeclaringClass();
54         return Factory.createReferenceType(outer, _env);
55     }
56
57     public String JavaDoc getSimpleName()
58     {
59         final IVariableBinding field = getDeclarationBinding();
60         final String JavaDoc name = field.getName();
61         return name == null ? "" : name; //$NON-NLS-1$
62
}
63
64     public TypeMirror getType()
65     {
66         final IVariableBinding field = getDeclarationBinding();
67         final TypeMirror typeMirror = Factory.createTypeMirror( field.getType(), _env );
68         if( typeMirror == null )
69             return Factory.createErrorClassType(field.getType());
70         return typeMirror;
71     }
72
73     public IVariableBinding getDeclarationBinding()
74     {
75         return (IVariableBinding)_binding;
76     }
77     
78     public String JavaDoc toString()
79     {
80     /*
81         final StringBuilder buffer = new StringBuilder();
82         final IVariableBinding field = getDeclarationBinding();
83         if( field.getType() != null ){
84             buffer.append( field.getType() );
85             buffer.append(' ');
86         }
87         buffer.append(field.getName());
88         return buffer.toString();
89     */

90         return getSimpleName();
91     }
92
93     public MirrorKind kind(){ return MirrorKind.FIELD; }
94
95     public boolean isFromSource()
96     {
97         final ITypeBinding type = getDeclarationBinding().getDeclaringClass();
98         return ( type != null && type.isFromSource() );
99     }
100 }
101
Popular Tags