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.util.HashtableOfPackage;
15 import org.eclipse.jdt.internal.compiler.util.HashtableOfType;
17 public class PackageBinding extends Binding implements TypeConstants {
18 public char[][] compoundName;
19 PackageBinding parent;
20 LookupEnvironment environment;
21 HashtableOfType knownTypes;
22 HashtableOfPackage knownPackages;
23 protected PackageBinding() {
24 // for creating problem package
26 public PackageBinding(char[][] compoundName, PackageBinding parent, LookupEnvironment environment) {
27 this.compoundName = compoundName;
29 this.environment = environment;
30 this.knownTypes = null; // initialized if used... class counts can be very large 300-600
31 this.knownPackages = new HashtableOfPackage(3); // sub-package counts are typically 0-3
33 public PackageBinding(char[] topLevelPackageName, LookupEnvironment environment) {
34 this(new char[][] {topLevelPackageName}, null, environment);
36 /* Create the default package.
39 public PackageBinding(LookupEnvironment environment) {
40 this(CharOperation.NO_CHAR_CHAR, null, environment);
42 private void addNotFoundPackage(char[] simpleName) {
43 knownPackages.put(simpleName, LookupEnvironment.TheNotFoundPackage);
45 private void addNotFoundType(char[] simpleName) {
46 if (knownTypes == null)
47 knownTypes = new HashtableOfType(25);
48 knownTypes.put(simpleName, LookupEnvironment.TheNotFoundType);
50 void addPackage(PackageBinding element) {
51 knownPackages.put(element.compoundName[element.compoundName.length - 1], element);
53 void addType(ReferenceBinding element) {
54 if (knownTypes == null)
55 knownTypes = new HashtableOfType(25);
56 knownTypes.put(element.compoundName[element.compoundName.length - 1], element);
59 * Answer the receiver's binding type from Binding.BindingID.
62 public final int bindingType() {
65 private PackageBinding findPackage(char[] name) {
66 if (!environment.isPackage(this.compoundName, name))
69 char[][] subPkgCompoundName = CharOperation.arrayConcat(this.compoundName, name);
70 PackageBinding subPackageBinding = new PackageBinding(subPkgCompoundName, this, environment);
71 addPackage(subPackageBinding);
72 return subPackageBinding;
74 /* Answer the subpackage named name; ask the oracle for the package if its not in the cache.
75 * Answer null if it could not be resolved.
77 * NOTE: This should only be used when we know there is NOT a type with the same name.
80 PackageBinding getPackage(char[] name) {
81 PackageBinding binding = getPackage0(name);
82 if (binding != null) {
83 if (binding == LookupEnvironment.TheNotFoundPackage)
88 if ((binding = findPackage(name)) != null)
91 // not found so remember a problem package binding in the cache for future lookups
92 addNotFoundPackage(name);
95 /* Answer the subpackage named name if it exists in the cache.
96 * Answer theNotFoundPackage if it could not be resolved the first time
97 * it was looked up, otherwise answer null.
99 * NOTE: Senders must convert theNotFoundPackage into a real problem
100 * package if its to returned.
103 PackageBinding getPackage0(char[] name) {
104 return knownPackages.get(name);
106 /* Answer the type named name; ask the oracle for the type if its not in the cache.
107 * Answer a NotVisible problem type if the type is not visible from the invocationPackage.
108 * Answer null if it could not be resolved.
110 * NOTE: This should only be used by source types/scopes which know there is NOT a
111 * package with the same name.
114 ReferenceBinding getType(char[] name) {
115 ReferenceBinding binding = getType0(name);
116 if (binding == null) {
117 if ((binding = environment.askForType(this, name)) == null) {
118 // not found so remember a problem type binding in the cache for future lookups
119 addNotFoundType(name);
124 if (binding == LookupEnvironment.TheNotFoundType)
126 if (binding instanceof UnresolvedReferenceBinding)
127 binding = ((UnresolvedReferenceBinding) binding).resolve(environment);
128 if (binding.isNestedType())
129 return new ProblemReferenceBinding(name, InternalNameProvided);
132 /* Answer the type named name if it exists in the cache.
133 * Answer theNotFoundType if it could not be resolved the first time
134 * it was looked up, otherwise answer null.
136 * NOTE: Senders must convert theNotFoundType into a real problem
137 * reference type if its to returned.
140 ReferenceBinding getType0(char[] name) {
141 if (knownTypes == null)
143 return knownTypes.get(name);
145 /* Answer the package or type named name; ask the oracle if it is not in the cache.
146 * Answer null if it could not be resolved.
148 * When collisions exist between a type name & a package name, answer the type.
149 * Treat the package as if it does not exist... a problem was already reported when the type was defined.
151 * NOTE: no visibility checks are performed.
152 * THIS SHOULD ONLY BE USED BY SOURCE TYPES/SCOPES.
155 public Binding getTypeOrPackage(char[] name) {
156 ReferenceBinding typeBinding = getType0(name);
157 if (typeBinding != null && typeBinding != LookupEnvironment.TheNotFoundType) {
158 if (typeBinding instanceof UnresolvedReferenceBinding)
159 typeBinding = ((UnresolvedReferenceBinding) typeBinding).resolve(environment);
160 if (typeBinding.isNestedType())
161 return new ProblemReferenceBinding(name, InternalNameProvided);
165 PackageBinding packageBinding = getPackage0(name);
166 if (packageBinding != null && packageBinding != LookupEnvironment.TheNotFoundPackage)
167 return packageBinding;
169 if (typeBinding == null) { // have not looked for it before
170 if ((typeBinding = environment.askForType(this, name)) != null) {
171 if (typeBinding.isNestedType())
172 return new ProblemReferenceBinding(name, InternalNameProvided);
176 // Since name could not be found, add a problem binding
177 // to the collections so it will be reported as an error next time.
178 addNotFoundType(name);
181 if (packageBinding == null) { // have not looked for it before
182 if ((packageBinding = findPackage(name)) != null)
183 return packageBinding;
184 addNotFoundPackage(name);
189 public char[] readableName() /*java.lang*/ {
190 return CharOperation.concatWith(compoundName, '.');
192 public String toString() {
193 if (compoundName == CharOperation.NO_CHAR_CHAR)
194 return "The Default Package"; //$NON-NLS-1$
196 return "package " + ((compoundName != null) ? CharOperation.toString(compoundName) : "UNNAMED"); //$NON-NLS-1$ //$NON-NLS-2$