1 /* 2 * Copyright (c) 2012, 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. 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 24 25 package org.graalvm.compiler.hotspot.replacements; 26 27 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG; 28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.ARRAY_KLASS_COMPONENT_MIRROR; 29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.KLASS_ACCESS_FLAGS_LOCATION; 30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.KLASS_MODIFIER_FLAGS_LOCATION; 31 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.KLASS_SUPER_KLASS_LOCATION; 32 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.arrayKlassComponentMirrorOffset; 33 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.klassAccessFlagsOffset; 34 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.klassIsArray; 35 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.klassModifierFlagsOffset; 36 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.klassSuperKlassOffset; 37 38 import java.lang.reflect.Modifier; 39 40 import org.graalvm.compiler.api.replacements.ClassSubstitution; 41 import org.graalvm.compiler.api.replacements.MethodSubstitution; 42 import org.graalvm.compiler.hotspot.word.KlassPointer; 43 import org.graalvm.compiler.nodes.PiNode; 44 import org.graalvm.compiler.nodes.SnippetAnchorNode; 45 46 // JaCoCo Exclude 47 48 /** 49 * Substitutions for {@link java.lang.Class} methods. 50 */ 51 @ClassSubstitution(Class.class) 52 public class HotSpotClassSubstitutions { 53 54 @MethodSubstitution(isStatic = false) 55 public static int getModifiers(final Class<?> thisObj) { 56 KlassPointer klass = ClassGetHubNode.readClass(thisObj); 57 if (klass.isNull()) { 58 // Class for primitive type 59 return Modifier.ABSTRACT | Modifier.FINAL | Modifier.PUBLIC; 60 } else { 61 return klass.readInt(klassModifierFlagsOffset(INJECTED_VMCONFIG), KLASS_MODIFIER_FLAGS_LOCATION); 62 } 63 } 64 65 @MethodSubstitution(isStatic = false) 66 public static boolean isInterface(final Class<?> thisObj) { 67 KlassPointer klass = ClassGetHubNode.readClass(thisObj); 68 if (klass.isNull()) { 69 // Class for primitive type 70 return false; 71 } else { 72 int accessFlags = klass.readInt(klassAccessFlagsOffset(INJECTED_VMCONFIG), KLASS_ACCESS_FLAGS_LOCATION); 73 return (accessFlags & Modifier.INTERFACE) != 0; 74 } 75 } 76 77 @MethodSubstitution(isStatic = false) 78 public static boolean isArray(final Class<?> thisObj) { 79 KlassPointer klass = ClassGetHubNode.readClass(thisObj); 80 if (klass.isNull()) { 81 // Class for primitive type 82 return false; 83 } else { 84 KlassPointer klassNonNull = ClassGetHubNode.piCastNonNull(klass, SnippetAnchorNode.anchor()); 85 return klassIsArray(klassNonNull); 86 } 87 } 88 89 @MethodSubstitution(isStatic = false) 90 public static boolean isPrimitive(final Class<?> thisObj) { 91 KlassPointer klass = ClassGetHubNode.readClass(thisObj); 92 return klass.isNull(); 93 } 94 95 @MethodSubstitution(isStatic = false) 96 public static Class<?> getSuperclass(final Class<?> thisObj) { 97 KlassPointer klass = ClassGetHubNode.readClass(thisObj); 98 if (!klass.isNull()) { 99 KlassPointer klassNonNull = ClassGetHubNode.piCastNonNull(klass, SnippetAnchorNode.anchor()); 100 int accessFlags = klassNonNull.readInt(klassAccessFlagsOffset(INJECTED_VMCONFIG), KLASS_ACCESS_FLAGS_LOCATION); 101 if ((accessFlags & Modifier.INTERFACE) == 0) { 102 if (klassIsArray(klassNonNull)) { 103 return Object.class; 104 } else { 105 KlassPointer superKlass = klassNonNull.readKlassPointer(klassSuperKlassOffset(INJECTED_VMCONFIG), KLASS_SUPER_KLASS_LOCATION); 106 if (superKlass.isNull()) { 107 return null; 108 } else { 109 KlassPointer superKlassNonNull = ClassGetHubNode.piCastNonNull(superKlass, SnippetAnchorNode.anchor()); 110 return HubGetClassNode.readClass(superKlassNonNull); 111 } 112 } 113 } 114 } else { 115 // Class for primitive type 116 } 117 return null; 118 } 119 120 @MethodSubstitution(isStatic = false) 121 public static Class<?> getComponentType(final Class<?> thisObj) { 122 KlassPointer klass = ClassGetHubNode.readClass(thisObj); 123 if (!klass.isNull()) { 124 KlassPointer klassNonNull = ClassGetHubNode.piCastNonNull(klass, SnippetAnchorNode.anchor()); 125 if (klassIsArray(klassNonNull)) { 126 return PiNode.asNonNullClass(klassNonNull.readObject(arrayKlassComponentMirrorOffset(INJECTED_VMCONFIG), ARRAY_KLASS_COMPONENT_MIRROR)); 127 } 128 } else { 129 // Class for primitive type 130 } 131 return null; 132 } 133 }