added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / Initializer.java
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
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.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.*;
18
19 public class Initializer extends FieldDeclaration {
20         
21         public Block block;
22         public int lastVisibleFieldID;
23         public int bodyStart;
24         public int bodyEnd;
25         
26         public boolean errorInSignature = false; 
27         
28         public Initializer(Block block, int modifiers) {
29                 this.block = block;
30                 this.modifiers = modifiers;
31
32                 declarationSourceStart = sourceStart = block.sourceStart;
33         }
34
35         public FlowInfo analyseCode(
36                 MethodScope currentScope,
37                 FlowContext flowContext,
38                 FlowInfo flowInfo) {
39
40                 return block.analyseCode(currentScope, flowContext, flowInfo);
41         }
42
43         /**
44          * Code generation for a non-static initializer: 
45          *    standard block code gen
46          *
47          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
48          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
49          */
50         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
51
52                 if ((bits & IsReachableMASK) == 0) {
53                         return;
54                 }
55                 int pc = codeStream.position;
56                 block.generateCode(currentScope, codeStream);
57                 codeStream.recordPositionsFrom(pc, this.sourceStart);
58         }
59
60         /**
61          * @see org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration#getKind()
62          */
63         public int getKind() {
64                 return INITIALIZER;
65         }
66         
67         public boolean isStatic() {
68
69                 return (modifiers & AccStatic) != 0;
70         }
71         
72         public void parseStatements(
73                 Parser parser,
74                 TypeDeclaration typeDeclaration,
75                 CompilationUnitDeclaration unit) {
76
77                 //fill up the method body with statement
78                 parser.parse(this, typeDeclaration, unit);
79         }
80
81         public StringBuffer printStatement(int indent, StringBuffer output) {
82
83                 if (modifiers != 0) {
84                         printIndent(indent, output);
85                         printModifiers(modifiers, output);
86                         if (this.annotations != null) printAnnotations(this.annotations, output);
87                         output.append("{\n"); //$NON-NLS-1$
88                         block.printBody(indent, output);
89                         printIndent(indent, output).append('}'); 
90                         return output;
91                 } else {
92                         return block.printStatement(indent, output);
93                 }
94         }
95         
96         public void resolve(MethodScope scope) {
97
98             FieldBinding previousField = scope.initializedField;
99                 int previousFieldID = scope.lastVisibleFieldID;
100                 try {
101                     scope.initializedField = null;
102                         scope.lastVisibleFieldID = lastVisibleFieldID;
103                         if (isStatic()) {
104                                 ReferenceBinding declaringType = scope.enclosingSourceType();
105                                 if (declaringType.isNestedType() && !declaringType.isStatic())
106                                         scope.problemReporter().innerTypesCannotDeclareStaticInitializers(
107                                                 declaringType,
108                                                 this);
109                         }
110                         block.resolve(scope);
111                 } finally {
112                     scope.initializedField = previousField;
113                         scope.lastVisibleFieldID = previousFieldID;
114                 }
115         }
116
117         public void traverse(ASTVisitor visitor, MethodScope scope) {
118
119                 if (visitor.visit(this, scope)) {
120                         block.traverse(visitor, scope);
121                 }
122                 visitor.endVisit(this, scope);
123         }
124 }