1. package com.sun.org.apache.bcel.internal.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 com.sun.org.apache.bcel.internal.Constants;
  56. import com.sun.org.apache.bcel.internal.Repository;
  57. import com.sun.org.apache.bcel.internal.classfile.JavaClass;
  58. /**
  59. * Super class for objects and arrays.
  60. *
  61. * @version $Id: ReferenceType.java,v 1.1.1.1 2001/10/29 20:00:26 jvanzyl Exp $
  62. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  63. */
  64. public class ReferenceType extends Type {
  65. protected ReferenceType(byte t, String s) {
  66. super(t, s);
  67. }
  68. /** Class is non-abstract but not instantiable from the outside
  69. */
  70. ReferenceType() {
  71. super(Constants.T_OBJECT, "<null object>");
  72. }
  73. /**
  74. * Return true iff this type is castable to another type t as defined in
  75. * the JVM specification. The case where this is Type.NULL is not
  76. * defined (see the CHECKCAST definition in the JVM specification).
  77. * However, because e.g. CHECKCAST doesn't throw a
  78. * ClassCastException when casting a null reference to any Object,
  79. * true is returned in this case.
  80. */
  81. public boolean isCastableTo(Type t){
  82. if(this.equals(Type.NULL))
  83. return true; // If this is ever changed in isAssignmentCompatible()
  84. return isAssignmentCompatibleWith(t); /* Yes, it's true: It's the same definition.
  85. * See vmspec2 AASTORE / CHECKCAST definitions.
  86. */
  87. }
  88. /**
  89. * Return true iff this is assignment compatible with another type t
  90. * as defined in the JVM specification; see the AASTORE definition
  91. * there.
  92. */
  93. public boolean isAssignmentCompatibleWith(Type t) {
  94. if(!(t instanceof ReferenceType))
  95. return false;
  96. ReferenceType T = (ReferenceType)t;
  97. if(this.equals(Type.NULL))
  98. return true; // This is not explicitely stated, but clear. Isn't it?
  99. /* If this is a class type then
  100. */
  101. if((this instanceof ObjectType) && (((ObjectType) this).referencesClass())) {
  102. /* If T is a class type, then this must be the same class as T,
  103. or this must be a subclass of T;
  104. */
  105. if((T instanceof ObjectType) && (((ObjectType) T).referencesClass())) {
  106. if(this.equals(T))
  107. return true;
  108. if(Repository.instanceOf( ((ObjectType) this).getClassName(),
  109. ((ObjectType) T).getClassName()))
  110. return true;
  111. }
  112. /* If T is an interface type, this must implement interface T.
  113. */
  114. if ((T instanceof ObjectType) && (((ObjectType) T).referencesInterface())) {
  115. if (Repository.implementationOf( ((ObjectType) this).getClassName(),
  116. ((ObjectType) T).getClassName() ))
  117. return true;
  118. }
  119. }
  120. /* If this is an interface type, then:
  121. */
  122. if ((this instanceof ObjectType) && (((ObjectType) this).referencesInterface())){
  123. /* If T is a class type, then T must be Object (÷2.4.7).
  124. */
  125. if ((T instanceof ObjectType) && (((ObjectType) T).referencesClass())){
  126. if (T.equals(Type.OBJECT)) return true;
  127. }
  128. /* If T is an interface type, then T must be the same interface
  129. as this or a superinterface of this (÷2.13.2).
  130. */
  131. if ((T instanceof ObjectType) && (((ObjectType) T).referencesInterface())){
  132. if (this.equals(T)) return true;
  133. if (Repository.implementationOf( ((ObjectType) this).getClassName(),
  134. ((ObjectType) T).getClassName() )) return true;
  135. }
  136. }
  137. /* If this is an array type, namely, the type SC[], that is, an
  138. array of components of type SC, then:
  139. */
  140. if(this instanceof ArrayType){
  141. /* If T is a class type, then T must be Object (÷2.4.7).
  142. */
  143. if ((T instanceof ObjectType) && (((ObjectType) T).referencesClass())){
  144. if (T.equals(Type.OBJECT)) return true;
  145. }
  146. /* If T is an array type TC[], that is, an array of components
  147. of type TC, then one of the following must be true:
  148. */
  149. if (T instanceof ArrayType) {
  150. /* TC and SC are the same primitive type (÷2.4.1).
  151. */
  152. Type sc = ((ArrayType) this).getElementType();
  153. Type tc = ((ArrayType) this).getElementType();
  154. if (sc instanceof BasicType && tc instanceof BasicType && sc.equals(tc))
  155. return true;
  156. /* TC and SC are reference types (÷2.4.6), and type SC is
  157. assignable to TC by these runtime rules.*/
  158. if (tc instanceof ReferenceType && sc instanceof ReferenceType &&
  159. ((ReferenceType) sc).isAssignmentCompatibleWith((ReferenceType) tc)) return true;
  160. }
  161. /* If T is an interface type, T must be one of the interfaces implemented by arrays (÷2.15). */
  162. // TODO: Check if this is still valid or find a way to dynamically find out which
  163. // interfaces arrays implement. However, as of the JVM specification edition 2, there
  164. // are at least two different pages where assignment compatibility is defined and
  165. // on one of them "interfaces implemented by arrays" is exchanged with "'Cloneable' or
  166. // 'java.io.Serializable'"
  167. if ((T instanceof ObjectType) && (((ObjectType) T).referencesInterface())){
  168. for (int ii=0; ii<Constants.INTERFACES_IMPLEMENTED_BY_ARRAYS.length; ii++){
  169. if (T.equals(new ObjectType(Constants.INTERFACES_IMPLEMENTED_BY_ARRAYS[ii]))) return true;
  170. }
  171. }
  172. }
  173. return false; // default.
  174. }
  175. /**
  176. * This commutative operation returns the first common superclass (narrowest ReferenceType
  177. * referencing a class, not an interface).
  178. * If one of the types is a superclass of the other, the former is returned.
  179. * If "this" is Type.NULL, then t is returned.
  180. * If t is Type.NULL, then "this" is returned.
  181. * If "this" equals t ['this.equals(t)'] "this" is returned.
  182. * If "this" or t is an ArrayType, then Type.OBJECT is returned.
  183. * If "this" or t is a ReferenceType referencing an interface, then Type.OBJECT is returned.
  184. * If not all of the two classes' superclasses cannot be found, "null" is returned.
  185. * See the JVM specification edition 2, "&247;4.9.2 The Bytecode Verifier".
  186. */
  187. public ReferenceType firstCommonSuperclass(ReferenceType t){
  188. if (this.equals(Type.NULL)) return t;
  189. if (t.equals(Type.NULL)) return this;
  190. if (this.equals(t)) return this;
  191. // TODO: This sounds a little arbitrary. On the other hand, there is
  192. // no object referenced by Type.NULL so we can also say all the objects
  193. // referenced by Type.NULL were derived from java.lang.Object.
  194. // However, the Java Language's "instanceof" operator proves us wrong:
  195. // "null" is not referring to an instance of java.lang.Object :)
  196. if ((this instanceof ArrayType) || (t instanceof ArrayType))
  197. return Type.OBJECT;
  198. // TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType?
  199. if ( ((this instanceof ObjectType) && ((ObjectType) this).referencesInterface()) ||
  200. (( t instanceof ObjectType) && ((ObjectType) t).referencesInterface()) )
  201. return Type.OBJECT;
  202. // TODO: The above line is correct comparing to the vmspec2. But one could
  203. // make class file verification a bit stronger here by using the notion of
  204. // superinterfaces or even castability or assignment compatibility.
  205. // this and t are ObjectTypes, see above.
  206. ObjectType thiz = (ObjectType) this;
  207. ObjectType other = (ObjectType) t;
  208. JavaClass[] thiz_sups = Repository.getSuperClasses( thiz.getClassName());
  209. JavaClass[] other_sups = Repository.getSuperClasses(other.getClassName());
  210. if ((thiz_sups == null) || (other_sups==null)){
  211. return null;
  212. }
  213. // Waaahh...
  214. JavaClass[] this_sups = new JavaClass[thiz_sups.length+1];
  215. JavaClass[] t_sups = new JavaClass[other_sups.length+1];
  216. System.arraycopy( thiz_sups, 0, this_sups, 1, thiz_sups.length);
  217. System.arraycopy(other_sups, 0, t_sups , 1, other_sups.length);
  218. this_sups[0] = Repository.lookupClass(thiz.getClassName());
  219. t_sups[0] = Repository.lookupClass(other.getClassName());
  220. for (int i=0; i<t_sups.length; i++){
  221. for (int j=0; j<this_sups.length; j++){
  222. if (this_sups[j].equals(t_sups[i])) return new ObjectType(this_sups[j].getClassName());
  223. }
  224. }
  225. // Huh? Did you ask for Type.OBJECT's superclass??
  226. return null;
  227. }
  228. }