added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / parser / RecoveredField.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.parser;
12
13 /**
14  * Internal field structure for parsing recovery 
15  */
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;
23
24 public class RecoveredField extends RecoveredElement {
25
26         public FieldDeclaration fieldDeclaration;
27         boolean alreadyCompletedFieldInitialization;
28         
29         public RecoveredType[] anonymousTypes;
30         public int anonymousTypeCount;
31 public RecoveredField(FieldDeclaration fieldDeclaration, RecoveredElement parent, int bracketBalance){
32         this(fieldDeclaration, parent, bracketBalance, null);
33 }
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;
38 }
39 /*
40  * Record an expression statement if field is expecting an initialization expression,
41  * used for completion inside field initializers.
42  */
43 public RecoveredElement add(Statement statement, int bracketBalanceValue) {
44
45         if (this.alreadyCompletedFieldInitialization || !(statement instanceof Expression)) {
46                 return super.add(statement, bracketBalanceValue);
47         } else {
48                 this.alreadyCompletedFieldInitialization = true;
49                 this.fieldDeclaration.initialization = (Expression)statement;
50                 this.fieldDeclaration.declarationSourceEnd = statement.sourceEnd;
51                 this.fieldDeclaration.declarationEnd = statement.sourceEnd;
52                 return this;
53         }
54 }
55 /*
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.
59  */
60 public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalanceValue) {
61
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);
66         } else { 
67                 // Prepare anonymous type list
68                 if (this.anonymousTypes == null) {
69                         this.anonymousTypes = new RecoveredType[5];
70                         this.anonymousTypeCount = 0;
71                 } else {
72                         if (this.anonymousTypeCount == this.anonymousTypes.length) {
73                                 System.arraycopy(
74                                         this.anonymousTypes, 
75                                         0, 
76                                         (this.anonymousTypes = new RecoveredType[2 * this.anonymousTypeCount]), 
77                                         0, 
78                                         this.anonymousTypeCount); 
79                         }
80                 }
81                 // Store type declaration as an anonymous type
82                 RecoveredType element = new RecoveredType(typeDeclaration, this, bracketBalanceValue);
83                 this.anonymousTypes[this.anonymousTypeCount++] = element;
84                 return element;
85         }
86 }
87 /* 
88  * Answer the associated parsed structure
89  */
90 public ASTNode parseTree(){
91         return fieldDeclaration;
92 }
93 /*
94  * Answer the very source end of the corresponding parse node
95  */
96 public int sourceEnd(){
97         return this.fieldDeclaration.declarationSourceEnd;
98 }
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));
107                 }
108         }
109         return buffer.toString();
110 }
111 public FieldDeclaration updatedFieldDeclaration(){
112
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;
118                                 }
119                         }
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();
126                                 }
127                         }
128                 }
129         }
130         return fieldDeclaration;
131 }
132 /*
133  * A closing brace got consumed, might have closed the current element,
134  * in which case both the currentElement is exited.
135  *
136  * Fields have no associated braces, thus if matches, then update parent.
137  */
138 public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
139         if (bracketBalance > 0){ // was an array initializer
140                 bracketBalance--;
141                 if (bracketBalance == 0) {
142                         if(fieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
143                                 updateSourceEndIfNecessary(braceEnd - 1);
144                                 return parent;
145                         } else {
146                                 alreadyCompletedFieldInitialization = true;
147                         }
148                 }
149                 return this;
150         } else if (bracketBalance == 0) {
151                 alreadyCompletedFieldInitialization = true;
152                 updateSourceEndIfNecessary(braceEnd - 1);
153         }
154         if (parent != null){
155                 return parent.updateOnClosingBrace(braceStart, braceEnd);
156         }
157         return this;
158 }
159 /*
160  * An opening brace got consumed, might be the expected opening one of the current element,
161  * in which case the bodyStart is updated.
162  */
163 public RecoveredElement updateOnOpeningBrace(int braceStart, int braceEnd){
164         if (fieldDeclaration.declarationSourceEnd == 0 
165                 && fieldDeclaration.type instanceof ArrayTypeReference
166                 && !alreadyCompletedFieldInitialization){
167                 bracketBalance++;
168                 return null; // no update is necessary  (array initializer)
169         }
170         if (fieldDeclaration.declarationSourceEnd == 0 
171                 && fieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT){
172                 bracketBalance++;
173                 return null; // no update is necessary  (enum constant)
174         }
175         // might be an array initializer
176         this.updateSourceEndIfNecessary(braceStart - 1, braceEnd - 1);  
177         return this.parent.updateOnOpeningBrace(braceStart, braceEnd);  
178 }
179 public void updateParseTree(){
180         this.updatedFieldDeclaration();
181 }
182 /*
183  * Update the declarationSourceEnd of the corresponding parse node
184  */
185 public void updateSourceEndIfNecessary(int bodyStart, int bodyEnd){
186         if (this.fieldDeclaration.declarationSourceEnd == 0) {
187                 this.fieldDeclaration.declarationSourceEnd = bodyEnd;
188                 this.fieldDeclaration.declarationEnd = bodyEnd;
189         }
190 }
191 }