10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang.constant;
26
27 import java.lang.invoke.MethodHandles;
28 import java.util.Optional;
29
30 import static java.lang.constant.ConstantDescs.BSM_INVOKE;
31 import static java.lang.constant.ConstantDescs.MHR_CLASSDESC_FACTORY;
32 import static java.lang.constant.ConstantUtils.dropFirstAndLastChar;
33 import static java.lang.constant.ConstantUtils.internalToBinary;
34 import static java.util.Objects.requireNonNull;
35
36 /**
37 * A <a href="package-summary.html#nominal">nominal descriptor</a> for a class,
38 * interface, or array type. A {@linkplain ReferenceClassDescImpl} corresponds to a
39 * {@code Constant_Class_info} entry in the constant pool of a classfile.
40 */
41 final class ReferenceClassDescImpl implements ClassDesc {
42 private final String descriptor;
43
44 /**
45 * Create a {@linkplain ClassDesc} from a descriptor string for a class or
46 * interface type
47 *
48 * @param descriptor a field descriptor string for a class or interface type,
49 * as per JVMS 4.3.2
50 * @throws IllegalArgumentException if the descriptor string is not a valid
51 * field descriptor string, or does not describe a class or interface type
68 @Override
69 public Class<?> resolveConstantDesc(MethodHandles.Lookup lookup)
70 throws ReflectiveOperationException {
71 ClassDesc c = this;
72 int depth = ConstantUtils.arrayDepth(descriptorString());
73 for (int i=0; i<depth; i++)
74 c = c.componentType();
75
76 if (c.isPrimitive())
77 return lookup.findClass(descriptorString());
78 else {
79 Class<?> clazz = lookup.findClass(internalToBinary(dropFirstAndLastChar(c.descriptorString())));
80 for (int i = 0; i < depth; i++)
81 clazz = clazz.arrayType();
82 return clazz;
83 }
84 }
85
86 @Override
87 public Optional<? extends ConstantDesc<ConstantDesc<Class<?>>>> describeConstable() {
88 return Optional.of(DynamicConstantDesc.of(BSM_INVOKE, MHR_CLASSDESC_FACTORY, descriptor));
89 }
90
91 @Override
92 public boolean equals(Object o) {
93 if (this == o) return true;
94 if (o == null || getClass() != o.getClass()) return false;
95
96 ClassDesc constant = (ClassDesc) o;
97 return descriptor.equals(constant.descriptorString());
98 }
99
100 @Override
101 public int hashCode() {
102 return descriptor.hashCode();
103 }
104
105 @Override
106 public String toString() {
107 return String.format("ClassDesc[%s]", displayName());
108 }
|
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang.constant;
26
27 import java.lang.invoke.MethodHandles;
28 import java.util.Optional;
29
30 import static java.lang.constant.ConstantUtils.dropFirstAndLastChar;
31 import static java.lang.constant.ConstantUtils.internalToBinary;
32 import static java.util.Objects.requireNonNull;
33
34 /**
35 * A <a href="package-summary.html#nominal">nominal descriptor</a> for a class,
36 * interface, or array type. A {@linkplain ReferenceClassDescImpl} corresponds to a
37 * {@code Constant_Class_info} entry in the constant pool of a classfile.
38 */
39 final class ReferenceClassDescImpl implements ClassDesc {
40 private final String descriptor;
41
42 /**
43 * Create a {@linkplain ClassDesc} from a descriptor string for a class or
44 * interface type
45 *
46 * @param descriptor a field descriptor string for a class or interface type,
47 * as per JVMS 4.3.2
48 * @throws IllegalArgumentException if the descriptor string is not a valid
49 * field descriptor string, or does not describe a class or interface type
66 @Override
67 public Class<?> resolveConstantDesc(MethodHandles.Lookup lookup)
68 throws ReflectiveOperationException {
69 ClassDesc c = this;
70 int depth = ConstantUtils.arrayDepth(descriptorString());
71 for (int i=0; i<depth; i++)
72 c = c.componentType();
73
74 if (c.isPrimitive())
75 return lookup.findClass(descriptorString());
76 else {
77 Class<?> clazz = lookup.findClass(internalToBinary(dropFirstAndLastChar(c.descriptorString())));
78 for (int i = 0; i < depth; i++)
79 clazz = clazz.arrayType();
80 return clazz;
81 }
82 }
83
84 @Override
85 public Optional<? extends ConstantDesc<ConstantDesc<Class<?>>>> describeConstable() {
86 return Optional.of(DynamicConstantDesc.ofSymbolic(this));
87 }
88
89 @Override
90 public boolean equals(Object o) {
91 if (this == o) return true;
92 if (o == null || getClass() != o.getClass()) return false;
93
94 ClassDesc constant = (ClassDesc) o;
95 return descriptor.equals(constant.descriptorString());
96 }
97
98 @Override
99 public int hashCode() {
100 return descriptor.hashCode();
101 }
102
103 @Override
104 public String toString() {
105 return String.format("ClassDesc[%s]", displayName());
106 }
|