1 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 20 21 public class SourceField extends NamedMember implements IField { 22 23 26 protected SourceField(JavaElement parent, String name) { 27 super(parent, name); 28 } 29 public boolean equals(Object 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 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 47 public Object getConstant() throws JavaModelException { 48 Object constant = null; 49 SourceFieldElementInfo info = (SourceFieldElementInfo) getElementInfo(); 50 final char[] constantSourceChars = info.initializationSource; 51 if (constantSourceChars == null) { 52 return null; 53 } 54 55 String constantSource = new String (constantSourceChars); 56 String signature = info.getTypeSignature(); 57 try { 58 if (signature.equals(Signature.SIG_INT)) { 59 constant = new Integer (constantSource); 60 } else if (signature.equals(Signature.SIG_SHORT)) { 61 constant = new Short (constantSource); 62 } else if (signature.equals(Signature.SIG_BYTE)) { 63 constant = new Byte (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 (constantSourceChars[1]); 71 } else if (signature.equals(Signature.SIG_DOUBLE)) { 72 constant = new Double (constantSource); 73 } else if (signature.equals(Signature.SIG_FLOAT)) { 74 constant = new Float (constantSource); 75 } else if (signature.equals(Signature.SIG_LONG)) { 76 if (constantSource.endsWith("L") || constantSource.endsWith("l")) { int index = constantSource.lastIndexOf("L"); if (index != -1) { 79 constant = new Long (constantSource.substring(0, index)); 80 } else { 81 constant = new Long (constantSource.substring(0, constantSource.lastIndexOf("l"))); } 83 } else { 84 constant = new Long (constantSource); 85 } 86 } else if (signature.equals("QString;")) { constant = constantSource; 88 } 89 } catch (NumberFormatException e) { 90 return null; 92 } 93 return constant; 94 } 95 98 public int getElementType() { 99 return FIELD; 100 } 101 104 public String getKey() { 105 try { 106 return getKey(this, false); 107 } catch (JavaModelException e) { 108 return null; 110 } 111 } 112 115 protected char getHandleMementoDelimiter() { 116 return JavaElement.JEM_FIELD; 117 } 118 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 132 public String getTypeSignature() throws JavaModelException { 133 SourceFieldElementInfo info = (SourceFieldElementInfo) getElementInfo(); 134 return info.getTypeSignature(); 135 } 136 public boolean isEnumConstant() throws JavaModelException { 139 return Flags.isEnum(getFlags()); 140 } 141 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 (binding.computeUniqueKey())); 149 resolvedHandle.occurrenceCount = this.occurrenceCount; 150 return resolvedHandle; 151 } 152 155 protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) { 156 buffer.append(this.tabString(tab)); 157 if (info == null) { 158 toStringName(buffer); 159 buffer.append(" (not open)"); } else if (info == NO_INFO) { 161 toStringName(buffer); 162 } else { 163 try { 164 buffer.append(Signature.toString(this.getTypeSignature())); 165 buffer.append(" "); toStringName(buffer); 167 } catch (JavaModelException e) { 168 buffer.append("<JavaModelException in toString of " + getElementName()); } 170 } 171 } 172 } 173 | Popular Tags |