1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 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.MethodHandle; 28 import java.lang.invoke.MethodHandles; 29 import java.lang.invoke.MethodType; 30 import java.util.Objects; 31 import java.util.Optional; 32 33 import static java.lang.constant.ConstantDescs.CR_DirectMethodHandleDesc; 34 import static java.lang.constant.ConstantDescs.DEFAULT_NAME; 35 import static java.lang.constant.ConstantUtils.validateClassOrInterface; 36 import static java.lang.constant.ConstantUtils.validateMemberName; 37 import static java.lang.constant.DirectMethodHandleDesc.Kind.CONSTRUCTOR; 38 import static java.util.Objects.requireNonNull; 39 40 /** 41 * A <a href="package-summary.html#nominal">nominal descriptor</a> for a direct 42 * {@link MethodHandle}. A {@linkplain DirectMethodHandleDescImpl} corresponds to 43 * a {@code Constant_MethodHandle_info} entry in the constant pool of a classfile. 44 */ 45 final class DirectMethodHandleDescImpl implements DirectMethodHandleDesc { 46 47 private final Kind kind; 48 private final ClassDesc owner; 49 private final String name; 50 private final MethodTypeDesc type; 51 52 /** 53 * Construct a {@linkplain DirectMethodHandleDescImpl} for a method or field 54 * from a kind, owner, name, and type 55 * 56 * @param kind the kind of the method handle 57 * @param owner the declaring class or interface for the method 58 * @param name the name of the method (ignored if {@code kind} is 59 * {@code CONSTRUCTOR}), as per JVMS 4.2.2 60 * @param type the type of the method 61 * @throws NullPointerException if any non-ignored argument is null 62 * @throws IllegalArgumentException if {@code kind} describes a field accessor, 63 * and {@code type} is not consistent with that kind of field accessor, or if 64 * {@code kind} describes a constructor, and the return type of {@code type} 65 * is not {@code void} 66 * @jvms 4.2.2 Unqualified Names 67 */ 68 DirectMethodHandleDescImpl(Kind kind, ClassDesc owner, String name, MethodTypeDesc type) { 69 if (kind == CONSTRUCTOR) 70 name = "<init>"; 71 72 requireNonNull(kind); 73 validateClassOrInterface(requireNonNull(owner)); 74 validateMemberName(requireNonNull(name)); 75 requireNonNull(type); 76 77 switch (kind) { 78 case CONSTRUCTOR: validateConstructor(type); break; 79 case GETTER: validateFieldType(type, false, true); break; 80 case SETTER: validateFieldType(type, true, true); break; 81 case STATIC_GETTER: validateFieldType(type, false, false); break; 82 case STATIC_SETTER: validateFieldType(type, true, false); break; 83 } 84 85 this.kind = kind; 86 this.owner = owner; 87 this.name = name; 88 this.type = type; 89 } 90 91 private static void validateFieldType(MethodTypeDesc type, boolean isSetter, boolean isVirtual) { 92 boolean isVoid = type.returnType().descriptorString().equals("V"); 93 int expectedParams = (isSetter ? 1 : 0) + (isVirtual ? 1 : 0); 94 if (isVoid != isSetter 95 || type.parameterCount() != expectedParams 96 || (isVirtual && type.parameterType(0).isPrimitive())) { 97 String expectedType = String.format("(%s%s)%s", (isVirtual ? "R" : ""), 98 (isSetter ? "T" : ""), (isSetter ? "V" : "T")); 99 throw new IllegalArgumentException(String.format("Expected type of %s for getter, found %s", expectedType, type)); 100 } 101 } 102 103 private static void validateConstructor(MethodTypeDesc type) { 104 if (!type.returnType().descriptorString().equals("V")) { 105 throw new IllegalArgumentException(String.format("Expected type of (T*)V for constructor, found %s", type)); 106 } 107 } 108 109 @Override 110 public Kind kind() { return kind; } 111 112 @Override 113 public int refKind() { return kind.refKind; } 114 115 @Override 116 public boolean isOwnerInterface() { return kind.isInterface; } 117 118 @Override 119 public ClassDesc owner() { 120 return owner; 121 } 122 123 @Override 124 public String methodName() { 125 return name; 126 } 127 128 @Override 129 public MethodTypeDesc methodType() { 130 return type; 131 } 132 133 public MethodHandle resolveConstantDesc(MethodHandles.Lookup lookup) 134 throws ReflectiveOperationException { 135 Class<?> resolvedOwner = owner.resolveConstantDesc(lookup); 136 MethodType resolvedType = this.type.resolveConstantDesc(lookup); 137 switch (kind) { 138 case STATIC: 139 case INTERFACE_STATIC: 140 return lookup.findStatic(resolvedOwner, name, resolvedType); 141 case INTERFACE_VIRTUAL: 142 case VIRTUAL: 143 return lookup.findVirtual(resolvedOwner, name, resolvedType); 144 case SPECIAL: 145 case INTERFACE_SPECIAL: 146 return lookup.findSpecial(resolvedOwner, name, resolvedType, lookup.lookupClass()); 147 case CONSTRUCTOR: 148 return lookup.findConstructor(resolvedOwner, resolvedType); 149 case GETTER: 150 return lookup.findGetter(resolvedOwner, name, resolvedType.returnType()); 151 case STATIC_GETTER: 152 return lookup.findStaticGetter(resolvedOwner, name, resolvedType.returnType()); 153 case SETTER: 154 return lookup.findSetter(resolvedOwner, name, resolvedType.parameterType(1)); 155 case STATIC_SETTER: 156 return lookup.findStaticSetter(resolvedOwner, name, resolvedType.parameterType(0)); 157 default: 158 throw new IllegalStateException(kind.name()); 159 } 160 } 161 162 @Override 163 public Optional<? extends ConstantDesc<ConstantDesc<MethodHandle>>> describeConstable() { 164 return Optional.of(DynamicConstantDesc.ofSymbolic(this)); 165 } 166 167 @Override 168 public boolean equals(Object o) { 169 if (this == o) return true; 170 if (o == null || getClass() != o.getClass()) return false; 171 DirectMethodHandleDescImpl desc = (DirectMethodHandleDescImpl) o; 172 return kind == desc.kind && 173 Objects.equals(owner, desc.owner) && 174 Objects.equals(name, desc.name) && 175 Objects.equals(type, desc.type); 176 } 177 178 @Override 179 public int hashCode() { 180 return Objects.hash(kind, owner, name, type); 181 } 182 183 @Override 184 public String toString() { 185 return String.format("MethodHandleDesc[%s/%s::%s%s]", kind, owner.displayName(), name, type.displayDescriptor()); 186 } 187 }