1 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 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 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 parser.parse(this, typeDeclaration, unit); 78 } 79 80 public StringBuffer printStatement(int indent, StringBuffer 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"); 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 |