< prev index next >

src/jdk.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotResolvedJavaFieldImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 2014, 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.*;
  26 import static jdk.vm.ci.hotspot.HotSpotResolvedJavaFieldImpl.Options.*;
  27 
  28 import java.lang.annotation.*;
  29 import java.lang.reflect.*;
  30 
  31 import jdk.vm.ci.common.*;
  32 import jdk.vm.ci.meta.*;
  33 import jdk.vm.ci.options.*;







  34 
  35 /**
  36  * Represents a field in a HotSpot type.
  37  */
  38 public class HotSpotResolvedJavaFieldImpl implements HotSpotResolvedJavaField, HotSpotProxified {
  39 
  40     static class Options {
  41         //@formatter:off
  42         @Option(help = "Mark well-known stable fields as such.", type = OptionType.Debug)
  43         public static final OptionValue<Boolean> ImplicitStableValues = new OptionValue<>(true);
  44         //@formatter:on
  45     }
  46 
  47     private final HotSpotResolvedObjectTypeImpl holder;
  48     private final String name;
  49     private JavaType type;
  50     private final int offset;
  51 
  52     /**
  53      * This value contains all flags as stored in the VM including internal ones.
  54      */
  55     private final int modifiers;
  56     private final LocationIdentity locationIdentity = new FieldLocationIdentity(this);
  57 
  58     public static class FieldLocationIdentity extends LocationIdentity {


  74             }
  75             if (obj instanceof FieldLocationIdentity) {
  76                 FieldLocationIdentity fieldLocationIdentity = (FieldLocationIdentity) obj;
  77                 return inner.equals(fieldLocationIdentity.inner);
  78 
  79             }
  80             return false;
  81         }
  82 
  83         @Override
  84         public int hashCode() {
  85             return inner.hashCode();
  86         }
  87 
  88         @Override
  89         public String toString() {
  90             return inner.getName();
  91         }
  92     }
  93 
  94     public HotSpotResolvedJavaFieldImpl(HotSpotResolvedObjectTypeImpl holder, String name, JavaType type, long offset, int modifiers) {
  95         this.holder = holder;
  96         this.name = name;
  97         this.type = type;
  98         assert offset != -1;
  99         assert offset == (int) offset : "offset larger than int";
 100         this.offset = (int) offset;
 101         this.modifiers = modifiers;
 102     }
 103 
 104     @Override
 105     public boolean equals(Object obj) {
 106         if (this == obj) {
 107             return true;
 108         }
 109         if (obj instanceof HotSpotResolvedJavaField) {
 110             HotSpotResolvedJavaFieldImpl that = (HotSpotResolvedJavaFieldImpl) obj;
 111             if (that.offset != this.offset || that.isStatic() != this.isStatic()) {
 112                 return false;
 113             } else if (this.holder.equals(that.holder)) {
 114                 assert this.name.equals(that.name) && this.type.equals(that.type);
 115                 return true;
 116             }
 117         }
 118         return false;
 119     }
 120 
 121     @Override
 122     public int hashCode() {
 123         return name.hashCode();
 124     }
 125 
 126     @Override
 127     public int getModifiers() {
 128         return modifiers & ModifiersProvider.jvmFieldModifiers();
 129     }
 130 
 131     @Override
 132     public boolean isInternal() {
 133         return (modifiers & runtime().getConfig().jvmAccFieldInternal) != 0;
 134     }
 135 
 136     /**
 137      * Determines if a given object contains this field.
 138      *
 139      * @return true iff this is a non-static field and its declaring class is assignable from
 140      *         {@code object}'s class
 141      */
 142     public boolean isInObject(Object object) {
 143         if (isStatic()) {
 144             return false;
 145         }
 146         return getDeclaringClass().isAssignableFrom(HotSpotResolvedObjectTypeImpl.fromObjectClass(object.getClass()));
 147     }
 148 
 149     @Override
 150     public HotSpotResolvedObjectTypeImpl getDeclaringClass() {
 151         return holder;
 152     }
 153 


 166             HotSpotUnresolvedJavaType unresolvedType = (HotSpotUnresolvedJavaType) currentType;
 167             ResolvedJavaType resolved = unresolvedType.reresolve(holder);
 168             if (resolved != null) {
 169                 type = resolved;
 170             }
 171         }
 172         return type;
 173     }
 174 
 175     public int offset() {
 176         return offset;
 177     }
 178 
 179     @Override
 180     public String toString() {
 181         return format("HotSpotField<%H.%n %t:") + offset + ">";
 182     }
 183 
 184     @Override
 185     public boolean isSynthetic() {
 186         return (runtime().getConfig().syntheticFlag & modifiers) != 0;
 187     }
 188 
 189     /**
 190      * Checks if this field has the {@link Stable} annotation.
 191      *
 192      * @return true if field has {@link Stable} annotation, false otherwise
 193      */
 194     public boolean isStable() {
 195         if ((runtime().getConfig().jvmAccFieldStable & modifiers) != 0) {
 196             return true;
 197         }
 198         assert getAnnotation(Stable.class) == null;
 199         if (ImplicitStableValues.getValue() && isImplicitStableField()) {
 200             return true;
 201         }
 202         return false;
 203     }
 204 
 205     @Override
 206     public Annotation[] getAnnotations() {
 207         Field javaField = toJava();
 208         if (javaField != null) {
 209             return javaField.getAnnotations();
 210         }
 211         return new Annotation[0];
 212     }
 213 
 214     @Override
 215     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
 216         Field javaField = toJava();
 217         if (javaField != null) {
 218             return javaField.getAnnotation(annotationClass);
 219         }


 226         if (toJavaCache != null) {
 227             return toJavaCache;
 228         }
 229 
 230         if (isInternal()) {
 231             return null;
 232         }
 233         try {
 234             return toJavaCache = holder.mirror().getDeclaredField(name);
 235         } catch (NoSuchFieldException | NoClassDefFoundError e) {
 236             return null;
 237         }
 238     }
 239 
 240     private boolean isArray() {
 241         JavaType fieldType = getType();
 242         return fieldType instanceof ResolvedJavaType && ((ResolvedJavaType) fieldType).isArray();
 243     }
 244 
 245     private boolean isImplicitStableField() {
 246         if (isSynthetic()) {
 247             if (isSyntheticImplicitStableField()) {
 248                 return true;
 249             }
 250         } else if (isWellKnownImplicitStableField()) {
 251             return true;
 252         }
 253         return false;
 254     }
 255 
 256     private boolean isSyntheticImplicitStableField() {
 257         assert this.isSynthetic();
 258         if (isStatic() && isArray()) {







 259             if (isFinal() && name.equals("$VALUES") || name.equals("ENUM$VALUES")) {
 260                 // generated int[] field for EnumClass::values()
 261                 return true;
 262             } else if (name.startsWith("$SwitchMap$") || name.startsWith("$SWITCH_TABLE$")) {
 263                 // javac and ecj generate a static field in an inner class for a switch on an enum
 264                 // named $SwitchMap$p$k$g$EnumClass and $SWITCH_TABLE$p$k$g$EnumClass, respectively
 265                 return true;
 266             }
 267         }
 268         return false;
 269     }
 270 
 271     private boolean isWellKnownImplicitStableField() {
 272         return WellKnownImplicitStableField.test(this);
 273     }
 274 
 275     static class WellKnownImplicitStableField {
 276         /**
 277          * @return {@code true} if the field is a well-known stable field.
 278          */
 279         public static boolean test(HotSpotResolvedJavaField field) {
 280             return field.equals(STRING_VALUE_FIELD);
 281         }
 282 
 283         private static final ResolvedJavaField STRING_VALUE_FIELD;

 284         static {
 285             try {
 286                 MetaAccessProvider metaAccess = runtime().getHostJVMCIBackend().getMetaAccess();
 287                 STRING_VALUE_FIELD = metaAccess.lookupJavaField(String.class.getDeclaredField("value"));
 288             } catch (SecurityException | NoSuchFieldException e) {
 289                 throw new JVMCIError(e);
 290             }
 291         }
 292     }
 293 
 294     public LocationIdentity getLocationIdentity() {
 295         return locationIdentity;
 296     }
 297 }
   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.vm.ci.common.JVMCIError;
  32 import jdk.vm.ci.meta.JavaType;
  33 import jdk.vm.ci.meta.LocationIdentity;
  34 import jdk.vm.ci.meta.MetaAccessProvider;
  35 import jdk.vm.ci.meta.ModifiersProvider;
  36 import jdk.vm.ci.meta.ResolvedJavaField;
  37 import jdk.vm.ci.meta.ResolvedJavaType;
  38 import jdk.vm.ci.options.Option;
  39 import jdk.vm.ci.options.OptionType;
  40 import jdk.vm.ci.options.OptionValue;
  41 
  42 /**
  43  * Represents a field in a HotSpot type.
  44  */
  45 class HotSpotResolvedJavaFieldImpl implements HotSpotResolvedJavaField, HotSpotProxified {
  46 
  47     static class Options {
  48         //@formatter:off
  49         @Option(help = "Mark well-known stable fields as such.", type = OptionType.Debug)
  50         public static final OptionValue<Boolean> ImplicitStableValues = new OptionValue<>(true);
  51         //@formatter:on
  52     }
  53 
  54     private final HotSpotResolvedObjectTypeImpl holder;
  55     private final String name;
  56     private JavaType type;
  57     private final int offset;
  58 
  59     /**
  60      * This value contains all flags as stored in the VM including internal ones.
  61      */
  62     private final int modifiers;
  63     private final LocationIdentity locationIdentity = new FieldLocationIdentity(this);
  64 
  65     public static class FieldLocationIdentity extends LocationIdentity {


  81             }
  82             if (obj instanceof FieldLocationIdentity) {
  83                 FieldLocationIdentity fieldLocationIdentity = (FieldLocationIdentity) obj;
  84                 return inner.equals(fieldLocationIdentity.inner);
  85 
  86             }
  87             return false;
  88         }
  89 
  90         @Override
  91         public int hashCode() {
  92             return inner.hashCode();
  93         }
  94 
  95         @Override
  96         public String toString() {
  97             return inner.getName();
  98         }
  99     }
 100 
 101     HotSpotResolvedJavaFieldImpl(HotSpotResolvedObjectTypeImpl holder, String name, JavaType type, long offset, int modifiers) {
 102         this.holder = holder;
 103         this.name = name;
 104         this.type = type;
 105         assert offset != -1;
 106         assert offset == (int) offset : "offset larger than int";
 107         this.offset = (int) offset;
 108         this.modifiers = modifiers;
 109     }
 110 
 111     @Override
 112     public boolean equals(Object obj) {
 113         if (this == obj) {
 114             return true;
 115         }
 116         if (obj instanceof HotSpotResolvedJavaField) {
 117             HotSpotResolvedJavaFieldImpl that = (HotSpotResolvedJavaFieldImpl) obj;
 118             if (that.offset != this.offset || that.isStatic() != this.isStatic()) {
 119                 return false;
 120             } else if (this.holder.equals(that.holder)) {
 121                 assert this.name.equals(that.name) && this.type.equals(that.type);
 122                 return true;
 123             }
 124         }
 125         return false;
 126     }
 127 
 128     @Override
 129     public int hashCode() {
 130         return name.hashCode();
 131     }
 132 
 133     @Override
 134     public int getModifiers() {
 135         return modifiers & ModifiersProvider.jvmFieldModifiers();
 136     }
 137 
 138     @Override
 139     public boolean isInternal() {
 140         return (modifiers & config().jvmAccFieldInternal) != 0;
 141     }
 142 
 143     /**
 144      * Determines if a given object contains this field.
 145      *
 146      * @return true iff this is a non-static field and its declaring class is assignable from
 147      *         {@code object}'s class
 148      */
 149     public boolean isInObject(Object object) {
 150         if (isStatic()) {
 151             return false;
 152         }
 153         return getDeclaringClass().isAssignableFrom(HotSpotResolvedObjectTypeImpl.fromObjectClass(object.getClass()));
 154     }
 155 
 156     @Override
 157     public HotSpotResolvedObjectTypeImpl getDeclaringClass() {
 158         return holder;
 159     }
 160 


 173             HotSpotUnresolvedJavaType unresolvedType = (HotSpotUnresolvedJavaType) currentType;
 174             ResolvedJavaType resolved = unresolvedType.reresolve(holder);
 175             if (resolved != null) {
 176                 type = resolved;
 177             }
 178         }
 179         return type;
 180     }
 181 
 182     public int offset() {
 183         return offset;
 184     }
 185 
 186     @Override
 187     public String toString() {
 188         return format("HotSpotField<%H.%n %t:") + offset + ">";
 189     }
 190 
 191     @Override
 192     public boolean isSynthetic() {
 193         return (config().syntheticFlag & modifiers) != 0;
 194     }
 195 
 196     /**
 197      * Checks if this field has the {@link Stable} annotation.
 198      *
 199      * @return true if field has {@link Stable} annotation, false otherwise
 200      */
 201     public boolean isStable() {
 202         if ((config().jvmAccFieldStable & modifiers) != 0) {
 203             return true;
 204         }
 205         assert getAnnotation(Stable.class) == null;
 206         if (Options.ImplicitStableValues.getValue() && isImplicitStableField()) {
 207             return true;
 208         }
 209         return false;
 210     }
 211 
 212     @Override
 213     public Annotation[] getAnnotations() {
 214         Field javaField = toJava();
 215         if (javaField != null) {
 216             return javaField.getAnnotations();
 217         }
 218         return new Annotation[0];
 219     }
 220 
 221     @Override
 222     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
 223         Field javaField = toJava();
 224         if (javaField != null) {
 225             return javaField.getAnnotation(annotationClass);
 226         }


 233         if (toJavaCache != null) {
 234             return toJavaCache;
 235         }
 236 
 237         if (isInternal()) {
 238             return null;
 239         }
 240         try {
 241             return toJavaCache = holder.mirror().getDeclaredField(name);
 242         } catch (NoSuchFieldException | NoClassDefFoundError e) {
 243             return null;
 244         }
 245     }
 246 
 247     private boolean isArray() {
 248         JavaType fieldType = getType();
 249         return fieldType instanceof ResolvedJavaType && ((ResolvedJavaType) fieldType).isArray();
 250     }
 251 
 252     private boolean isImplicitStableField() {
 253         if (isSyntheticEnumSwitchMap()) {

 254             return true;
 255         }
 256         if (isWellKnownImplicitStableField()) {
 257             return true;
 258         }
 259         return false;
 260     }
 261 
 262     public boolean isDefaultStable() {
 263         assert this.isStable();
 264         if (isSyntheticEnumSwitchMap()) {
 265             return true;
 266         }
 267         return false;
 268     }
 269 
 270     private boolean isSyntheticEnumSwitchMap() {
 271         if (isSynthetic() && isStatic() && isArray()) {
 272             if (isFinal() && name.equals("$VALUES") || name.equals("ENUM$VALUES")) {
 273                 // generated int[] field for EnumClass::values()
 274                 return true;
 275             } else if (name.startsWith("$SwitchMap$") || name.startsWith("$SWITCH_TABLE$")) {
 276                 // javac and ecj generate a static field in an inner class for a switch on an enum
 277                 // named $SwitchMap$p$k$g$EnumClass and $SWITCH_TABLE$p$k$g$EnumClass, respectively
 278                 return true;
 279             }
 280         }
 281         return false;
 282     }
 283 
 284     private boolean isWellKnownImplicitStableField() {
 285         return WellKnownImplicitStableField.test(this);
 286     }
 287 
 288     static class WellKnownImplicitStableField {
 289         /**
 290          * @return {@code true} if the field is a well-known stable field.
 291          */
 292         public static boolean test(HotSpotResolvedJavaField field) {
 293             return field.equals(STRING_VALUE_FIELD);
 294         }
 295 
 296         private static final ResolvedJavaField STRING_VALUE_FIELD;
 297 
 298         static {
 299             try {
 300                 MetaAccessProvider metaAccess = runtime().getHostJVMCIBackend().getMetaAccess();
 301                 STRING_VALUE_FIELD = metaAccess.lookupJavaField(String.class.getDeclaredField("value"));
 302             } catch (SecurityException | NoSuchFieldException e) {
 303                 throw new JVMCIError(e);
 304             }
 305         }
 306     }
 307 
 308     public LocationIdentity getLocationIdentity() {
 309         return locationIdentity;
 310     }
 311 }
< prev index next >