1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
15 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
16 import net.sourceforge.phpdt.internal.compiler.lookup.LocalTypeBinding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
18 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
20 import net.sourceforge.phpdt.internal.compiler.problem.AbortType;
22 public class AnonymousLocalTypeDeclaration extends LocalTypeDeclaration {
24 public static final char[] ANONYMOUS_EMPTY_NAME = new char[] {};
26 public QualifiedAllocationExpression allocation;
28 public AnonymousLocalTypeDeclaration(CompilationResult compilationResult) {
29 super(compilationResult);
30 modifiers = AccDefault;
31 name = ANONYMOUS_EMPTY_NAME;
34 // use a default name in order to th name lookup
35 // to operate just like a regular type (which has a name)
36 // without checking systematically if the naem is null ....
37 public MethodBinding createsInternalConstructorWithBinding(
38 MethodBinding inheritedConstructorBinding) {
40 // Add to method'set, the default constuctor that just recall the
41 // super constructor with the same arguments
42 String baseName = "$anonymous"; //$NON-NLS-1$
43 TypeBinding[] argumentTypes = inheritedConstructorBinding.parameters;
44 int argumentsLength = argumentTypes.length;
46 ConstructorDeclaration cd = new ConstructorDeclaration(
47 this.compilationResult);
48 cd.selector = new char[] { 'x' }; // no maining
49 cd.sourceStart = sourceStart;
50 cd.sourceEnd = sourceEnd;
51 cd.modifiers = modifiers & AccVisibilityMASK;
52 cd.isDefaultConstructor = true;
54 if (argumentsLength > 0) {
55 Argument[] arguments = (cd.arguments = new Argument[argumentsLength]);
56 for (int i = argumentsLength; --i >= 0;) {
57 arguments[i] = new Argument((baseName + i).toCharArray(), 0L,
58 null /* type ref */, AccDefault);
62 // the super call inside the constructor
63 cd.constructorCall = SuperReference.implicitSuperConstructorCall();
64 cd.constructorCall.sourceStart = sourceStart;
65 cd.constructorCall.sourceEnd = sourceEnd;
67 if (argumentsLength > 0) {
69 args = cd.constructorCall.arguments = new Expression[argumentsLength];
70 for (int i = argumentsLength; --i >= 0;) {
71 args[i] = new SingleNameReference((baseName + i).toCharArray(),
76 // adding the constructor in the methods list
77 if (methods == null) {
78 methods = new AbstractMethodDeclaration[] { cd };
80 AbstractMethodDeclaration[] newMethods;
85 newMethods = new AbstractMethodDeclaration[methods.length + 1],
91 // ============BINDING UPDATE==========================
92 cd.binding = new MethodBinding(cd.modifiers, // methodDeclaration
93 argumentsLength == 0 ? NoParameters : argumentTypes, // arguments
95 inheritedConstructorBinding.thrownExceptions, // exceptions
96 binding); // declaringClass
98 cd.scope = new MethodScope(scope, cd, true);
100 cd.constructorCall.resolve(cd.scope);
102 if (binding.methods == null) {
103 binding.methods = new MethodBinding[] { cd.binding };
105 MethodBinding[] newMethods;
106 System.arraycopy(binding.methods, 0,
107 newMethods = new MethodBinding[binding.methods.length + 1],
108 1, binding.methods.length);
109 newMethods[0] = cd.binding;
110 binding.methods = newMethods;
112 // ===================================================
118 public void resolve(BlockScope scope) {
120 if (binding != null) {
121 // remember local types binding for innerclass emulation propagation
122 scope.referenceCompilationUnit().record((LocalTypeBinding) binding);
124 // scope and binding are provided in updateBindingSuperclass
126 updateMaxFieldCount();
129 public String toString(int tab) {
131 return toStringBody(tab);
135 * Iteration for a local anonymous innertype
138 public void traverse(ASTVisitor visitor, BlockScope blockScope) {
140 if (ignoreFurtherInvestigation)
143 if (visitor.visit(this, blockScope)) {
147 int memberTypesLength;
149 // <superclass> is bound to the actual type from the allocation
151 // therefore it has already been iterated at this point.
153 if (memberTypes != null) {
154 memberTypesLength = memberTypes.length;
155 for (int i = 0; i < memberTypesLength; i++)
156 memberTypes[i].traverse(visitor, scope);
158 if (fields != null) {
159 fieldsLength = fields.length;
160 for (int i = 0; i < fieldsLength; i++) {
161 FieldDeclaration field;
162 if ((field = fields[i]).isStatic()) {
163 // local type cannot have static fields
165 field.traverse(visitor, initializerScope);
169 if (methods != null) {
170 methodsLength = methods.length;
171 for (int i = 0; i < methodsLength; i++)
172 methods[i].traverse(visitor, scope);
175 visitor.endVisit(this, blockScope);
176 } catch (AbortType e) {