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.flow.*;
17 import org.eclipse.jdt.internal.compiler.lookup.*;
18 import org.eclipse.jdt.internal.compiler.parser.*;
19 import org.eclipse.jdt.internal.compiler.problem.*;
21 public class Clinit extends AbstractMethodDeclaration {
23 public final static char[] ConstantPoolName = "<clinit>".toCharArray(); //$NON-NLS-1$
25 private FieldBinding assertionSyntheticFieldBinding = null;
26 private FieldBinding classLiteralSyntheticField = null;
28 public Clinit(CompilationResult compilationResult) {
29 super(compilationResult);
31 selector = ConstantPoolName;
34 public void analyseCode(
35 ClassScope classScope,
36 InitializationFlowContext staticInitializerFlowContext,
39 if (ignoreFurtherInvestigation)
42 ExceptionHandlingFlowContext clinitContext =
43 new ExceptionHandlingFlowContext(
44 staticInitializerFlowContext.parent,
50 // check for missing returning path
51 this.needFreeReturn = flowInfo.isReachable();
53 // check missing blank final field initializations
54 flowInfo = flowInfo.mergedWith(staticInitializerFlowContext.initsOnReturn);
55 FieldBinding[] fields = scope.enclosingSourceType().fields();
56 for (int i = 0, count = fields.length; i < count; i++) {
58 if ((field = fields[i]).isStatic()
60 && (!flowInfo.isDefinitelyAssigned(fields[i]))) {
61 scope.problemReporter().uninitializedBlankFinalField(
63 scope.referenceType().declarationOf(field));
64 // can complain against the field decl, since only one <clinit>
67 // check static initializers thrown exceptions
68 staticInitializerFlowContext.checkInitializerExceptions(
72 } catch (AbortMethod e) {
73 this.ignoreFurtherInvestigation = true;
78 * Bytecode generation for a <clinit> method
80 * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
81 * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
83 public void generateCode(ClassScope classScope, ClassFile classFile) {
86 if (ignoreFurtherInvestigation) {
87 // should never have to add any <clinit> problem method
91 clinitOffset = classFile.contentsOffset;
92 this.generateCode(classScope, classFile, clinitOffset);
93 } catch (AbortMethod e) {
95 // the clinit referenceContext is the type declaration
96 // All clinit problems will be reported against the type: AbortType instead of AbortMethod
97 // reset the contentsOffset to the value before generating the clinit code
98 // decrement the number of method info as well.
99 // This is done in the addProblemMethod and addProblemConstructor for other
101 if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
102 // a branch target required a goto_w, restart code gen in wide mode.
104 classFile.contentsOffset = clinitOffset;
105 classFile.methodCount--;
106 classFile.codeStream.wideMode = true; // request wide mode
107 this.generateCode(classScope, classFile, clinitOffset);
108 // restart method generation
109 } catch (AbortMethod e2) {
110 classFile.contentsOffset = clinitOffset;
111 classFile.methodCount--;
114 // produce a problem method accounting for this fatal error
115 classFile.contentsOffset = clinitOffset;
116 classFile.methodCount--;
122 * Bytecode generation for a <clinit> method
124 * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
125 * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
127 private void generateCode(
128 ClassScope classScope,
132 ConstantPool constantPool = classFile.constantPool;
133 int constantPoolOffset = constantPool.currentOffset;
134 int constantPoolIndex = constantPool.currentIndex;
135 classFile.generateMethodInfoHeaderForClinit();
136 int codeAttributeOffset = classFile.contentsOffset;
137 classFile.generateCodeAttributeHeader();
138 CodeStream codeStream = classFile.codeStream;
139 this.resolve(classScope);
141 codeStream.reset(this, classFile);
142 TypeDeclaration declaringType = classScope.referenceContext;
144 // initialize local positions - including initializer scope.
145 MethodScope staticInitializerScope = declaringType.staticInitializerScope;
146 staticInitializerScope.computeLocalVariablePositions(0, codeStream);
149 // This has to be done before any other initialization
150 if (this.assertionSyntheticFieldBinding != null) {
151 // generate code related to the activation of assertion for this class
152 codeStream.generateClassLiteralAccessForType(
153 classScope.enclosingSourceType(),
154 classLiteralSyntheticField);
155 codeStream.invokeJavaLangClassDesiredAssertionStatus();
156 Label falseLabel = new Label(codeStream);
157 codeStream.ifne(falseLabel);
158 codeStream.iconst_1();
159 Label jumpLabel = new Label(codeStream);
160 codeStream.goto_(jumpLabel);
162 codeStream.iconst_0();
164 codeStream.putstatic(this.assertionSyntheticFieldBinding);
166 // generate initializers
167 if (declaringType.fields != null) {
168 for (int i = 0, max = declaringType.fields.length; i < max; i++) {
169 FieldDeclaration fieldDecl;
170 if ((fieldDecl = declaringType.fields[i]).isStatic()) {
171 fieldDecl.generateCode(staticInitializerScope, codeStream);
175 if (codeStream.position == 0) {
176 // do not need to output a Clinit if no bytecodes
177 // so we reset the offset inside the byte array contents.
178 classFile.contentsOffset = clinitOffset;
179 // like we don't addd a method we need to undo the increment on the method count
180 classFile.methodCount--;
181 // reset the constant pool to its state before the clinit
182 constantPool.resetForClinit(constantPoolIndex, constantPoolOffset);
184 if (this.needFreeReturn) {
185 int oldPosition = codeStream.position;
186 codeStream.return_();
187 codeStream.updateLocalVariablesAttribute(oldPosition);
189 // Record the end of the clinit: point to the declaration of the class
190 codeStream.recordPositionsFrom(0, declaringType.sourceStart);
191 classFile.completeCodeAttributeForClinit(codeAttributeOffset);
195 public boolean isClinit() {
200 public boolean isInitializationMethod() {
205 public boolean isStatic() {
210 public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
211 //the clinit is filled by hand ....
214 public StringBuffer print(int tab, StringBuffer output) {
216 printIndent(tab, output).append("<clinit>()"); //$NON-NLS-1$
217 printBody(tab + 1, output);
221 public void resolve(ClassScope classScope) {
223 this.scope = new MethodScope(classScope, classScope.referenceContext, true);
226 public void traverse(
228 ClassScope classScope) {
230 visitor.visit(this, classScope);
231 visitor.endVisit(this, classScope);
235 public void setAssertionSupport(FieldBinding assertionSyntheticFieldBinding) {
237 this.assertionSyntheticFieldBinding = assertionSyntheticFieldBinding;
239 // we need to add the field right now, because the field infos are generated before the methods
240 SourceTypeBinding sourceType =
241 this.scope.outerMostMethodScope().enclosingSourceType();
242 this.classLiteralSyntheticField =
243 sourceType.addSyntheticField(sourceType, scope);