Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ReferenceType.java 14KB

pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 16 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package org.aspectj.apache.bcel.generic;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import org.aspectj.apache.bcel.Constants;
  56. import org.aspectj.apache.bcel.Repository;
  57. import org.aspectj.apache.bcel.classfile.JavaClass;
  58. /**
  59. * Super class for object and array types.
  60. *
  61. * @version $Id: ReferenceType.java,v 1.6 2009/09/09 22:18:20 aclement Exp $
  62. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  63. */
  64. public abstract class ReferenceType extends Type {
  65. protected ReferenceType(byte t, String s) {
  66. super(t, s);
  67. }
  68. ReferenceType() {
  69. super(Constants.T_OBJECT, "<null object>");
  70. }
  71. /**
  72. * Return true iff this type is castable to another type t as defined in the JVM specification. The case where this is Type.NULL
  73. * is not defined (see the CHECKCAST definition in the JVM specification). However, because e.g. CHECKCAST doesn't throw a
  74. * ClassCastException when casting a null reference to any Object, true is returned in this case.
  75. */
  76. public boolean isCastableTo(Type t) {
  77. if (this.equals(Type.NULL)) {
  78. return true; // If this is ever changed in isAssignmentCompatible()
  79. }
  80. return isAssignmentCompatibleWith(t);
  81. /*
  82. * Yes, it's true: It's the same definition. See vmspec2 AASTORE / CHECKCAST definitions.
  83. */
  84. }
  85. /**
  86. * Return true iff this is assignment compatible with another type t as defined in the JVM specification; see the AASTORE
  87. * definition there.
  88. */
  89. public boolean isAssignmentCompatibleWith(Type t) {
  90. if (!(t instanceof ReferenceType)) {
  91. return false;
  92. }
  93. ReferenceType T = (ReferenceType) t;
  94. if (this.equals(Type.NULL)) {
  95. return true; // This is not explicitely stated, but clear. Isn't it?
  96. }
  97. /*
  98. * If this is a class type then
  99. */
  100. if (this instanceof ObjectType && ((ObjectType) this).referencesClass()) {
  101. /*
  102. * If T is a class type, then this must be the same class as T, or this must be a subclass of T;
  103. */
  104. if (T instanceof ObjectType && ((ObjectType) T).referencesClass()) {
  105. if (this.equals(T)) {
  106. return true;
  107. }
  108. if (Repository.instanceOf(((ObjectType) this).getClassName(), ((ObjectType) T).getClassName())) {
  109. return true;
  110. }
  111. }
  112. /*
  113. * If T is an interface type, this must implement interface T.
  114. */
  115. if (T instanceof ObjectType && ((ObjectType) T).referencesInterface()) {
  116. if (Repository.implementationOf(((ObjectType) this).getClassName(), ((ObjectType) T).getClassName())) {
  117. return true;
  118. }
  119. }
  120. }
  121. /*
  122. * If this is an interface type, then:
  123. */
  124. if (this instanceof ObjectType && ((ObjectType) this).referencesInterface()) {
  125. /*
  126. * If T is a class type, then T must be Object (2.4.7).
  127. */
  128. if (T instanceof ObjectType && ((ObjectType) T).referencesClass()) {
  129. if (T.equals(Type.OBJECT)) {
  130. return true;
  131. }
  132. }
  133. /*
  134. * If T is an interface type, then T must be the same interface as this or a superinterface of this (2.13.2).
  135. */
  136. if (T instanceof ObjectType && ((ObjectType) T).referencesInterface()) {
  137. if (this.equals(T)) {
  138. return true;
  139. }
  140. if (Repository.implementationOf(((ObjectType) this).getClassName(), ((ObjectType) T).getClassName())) {
  141. return true;
  142. }
  143. }
  144. }
  145. /*
  146. * If this is an array type, namely, the type SC[], that is, an array of components of type SC, then:
  147. */
  148. if (this instanceof ArrayType) {
  149. /*
  150. * If T is a class type, then T must be Object (2.4.7).
  151. */
  152. if (T instanceof ObjectType && ((ObjectType) T).referencesClass()) {
  153. if (T.equals(Type.OBJECT)) {
  154. return true;
  155. }
  156. }
  157. /*
  158. * If T is an array type TC[], that is, an array of components of type TC, then one of the following must be true:
  159. */
  160. if (T instanceof ArrayType) {
  161. /*
  162. * TC and SC are the same primitive type (2.4.1).
  163. */
  164. Type sc = ((ArrayType) this).getElementType();
  165. Type tc = ((ArrayType) this).getElementType();
  166. if (sc instanceof BasicType && tc instanceof BasicType && sc.equals(tc)) {
  167. return true;
  168. }
  169. /*
  170. * TC and SC are reference types (2.4.6), and type SC is assignable to TC by these runtime rules.
  171. */
  172. if (tc instanceof ReferenceType && sc instanceof ReferenceType
  173. && ((ReferenceType) sc).isAssignmentCompatibleWith(tc)) {
  174. return true;
  175. }
  176. }
  177. /* If T is an interface type, T must be one of the interfaces implemented by arrays (2.15). */
  178. // TODO: Check if this is still valid or find a way to dynamically find out which
  179. // interfaces arrays implement. However, as of the JVM specification edition 2, there
  180. // are at least two different pages where assignment compatibility is defined and
  181. // on one of them "interfaces implemented by arrays" is exchanged with "'Cloneable' or
  182. // 'java.io.Serializable'"
  183. if (T instanceof ObjectType && ((ObjectType) T).referencesInterface()) {
  184. for (int ii = 0; ii < Constants.INTERFACES_IMPLEMENTED_BY_ARRAYS.length; ii++) {
  185. if (T.equals(new ObjectType(Constants.INTERFACES_IMPLEMENTED_BY_ARRAYS[ii]))) {
  186. return true;
  187. }
  188. }
  189. }
  190. }
  191. return false; // default.
  192. }
  193. /**
  194. * This commutative operation returns the first common superclass (narrowest ReferenceType referencing a class, not an
  195. * interface). If one of the types is a superclass of the other, the former is returned. If "this" is Type.NULL, then t is
  196. * returned. If t is Type.NULL, then "this" is returned. If "this" equals t ['this.equals(t)'] "this" is returned. If "this" or
  197. * t is an ArrayType, then Type.OBJECT is returned; unless their dimensions match. Then an ArrayType of the same number of
  198. * dimensions is returned, with its basic type being the first common super class of the basic types of "this" and t. If "this"
  199. * or t is a ReferenceType referencing an interface, then Type.OBJECT is returned. If not all of the two classes' superclasses
  200. * cannot be found, "null" is returned. See the JVM specification edition 2, "4.9.2 The Bytecode Verifier".
  201. */
  202. public ReferenceType getFirstCommonSuperclass(ReferenceType t) {
  203. if (this.equals(Type.NULL)) {
  204. return t;
  205. }
  206. if (t.equals(Type.NULL)) {
  207. return this;
  208. }
  209. if (this.equals(t)) {
  210. return this;
  211. /*
  212. * TODO: Above sounds a little arbitrary. On the other hand, there is no object referenced by Type.NULL so we can also
  213. * say all the objects referenced by Type.NULL were derived from java.lang.Object. However, the Java Language's
  214. * "instanceof" operator proves us wrong: "null" is not referring to an instance of java.lang.Object :)
  215. */
  216. }
  217. /* This code is from a bug report by Konstantin Shagin <konst@cs.technion.ac.il> */
  218. if (this instanceof ArrayType && t instanceof ArrayType) {
  219. ArrayType arrType1 = (ArrayType) this;
  220. ArrayType arrType2 = (ArrayType) t;
  221. if (arrType1.getDimensions() == arrType2.getDimensions() && arrType1.getBasicType() instanceof ObjectType
  222. && arrType2.getBasicType() instanceof ObjectType) {
  223. return new ArrayType(((ObjectType) arrType1.getBasicType()).getFirstCommonSuperclass((ObjectType) arrType2
  224. .getBasicType()), arrType1.getDimensions());
  225. }
  226. }
  227. if (this instanceof ArrayType || t instanceof ArrayType) {
  228. return Type.OBJECT;
  229. // TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType?
  230. }
  231. if (this instanceof ObjectType && ((ObjectType) this).referencesInterface() || t instanceof ObjectType
  232. && ((ObjectType) t).referencesInterface()) {
  233. return Type.OBJECT;
  234. // TODO: The above line is correct comparing to the vmspec2. But one could
  235. // make class file verification a bit stronger here by using the notion of
  236. // superinterfaces or even castability or assignment compatibility.
  237. }
  238. // this and t are ObjectTypes, see above.
  239. ObjectType thiz = (ObjectType) this;
  240. ObjectType other = (ObjectType) t;
  241. JavaClass[] thiz_sups = Repository.lookupClass(thiz.getClassName()).getSuperClasses();// getSuperClasses(thiz.getClassName());
  242. JavaClass[] other_sups = Repository.lookupClass(other.getClassName()).getSuperClasses();// getSuperClasses(other.getClassName());
  243. if (thiz_sups == null || other_sups == null) {
  244. return null;
  245. }
  246. // Waaahh...
  247. JavaClass[] this_sups = new JavaClass[thiz_sups.length + 1];
  248. JavaClass[] t_sups = new JavaClass[other_sups.length + 1];
  249. System.arraycopy(thiz_sups, 0, this_sups, 1, thiz_sups.length);
  250. System.arraycopy(other_sups, 0, t_sups, 1, other_sups.length);
  251. this_sups[0] = Repository.lookupClass(thiz.getClassName());
  252. t_sups[0] = Repository.lookupClass(other.getClassName());
  253. for (JavaClass t_sup : t_sups) {
  254. for (JavaClass this_sup : this_sups) {
  255. if (this_sup.equals(t_sup)) {
  256. return new ObjectType(this_sup.getClassName());
  257. }
  258. }
  259. }
  260. // Huh? Did you ask for Type.OBJECT's superclass??
  261. return null;
  262. }
  263. // /**
  264. // * This commutative operation returns the first common superclass (narrowest ReferenceType referencing a class, not an
  265. // * interface). If one of the types is a superclass of the other, the former is returned. If "this" is Type.NULL, then t is
  266. // * returned. If t is Type.NULL, then "this" is returned. If "this" equals t ['this.equals(t)'] "this" is returned. If "this"
  267. // or
  268. // * t is an ArrayType, then Type.OBJECT is returned. If "this" or t is a ReferenceType referencing an interface, then
  269. // Type.OBJECT
  270. // * is returned. If not all of the two classes' superclasses cannot be found, "null" is returned. See the JVM specification
  271. // * edition 2, "4.9.2 The Bytecode Verifier".
  272. // *
  273. // * @deprecated use getFirstCommonSuperclass(ReferenceType t) which has slightly changed semantics.
  274. // */
  275. // public ReferenceType firstCommonSuperclass(ReferenceType t) {
  276. // if (this.equals(Type.NULL)) {
  277. // return t;
  278. // }
  279. // if (t.equals(Type.NULL)) {
  280. // return this;
  281. // }
  282. // if (this.equals(t)) {
  283. // return this;
  284. // /*
  285. // * TODO: Above sounds a little arbitrary. On the other hand, there is no object referenced by Type.NULL so we can also
  286. // * say all the objects referenced by Type.NULL were derived from java.lang.Object. However, the Java Language's
  287. // * "instanceof" operator proves us wrong: "null" is not referring to an instance of java.lang.Object :)
  288. // */
  289. // }
  290. //
  291. // if (this instanceof ArrayType || t instanceof ArrayType) {
  292. // return Type.OBJECT;
  293. // // TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType?
  294. // }
  295. //
  296. // if (this instanceof ObjectType && ((ObjectType) this).referencesInterface() || t instanceof ObjectType
  297. // && ((ObjectType) t).referencesInterface()) {
  298. // return Type.OBJECT;
  299. // // TODO: The above line is correct comparing to the vmspec2. But one could
  300. // // make class file verification a bit stronger here by using the notion of
  301. // // superinterfaces or even castability or assignment compatibility.
  302. // }
  303. //
  304. // // this and t are ObjectTypes, see above.
  305. // ObjectType thiz = (ObjectType) this;
  306. // ObjectType other = (ObjectType) t;
  307. // JavaClass[] thiz_sups = Repository.getSuperClasses(thiz.getClassName());
  308. // JavaClass[] other_sups = Repository.getSuperClasses(other.getClassName());
  309. //
  310. // if (thiz_sups == null || other_sups == null) {
  311. // return null;
  312. // }
  313. //
  314. // // Waaahh...
  315. // JavaClass[] this_sups = new JavaClass[thiz_sups.length + 1];
  316. // JavaClass[] t_sups = new JavaClass[other_sups.length + 1];
  317. // System.arraycopy(thiz_sups, 0, this_sups, 1, thiz_sups.length);
  318. // System.arraycopy(other_sups, 0, t_sups, 1, other_sups.length);
  319. // this_sups[0] = Repository.lookupClass(thiz.getClassName());
  320. // t_sups[0] = Repository.lookupClass(other.getClassName());
  321. //
  322. // for (int i = 0; i < t_sups.length; i++) {
  323. // for (int j = 0; j < this_sups.length; j++) {
  324. // if (this_sups[j].equals(t_sups[i])) {
  325. // return new ObjectType(this_sups[j].getClassName());
  326. // }
  327. // }
  328. // }
  329. //
  330. // // Huh? Did you ask for Type.OBJECT's superclass??
  331. // return null;
  332. // }
  333. }