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.phpeclipse.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.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[] {};
25 public QualifiedAllocationExpression allocation;
27 public AnonymousLocalTypeDeclaration(CompilationResult compilationResult) {
28 super(compilationResult);
29 modifiers = AccDefault;
30 name = ANONYMOUS_EMPTY_NAME;
33 // use a default name in order to th name lookup
34 // to operate just like a regular type (which has a name)
35 //without checking systematically if the naem is null ....
36 public MethodBinding createsInternalConstructorWithBinding(MethodBinding inheritedConstructorBinding) {
38 //Add to method'set, the default constuctor that just recall the
39 //super constructor with the same arguments
40 String baseName = "$anonymous"; //$NON-NLS-1$
41 TypeBinding[] argumentTypes = inheritedConstructorBinding.parameters;
42 int argumentsLength = argumentTypes.length;
44 ConstructorDeclaration cd = new ConstructorDeclaration(this.compilationResult);
45 cd.selector = new char[] { 'x' }; //no maining
46 cd.sourceStart = sourceStart;
47 cd.sourceEnd = sourceEnd;
48 cd.modifiers = modifiers & AccVisibilityMASK;
49 cd.isDefaultConstructor = true;
51 if (argumentsLength > 0) {
52 Argument[] arguments = (cd.arguments = new Argument[argumentsLength]);
53 for (int i = argumentsLength; --i >= 0;) {
54 arguments[i] = new Argument((baseName + i).toCharArray(), 0L, null /*type ref*/, AccDefault);
58 //the super call inside the constructor
59 cd.constructorCall = SuperReference.implicitSuperConstructorCall();
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, cd, 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 if (binding != null) {
118 // remember local types binding for innerclass emulation propagation
119 scope.referenceCompilationUnit().record((LocalTypeBinding)binding);
121 // scope and binding are provided in updateBindingSuperclass
123 updateMaxFieldCount();
126 public String toString(int tab) {
128 return toStringBody(tab);
132 * Iteration for a local anonymous innertype
135 public void traverse(
136 IAbstractSyntaxTreeVisitor visitor,
137 BlockScope blockScope) {
139 if (ignoreFurtherInvestigation)
142 if (visitor.visit(this, blockScope)) {
146 int memberTypesLength;
148 // <superclass> is bound to the actual type from the allocation expression
149 // therefore it has already been iterated at this point.
151 if (memberTypes != null) {
152 memberTypesLength = memberTypes.length;
153 for (int i = 0; i < memberTypesLength; i++)
154 memberTypes[i].traverse(visitor, scope);
156 if (fields != null) {
157 fieldsLength = fields.length;
158 for (int i = 0; i < fieldsLength; i++) {
159 FieldDeclaration field;
160 if ((field = fields[i]).isStatic()) {
161 // local type cannot have static fields
163 field.traverse(visitor, initializerScope);
167 if (methods != null) {
168 methodsLength = methods.length;
169 for (int i = 0; i < methodsLength; i++)
170 methods[i].traverse(visitor, scope);
173 visitor.endVisit(this, blockScope);
174 } catch (AbortType e) {