001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.generic; 020 021import org.apache.bcel.Const; 022import org.apache.bcel.Repository; 023import org.apache.bcel.classfile.JavaClass; 024import org.apache.bcel.classfile.Utility; 025 026/** 027 * Denotes reference such as {@link String}. 028 */ 029public class ObjectType extends ReferenceType { 030 031 /** 032 * Constructs a new instance. 033 * 034 * @param className fully qualified class name, for example {@link String}. 035 * @return a new instance. 036 * @since 6.0 037 */ 038 public static ObjectType getInstance(final String className) { 039 return new ObjectType(className); 040 } 041 042 private final String className; // Class name of type 043 044 /** 045 * Constructs a new instance. 046 * 047 * @param className fully qualified class name, for example {@link String}. 048 */ 049 public ObjectType(final String className) { 050 super(Const.T_REFERENCE, "L" + Utility.packageToPath(className) + ";"); 051 this.className = Utility.pathToPackage(className); 052 } 053 054 /** 055 * Java Virtual Machine Specification edition 2, � 5.4.4 Access Control 056 * 057 * @throws ClassNotFoundException if the class referenced by this type can't be found 058 */ 059 public boolean accessibleTo(final ObjectType accessor) throws ClassNotFoundException { 060 final JavaClass jc = Repository.lookupClass(className); 061 if (jc.isPublic()) { 062 return true; 063 } 064 final JavaClass acc = Repository.lookupClass(accessor.className); 065 return acc.getPackageName().equals(jc.getPackageName()); 066 } 067 068 /** 069 * @return true if both type objects refer to the same class. 070 */ 071 @Override 072 public boolean equals(final Object type) { 073 return type instanceof ObjectType && ((ObjectType) type).className.equals(className); 074 } 075 076 /** 077 * @return name of referenced class. 078 */ 079 @Override 080 public String getClassName() { 081 return className; 082 } 083 084 /** 085 * @return a hash code value for the object. 086 */ 087 @Override 088 public int hashCode() { 089 return className.hashCode(); 090 } 091 092 /** 093 * If "this" doesn't reference a class, it references an interface or a non-existant entity. 094 * 095 * @deprecated (since 6.0) this method returns an inaccurate result if the class or interface referenced cannot be 096 * found: use referencesClassExact() instead 097 */ 098 @Deprecated 099 public boolean referencesClass() { 100 try { 101 final JavaClass jc = Repository.lookupClass(className); 102 return jc.isClass(); 103 } catch (final ClassNotFoundException e) { 104 return false; 105 } 106 } 107 108 /** 109 * Return true if this type references a class, false if it references an interface. 110 * 111 * @return true if the type references a class, false if it references an interface. 112 * @throws ClassNotFoundException if the class or interface referenced by this type can't be found 113 */ 114 public boolean referencesClassExact() throws ClassNotFoundException { 115 final JavaClass jc = Repository.lookupClass(className); 116 return jc.isClass(); 117 } 118 119 /** 120 * If "this" doesn't reference an interface, it references a class or a non-existant entity. 121 * 122 * @deprecated (since 6.0) this method returns an inaccurate result if the class or interface referenced cannot be 123 * found: use referencesInterfaceExact() instead 124 */ 125 @Deprecated 126 public boolean referencesInterface() { 127 try { 128 final JavaClass jc = Repository.lookupClass(className); 129 return !jc.isClass(); 130 } catch (final ClassNotFoundException e) { 131 return false; 132 } 133 } 134 135 /** 136 * Return true if this type references an interface, false if it references a class. 137 * 138 * @return true if the type references an interface, false if it references a class. 139 * @throws ClassNotFoundException if the class or interface referenced by this type can't be found 140 */ 141 public boolean referencesInterfaceExact() throws ClassNotFoundException { 142 final JavaClass jc = Repository.lookupClass(className); 143 return !jc.isClass(); 144 } 145 146 /** 147 * Return true if this type is a subclass of given ObjectType. 148 * 149 * @throws ClassNotFoundException if any of this class's superclasses can't be found 150 */ 151 public boolean subclassOf(final ObjectType superclass) throws ClassNotFoundException { 152 if (referencesInterfaceExact() || superclass.referencesInterfaceExact()) { 153 return false; 154 } 155 return Repository.instanceOf(this.className, superclass.className); 156 } 157}