1 /* 2 * Copyright (c) 2024, 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 26 package java.lang.reflect.code.type.impl; 27 28 import java.lang.reflect.code.type.FieldRef; 29 import java.lang.invoke.MethodHandles; 30 import java.lang.invoke.VarHandle; 31 import java.lang.reflect.Field; 32 import java.lang.reflect.code.type.JavaType; 33 import java.lang.reflect.code.TypeElement; 34 35 public final class FieldRefImpl implements FieldRef { 36 final TypeElement refType; 37 final String name; 38 final TypeElement type; 39 40 public FieldRefImpl(TypeElement refType, String name, TypeElement type) { 41 this.refType = refType; 42 this.name = name; 43 this.type = type; 44 } 45 46 @Override 47 public TypeElement refType() { 48 return refType; 49 } 50 51 @Override 52 public String name() { 53 return name; 54 } 55 56 @Override 57 public TypeElement type() { 58 return type; 59 } 60 61 @Override 62 public Field resolveToMember(MethodHandles.Lookup l) throws ReflectiveOperationException { 63 Class<?> refC = resolve(l, refType); 64 Class<?> typeC = resolve(l, type); 65 66 Field f = refC.getDeclaredField(name); 67 if (!f.getType().equals(typeC)) { 68 throw new NoSuchFieldException(); 69 } 70 71 return f; 72 } 73 74 @Override 75 public VarHandle resolveToHandle(MethodHandles.Lookup l) throws ReflectiveOperationException { 76 Class<?> refC = resolve(l, refType); 77 Class<?> typeC = resolve(l, type); 78 79 VarHandle vh = null; 80 ReflectiveOperationException c = null; 81 82 try { 83 vh = l.findStaticVarHandle(refC, name, typeC); 84 } catch (NoSuchFieldException | IllegalAccessException e) { 85 c = e; 86 } 87 88 if (c != null) { 89 c = null; 90 try { 91 vh = l.findVarHandle(refC, name, typeC); 92 } catch (NoSuchFieldException | IllegalAccessException e) { 93 c = e; 94 } 95 } 96 97 if (c != null) { 98 throw c; 99 } 100 101 assert vh != null; 102 return vh; 103 } 104 105 static Class<?> resolve(MethodHandles.Lookup l, TypeElement t) throws ReflectiveOperationException { 106 if (t instanceof JavaType jt) { 107 return (Class<?>)jt.erasure().resolve(l); 108 } else { 109 // @@@ 110 throw new ReflectiveOperationException(); 111 } 112 } 113 114 @Override 115 public String toString() { 116 return refType + "::" + name + "()" + type; 117 } 118 119 @Override 120 public boolean equals(Object o) { 121 if (this == o) return true; 122 if (o == null || getClass() != o.getClass()) return false; 123 124 FieldRefImpl fieldDesc = (FieldRefImpl) o; 125 126 if (!refType.equals(fieldDesc.refType)) return false; 127 if (!name.equals(fieldDesc.name)) return false; 128 return type.equals(fieldDesc.type); 129 } 130 131 @Override 132 public int hashCode() { 133 int result = refType.hashCode(); 134 result = 31 * result + name.hashCode(); 135 result = 31 * result + type.hashCode(); 136 return result; 137 } 138 }