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.lookup;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
16 public final class ArrayBinding extends TypeBinding {
17 // creation and initialization of the length field
18 // the declaringClass of this field is intentionally set to null so it can be distinguished.
19 public static final FieldBinding LengthField = new FieldBinding(LENGTH, IntBinding, AccPublic | AccFinal, null, Constant.NotAConstant);
21 public TypeBinding leafComponentType;
22 public int dimensions;
24 char[] constantPoolName;
25 public ArrayBinding(TypeBinding type, int dimensions) {
26 this.tagBits |= IsArrayType;
27 this.leafComponentType = type;
28 this.dimensions = dimensions;
30 /* Answer the receiver's constant pool name.
32 * NOTE: This method should only be used during/after code gen.
35 public char[] constantPoolName() /* [Ljava/lang/Object; */ {
36 if (constantPoolName != null)
37 return constantPoolName;
39 char[] brackets = new char[dimensions];
40 for (int i = dimensions - 1; i >= 0; i--)
42 return constantPoolName = CharOperation.concat(brackets, leafComponentType.signature());
45 StringBuffer brackets = new StringBuffer(dimensions * 2);
46 for (int i = dimensions; --i >= 0;)
47 brackets.append("[]"); //$NON-NLS-1$
48 return leafComponentType.debugName() + brackets.toString();
50 public int dimensions() {
51 return this.dimensions;
54 /* Answer an array whose dimension size is one less than the receiver.
56 * When the receiver's dimension size is one then answer the leaf component type.
59 public TypeBinding elementsType(Scope scope) {
61 return leafComponentType;
63 return scope.createArray(leafComponentType, dimensions - 1);
65 public PackageBinding getPackage() {
66 return leafComponentType.getPackage();
68 /* Answer true if the receiver type can be assigned to the argument type (right)
71 public boolean isCompatibleWith(TypeBinding right) {
76 if (right.isArrayType()) {
77 ArrayBinding rightArray = (ArrayBinding) right;
78 if (rightArray.leafComponentType.isBaseType())
79 return false; // relying on the fact that all equal arrays are identical
80 if (dimensions == rightArray.dimensions)
81 return leafComponentType.isCompatibleWith(rightArray.leafComponentType);
82 if (dimensions < rightArray.dimensions)
83 return false; // cannot assign 'String[]' into 'Object[][]' but can assign 'byte[][]' into 'Object[]'
84 rightName = ((ReferenceBinding) rightArray.leafComponentType).compoundName;
86 if (right.isBaseType())
88 rightName = ((ReferenceBinding) right).compoundName;
90 //Check dimensions - Java does not support explicitly sized dimensions for types.
91 //However, if it did, the type checking support would go here.
93 if (CharOperation.equals(rightName, JAVA_LANG_OBJECT))
95 if (CharOperation.equals(rightName, JAVA_LANG_CLONEABLE))
97 if (CharOperation.equals(rightName, JAVA_IO_SERIALIZABLE))
102 public TypeBinding leafComponentType(){
103 return leafComponentType;
107 * Answer the problem id associated with the receiver.
108 * NoError if the receiver is a valid binding.
111 public int problemId() {
112 return leafComponentType.problemId();
115 * Answer the source name for the type.
116 * In the case of member types, as the qualified name from its top level type.
117 * For example, for a member type N defined inside M & A: "A.M.N".
120 public char[] qualifiedSourceName() {
121 char[] brackets = new char[dimensions * 2];
122 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
124 brackets[i - 1] = '[';
126 return CharOperation.concat(leafComponentType.qualifiedSourceName(), brackets);
128 public char[] readableName() /* java.lang.Object[] */ {
129 char[] brackets = new char[dimensions * 2];
130 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
132 brackets[i - 1] = '[';
134 return CharOperation.concat(leafComponentType.readableName(), brackets);
136 public char[] shortReadableName(){
137 char[] brackets = new char[dimensions * 2];
138 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
140 brackets[i - 1] = '[';
142 return CharOperation.concat(leafComponentType.shortReadableName(), brackets);
144 public char[] sourceName() {
145 char[] brackets = new char[dimensions * 2];
146 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
148 brackets[i - 1] = '[';
150 return CharOperation.concat(leafComponentType.sourceName(), brackets);
152 public String toString() {
153 return leafComponentType != null ? debugName() : "NULL TYPE ARRAY"; //$NON-NLS-1$