1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.lookup;
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.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 ArrayLength = 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;
31 * Answer the receiver's constant pool name.
32 * NOTE: This method should only be used during/after code gen.
33 * e.g. '[Ljava/lang/Object;'
36 public char[] constantPoolName() {
37 if (constantPoolName != null)
38 return constantPoolName;
40 char[] brackets = new char[dimensions];
41 for (int i = dimensions - 1; i >= 0; i--) brackets[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) {
60 if (dimensions == 1) return leafComponentType;
61 return scope.createArray(leafComponentType, dimensions - 1);
63 public PackageBinding getPackage() {
64 return leafComponentType.getPackage();
66 /* Answer true if the receiver type can be assigned to the argument type (right)
69 public boolean isCompatibleWith(TypeBinding right) {
73 if (right.isArrayType()) {
74 ArrayBinding rightArray = (ArrayBinding) right;
75 if (rightArray.leafComponentType.isBaseType())
76 return false; // relying on the fact that all equal arrays are identical
77 if (dimensions == rightArray.dimensions)
78 return leafComponentType.isCompatibleWith(rightArray.leafComponentType);
79 if (dimensions < rightArray.dimensions)
80 return false; // cannot assign 'String[]' into 'Object[][]' but can assign 'byte[][]' into 'Object[]'
82 if (right.isBaseType())
85 //Check dimensions - Java does not support explicitly sized dimensions for types.
86 //However, if it did, the type checking support would go here.
87 switch (right.leafComponentType().id) {
88 case T_JavaLangObject :
89 case T_JavaLangCloneable :
90 case T_JavaIoSerializable :
96 public TypeBinding leafComponentType(){
97 return leafComponentType;
101 * Answer the problem id associated with the receiver.
102 * NoError if the receiver is a valid binding.
105 public int problemId() {
106 return leafComponentType.problemId();
109 * Answer the source name for the type.
110 * In the case of member types, as the qualified name from its top level type.
111 * For example, for a member type N defined inside M & A: "A.M.N".
114 public char[] qualifiedSourceName() {
115 char[] brackets = new char[dimensions * 2];
116 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
118 brackets[i - 1] = '[';
120 return CharOperation.concat(leafComponentType.qualifiedSourceName(), brackets);
122 public char[] readableName() /* java.lang.Object[] */ {
123 char[] brackets = new char[dimensions * 2];
124 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
126 brackets[i - 1] = '[';
128 return CharOperation.concat(leafComponentType.readableName(), brackets);
130 public char[] shortReadableName(){
131 char[] brackets = new char[dimensions * 2];
132 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
134 brackets[i - 1] = '[';
136 return CharOperation.concat(leafComponentType.shortReadableName(), brackets);
138 public char[] sourceName() {
139 char[] brackets = new char[dimensions * 2];
140 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
142 brackets[i - 1] = '[';
144 return CharOperation.concat(leafComponentType.sourceName(), brackets);
146 public String toString() {
147 return leafComponentType != null ? debugName() : "NULL TYPE ARRAY"; //$NON-NLS-1$