1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.jdt.core.compiler.InvalidInputException; 18 import org.eclipse.jdt.internal.compiler.parser.Scanner; 19 import org.eclipse.jdt.internal.compiler.parser.TerminalTokens; 20 21 31 public class SimpleName extends Name { 32 33 38 public static final SimplePropertyDescriptor IDENTIFIER_PROPERTY = 39 new SimplePropertyDescriptor(SimpleName.class, "identifier", String .class, MANDATORY); 41 47 private static final List PROPERTY_DESCRIPTORS; 48 49 static { 50 List propertyList = new ArrayList (2); 51 createPropertyList(SimpleName.class, propertyList); 52 addProperty(IDENTIFIER_PROPERTY, propertyList); 53 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); 54 } 55 56 65 public static List propertyDescriptors(int apiLevel) { 66 return PROPERTY_DESCRIPTORS; 67 } 68 69 72 private static final String MISSING_IDENTIFIER = "MISSING"; 74 77 private String identifier = MISSING_IDENTIFIER; 78 79 90 SimpleName(AST ast) { 91 super(ast); 92 } 93 94 98 final List internalStructuralPropertiesForType(int apiLevel) { 99 return propertyDescriptors(apiLevel); 100 } 101 102 105 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) { 106 if (property == IDENTIFIER_PROPERTY) { 107 if (get) { 108 return getIdentifier(); 109 } else { 110 setIdentifier((String ) value); 111 return null; 112 } 113 } 114 return super.internalGetSetObjectProperty(property, get, value); 116 } 117 118 121 final int getNodeType0() { 122 return SIMPLE_NAME; 123 } 124 125 128 ASTNode clone0(AST target) { 129 SimpleName result = new SimpleName(target); 130 result.setSourceRange(this.getStartPosition(), this.getLength()); 131 result.setIdentifier(getIdentifier()); 132 return result; 133 } 134 135 138 final boolean subtreeMatch0(ASTMatcher matcher, Object other) { 139 return matcher.match(this, other); 141 } 142 143 146 void accept0(ASTVisitor visitor) { 147 visitor.visit(this); 148 visitor.endVisit(this); 149 } 150 151 156 public String getIdentifier() { 157 return this.identifier; 158 } 159 160 173 public void setIdentifier(String identifier) { 174 if (identifier == null) { 176 throw new IllegalArgumentException (); 177 } 178 Scanner scanner = this.ast.scanner; 179 char[] source = identifier.toCharArray(); 180 scanner.setSource(source); 181 final int length = source.length; 182 scanner.resetTo(0, length); 183 try { 184 int tokenType = scanner.getNextToken(); 185 switch(tokenType) { 186 case TerminalTokens.TokenNameIdentifier: 187 if (scanner.getCurrentTokenEndPosition() != length - 1) { 188 throw new IllegalArgumentException (); 190 } 191 break; 192 default: 193 throw new IllegalArgumentException (); 194 } 195 } catch(InvalidInputException e) { 196 throw new IllegalArgumentException (); 197 } 198 preValueChange(IDENTIFIER_PROPERTY); 199 this.identifier = identifier; 200 postValueChange(IDENTIFIER_PROPERTY); 201 } 202 203 206 void internalSetIdentifier(String ident) { 207 preValueChange(IDENTIFIER_PROPERTY); 208 this.identifier = ident; 209 postValueChange(IDENTIFIER_PROPERTY); 210 } 211 212 241 public boolean isDeclaration() { 242 StructuralPropertyDescriptor d = getLocationInParent(); 243 if (d == null) { 244 return false; 246 } 247 ASTNode parent = getParent(); 248 if (parent instanceof TypeDeclaration) { 249 return (d == TypeDeclaration.NAME_PROPERTY); 250 } 251 if (parent instanceof MethodDeclaration) { 252 MethodDeclaration p = (MethodDeclaration) parent; 253 return !p.isConstructor() && (d == MethodDeclaration.NAME_PROPERTY); 255 } 256 if (parent instanceof SingleVariableDeclaration) { 257 return (d == SingleVariableDeclaration.NAME_PROPERTY); 258 } 259 if (parent instanceof VariableDeclarationFragment) { 260 return (d == VariableDeclarationFragment.NAME_PROPERTY); 261 } 262 if (parent instanceof EnumDeclaration) { 263 return (d == EnumDeclaration.NAME_PROPERTY); 264 } 265 if (parent instanceof EnumConstantDeclaration) { 266 return (d == EnumConstantDeclaration.NAME_PROPERTY); 267 } 268 if (parent instanceof TypeParameter) { 269 return (d == TypeParameter.NAME_PROPERTY); 270 } 271 if (parent instanceof AnnotationTypeDeclaration) { 272 return (d == AnnotationTypeDeclaration.NAME_PROPERTY); 273 } 274 if (parent instanceof AnnotationTypeMemberDeclaration) { 275 return (d == AnnotationTypeMemberDeclaration.NAME_PROPERTY); 276 } 277 return false; 278 } 279 280 283 void appendName(StringBuffer buffer) { 284 buffer.append(getIdentifier()); 285 } 286 287 290 int memSize() { 291 int size = BASE_NAME_NODE_SIZE + 2 * 4; 292 if (identifier != MISSING_IDENTIFIER) { 293 size += stringSize(identifier); 295 } 296 return size; 297 } 298 299 302 int treeSize() { 303 return memSize(); 304 } 305 } 306 307 | Popular Tags |