added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / Clinit.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.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.*;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.env.IGenericType;
17 import org.eclipse.jdt.internal.compiler.flow.*;
18 import org.eclipse.jdt.internal.compiler.lookup.*;
19 import org.eclipse.jdt.internal.compiler.parser.*;
20 import org.eclipse.jdt.internal.compiler.problem.*;
21
22 public class Clinit extends AbstractMethodDeclaration {
23         
24         private FieldBinding assertionSyntheticFieldBinding = null;
25         private FieldBinding classLiteralSyntheticField = null;
26
27         public Clinit(CompilationResult compilationResult) {
28                 super(compilationResult);
29                 modifiers = 0;
30                 selector = TypeConstants.CLINIT;
31         }
32
33         public void analyseCode(
34                 ClassScope classScope,
35                 InitializationFlowContext staticInitializerFlowContext,
36                 FlowInfo flowInfo) {
37
38                 if (ignoreFurtherInvestigation)
39                         return;
40                 try {
41                         ExceptionHandlingFlowContext clinitContext =
42                                 new ExceptionHandlingFlowContext(
43                                         staticInitializerFlowContext.parent,
44                                         this,
45                                         NoExceptions,
46                                         scope,
47                                         FlowInfo.DEAD_END);
48
49                         // check for missing returning path
50                         this.needFreeReturn = flowInfo.isReachable();
51
52                         // check missing blank final field initializations
53                         flowInfo = flowInfo.mergedWith(staticInitializerFlowContext.initsOnReturn);
54                         FieldBinding[] fields = scope.enclosingSourceType().fields();
55                         for (int i = 0, count = fields.length; i < count; i++) {
56                                 FieldBinding field;
57                                 if ((field = fields[i]).isStatic()
58                                         && field.isFinal()
59                                         && (!flowInfo.isDefinitelyAssigned(fields[i]))) {
60                                         scope.problemReporter().uninitializedBlankFinalField(
61                                                 field,
62                                                 scope.referenceType().declarationOf(field.original()));
63                                         // can complain against the field decl, since only one <clinit>
64                                 }
65                         }
66                         // check static initializers thrown exceptions
67                         staticInitializerFlowContext.checkInitializerExceptions(
68                                 scope,
69                                 clinitContext,
70                                 flowInfo);
71                 } catch (AbortMethod e) {
72                         this.ignoreFurtherInvestigation = true;
73                 }
74         }
75
76         /**
77          * Bytecode generation for a <clinit> method
78          *
79          * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
80          * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
81          */
82         public void generateCode(ClassScope classScope, ClassFile classFile) {
83
84                 int clinitOffset = 0;
85                 if (ignoreFurtherInvestigation) {
86                         // should never have to add any <clinit> problem method
87                         return;
88                 }
89                 try {
90                         clinitOffset = classFile.contentsOffset;
91                         this.generateCode(classScope, classFile, clinitOffset);
92                 } catch (AbortMethod e) {
93                         // should never occur
94                         // the clinit referenceContext is the type declaration
95                         // All clinit problems will be reported against the type: AbortType instead of AbortMethod
96                         // reset the contentsOffset to the value before generating the clinit code
97                         // decrement the number of method info as well.
98                         // This is done in the addProblemMethod and addProblemConstructor for other
99                         // cases.
100                         if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
101                                 // a branch target required a goto_w, restart code gen in wide mode.
102                                 try {
103                                         classFile.contentsOffset = clinitOffset;
104                                         classFile.methodCount--;
105                                         classFile.codeStream.wideMode = true; // request wide mode 
106                                         this.generateCode(classScope, classFile, clinitOffset);
107                                         // restart method generation
108                                 } catch (AbortMethod e2) {
109                                         classFile.contentsOffset = clinitOffset;
110                                         classFile.methodCount--;
111                                 }
112                         } else {
113                                 // produce a problem method accounting for this fatal error
114                                 classFile.contentsOffset = clinitOffset;
115                                 classFile.methodCount--;
116                         }
117                 }
118         }
119
120         /**
121          * Bytecode generation for a <clinit> method
122          *
123          * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
124          * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
125          */
126         private void generateCode(
127                 ClassScope classScope,
128                 ClassFile classFile,
129                 int clinitOffset) {
130
131                 ConstantPool constantPool = classFile.constantPool;
132                 int constantPoolOffset = constantPool.currentOffset;
133                 int constantPoolIndex = constantPool.currentIndex;
134                 classFile.generateMethodInfoHeaderForClinit();
135                 int codeAttributeOffset = classFile.contentsOffset;
136                 classFile.generateCodeAttributeHeader();
137                 CodeStream codeStream = classFile.codeStream;
138                 this.resolve(classScope);
139
140                 codeStream.reset(this, classFile);
141                 TypeDeclaration declaringType = classScope.referenceContext;
142
143                 // initialize local positions - including initializer scope.
144                 MethodScope staticInitializerScope = declaringType.staticInitializerScope;
145                 staticInitializerScope.computeLocalVariablePositions(0, codeStream);
146
147                 // 1.4 feature
148                 // This has to be done before any other initialization
149                 if (this.assertionSyntheticFieldBinding != null) {
150                         // generate code related to the activation of assertion for this class
151                         codeStream.generateClassLiteralAccessForType(
152                                         classScope.enclosingSourceType(),
153                                 classLiteralSyntheticField);
154                         codeStream.invokeJavaLangClassDesiredAssertionStatus();
155                         Label falseLabel = new Label(codeStream);
156                         codeStream.ifne(falseLabel);
157                         codeStream.iconst_1();
158                         Label jumpLabel = new Label(codeStream);
159                         codeStream.goto_(jumpLabel);
160                         falseLabel.place();
161                         codeStream.iconst_0();
162                         jumpLabel.place();
163                         codeStream.putstatic(this.assertionSyntheticFieldBinding);
164                 }
165                 // generate static fields/initializers/enum constants
166                 final FieldDeclaration[] fieldDeclarations = declaringType.fields;
167                 if (declaringType.kind() == IGenericType.ENUM_DECL) {
168                         int enumCount = 0;
169                         int notEnumConstants = 0;
170                         if (fieldDeclarations != null) {
171                                 for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
172                                         FieldDeclaration fieldDecl = fieldDeclarations[i];
173                                         if (fieldDecl.isStatic()) {
174                                                 if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
175                                                         fieldDecl.generateCode(staticInitializerScope, codeStream);
176                                                         enumCount++;
177                                                 } else {
178                                                         notEnumConstants++;
179                                                 }
180                                         }
181                                 }
182                         }
183                         // enum need to initialize $VALUES synthetic cache of enum constants
184                         if (enumCount > 0) {
185                                 if (fieldDeclarations != null) {
186                                         // $VALUES := new <EnumType>[<enumCount>]
187                                         codeStream.generateInlinedValue(enumCount);
188                                         codeStream.anewarray(declaringType.binding);
189                                         for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
190                                                 FieldDeclaration fieldDecl = fieldDeclarations[i];
191                                                 // $VALUES[i] = <enum-constant-i>
192                                                 if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
193                                                         codeStream.dup();
194                                                         codeStream.generateInlinedValue(fieldDecl.binding.id);
195                                                         codeStream.getstatic(fieldDecl.binding);
196                                                         codeStream.aastore();
197                                                 }
198                                         }
199                                         codeStream.putstatic(declaringType.enumValuesSyntheticfield);
200                                 }
201                         }
202                         if (notEnumConstants != 0) {
203                                 // if fields that are not enum constants need to be generated (static initializer/static field)
204                                 for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
205                                         FieldDeclaration fieldDecl = fieldDeclarations[i];
206                                         if (fieldDecl.isStatic() && fieldDecl.getKind() != AbstractVariableDeclaration.ENUM_CONSTANT) {
207                                                 fieldDecl.generateCode(staticInitializerScope, codeStream);
208                                         }
209                                 }
210                         }
211                 } else {
212                         if (fieldDeclarations != null) {
213                                 for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
214                                         FieldDeclaration fieldDecl = fieldDeclarations[i];
215                                         if (fieldDecl.isStatic()) {
216                                                 fieldDecl.generateCode(staticInitializerScope, codeStream);
217                                         }
218                                 }
219                         }
220                 }
221                 
222                 if (codeStream.position == 0) {
223                         // do not need to output a Clinit if no bytecodes
224                         // so we reset the offset inside the byte array contents.
225                         classFile.contentsOffset = clinitOffset;
226                         // like we don't addd a method we need to undo the increment on the method count
227                         classFile.methodCount--;
228                         // reset the constant pool to its state before the clinit
229                         constantPool.resetForClinit(constantPoolIndex, constantPoolOffset);
230                 } else {
231                         if (this.needFreeReturn) {
232                                 int oldPosition = codeStream.position;
233                                 codeStream.return_();
234                                 codeStream.updateLocalVariablesAttribute(oldPosition);
235                         }
236                         // Record the end of the clinit: point to the declaration of the class
237                         codeStream.recordPositionsFrom(0, declaringType.sourceStart);
238                         classFile.completeCodeAttributeForClinit(codeAttributeOffset);
239                 }
240         }
241
242         public boolean isClinit() {
243
244                 return true;
245         }
246
247         public boolean isInitializationMethod() {
248
249                 return true;
250         }
251
252         public boolean isStatic() {
253
254                 return true;
255         }
256
257         public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
258                 //the clinit is filled by hand .... 
259         }
260
261         public StringBuffer print(int tab, StringBuffer output) {
262
263                 printIndent(tab, output).append("<clinit>()"); //$NON-NLS-1$
264                 printBody(tab + 1, output);
265                 return output;
266         }
267
268         public void resolve(ClassScope classScope) {
269
270                 this.scope = new MethodScope(classScope, classScope.referenceContext, true);
271         }
272
273         public void traverse(
274                 ASTVisitor visitor,
275                 ClassScope classScope) {
276
277                 visitor.visit(this, classScope);
278                 visitor.endVisit(this, classScope);
279         }
280
281         public void setAssertionSupport(FieldBinding assertionSyntheticFieldBinding, boolean needClassLiteralField) {
282
283                 this.assertionSyntheticFieldBinding = assertionSyntheticFieldBinding;
284
285                 // we need to add the field right now, because the field infos are generated before the methods
286                 SourceTypeBinding sourceType =
287                         this.scope.outerMostMethodScope().enclosingSourceType();
288                 if (needClassLiteralField) {
289                         this.classLiteralSyntheticField =
290                                 sourceType.addSyntheticFieldForClassLiteral(sourceType, scope);
291                 }
292         }
293
294 }