ObjectType.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.generic;

  18. import org.apache.bcel.Const;
  19. import org.apache.bcel.Repository;
  20. import org.apache.bcel.classfile.JavaClass;
  21. import org.apache.bcel.classfile.Utility;

  22. /**
  23.  * Denotes reference such as {@link String}.
  24.  */
  25. public class ObjectType extends ReferenceType {

  26.     /**
  27.      * Constructs a new instance.
  28.      *
  29.      * @param className fully qualified class name, e.g. java.lang.String
  30.      * @return a new instance.
  31.      * @since 6.0
  32.      */
  33.     public static ObjectType getInstance(final String className) {
  34.         return new ObjectType(className);
  35.     }

  36.     private final String className; // Class name of type

  37.     /**
  38.      * Constructs a new instance.
  39.      *
  40.      * @param className fully qualified class name, e.g. {@link String}
  41.      */
  42.     public ObjectType(final String className) {
  43.         super(Const.T_REFERENCE, "L" + Utility.packageToPath(className) + ";");
  44.         this.className = Utility.pathToPackage(className);
  45.     }

  46.     /**
  47.      * Java Virtual Machine Specification edition 2, � 5.4.4 Access Control
  48.      *
  49.      * @throws ClassNotFoundException if the class referenced by this type can't be found
  50.      */
  51.     public boolean accessibleTo(final ObjectType accessor) throws ClassNotFoundException {
  52.         final JavaClass jc = Repository.lookupClass(className);
  53.         if (jc.isPublic()) {
  54.             return true;
  55.         }
  56.         final JavaClass acc = Repository.lookupClass(accessor.className);
  57.         return acc.getPackageName().equals(jc.getPackageName());
  58.     }

  59.     /**
  60.      * @return true if both type objects refer to the same class.
  61.      */
  62.     @Override
  63.     public boolean equals(final Object type) {
  64.         return type instanceof ObjectType && ((ObjectType) type).className.equals(className);
  65.     }

  66.     /**
  67.      * @return name of referenced class
  68.      */
  69.     @Override
  70.     public String getClassName() {
  71.         return className;
  72.     }

  73.     /**
  74.      * @return a hash code value for the object.
  75.      */
  76.     @Override
  77.     public int hashCode() {
  78.         return className.hashCode();
  79.     }

  80.     /**
  81.      * If "this" doesn't reference a class, it references an interface or a non-existant entity.
  82.      * @deprecated (since 6.0) this method returns an inaccurate result if the class or interface referenced cannot be
  83.      *             found: use referencesClassExact() instead
  84.      */
  85.     @Deprecated
  86.     public boolean referencesClass() {
  87.         try {
  88.             final JavaClass jc = Repository.lookupClass(className);
  89.             return jc.isClass();
  90.         } catch (final ClassNotFoundException e) {
  91.             return false;
  92.         }
  93.     }

  94.     /**
  95.      * Return true if this type references a class, false if it references an interface.
  96.      *
  97.      * @return true if the type references a class, false if it references an interface
  98.      * @throws ClassNotFoundException if the class or interface referenced by this type can't be found
  99.      */
  100.     public boolean referencesClassExact() throws ClassNotFoundException {
  101.         final JavaClass jc = Repository.lookupClass(className);
  102.         return jc.isClass();
  103.     }

  104.     /**
  105.      * If "this" doesn't reference an interface, it references a class or a non-existant entity.
  106.      *
  107.      * @deprecated (since 6.0) this method returns an inaccurate result if the class or interface referenced cannot be
  108.      *             found: use referencesInterfaceExact() instead
  109.      */
  110.     @Deprecated
  111.     public boolean referencesInterface() {
  112.         try {
  113.             final JavaClass jc = Repository.lookupClass(className);
  114.             return !jc.isClass();
  115.         } catch (final ClassNotFoundException e) {
  116.             return false;
  117.         }
  118.     }

  119.     /**
  120.      * Return true if this type references an interface, false if it references a class.
  121.      *
  122.      * @return true if the type references an interface, false if it references a class
  123.      * @throws ClassNotFoundException if the class or interface referenced by this type can't be found
  124.      */
  125.     public boolean referencesInterfaceExact() throws ClassNotFoundException {
  126.         final JavaClass jc = Repository.lookupClass(className);
  127.         return !jc.isClass();
  128.     }

  129.     /**
  130.      * Return true if this type is a subclass of given ObjectType.
  131.      *
  132.      * @throws ClassNotFoundException if any of this class's superclasses can't be found
  133.      */
  134.     public boolean subclassOf(final ObjectType superclass) throws ClassNotFoundException {
  135.         if (referencesInterfaceExact() || superclass.referencesInterfaceExact()) {
  136.             return false;
  137.         }
  138.         return Repository.instanceOf(this.className, superclass.className);
  139.     }
  140. }