1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
14 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
15 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
16 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
19 import net.sourceforge.phpdt.internal.compiler.problem.AbortType;
21 public class AnonymousLocalTypeDeclaration extends LocalTypeDeclaration {
23 public static final char[] ANONYMOUS_EMPTY_NAME = new char[] {};
24 public QualifiedAllocationExpression allocation;
26 public AnonymousLocalTypeDeclaration(CompilationResult compilationResult) {
27 super(compilationResult);
28 modifiers = AccDefault;
29 name = ANONYMOUS_EMPTY_NAME;
32 // use a default name in order to th name lookup
33 // to operate juat like a regular type (which has a name)
34 //without checking systematically if the naem is null ....
35 public MethodBinding createsInternalConstructorWithBinding(MethodBinding inheritedConstructorBinding) {
37 //Add to method'set, the default constuctor that just recall the
38 //super constructor with the same arguments
39 String baseName = "$anonymous"; //$NON-NLS-1$
40 TypeBinding[] argumentTypes = inheritedConstructorBinding.parameters;
41 int argumentsLength = argumentTypes.length;
43 ConstructorDeclaration cd = new ConstructorDeclaration(this.compilationResult);
44 cd.selector = new char[] { 'x' }; //no maining
45 cd.sourceStart = sourceStart;
46 cd.sourceEnd = sourceEnd;
47 cd.modifiers = modifiers & AccVisibilityMASK;
48 cd.isDefaultConstructor = true;
50 if (argumentsLength > 0) {
51 Argument[] arguments = (cd.arguments = new Argument[argumentsLength]);
52 for (int i = argumentsLength; --i >= 0;) {
53 arguments[i] = new Argument((baseName + i).toCharArray(), 0L, null /*type ref*/, AccDefault);
57 //the super call inside the constructor
59 new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
60 cd.constructorCall.sourceStart = sourceStart;
61 cd.constructorCall.sourceEnd = sourceEnd;
63 if (argumentsLength > 0) {
65 args = cd.constructorCall.arguments = new Expression[argumentsLength];
66 for (int i = argumentsLength; --i >= 0;) {
67 args[i] = new SingleNameReference((baseName + i).toCharArray(), 0L);
71 //adding the constructor in the methods list
72 if (methods == null) {
73 methods = new AbstractMethodDeclaration[] { cd };
75 AbstractMethodDeclaration[] newMethods;
79 newMethods = new AbstractMethodDeclaration[methods.length + 1],
86 //============BINDING UPDATE==========================
87 cd.binding = new MethodBinding(
88 cd.modifiers, //methodDeclaration
89 argumentsLength == 0 ? NoParameters : argumentTypes, //arguments bindings
90 inheritedConstructorBinding.thrownExceptions, //exceptions
91 binding); //declaringClass
93 cd.scope = new MethodScope(scope, this, true);
95 cd.constructorCall.resolve(cd.scope);
97 if (binding.methods == null) {
98 binding.methods = new MethodBinding[] { cd.binding };
100 MethodBinding[] newMethods;
104 newMethods = new MethodBinding[binding.methods.length + 1],
106 binding.methods.length);
107 newMethods[0] = cd.binding;
108 binding.methods = newMethods;
110 //===================================================
115 public void resolve(BlockScope scope) {
117 // scope and binding are provided in updateBindingSuperclass
119 updateMaxFieldCount();
122 public String toString(int tab) {
124 return toStringBody(tab);
128 * Iteration for a local anonymous innertype
131 public void traverse(
132 IAbstractSyntaxTreeVisitor visitor,
133 BlockScope blockScope) {
135 if (ignoreFurtherInvestigation)
138 if (visitor.visit(this, blockScope)) {
142 int memberTypesLength;
144 // <superclass> is bound to the actual type from the allocation expression
145 // therefore it has already been iterated at this point.
147 if (memberTypes != null) {
148 memberTypesLength = memberTypes.length;
149 for (int i = 0; i < memberTypesLength; i++)
150 memberTypes[i].traverse(visitor, scope);
152 if (fields != null) {
153 fieldsLength = fields.length;
154 for (int i = 0; i < fieldsLength; i++) {
155 FieldDeclaration field;
156 if ((field = fields[i]).isStatic()) {
157 // local type cannot have static fields
159 field.traverse(visitor, initializerScope);
163 if (methods != null) {
164 methodsLength = methods.length;
165 for (int i = 0; i < methodsLength; i++)
166 methods[i].traverse(visitor, scope);
169 visitor.endVisit(this, blockScope);
170 } catch (AbortType e) {