001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.ast;
011:
012: import org.eclipse.jdt.internal.compiler.ASTVisitor;
013: import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
014: import org.eclipse.jdt.internal.compiler.codegen.*;
015: import org.eclipse.jdt.internal.compiler.flow.*;
016: import org.eclipse.jdt.internal.compiler.lookup.*;
017: import org.eclipse.jdt.internal.compiler.parser.*;
018:
019: public class Initializer extends FieldDeclaration {
020:
021: public Block block;
022: public int lastVisibleFieldID;
023: public int bodyStart;
024: public int bodyEnd;
025:
026: public Initializer(Block block, int modifiers) {
027: this .block = block;
028: this .modifiers = modifiers;
029:
030: if (block != null) {
031: declarationSourceStart = sourceStart = block.sourceStart;
032: }
033: }
034:
035: public FlowInfo analyseCode(MethodScope currentScope,
036: FlowContext flowContext, FlowInfo flowInfo) {
037:
038: if (block != null) {
039: return block.analyseCode(currentScope, flowContext,
040: flowInfo);
041: }
042: return flowInfo;
043: }
044:
045: /**
046: * Code generation for a non-static initializer:
047: * standard block code gen
048: *
049: * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
050: * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
051: */
052: public void generateCode(BlockScope currentScope,
053: CodeStream codeStream) {
054:
055: if ((bits & IsReachable) == 0) {
056: return;
057: }
058: int pc = codeStream.position;
059: if (block != null)
060: block.generateCode(currentScope, codeStream);
061: codeStream.recordPositionsFrom(pc, this .sourceStart);
062: }
063:
064: /**
065: * @see org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration#getKind()
066: */
067: public int getKind() {
068: return INITIALIZER;
069: }
070:
071: public boolean isStatic() {
072:
073: return (this .modifiers & ClassFileConstants.AccStatic) != 0;
074: }
075:
076: public void parseStatements(Parser parser,
077: TypeDeclaration typeDeclaration,
078: CompilationUnitDeclaration unit) {
079:
080: //fill up the method body with statement
081: parser.parse(this , typeDeclaration, unit);
082: }
083:
084: public StringBuffer printStatement(int indent, StringBuffer output) {
085:
086: if (modifiers != 0) {
087: printIndent(indent, output);
088: printModifiers(modifiers, output);
089: if (this .annotations != null)
090: printAnnotations(this .annotations, output);
091: output.append("{\n"); //$NON-NLS-1$
092: if (block != null) {
093: block.printBody(indent, output);
094: }
095: printIndent(indent, output).append('}');
096: return output;
097: } else if (block != null) {
098: block.printStatement(indent, output);
099: } else {
100: printIndent(indent, output).append("{}"); //$NON-NLS-1$
101: }
102: return output;
103: }
104:
105: public void resolve(MethodScope scope) {
106:
107: FieldBinding previousField = scope.initializedField;
108: int previousFieldID = scope.lastVisibleFieldID;
109: try {
110: scope.initializedField = null;
111: scope.lastVisibleFieldID = lastVisibleFieldID;
112: if (isStatic()) {
113: ReferenceBinding declaringType = scope
114: .enclosingSourceType();
115: if (declaringType.isNestedType()
116: && !declaringType.isStatic())
117: scope.problemReporter()
118: .innerTypesCannotDeclareStaticInitializers(
119: declaringType, this );
120: }
121: if (block != null)
122: block.resolve(scope);
123: } finally {
124: scope.initializedField = previousField;
125: scope.lastVisibleFieldID = previousFieldID;
126: }
127: }
128:
129: public void traverse(ASTVisitor visitor, MethodScope scope) {
130: if (visitor.visit(this, scope)) {
131: if (block != null)
132: block.traverse(visitor, scope);
133: }
134: visitor.endVisit(this, scope);
135: }
136: }
|