1 /*
   2  * Copyright (c) 2011, 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.vm.ci.hotspot;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
  26 import static jdk.vm.ci.hotspot.HotSpotVMConfig.config;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.reflect.Field;
  30 
  31 import jdk.internal.vm.annotation.Stable;
  32 import jdk.vm.ci.common.JVMCIError;
  33 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option;
  34 import jdk.vm.ci.meta.JavaType;
  35 import jdk.vm.ci.meta.MetaAccessProvider;
  36 import jdk.vm.ci.meta.ModifiersProvider;
  37 import jdk.vm.ci.meta.ResolvedJavaField;
  38 import jdk.vm.ci.meta.ResolvedJavaType;
  39 
  40 /**
  41  * Represents a field in a HotSpot type.
  42  */
  43 class HotSpotResolvedJavaFieldImpl implements HotSpotResolvedJavaField, HotSpotProxified {
  44 
  45     private final HotSpotResolvedObjectTypeImpl holder;
  46     private final String name;
  47     private JavaType type;
  48     private final int offset;
  49 
  50     /**
  51      * This value contains all flags as stored in the VM including internal ones.
  52      */
  53     private final int modifiers;
  54 
  55     HotSpotResolvedJavaFieldImpl(HotSpotResolvedObjectTypeImpl holder, String name, JavaType type, long offset, int modifiers) {
  56         this.holder = holder;
  57         this.name = name;
  58         this.type = type;
  59         assert offset != -1;
  60         assert offset == (int) offset : "offset larger than int";
  61         this.offset = (int) offset;
  62         this.modifiers = modifiers;
  63     }
  64 
  65     @Override
  66     public boolean equals(Object obj) {
  67         if (this == obj) {
  68             return true;
  69         }
  70         if (obj instanceof HotSpotResolvedJavaField) {
  71             HotSpotResolvedJavaFieldImpl that = (HotSpotResolvedJavaFieldImpl) obj;
  72             if (that.offset != this.offset || that.isStatic() != this.isStatic()) {
  73                 return false;
  74             } else if (this.holder.equals(that.holder)) {
  75                 assert this.name.equals(that.name) && this.type.equals(that.type);
  76                 return true;
  77             }
  78         }
  79         return false;
  80     }
  81 
  82     @Override
  83     public int hashCode() {
  84         return name.hashCode();
  85     }
  86 
  87     @Override
  88     public int getModifiers() {
  89         return modifiers & ModifiersProvider.jvmFieldModifiers();
  90     }
  91 
  92     @Override
  93     public boolean isInternal() {
  94         return (modifiers & config().jvmAccFieldInternal) != 0;
  95     }
  96 
  97     /**
  98      * Determines if a given object contains this field.
  99      *
 100      * @return true iff this is a non-static field and its declaring class is assignable from
 101      *         {@code object}'s class
 102      */
 103     public boolean isInObject(Object object) {
 104         if (isStatic()) {
 105             return false;
 106         }
 107         return getDeclaringClass().isAssignableFrom(HotSpotResolvedObjectTypeImpl.fromObjectClass(object.getClass()));
 108     }
 109 
 110     @Override
 111     public HotSpotResolvedObjectTypeImpl getDeclaringClass() {
 112         return holder;
 113     }
 114 
 115     @Override
 116     public String getName() {
 117         return name;
 118     }
 119 
 120     @Override
 121     public JavaType getType() {
 122         // Pull field into local variable to prevent a race causing
 123         // a ClassCastException below
 124         JavaType currentType = type;
 125         if (currentType instanceof HotSpotUnresolvedJavaType) {
 126             // Don't allow unresolved types to hang around forever
 127             HotSpotUnresolvedJavaType unresolvedType = (HotSpotUnresolvedJavaType) currentType;
 128             ResolvedJavaType resolved = unresolvedType.reresolve(holder);
 129             if (resolved != null) {
 130                 type = resolved;
 131             }
 132         }
 133         return type;
 134     }
 135 
 136     public int offset() {
 137         return offset;
 138     }
 139 
 140     @Override
 141     public String toString() {
 142         return format("HotSpotField<%H.%n %t:") + offset + ">";
 143     }
 144 
 145     @Override
 146     public boolean isSynthetic() {
 147         return (config().jvmAccSynthetic & modifiers) != 0;
 148     }
 149 
 150     /**
 151      * Checks if this field has the {@link Stable} annotation.
 152      *
 153      * @return true if field has {@link Stable} annotation, false otherwise
 154      */
 155     public boolean isStable() {
 156         if ((config().jvmAccFieldStable & modifiers) != 0) {
 157             return true;
 158         }
 159         assert getAnnotation(Stable.class) == null;
 160         if (Option.ImplicitStableValues.getBoolean() && isImplicitStableField()) {
 161             return true;
 162         }
 163         return false;
 164     }
 165 
 166     @Override
 167     public Annotation[] getAnnotations() {
 168         Field javaField = toJava();
 169         if (javaField != null) {
 170             return javaField.getAnnotations();
 171         }
 172         return new Annotation[0];
 173     }
 174 
 175     @Override
 176     public Annotation[] getDeclaredAnnotations() {
 177         Field javaField = toJava();
 178         if (javaField != null) {
 179             return javaField.getDeclaredAnnotations();
 180         }
 181         return new Annotation[0];
 182     }
 183 
 184     @Override
 185     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
 186         Field javaField = toJava();
 187         if (javaField != null) {
 188             return javaField.getAnnotation(annotationClass);
 189         }
 190         return null;
 191     }
 192 
 193     private Field toJavaCache;
 194 
 195     private Field toJava() {
 196         if (toJavaCache != null) {
 197             return toJavaCache;
 198         }
 199 
 200         if (isInternal()) {
 201             return null;
 202         }
 203         try {
 204             return toJavaCache = holder.mirror().getDeclaredField(name);
 205         } catch (NoSuchFieldException | NoClassDefFoundError e) {
 206             return null;
 207         }
 208     }
 209 
 210     private boolean isArray() {
 211         JavaType fieldType = getType();
 212         return fieldType instanceof ResolvedJavaType && ((ResolvedJavaType) fieldType).isArray();
 213     }
 214 
 215     private boolean isImplicitStableField() {
 216         if (isSyntheticEnumSwitchMap()) {
 217             return true;
 218         }
 219         if (isWellKnownImplicitStableField()) {
 220             return true;
 221         }
 222         return false;
 223     }
 224 
 225     public boolean isDefaultStable() {
 226         assert this.isStable();
 227         if (isSyntheticEnumSwitchMap()) {
 228             return true;
 229         }
 230         return false;
 231     }
 232 
 233     private boolean isSyntheticEnumSwitchMap() {
 234         if (isSynthetic() && isStatic() && isArray()) {
 235             if (isFinal() && name.equals("$VALUES") || name.equals("ENUM$VALUES")) {
 236                 // generated int[] field for EnumClass::values()
 237                 return true;
 238             } else if (name.startsWith("$SwitchMap$") || name.startsWith("$SWITCH_TABLE$")) {
 239                 // javac and ecj generate a static field in an inner class for a switch on an enum
 240                 // named $SwitchMap$p$k$g$EnumClass and $SWITCH_TABLE$p$k$g$EnumClass, respectively
 241                 return true;
 242             }
 243         }
 244         return false;
 245     }
 246 
 247     private boolean isWellKnownImplicitStableField() {
 248         return WellKnownImplicitStableField.test(this);
 249     }
 250 
 251     static class WellKnownImplicitStableField {
 252         /**
 253          * @return {@code true} if the field is a well-known stable field.
 254          */
 255         public static boolean test(HotSpotResolvedJavaField field) {
 256             return field.equals(STRING_VALUE_FIELD);
 257         }
 258 
 259         private static final ResolvedJavaField STRING_VALUE_FIELD;
 260 
 261         static {
 262             try {
 263                 MetaAccessProvider metaAccess = runtime().getHostJVMCIBackend().getMetaAccess();
 264                 STRING_VALUE_FIELD = metaAccess.lookupJavaField(String.class.getDeclaredField("value"));
 265             } catch (SecurityException | NoSuchFieldException e) {
 266                 throw new JVMCIError(e);
 267             }
 268         }
 269     }
 270 }