KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > lookup > PackageBinding


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.lookup;
12
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;
16
17 public class PackageBinding extends Binding implements TypeConstants {
18     public long tagBits = 0; // See values in the interface TagBits below
19

20     public char[][] compoundName;
21     PackageBinding parent;
22     public LookupEnvironment environment;
23     HashtableOfType knownTypes;
24     HashtableOfPackage knownPackages;
25 protected PackageBinding() {
26     // for creating problem package
27
}
28 public PackageBinding(char[][] compoundName, PackageBinding parent, LookupEnvironment environment) {
29     this.compoundName = compoundName;
30     this.parent = parent;
31     this.environment = environment;
32     this.knownTypes = null; // initialized if used... class counts can be very large 300-600
33
this.knownPackages = new HashtableOfPackage(3); // sub-package counts are typically 0-3
34
}
35 public PackageBinding(char[] topLevelPackageName, LookupEnvironment environment) {
36     this(new char[][] {topLevelPackageName}, null, environment);
37 }
38 /* Create the default package.
39 */

40
41 public PackageBinding(LookupEnvironment environment) {
42     this(CharOperation.NO_CHAR_CHAR, null, environment);
43 }
44 private void addNotFoundPackage(char[] simpleName) {
45     knownPackages.put(simpleName, LookupEnvironment.TheNotFoundPackage);
46 }
47 private void addNotFoundType(char[] simpleName) {
48     if (knownTypes == null)
49         knownTypes = new HashtableOfType(25);
50     knownTypes.put(simpleName, LookupEnvironment.TheNotFoundType);
51 }
52 void addPackage(PackageBinding element) {
53     knownPackages.put(element.compoundName[element.compoundName.length - 1], element);
54 }
55 void addType(ReferenceBinding element) {
56     if (knownTypes == null)
57         knownTypes = new HashtableOfType(25);
58     knownTypes.put(element.compoundName[element.compoundName.length - 1], element);
59 }
60 /* API
61 * Answer the receiver's binding type from Binding.BindingID.
62 */

63
64 public final int kind() {
65     return Binding.PACKAGE;
66 }
67 /*
68  * slash separated name
69  * org.eclipse.jdt.core --> org/eclipse/jdt/core
70  */

71 public char[] computeUniqueKey(boolean isLeaf) {
72     return CharOperation.concatWith(compoundName, '/');
73 }
74 private PackageBinding findPackage(char[] name) {
75     if (!environment.isPackage(this.compoundName, name))
76         return null;
77
78     char[][] subPkgCompoundName = CharOperation.arrayConcat(this.compoundName, name);
79     PackageBinding subPackageBinding = new PackageBinding(subPkgCompoundName, this, environment);
80     addPackage(subPackageBinding);
81     return subPackageBinding;
82 }
83 /* Answer the subpackage named name; ask the oracle for the package if its not in the cache.
84 * Answer null if it could not be resolved.
85 *
86 * NOTE: This should only be used when we know there is NOT a type with the same name.
87 */

88
89 PackageBinding getPackage(char[] name) {
90     PackageBinding binding = getPackage0(name);
91     if (binding != null) {
92         if (binding == LookupEnvironment.TheNotFoundPackage)
93             return null;
94         else
95             return binding;
96     }
97     if ((binding = findPackage(name)) != null)
98         return binding;
99
100     // not found so remember a problem package binding in the cache for future lookups
101
addNotFoundPackage(name);
102     return null;
103 }
104 /* Answer the subpackage named name if it exists in the cache.
105 * Answer theNotFoundPackage if it could not be resolved the first time
106 * it was looked up, otherwise answer null.
107 *
108 * NOTE: Senders must convert theNotFoundPackage into a real problem
109 * package if its to returned.
110 */

111
112 PackageBinding getPackage0(char[] name) {
113     return knownPackages.get(name);
114 }
115 /* Answer the type named name; ask the oracle for the type if its not in the cache.
116 * Answer a NotVisible problem type if the type is not visible from the invocationPackage.
117 * Answer null if it could not be resolved.
118 *
119 * NOTE: This should only be used by source types/scopes which know there is NOT a
120 * package with the same name.
121 */

122
123 ReferenceBinding getType(char[] name) {
124     ReferenceBinding typeBinding = getType0(name);
125     if (typeBinding == null) {
126         if ((typeBinding = environment.askForType(this, name)) == null) {
127             // not found so remember a problem type binding in the cache for future lookups
128
addNotFoundType(name);
129             return null;
130         }
131     }
132
133     if (typeBinding == LookupEnvironment.TheNotFoundType)
134         return null;
135
136     typeBinding = BinaryTypeBinding.resolveType(typeBinding, environment, false); // no raw conversion for now
137
if (typeBinding.isNestedType())
138         return new ProblemReferenceBinding(name, typeBinding, ProblemReasons.InternalNameProvided);
139     return typeBinding;
140 }
141 /* Answer the type named name if it exists in the cache.
142 * Answer theNotFoundType if it could not be resolved the first time
143 * it was looked up, otherwise answer null.
144 *
145 * NOTE: Senders must convert theNotFoundType into a real problem
146 * reference type if its to returned.
147 */

148
149 ReferenceBinding getType0(char[] name) {
150     if (knownTypes == null)
151         return null;
152     return knownTypes.get(name);
153 }
154 /* Answer the package or type named name; ask the oracle if it is not in the cache.
155 * Answer null if it could not be resolved.
156 *
157 * When collisions exist between a type name & a package name, answer the type.
158 * Treat the package as if it does not exist... a problem was already reported when the type was defined.
159 *
160 * NOTE: no visibility checks are performed.
161 * THIS SHOULD ONLY BE USED BY SOURCE TYPES/SCOPES.
162 */

163
164 public Binding getTypeOrPackage(char[] name) {
165     ReferenceBinding typeBinding = getType0(name);
166     if (typeBinding != null && typeBinding != LookupEnvironment.TheNotFoundType) {
167         typeBinding = BinaryTypeBinding.resolveType(typeBinding, environment, false); // no raw conversion for now
168
if (typeBinding.isNestedType())
169             return new ProblemReferenceBinding(name, typeBinding, ProblemReasons.InternalNameProvided);
170         return typeBinding;
171     }
172
173     PackageBinding packageBinding = getPackage0(name);
174     if (packageBinding != null && packageBinding != LookupEnvironment.TheNotFoundPackage)
175         return packageBinding;
176
177     if (typeBinding == null) { // have not looked for it before
178
if ((typeBinding = environment.askForType(this, name)) != null) {
179             if (typeBinding.isNestedType())
180                 return new ProblemReferenceBinding(name, typeBinding, ProblemReasons.InternalNameProvided);
181             return typeBinding;
182         }
183
184         // Since name could not be found, add a problem binding
185
// to the collections so it will be reported as an error next time.
186
addNotFoundType(name);
187     }
188
189     if (packageBinding == null) { // have not looked for it before
190
if ((packageBinding = findPackage(name)) != null)
191             return packageBinding;
192         addNotFoundPackage(name);
193     }
194
195     return null;
196 }
197 public char[] readableName() /*java.lang*/ {
198     return CharOperation.concatWith(compoundName, '.');
199 }
200 public String JavaDoc toString() {
201     if (compoundName == CharOperation.NO_CHAR_CHAR)
202         return "The Default Package"; //$NON-NLS-1$
203
else
204         return "package " + ((compoundName != null) ? CharOperation.toString(compoundName) : "UNNAMED"); //$NON-NLS-1$ //$NON-NLS-2$
205
}
206 }
207
Popular Tags