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.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17 import org.eclipse.jdt.internal.compiler.parser.*;
19 public class Initializer extends FieldDeclaration {
22 public int lastVisibleFieldID;
26 public boolean errorInSignature = false;
28 public Initializer(Block block, int modifiers) {
30 this.modifiers = modifiers;
32 declarationSourceStart = sourceStart = block.sourceStart;
35 public FlowInfo analyseCode(
36 MethodScope currentScope,
37 FlowContext flowContext,
40 return block.analyseCode(currentScope, flowContext, flowInfo);
44 * Code generation for a non-static initializer:
45 * standard block code gen
47 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
48 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
50 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
52 if ((bits & IsReachableMASK) == 0) {
55 int pc = codeStream.position;
56 block.generateCode(currentScope, codeStream);
57 codeStream.recordPositionsFrom(pc, this.sourceStart);
60 public boolean isField() {
65 public boolean isStatic() {
67 return (modifiers & AccStatic) != 0;
70 public void parseStatements(
72 TypeDeclaration typeDeclaration,
73 CompilationUnitDeclaration unit) {
75 //fill up the method body with statement
76 parser.parse(this, typeDeclaration, unit);
79 public StringBuffer printStatement(int indent, StringBuffer output) {
82 printIndent(indent, output);
83 printModifiers(modifiers, output).append("{\n"); //$NON-NLS-1$
84 block.printBody(indent, output);
85 printIndent(indent, output).append('}');
88 return block.printStatement(indent, output);
92 public void resolve(MethodScope scope) {
94 FieldBinding previousField = scope.initializedField;
95 int previousFieldID = scope.lastVisibleFieldID;
97 scope.initializedField = null;
98 scope.lastVisibleFieldID = lastVisibleFieldID;
100 ReferenceBinding declaringType = scope.enclosingSourceType();
101 if (declaringType.isNestedType() && !declaringType.isStatic())
102 scope.problemReporter().innerTypesCannotDeclareStaticInitializers(
106 block.resolve(scope);
108 scope.initializedField = previousField;
109 scope.lastVisibleFieldID = previousFieldID;
113 public void traverse(ASTVisitor visitor, MethodScope scope) {
115 if (visitor.visit(this, scope)) {
116 block.traverse(visitor, scope);
118 visitor.endVisit(this, scope);