1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.parser;
14 * Internal field structure for parsing recovery
16 import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration;
17 import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
18 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
19 import org.eclipse.jdt.internal.compiler.ast.Expression;
20 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
21 import org.eclipse.jdt.internal.compiler.ast.Statement;
22 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
24 public class RecoveredField extends RecoveredElement {
26 public FieldDeclaration fieldDeclaration;
27 boolean alreadyCompletedFieldInitialization;
29 public RecoveredType[] anonymousTypes;
30 public int anonymousTypeCount;
31 public RecoveredField(FieldDeclaration fieldDeclaration, RecoveredElement parent, int bracketBalance){
32 this(fieldDeclaration, parent, bracketBalance, null);
34 public RecoveredField(FieldDeclaration fieldDeclaration, RecoveredElement parent, int bracketBalance, Parser parser){
35 super(parent, bracketBalance, parser);
36 this.fieldDeclaration = fieldDeclaration;
37 this.alreadyCompletedFieldInitialization = fieldDeclaration.initialization != null;
40 * Record an expression statement if field is expecting an initialization expression,
41 * used for completion inside field initializers.
43 public RecoveredElement add(Statement statement, int bracketBalanceValue) {
45 if (this.alreadyCompletedFieldInitialization || !(statement instanceof Expression)) {
46 return super.add(statement, bracketBalanceValue);
48 this.alreadyCompletedFieldInitialization = true;
49 this.fieldDeclaration.initialization = (Expression)statement;
50 this.fieldDeclaration.declarationSourceEnd = statement.sourceEnd;
51 this.fieldDeclaration.declarationEnd = statement.sourceEnd;
56 * Record a type declaration if this field is expecting an initialization expression
57 * and the type is an anonymous type.
58 * Used for completion inside field initializers.
60 public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalanceValue) {
62 if (this.alreadyCompletedFieldInitialization
63 || ((typeDeclaration.bits & ASTNode.IsAnonymousTypeMASK) == 0)
64 || (this.fieldDeclaration.declarationSourceEnd != 0 && typeDeclaration.sourceStart > this.fieldDeclaration.declarationSourceEnd)) {
65 return super.add(typeDeclaration, bracketBalanceValue);
67 // Prepare anonymous type list
68 if (this.anonymousTypes == null) {
69 this.anonymousTypes = new RecoveredType[5];
70 this.anonymousTypeCount = 0;
72 if (this.anonymousTypeCount == this.anonymousTypes.length) {
76 (this.anonymousTypes = new RecoveredType[2 * this.anonymousTypeCount]),
78 this.anonymousTypeCount);
81 // Store type declaration as an anonymous type
82 RecoveredType element = new RecoveredType(typeDeclaration, this, bracketBalanceValue);
83 this.anonymousTypes[this.anonymousTypeCount++] = element;
88 * Answer the associated parsed structure
90 public ASTNode parseTree(){
91 return fieldDeclaration;
94 * Answer the very source end of the corresponding parse node
96 public int sourceEnd(){
97 return this.fieldDeclaration.declarationSourceEnd;
99 public String toString(int tab){
100 StringBuffer buffer = new StringBuffer(tabString(tab));
101 buffer.append("Recovered field:\n"); //$NON-NLS-1$
102 fieldDeclaration.print(tab + 1, buffer);
103 if (this.anonymousTypes != null) {
104 for (int i = 0; i < this.anonymousTypeCount; i++){
105 buffer.append("\n"); //$NON-NLS-1$
106 buffer.append(anonymousTypes[i].toString(tab + 1));
109 return buffer.toString();
111 public FieldDeclaration updatedFieldDeclaration(){
113 if (this.anonymousTypes != null) {
114 if(fieldDeclaration.initialization == null) {
115 for (int i = 0; i < this.anonymousTypeCount; i++){
116 if (anonymousTypes[i].preserveContent){
117 fieldDeclaration.initialization = this.anonymousTypes[i].updatedTypeDeclaration().allocation;
120 if (this.anonymousTypeCount > 0) fieldDeclaration.bits |= ASTNode.HasLocalTypeMASK;
121 } else if(fieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
122 // fieldDeclaration is an enum constant
123 for (int i = 0; i < this.anonymousTypeCount; i++){
124 if (anonymousTypes[i].preserveContent){
125 this.anonymousTypes[i].updatedTypeDeclaration();
130 return fieldDeclaration;
133 * A closing brace got consumed, might have closed the current element,
134 * in which case both the currentElement is exited.
136 * Fields have no associated braces, thus if matches, then update parent.
138 public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
139 if (bracketBalance > 0){ // was an array initializer
141 if (bracketBalance == 0) {
142 if(fieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
143 updateSourceEndIfNecessary(braceEnd - 1);
146 alreadyCompletedFieldInitialization = true;
150 } else if (bracketBalance == 0) {
151 alreadyCompletedFieldInitialization = true;
152 updateSourceEndIfNecessary(braceEnd - 1);
155 return parent.updateOnClosingBrace(braceStart, braceEnd);
160 * An opening brace got consumed, might be the expected opening one of the current element,
161 * in which case the bodyStart is updated.
163 public RecoveredElement updateOnOpeningBrace(int braceStart, int braceEnd){
164 if (fieldDeclaration.declarationSourceEnd == 0
165 && fieldDeclaration.type instanceof ArrayTypeReference
166 && !alreadyCompletedFieldInitialization){
168 return null; // no update is necessary (array initializer)
170 if (fieldDeclaration.declarationSourceEnd == 0
171 && fieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT){
173 return null; // no update is necessary (enum constant)
175 // might be an array initializer
176 this.updateSourceEndIfNecessary(braceStart - 1, braceEnd - 1);
177 return this.parent.updateOnOpeningBrace(braceStart, braceEnd);
179 public void updateParseTree(){
180 this.updatedFieldDeclaration();
183 * Update the declarationSourceEnd of the corresponding parse node
185 public void updateSourceEndIfNecessary(int bodyStart, int bodyEnd){
186 if (this.fieldDeclaration.declarationSourceEnd == 0) {
187 this.fieldDeclaration.declarationSourceEnd = bodyEnd;
188 this.fieldDeclaration.declarationEnd = bodyEnd;