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.ast;
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.*;
22 public class Clinit extends AbstractMethodDeclaration {
24 private FieldBinding assertionSyntheticFieldBinding = null;
25 private FieldBinding classLiteralSyntheticField = null;
27 public Clinit(CompilationResult compilationResult) {
28 super(compilationResult);
30 selector = TypeConstants.CLINIT;
33 public void analyseCode(
34 ClassScope classScope,
35 InitializationFlowContext staticInitializerFlowContext,
38 if (ignoreFurtherInvestigation)
41 ExceptionHandlingFlowContext clinitContext =
42 new ExceptionHandlingFlowContext(
43 staticInitializerFlowContext.parent,
49 // check for missing returning path
50 this.needFreeReturn = flowInfo.isReachable();
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++) {
57 if ((field = fields[i]).isStatic()
59 && (!flowInfo.isDefinitelyAssigned(fields[i]))) {
60 scope.problemReporter().uninitializedBlankFinalField(
62 scope.referenceType().declarationOf(field.original()));
63 // can complain against the field decl, since only one <clinit>
66 // check static initializers thrown exceptions
67 staticInitializerFlowContext.checkInitializerExceptions(
71 } catch (AbortMethod e) {
72 this.ignoreFurtherInvestigation = true;
77 * Bytecode generation for a <clinit> method
79 * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
80 * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
82 public void generateCode(ClassScope classScope, ClassFile classFile) {
85 if (ignoreFurtherInvestigation) {
86 // should never have to add any <clinit> problem method
90 clinitOffset = classFile.contentsOffset;
91 this.generateCode(classScope, classFile, clinitOffset);
92 } catch (AbortMethod e) {
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
100 if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
101 // a branch target required a goto_w, restart code gen in wide mode.
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--;
113 // produce a problem method accounting for this fatal error
114 classFile.contentsOffset = clinitOffset;
115 classFile.methodCount--;
121 * Bytecode generation for a <clinit> method
123 * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
124 * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
126 private void generateCode(
127 ClassScope classScope,
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);
140 codeStream.reset(this, classFile);
141 TypeDeclaration declaringType = classScope.referenceContext;
143 // initialize local positions - including initializer scope.
144 MethodScope staticInitializerScope = declaringType.staticInitializerScope;
145 staticInitializerScope.computeLocalVariablePositions(0, codeStream);
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);
161 codeStream.iconst_0();
163 codeStream.putstatic(this.assertionSyntheticFieldBinding);
165 // generate static fields/initializers/enum constants
166 final FieldDeclaration[] fieldDeclarations = declaringType.fields;
167 if (declaringType.kind() == IGenericType.ENUM_DECL) {
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);
183 // enum need to initialize $VALUES synthetic cache of enum constants
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) {
194 codeStream.generateInlinedValue(fieldDecl.binding.id);
195 codeStream.getstatic(fieldDecl.binding);
196 codeStream.aastore();
199 codeStream.putstatic(declaringType.enumValuesSyntheticfield);
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);
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);
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);
231 if (this.needFreeReturn) {
232 int oldPosition = codeStream.position;
233 codeStream.return_();
234 codeStream.updateLocalVariablesAttribute(oldPosition);
236 // Record the end of the clinit: point to the declaration of the class
237 codeStream.recordPositionsFrom(0, declaringType.sourceStart);
238 classFile.completeCodeAttributeForClinit(codeAttributeOffset);
242 public boolean isClinit() {
247 public boolean isInitializationMethod() {
252 public boolean isStatic() {
257 public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
258 //the clinit is filled by hand ....
261 public StringBuffer print(int tab, StringBuffer output) {
263 printIndent(tab, output).append("<clinit>()"); //$NON-NLS-1$
264 printBody(tab + 1, output);
268 public void resolve(ClassScope classScope) {
270 this.scope = new MethodScope(classScope, classScope.referenceContext, true);
273 public void traverse(
275 ClassScope classScope) {
277 visitor.visit(this, classScope);
278 visitor.endVisit(this, classScope);
281 public void setAssertionSupport(FieldBinding assertionSyntheticFieldBinding, boolean needClassLiteralField) {
283 this.assertionSyntheticFieldBinding = assertionSyntheticFieldBinding;
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);