KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SourceField


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import org.eclipse.jdt.core.*;
14 import org.eclipse.jdt.core.dom.ASTNode;
15 import org.eclipse.jdt.internal.compiler.lookup.Binding;
16
17 /**
18  * @see IField
19  */

20
21 public class SourceField extends NamedMember implements IField {
22
23 /**
24  * Constructs a handle to the field with the given name in the specified type.
25  */

26 protected SourceField(JavaElement parent, String JavaDoc name) {
27     super(parent, name);
28 }
29 public boolean equals(Object JavaDoc o) {
30     if (!(o instanceof SourceField)) return false;
31     return super.equals(o);
32 }
33 public ASTNode findNode(org.eclipse.jdt.core.dom.CompilationUnit ast) {
34     // For field declarations, a variable declaration fragment is returned
35
// Return the FieldDeclaration instead
36
// For enum constant declaration, we return the node directly
37
ASTNode node = super.findNode(ast);
38     if (node == null) return null;
39     if (node.getNodeType() == ASTNode.ENUM_CONSTANT_DECLARATION) {
40         return node;
41     }
42     return node.getParent();
43 }
44 /**
45  * @see IField
46  */

47 public Object JavaDoc getConstant() throws JavaModelException {
48     Object JavaDoc constant = null;
49     SourceFieldElementInfo info = (SourceFieldElementInfo) getElementInfo();
50     final char[] constantSourceChars = info.initializationSource;
51     if (constantSourceChars == null) {
52         return null;
53     }
54             
55     String JavaDoc constantSource = new String JavaDoc(constantSourceChars);
56     String JavaDoc signature = info.getTypeSignature();
57     try {
58         if (signature.equals(Signature.SIG_INT)) {
59             constant = new Integer JavaDoc(constantSource);
60         } else if (signature.equals(Signature.SIG_SHORT)) {
61             constant = new Short JavaDoc(constantSource);
62         } else if (signature.equals(Signature.SIG_BYTE)) {
63             constant = new Byte JavaDoc(constantSource);
64         } else if (signature.equals(Signature.SIG_BOOLEAN)) {
65             constant = Boolean.valueOf(constantSource);
66         } else if (signature.equals(Signature.SIG_CHAR)) {
67             if (constantSourceChars.length != 3) {
68                 return null;
69             }
70             constant = new Character JavaDoc(constantSourceChars[1]);
71         } else if (signature.equals(Signature.SIG_DOUBLE)) {
72             constant = new Double JavaDoc(constantSource);
73         } else if (signature.equals(Signature.SIG_FLOAT)) {
74             constant = new Float JavaDoc(constantSource);
75         } else if (signature.equals(Signature.SIG_LONG)) {
76             if (constantSource.endsWith("L") || constantSource.endsWith("l")) { //$NON-NLS-1$ //$NON-NLS-2$
77
int index = constantSource.lastIndexOf("L");//$NON-NLS-1$
78
if (index != -1) {
79                     constant = new Long JavaDoc(constantSource.substring(0, index));
80                 } else {
81                     constant = new Long JavaDoc(constantSource.substring(0, constantSource.lastIndexOf("l")));//$NON-NLS-1$
82
}
83             } else {
84                 constant = new Long JavaDoc(constantSource);
85             }
86         } else if (signature.equals("QString;")) {//$NON-NLS-1$
87
constant = constantSource;
88         }
89     } catch (NumberFormatException JavaDoc e) {
90         // not a parsable constant
91
return null;
92     }
93     return constant;
94 }
95 /**
96  * @see IJavaElement
97  */

98 public int getElementType() {
99     return FIELD;
100 }
101 /* (non-Javadoc)
102  * @see org.eclipse.jdt.core.IField#getKey()
103  */

104 public String JavaDoc getKey() {
105     try {
106         return getKey(this, false/*don't open*/);
107     } catch (JavaModelException e) {
108         // happen only if force open is true
109
return null;
110     }
111 }
112 /**
113  * @see JavaElement#getHandleMemento()
114  */

115 protected char getHandleMementoDelimiter() {
116     return JavaElement.JEM_FIELD;
117 }
118 /*
119  * @see JavaElement#getPrimaryElement(boolean)
120  */

121 public IJavaElement getPrimaryElement(boolean checkOwner) {
122     if (checkOwner) {
123         CompilationUnit cu = (CompilationUnit)getAncestor(COMPILATION_UNIT);
124         if (cu.isPrimary()) return this;
125     }
126     IJavaElement primaryParent =this.parent.getPrimaryElement(false);
127     return ((IType)primaryParent).getField(this.name);
128 }
129 /**
130  * @see IField
131  */

132 public String JavaDoc getTypeSignature() throws JavaModelException {
133     SourceFieldElementInfo info = (SourceFieldElementInfo) getElementInfo();
134     return info.getTypeSignature();
135 }
136 /* (non-Javadoc)
137  * @see org.eclipse.jdt.core.IField#isEnumConstant()
138  */
public boolean isEnumConstant() throws JavaModelException {
139     return Flags.isEnum(getFlags());
140 }
141 /* (non-Javadoc)
142  * @see org.eclipse.jdt.core.IField#isResolved()
143  */

144 public boolean isResolved() {
145     return false;
146 }
147 public JavaElement resolved(Binding binding) {
148     SourceRefElement resolvedHandle = new ResolvedSourceField(this.parent, this.name, new String JavaDoc(binding.computeUniqueKey()));
149     resolvedHandle.occurrenceCount = this.occurrenceCount;
150     return resolvedHandle;
151 }
152 /**
153  * @private Debugging purposes
154  */

155 protected void toStringInfo(int tab, StringBuffer JavaDoc buffer, Object JavaDoc info, boolean showResolvedInfo) {
156     buffer.append(this.tabString(tab));
157     if (info == null) {
158         toStringName(buffer);
159         buffer.append(" (not open)"); //$NON-NLS-1$
160
} else if (info == NO_INFO) {
161         toStringName(buffer);
162     } else {
163         try {
164             buffer.append(Signature.toString(this.getTypeSignature()));
165             buffer.append(" "); //$NON-NLS-1$
166
toStringName(buffer);
167         } catch (JavaModelException e) {
168             buffer.append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$
169
}
170     }
171 }
172 }
173
Popular Tags