You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReferenceBinding.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Common Public License v0.5
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/cpl-v05.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. * Palo Alto Research Center, Incorporated - AspectJ adaptation
  11. ******************************************************************************/
  12. package org.eclipse.jdt.internal.compiler.lookup;
  13. import org.eclipse.jdt.internal.compiler.env.IDependent;
  14. import org.eclipse.jdt.internal.compiler.util.CharOperation;
  15. /*
  16. Not all fields defined by this type (& its subclasses) are initialized when it is created.
  17. Some are initialized only when needed.
  18. Accessors have been provided for some public fields so all TypeBindings have the same API...
  19. but access public fields directly whenever possible.
  20. Non-public fields have accessors which should be used everywhere you expect the field to be initialized.
  21. null is NOT a valid value for a non-public field... it just means the field is not initialized.
  22. */
  23. // AspectJ - added hooks for more sophisticated field lookup
  24. abstract public class ReferenceBinding extends TypeBinding implements IDependent {
  25. public char[][] compoundName;
  26. public char[] sourceName;
  27. public int modifiers;
  28. public PackageBinding fPackage;
  29. char[] fileName;
  30. char[] constantPoolName;
  31. char[] signature;
  32. public FieldBinding[] availableFields() {
  33. return fields();
  34. }
  35. public MethodBinding[] availableMethods() {
  36. return methods();
  37. }
  38. /* Answer true if the receiver can be instantiated
  39. */
  40. public boolean canBeInstantiated() {
  41. return !(isAbstract() || isInterface());
  42. }
  43. /* Answer true if the receiver is visible to the invocationPackage.
  44. */
  45. public final boolean canBeSeenBy(PackageBinding invocationPackage) {
  46. if (isPublic()) return true;
  47. if (isPrivate()) return false;
  48. // isProtected() or isDefault()
  49. return invocationPackage == fPackage;
  50. }
  51. /* Answer true if the receiver is visible to the receiverType and the invocationType.
  52. */
  53. public final boolean canBeSeenBy(ReferenceBinding receiverType, SourceTypeBinding invocationType) {
  54. boolean ret = innerCanBeSeenBy(receiverType, invocationType);
  55. return ret;
  56. // if (ret) return true;
  57. //
  58. // System.err.println("trying to see: " + new String(sourceName));
  59. //
  60. // if (invocationType.privilegedHandler != null) {
  61. // invocationType.privilegedHandler.notePrivilegedTypeAccess(this);
  62. // return true;
  63. // }
  64. // return false;
  65. }
  66. private final boolean innerCanBeSeenBy(ReferenceBinding receiverType, SourceTypeBinding invocationType) {
  67. if (isPublic()) return true;
  68. if (invocationType == this && invocationType == receiverType) return true;
  69. if (isProtected()) {
  70. // answer true if the invocationType is the declaringClass or they are in the same package
  71. // OR the invocationType is a subclass of the declaringClass
  72. // AND the invocationType is the invocationType or its subclass
  73. // OR the type is a static method accessed directly through a type
  74. // OR previous assertions are true for one of the enclosing type
  75. if (invocationType == this) return true;
  76. if (invocationType.fPackage == fPackage) return true;
  77. ReferenceBinding currentType = invocationType;
  78. ReferenceBinding declaringClass = enclosingType(); // protected types always have an enclosing one
  79. if (declaringClass == null) return false; // could be null if incorrect top-level protected type
  80. //int depth = 0;
  81. do {
  82. if (declaringClass == invocationType) return true;
  83. if (declaringClass.isSuperclassOf(currentType)) return true;
  84. //depth++;
  85. currentType = currentType.enclosingType();
  86. } while (currentType != null);
  87. return false;
  88. }
  89. if (isPrivate()) {
  90. // answer true if the receiverType is the receiver or its enclosingType
  91. // AND the invocationType and the receiver have a common enclosingType
  92. if (!(receiverType == this || receiverType == enclosingType())) return false;
  93. if (invocationType != this) {
  94. ReferenceBinding outerInvocationType = invocationType;
  95. ReferenceBinding temp = outerInvocationType.enclosingType();
  96. while (temp != null) {
  97. outerInvocationType = temp;
  98. temp = temp.enclosingType();
  99. }
  100. ReferenceBinding outerDeclaringClass = this;
  101. temp = outerDeclaringClass.enclosingType();
  102. while (temp != null) {
  103. outerDeclaringClass = temp;
  104. temp = temp.enclosingType();
  105. }
  106. if (outerInvocationType != outerDeclaringClass) return false;
  107. }
  108. return true;
  109. }
  110. // isDefault()
  111. if (invocationType.fPackage != fPackage) return false;
  112. ReferenceBinding type = receiverType;
  113. ReferenceBinding declaringClass = enclosingType() == null ? this : enclosingType();
  114. do {
  115. if (declaringClass == type) return true;
  116. if (fPackage != type.fPackage) return false;
  117. } while ((type = type.superclass()) != null);
  118. return false;
  119. }
  120. /* Answer true if the receiver is visible to the type provided by the scope.
  121. *
  122. * NOTE: Cannot invoke this method with a compilation unit scope.
  123. */
  124. public final boolean canBeSeenBy(Scope scope) {
  125. boolean ret = innerCanBeSeenBy(scope);
  126. if (ret) return true;
  127. SourceTypeBinding invocationType = scope.invocationType();
  128. // System.err.println("trying to see (scope): " + new String(sourceName) +
  129. // " from " + new String(invocationType.sourceName));
  130. if (invocationType.privilegedHandler != null) {
  131. //System.err.println(" is privileged!");
  132. invocationType.privilegedHandler.notePrivilegedTypeAccess(this);
  133. return true;
  134. }
  135. return false;
  136. }
  137. private final boolean innerCanBeSeenBy(Scope scope) {
  138. if (isPublic()) return true;
  139. SourceTypeBinding invocationType = scope.enclosingSourceType();
  140. if (invocationType == this) return true;
  141. if (isProtected()) {
  142. // answer true if the invocationType is the declaringClass or they are in the same package
  143. // OR the invocationType is a subclass of the declaringClass
  144. // AND the invocationType is the invocationType or its subclass
  145. // OR the type is a static method accessed directly through a type
  146. // OR previous assertions are true for one of the enclosing type
  147. if (invocationType.fPackage == fPackage) return true;
  148. ReferenceBinding currentType = invocationType;
  149. ReferenceBinding declaringClass = enclosingType(); // protected types always have an enclosing one
  150. if (declaringClass == null) return false; // could be null if incorrect top-level protected type
  151. // int depth = 0;
  152. do {
  153. if (declaringClass == invocationType) return true;
  154. if (declaringClass.isSuperclassOf(currentType)) return true;
  155. // depth++;
  156. currentType = currentType.enclosingType();
  157. } while (currentType != null);
  158. return false;
  159. }
  160. if (isPrivate()) {
  161. // answer true if the receiver and the invocationType have a common enclosingType
  162. // already know they are not the identical type
  163. ReferenceBinding outerInvocationType = invocationType;
  164. ReferenceBinding temp = outerInvocationType.enclosingType();
  165. while (temp != null) {
  166. outerInvocationType = temp;
  167. temp = temp.enclosingType();
  168. }
  169. ReferenceBinding outerDeclaringClass = this;
  170. temp = outerDeclaringClass.enclosingType();
  171. while (temp != null) {
  172. outerDeclaringClass = temp;
  173. temp = temp.enclosingType();
  174. }
  175. return outerInvocationType == outerDeclaringClass;
  176. }
  177. // isDefault()
  178. return invocationType.fPackage == fPackage;
  179. }
  180. public void computeId() {
  181. if (compoundName.length != 3) {
  182. if (compoundName.length == 4 && CharOperation.equals(JAVA_LANG_REFLECT_CONSTRUCTOR, compoundName)) {
  183. id = T_JavaLangReflectConstructor;
  184. return;
  185. }
  186. return; // all other types are in java.*.*
  187. }
  188. if (!CharOperation.equals(JAVA, compoundName[0]))
  189. return; // assumes we only look up types in java
  190. if (!CharOperation.equals(LANG, compoundName[1])) {
  191. if (CharOperation.equals(JAVA_IO_PRINTSTREAM, compoundName)) {
  192. id = T_JavaIoPrintStream;
  193. return;
  194. }
  195. return; // all other types are in java.lang
  196. }
  197. if (CharOperation.equals(JAVA_LANG_OBJECT, compoundName)) {
  198. id = T_JavaLangObject;
  199. return;
  200. }
  201. if (CharOperation.equals(JAVA_LANG_STRING, compoundName)) {
  202. id = T_JavaLangString;
  203. return;
  204. }
  205. // well-known exception types
  206. if (CharOperation.equals(JAVA_LANG_THROWABLE, compoundName)) {
  207. id = T_JavaLangThrowable;
  208. return;
  209. }
  210. if (CharOperation.equals(JAVA_LANG_ERROR, compoundName)) {
  211. id = T_JavaLangError;
  212. return;
  213. }
  214. if (CharOperation.equals(JAVA_LANG_EXCEPTION, compoundName)) {
  215. id = T_JavaLangException;
  216. return;
  217. }
  218. if (CharOperation.equals(JAVA_LANG_CLASSNOTFOUNDEXCEPTION, compoundName)) {
  219. id = T_JavaLangClassNotFoundException;
  220. return;
  221. }
  222. if (CharOperation.equals(JAVA_LANG_NOCLASSDEFERROR, compoundName)) {
  223. id = T_JavaLangNoClassDefError;
  224. return;
  225. }
  226. // other well-known types
  227. if (CharOperation.equals(JAVA_LANG_CLASS, compoundName)) {
  228. id = T_JavaLangClass;
  229. return;
  230. }
  231. if (CharOperation.equals(JAVA_LANG_STRINGBUFFER, compoundName)) {
  232. id = T_JavaLangStringBuffer;
  233. return;
  234. }
  235. if (CharOperation.equals(JAVA_LANG_SYSTEM, compoundName)) {
  236. id = T_JavaLangSystem;
  237. return;
  238. }
  239. if (CharOperation.equals(JAVA_LANG_INTEGER, compoundName)) {
  240. id = T_JavaLangInteger;
  241. return;
  242. }
  243. if (CharOperation.equals(JAVA_LANG_BYTE, compoundName)) {
  244. id = T_JavaLangByte;
  245. return;
  246. }
  247. if (CharOperation.equals(JAVA_LANG_CHARACTER, compoundName)) {
  248. id = T_JavaLangCharacter;
  249. return;
  250. }
  251. if (CharOperation.equals(JAVA_LANG_FLOAT, compoundName)) {
  252. id = T_JavaLangFloat;
  253. return;
  254. }
  255. if (CharOperation.equals(JAVA_LANG_DOUBLE, compoundName)) {
  256. id = T_JavaLangDouble;
  257. return;
  258. }
  259. if (CharOperation.equals(JAVA_LANG_BOOLEAN, compoundName)) {
  260. id = T_JavaLangBoolean;
  261. return;
  262. }
  263. if (CharOperation.equals(JAVA_LANG_SHORT, compoundName)) {
  264. id = T_JavaLangShort;
  265. return;
  266. }
  267. if (CharOperation.equals(JAVA_LANG_LONG, compoundName)) {
  268. id = T_JavaLangLong;
  269. return;
  270. }
  271. if (CharOperation.equals(JAVA_LANG_VOID, compoundName)) {
  272. id = T_JavaLangVoid;
  273. return;
  274. }
  275. if (CharOperation.equals(JAVA_LANG_ASSERTIONERROR, compoundName)) {
  276. id = T_JavaLangAssertionError;
  277. return;
  278. }
  279. }
  280. /* Answer the receiver's constant pool name.
  281. *
  282. * NOTE: This method should only be used during/after code gen.
  283. */
  284. public char[] constantPoolName() /* java/lang/Object */ {
  285. if (constantPoolName != null) return constantPoolName;
  286. return constantPoolName = CharOperation.concatWith(compoundName, '/');
  287. }
  288. String debugName() {
  289. return (compoundName != null) ? new String(readableName()) : "UNNAMED TYPE"; //$NON-NLS-1$
  290. }
  291. public final int depth() {
  292. int depth = 0;
  293. ReferenceBinding current = this;
  294. while ((current = current.enclosingType()) != null)
  295. depth++;
  296. return depth;
  297. }
  298. /* Answer the receiver's enclosing type... null if the receiver is a top level type.
  299. */
  300. public ReferenceBinding enclosingType() {
  301. return null;
  302. }
  303. public final ReferenceBinding enclosingTypeAt(int relativeDepth) {
  304. ReferenceBinding current = this;
  305. while (relativeDepth-- > 0 && current != null)
  306. current = current.enclosingType();
  307. return current;
  308. }
  309. public int fieldCount() {
  310. return fields().length;
  311. }
  312. public FieldBinding[] fields() {
  313. return NoFields;
  314. }
  315. public final int getAccessFlags() {
  316. return modifiers & AccJustFlag;
  317. }
  318. public MethodBinding getExactConstructor(TypeBinding[] argumentTypes) {
  319. return null;
  320. }
  321. public MethodBinding getExactMethod(char[] selector, TypeBinding[] argumentTypes) {
  322. return null;
  323. }
  324. public FieldBinding getField(char[] fieldName) {
  325. return null;
  326. }
  327. public FieldBinding getBestField(char[] fieldName, InvocationSite site, Scope scope) {
  328. //XXX this is mostly correct
  329. return getField(fieldName, site, scope);
  330. }
  331. /**
  332. * Where multiple fields with the same name are defined, this will
  333. * return the one most visible one...
  334. */
  335. public FieldBinding getField(char[] fieldName, InvocationSite site, Scope scope) {
  336. return getField(fieldName);
  337. }
  338. /**
  339. * Answer the file name which defines the type.
  340. *
  341. * The path part (optional) must be separated from the actual
  342. * file proper name by a java.io.File.separator.
  343. *
  344. * The proper file name includes the suffix extension (e.g. ".java")
  345. *
  346. * e.g. "c:/com/ibm/compiler/java/api/Compiler.java"
  347. */
  348. public char[] getFileName() {
  349. return fileName;
  350. }
  351. public ReferenceBinding getMemberType(char[] typeName) {
  352. ReferenceBinding[] memberTypes = memberTypes();
  353. for (int i = memberTypes.length; --i >= 0;)
  354. if (CharOperation.equals(memberTypes[i].sourceName, typeName))
  355. return memberTypes[i];
  356. return null;
  357. }
  358. public MethodBinding[] getMethods(char[] selector) {
  359. return NoMethods;
  360. }
  361. public PackageBinding getPackage() {
  362. return fPackage;
  363. }
  364. /* Answer true if the receiver implements anInterface or is identical to anInterface.
  365. * If searchHierarchy is true, then also search the receiver's superclasses.
  366. *
  367. * NOTE: Assume that anInterface is an interface.
  368. */
  369. public boolean implementsInterface(ReferenceBinding anInterface, boolean searchHierarchy) {
  370. if (this == anInterface)
  371. return true;
  372. ReferenceBinding[][] interfacesToVisit = new ReferenceBinding[5][];
  373. int lastPosition = -1;
  374. ReferenceBinding currentType = this;
  375. do {
  376. ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
  377. if (itsInterfaces != NoSuperInterfaces) {
  378. if (++lastPosition == interfacesToVisit.length)
  379. System.arraycopy(interfacesToVisit, 0, interfacesToVisit = new ReferenceBinding[lastPosition * 2][], 0, lastPosition);
  380. interfacesToVisit[lastPosition] = itsInterfaces;
  381. }
  382. } while (searchHierarchy && (currentType = currentType.superclass()) != null);
  383. for (int i = 0; i <= lastPosition; i++) {
  384. ReferenceBinding[] interfaces = interfacesToVisit[i];
  385. for (int j = 0, length = interfaces.length; j < length; j++) {
  386. if ((currentType = interfaces[j]) == anInterface)
  387. return true;
  388. ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
  389. if (itsInterfaces != NoSuperInterfaces) {
  390. if (++lastPosition == interfacesToVisit.length)
  391. System.arraycopy(interfacesToVisit, 0, interfacesToVisit = new ReferenceBinding[lastPosition * 2][], 0, lastPosition);
  392. interfacesToVisit[lastPosition] = itsInterfaces;
  393. }
  394. }
  395. }
  396. return false;
  397. }
  398. // Internal method... assume its only sent to classes NOT interfaces
  399. boolean implementsMethod(MethodBinding method) {
  400. ReferenceBinding type = this;
  401. while (type != null) {
  402. MethodBinding[] methods = type.getMethods(method.selector);
  403. for (int i = methods.length; --i >= 0;)
  404. if (methods[i].areParametersEqual(method))
  405. return true;
  406. type = type.superclass();
  407. }
  408. return false;
  409. }
  410. /* Answer true if the receiver is an abstract type
  411. */
  412. public final boolean isAbstract() {
  413. return (modifiers & AccAbstract) != 0;
  414. }
  415. public final boolean isAnonymousType() {
  416. return (tagBits & IsAnonymousType) != 0;
  417. }
  418. public final boolean isBinaryBinding() {
  419. return (tagBits & IsBinaryBinding) != 0;
  420. }
  421. public final boolean isClass() {
  422. return (modifiers & AccInterface) == 0;
  423. }
  424. /* Answer true if the receiver type can be assigned to the argument type (right)
  425. */
  426. boolean isCompatibleWith(TypeBinding right) {
  427. if (right == this)
  428. return true;
  429. if (right.id == T_Object)
  430. return true;
  431. if (!(right instanceof ReferenceBinding))
  432. return false;
  433. ReferenceBinding referenceBinding = (ReferenceBinding) right;
  434. if (referenceBinding.isInterface())
  435. return implementsInterface(referenceBinding, true);
  436. if (isInterface()) // Explicit conversion from an interface to a class is not allowed
  437. return false;
  438. return referenceBinding.isSuperclassOf(this);
  439. }
  440. /* Answer true if the receiver has default visibility
  441. */
  442. public final boolean isDefault() {
  443. return (modifiers & (AccPublic | AccProtected | AccPrivate)) == 0;
  444. }
  445. /* Answer true if the receiver is a deprecated type
  446. */
  447. public final boolean isDeprecated() {
  448. return (modifiers & AccDeprecated) != 0;
  449. }
  450. /* Answer true if the receiver is final and cannot be subclassed
  451. */
  452. public final boolean isFinal() {
  453. return (modifiers & AccFinal) != 0;
  454. }
  455. public final boolean isInterface() {
  456. return (modifiers & AccInterface) != 0;
  457. }
  458. public final boolean isLocalType() {
  459. return (tagBits & IsLocalType) != 0;
  460. }
  461. public final boolean isMemberType() {
  462. return (tagBits & IsMemberType) != 0;
  463. }
  464. public final boolean isNestedType() {
  465. return (tagBits & IsNestedType) != 0;
  466. }
  467. /* Answer true if the receiver has private visibility
  468. */
  469. public final boolean isPrivate() {
  470. return (modifiers & AccPrivate) != 0;
  471. }
  472. /* Answer true if the receiver has protected visibility
  473. */
  474. public final boolean isProtected() {
  475. return (modifiers & AccProtected) != 0;
  476. }
  477. /* Answer true if the receiver has public visibility
  478. */
  479. public final boolean isPublic() {
  480. return (modifiers & AccPublic) != 0;
  481. }
  482. /* Answer true if the receiver is a static member type (or toplevel)
  483. */
  484. public final boolean isStatic() {
  485. return (modifiers & (AccStatic | AccInterface)) != 0 ||
  486. (tagBits & IsNestedType) == 0;
  487. }
  488. /* Answer true if all float operations must adher to IEEE 754 float/double rules
  489. */
  490. public final boolean isStrictfp() {
  491. return (modifiers & AccStrictfp) != 0;
  492. }
  493. /* Answer true if the receiver is in the superclass hierarchy of aType
  494. *
  495. * NOTE: Object.isSuperclassOf(Object) -> false
  496. */
  497. public boolean isSuperclassOf(ReferenceBinding type) {
  498. do {
  499. if (this == (type = type.superclass())) return true;
  500. } while (type != null);
  501. return false;
  502. }
  503. /* Answer true if the receiver is deprecated (or any of its enclosing types)
  504. */
  505. public final boolean isViewedAsDeprecated() {
  506. return (modifiers & AccDeprecated) != 0 ||
  507. (modifiers & AccDeprecatedImplicitly) != 0;
  508. }
  509. public ReferenceBinding[] memberTypes() {
  510. return NoMemberTypes;
  511. }
  512. public MethodBinding[] methods() {
  513. return NoMethods;
  514. }
  515. /**
  516. * Answer the source name for the type.
  517. * In the case of member types, as the qualified name from its top level type.
  518. * For example, for a member type N defined inside M & A: "A.M.N".
  519. */
  520. public char[] qualifiedSourceName() {
  521. if (isMemberType()) {
  522. return CharOperation.concat(enclosingType().qualifiedSourceName(), sourceName(), '.');
  523. } else {
  524. return sourceName();
  525. }
  526. }
  527. public char[] readableName() /*java.lang.Object*/ {
  528. if (isMemberType())
  529. return CharOperation.concat(enclosingType().readableName(), sourceName, '.');
  530. else
  531. return CharOperation.concatWith(compoundName, '.');
  532. }
  533. /* Answer the receiver's signature.
  534. *
  535. * NOTE: This method should only be used during/after code gen.
  536. */
  537. public char[] signature() /* Ljava/lang/Object; */ {
  538. if (signature != null)
  539. return signature;
  540. return signature = CharOperation.concat('L', constantPoolName(), ';');
  541. }
  542. public char[] sourceName() {
  543. return sourceName;
  544. }
  545. public ReferenceBinding superclass() {
  546. return null;
  547. }
  548. public ReferenceBinding[] superInterfaces() {
  549. return NoSuperInterfaces;
  550. }
  551. public ReferenceBinding[] syntheticEnclosingInstanceTypes() {
  552. if (isStatic()) return null;
  553. ReferenceBinding enclosingType = enclosingType();
  554. if (enclosingType == null)
  555. return null;
  556. else
  557. return new ReferenceBinding[] {enclosingType};
  558. }
  559. public SyntheticArgumentBinding[] syntheticOuterLocalVariables() {
  560. return null; // is null if no enclosing instances are required
  561. }
  562. }