1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.generic;
18
19 import org.apache.bcel.Constants;
20 import org.apache.bcel.Repository;
21 import org.apache.bcel.classfile.JavaClass;
22
23 /***
24 * Denotes reference such as java.lang.String.
25 *
26 * @version $Id: ObjectType.java 386056 2006-03-15 11:31:56Z tcurdt $
27 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
28 */
29 public class ObjectType extends ReferenceType {
30
31 private String class_name;
32
33
34 /***
35 * @param class_name fully qualified class name, e.g. java.lang.String
36 */
37 public ObjectType(String class_name) {
38 super(Constants.T_REFERENCE, "L" + class_name.replace('.', '/') + ";");
39 this.class_name = class_name.replace('/', '.');
40 }
41
42
43 /*** @return name of referenced class
44 */
45 public String getClassName() {
46 return class_name;
47 }
48
49
50 /*** @return a hash code value for the object.
51 */
52 public int hashCode() {
53 return class_name.hashCode();
54 }
55
56
57 /*** @return true if both type objects refer to the same class.
58 */
59 public boolean equals( Object type ) {
60 return (type instanceof ObjectType)
61 ? ((ObjectType) type).class_name.equals(class_name)
62 : false;
63 }
64
65
66 /***
67 * If "this" doesn't reference a class, it references an interface
68 * or a non-existant entity.
69 * @deprecated this method returns an inaccurate result
70 * if the class or interface referenced cannot
71 * be found: use referencesClassExact() instead
72 */
73 public boolean referencesClass() {
74 try {
75 JavaClass jc = Repository.lookupClass(class_name);
76 return jc.isClass();
77 } catch (ClassNotFoundException e) {
78 return false;
79 }
80 }
81
82
83 /***
84 * If "this" doesn't reference an interface, it references a class
85 * or a non-existant entity.
86 * @deprecated this method returns an inaccurate result
87 * if the class or interface referenced cannot
88 * be found: use referencesInterfaceExact() instead
89 */
90 public boolean referencesInterface() {
91 try {
92 JavaClass jc = Repository.lookupClass(class_name);
93 return !jc.isClass();
94 } catch (ClassNotFoundException e) {
95 return false;
96 }
97 }
98
99
100 /***
101 * Return true if this type references a class,
102 * false if it references an interface.
103 * @return true if the type references a class, false if
104 * it references an interface
105 * @throws ClassNotFoundException if the class or interface
106 * referenced by this type can't be found
107 */
108 public boolean referencesClassExact() throws ClassNotFoundException {
109 JavaClass jc = Repository.lookupClass(class_name);
110 return jc.isClass();
111 }
112
113
114 /***
115 * Return true if this type references an interface,
116 * false if it references a class.
117 * @return true if the type references an interface, false if
118 * it references a class
119 * @throws ClassNotFoundException if the class or interface
120 * referenced by this type can't be found
121 */
122 public boolean referencesInterfaceExact() throws ClassNotFoundException {
123 JavaClass jc = Repository.lookupClass(class_name);
124 return !jc.isClass();
125 }
126
127
128 /***
129 * Return true if this type is a subclass of given ObjectType.
130 * @throws ClassNotFoundException if any of this class's superclasses
131 * can't be found
132 */
133 public boolean subclassOf( ObjectType superclass ) throws ClassNotFoundException {
134 if (this.referencesInterface() || superclass.referencesInterface()) {
135 return false;
136 }
137 return Repository.instanceOf(this.class_name, superclass.class_name);
138 }
139
140
141 /***
142 * Java Virtual Machine Specification edition 2, ß 5.4.4 Access Control
143 * @throws ClassNotFoundException if the class referenced by this type
144 * can't be found
145 */
146 public boolean accessibleTo( ObjectType accessor ) throws ClassNotFoundException {
147 JavaClass jc = Repository.lookupClass(class_name);
148 if (jc.isPublic()) {
149 return true;
150 } else {
151 JavaClass acc = Repository.lookupClass(accessor.class_name);
152 return acc.getPackageName().equals(jc.getPackageName());
153 }
154 }
155 }