KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > Initializer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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.classfmt.ClassFileConstants;
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
20 public class Initializer extends FieldDeclaration {
21
22     public Block block;
23     public int lastVisibleFieldID;
24     public int bodyStart;
25     public int bodyEnd;
26
27     public Initializer(Block block, int modifiers) {
28         this.block = block;
29         this.modifiers = modifiers;
30
31         declarationSourceStart = sourceStart = block.sourceStart;
32     }
33
34     public FlowInfo analyseCode(
35         MethodScope currentScope,
36         FlowContext flowContext,
37         FlowInfo flowInfo) {
38
39         return block.analyseCode(currentScope, flowContext, flowInfo);
40     }
41
42     /**
43      * Code generation for a non-static initializer:
44      * standard block code gen
45      *
46      * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
47      * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
48      */

49     public void generateCode(BlockScope currentScope, CodeStream codeStream) {
50
51         if ((bits & IsReachable) == 0) {
52             return;
53         }
54         int pc = codeStream.position;
55         block.generateCode(currentScope, codeStream);
56         codeStream.recordPositionsFrom(pc, this.sourceStart);
57     }
58
59     /**
60      * @see org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration#getKind()
61      */

62     public int getKind() {
63         return INITIALIZER;
64     }
65     
66     public boolean isStatic() {
67
68         return (this.modifiers & ClassFileConstants.AccStatic) != 0;
69     }
70     
71     public void parseStatements(
72         Parser parser,
73         TypeDeclaration typeDeclaration,
74         CompilationUnitDeclaration unit) {
75
76         //fill up the method body with statement
77
parser.parse(this, typeDeclaration, unit);
78     }
79
80     public StringBuffer JavaDoc printStatement(int indent, StringBuffer JavaDoc output) {
81
82         if (modifiers != 0) {
83             printIndent(indent, output);
84             printModifiers(modifiers, output);
85             if (this.annotations != null) printAnnotations(this.annotations, output);
86             output.append("{\n"); //$NON-NLS-1$
87
block.printBody(indent, output);
88             printIndent(indent, output).append('}');
89             return output;
90         } else {
91             return block.printStatement(indent, output);
92         }
93     }
94     
95     public void resolve(MethodScope scope) {
96
97         FieldBinding previousField = scope.initializedField;
98         int previousFieldID = scope.lastVisibleFieldID;
99         try {
100             scope.initializedField = null;
101             scope.lastVisibleFieldID = lastVisibleFieldID;
102             if (isStatic()) {
103                 ReferenceBinding declaringType = scope.enclosingSourceType();
104                 if (declaringType.isNestedType() && !declaringType.isStatic())
105                     scope.problemReporter().innerTypesCannotDeclareStaticInitializers(
106                         declaringType,
107                         this);
108             }
109             block.resolve(scope);
110         } finally {
111             scope.initializedField = previousField;
112             scope.lastVisibleFieldID = previousFieldID;
113         }
114     }
115
116     public void traverse(ASTVisitor visitor, MethodScope scope) {
117
118         if (visitor.visit(this, scope)) {
119             block.traverse(visitor, scope);
120         }
121         visitor.endVisit(this, scope);
122     }
123 }
124
Popular Tags