author | joehw |
Sun, 13 Aug 2017 21:10:40 -0700 | |
changeset 46174 | 5611d2529b49 |
parent 44797 | 8b3b3b911b8a |
permissions | -rw-r--r-- |
6 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
5 |
/* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
12 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
14 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
15 |
* Unless required by applicable law or agreed to in writing, software |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
18 |
* See the License for the specific language governing permissions and |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
19 |
* limitations under the License. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
20 |
*/ |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
21 |
|
6 | 22 |
package com.sun.org.apache.bcel.internal.generic; |
23 |
||
46174 | 24 |
import com.sun.org.apache.bcel.internal.Const; |
6 | 25 |
import com.sun.org.apache.bcel.internal.Repository; |
26 |
import com.sun.org.apache.bcel.internal.classfile.JavaClass; |
|
27 |
||
28 |
/** |
|
29 |
* Denotes reference such as java.lang.String. |
|
30 |
* |
|
46174 | 31 |
* @version $Id: ObjectType.java 1749603 2016-06-21 20:50:19Z ggregory $ |
6 | 32 |
*/ |
46174 | 33 |
public class ObjectType extends ReferenceType { |
34 |
||
35 |
private final String class_name; // Class name of type |
|
36 |
||
37 |
/** |
|
38 |
* @since 6.0 |
|
39 |
*/ |
|
40 |
public static ObjectType getInstance(final String class_name) { |
|
41 |
return new ObjectType(class_name); |
|
42 |
} |
|
6 | 43 |
|
46174 | 44 |
/** |
45 |
* @param class_name fully qualified class name, e.g. java.lang.String |
|
46 |
*/ |
|
47 |
public ObjectType(final String class_name) { |
|
48 |
super(Const.T_REFERENCE, "L" + class_name.replace('.', '/') + ";"); |
|
49 |
this.class_name = class_name.replace('/', '.'); |
|
50 |
} |
|
51 |
||
52 |
||
53 |
/** @return name of referenced class |
|
54 |
*/ |
|
55 |
public String getClassName() { |
|
56 |
return class_name; |
|
57 |
} |
|
58 |
||
6 | 59 |
|
46174 | 60 |
/** @return a hash code value for the object. |
61 |
*/ |
|
62 |
@Override |
|
63 |
public int hashCode() { |
|
64 |
return class_name.hashCode(); |
|
65 |
} |
|
66 |
||
67 |
||
68 |
/** @return true if both type objects refer to the same class. |
|
69 |
*/ |
|
70 |
@Override |
|
71 |
public boolean equals( final Object type ) { |
|
72 |
return (type instanceof ObjectType) |
|
73 |
? ((ObjectType) type).class_name.equals(class_name) |
|
74 |
: false; |
|
75 |
} |
|
76 |
||
6 | 77 |
|
46174 | 78 |
/** |
79 |
* If "this" doesn't reference a class, it references an interface |
|
80 |
* or a non-existant entity. |
|
81 |
* @deprecated (since 6.0) this method returns an inaccurate result |
|
82 |
* if the class or interface referenced cannot |
|
83 |
* be found: use referencesClassExact() instead |
|
84 |
*/ |
|
85 |
@Deprecated |
|
86 |
public boolean referencesClass() { |
|
87 |
try { |
|
88 |
final JavaClass jc = Repository.lookupClass(class_name); |
|
89 |
return jc.isClass(); |
|
90 |
} catch (final ClassNotFoundException e) { |
|
91 |
return false; |
|
92 |
} |
|
93 |
} |
|
6 | 94 |
|
95 |
||
46174 | 96 |
/** |
97 |
* If "this" doesn't reference an interface, it references a class |
|
98 |
* or a non-existant entity. |
|
99 |
* @deprecated (since 6.0) this method returns an inaccurate result |
|
100 |
* if the class or interface referenced cannot |
|
101 |
* be found: use referencesInterfaceExact() instead |
|
102 |
*/ |
|
103 |
@Deprecated |
|
104 |
public boolean referencesInterface() { |
|
105 |
try { |
|
106 |
final JavaClass jc = Repository.lookupClass(class_name); |
|
107 |
return !jc.isClass(); |
|
108 |
} catch (final ClassNotFoundException e) { |
|
109 |
return false; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
||
114 |
/** |
|
115 |
* Return true if this type references a class, |
|
116 |
* false if it references an interface. |
|
117 |
* @return true if the type references a class, false if |
|
118 |
* it references an interface |
|
119 |
* @throws ClassNotFoundException if the class or interface |
|
120 |
* referenced by this type can't be found |
|
121 |
*/ |
|
122 |
public boolean referencesClassExact() throws ClassNotFoundException { |
|
123 |
final JavaClass jc = Repository.lookupClass(class_name); |
|
124 |
return jc.isClass(); |
|
125 |
} |
|
126 |
||
6 | 127 |
|
46174 | 128 |
/** |
129 |
* Return true if this type references an interface, |
|
130 |
* false if it references a class. |
|
131 |
* @return true if the type references an interface, false if |
|
132 |
* it references a class |
|
133 |
* @throws ClassNotFoundException if the class or interface |
|
134 |
* referenced by this type can't be found |
|
135 |
*/ |
|
136 |
public boolean referencesInterfaceExact() throws ClassNotFoundException { |
|
137 |
final JavaClass jc = Repository.lookupClass(class_name); |
|
138 |
return !jc.isClass(); |
|
139 |
} |
|
6 | 140 |
|
141 |
||
46174 | 142 |
/** |
143 |
* Return true if this type is a subclass of given ObjectType. |
|
144 |
* @throws ClassNotFoundException if any of this class's superclasses |
|
145 |
* can't be found |
|
146 |
*/ |
|
147 |
public boolean subclassOf( final ObjectType superclass ) throws ClassNotFoundException { |
|
148 |
if (this.referencesInterfaceExact() || superclass.referencesInterfaceExact()) { |
|
149 |
return false; |
|
150 |
} |
|
151 |
return Repository.instanceOf(this.class_name, superclass.class_name); |
|
152 |
} |
|
6 | 153 |
|
154 |
||
46174 | 155 |
/** |
156 |
* Java Virtual Machine Specification edition 2, 5.4.4 Access Control |
|
157 |
* @throws ClassNotFoundException if the class referenced by this type |
|
158 |
* can't be found |
|
159 |
*/ |
|
160 |
public boolean accessibleTo( final ObjectType accessor ) throws ClassNotFoundException { |
|
161 |
final JavaClass jc = Repository.lookupClass(class_name); |
|
162 |
if (jc.isPublic()) { |
|
163 |
return true; |
|
164 |
} |
|
165 |
final JavaClass acc = Repository.lookupClass(accessor.class_name); |
|
166 |
return acc.getPackageName().equals(jc.getPackageName()); |
|
6 | 167 |
} |
168 |
} |