001/*
002 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.replacements;
024
025import static com.oracle.graal.hotspot.replacements.HotSpotReplacementsUtil.*;
026
027import java.lang.reflect.*;
028
029import com.oracle.graal.hotspot.word.*;
030import com.oracle.graal.nodes.*;
031
032// JaCoCo Exclude
033
034/**
035 * Substitutions for {@link java.lang.Class} methods.
036 */
037public class HotSpotClassSubstitutions {
038
039    public static int getModifiers(final Class<?> thisObj) {
040        KlassPointer klass = ClassGetHubNode.readClass(thisObj);
041        if (klass.isNull()) {
042            // Class for primitive type
043            return Modifier.ABSTRACT | Modifier.FINAL | Modifier.PUBLIC;
044        } else {
045            return klass.readInt(klassModifierFlagsOffset(), KLASS_MODIFIER_FLAGS_LOCATION);
046        }
047    }
048
049    public static boolean isInterface(final Class<?> thisObj) {
050        KlassPointer klass = ClassGetHubNode.readClass(thisObj);
051        if (klass.isNull()) {
052            // Class for primitive type
053            return false;
054        } else {
055            int accessFlags = klass.readInt(klassAccessFlagsOffset(), KLASS_ACCESS_FLAGS_LOCATION);
056            return (accessFlags & Modifier.INTERFACE) != 0;
057        }
058    }
059
060    public static boolean isArray(final Class<?> thisObj) {
061        KlassPointer klass = ClassGetHubNode.readClass(thisObj);
062        if (klass.isNull()) {
063            // Class for primitive type
064            return false;
065        } else {
066            return klassIsArray(klass);
067        }
068    }
069
070    public static boolean isPrimitive(final Class<?> thisObj) {
071        KlassPointer klass = ClassGetHubNode.readClass(thisObj);
072        return klass.isNull();
073    }
074
075    public static Class<?> getSuperclass(final Class<?> thisObj) {
076        KlassPointer klass = ClassGetHubNode.readClass(thisObj);
077        if (!klass.isNull()) {
078            int accessFlags = klass.readInt(klassAccessFlagsOffset(), KLASS_ACCESS_FLAGS_LOCATION);
079            if ((accessFlags & Modifier.INTERFACE) == 0) {
080                if (klassIsArray(klass)) {
081                    return Object.class;
082                } else {
083                    KlassPointer superKlass = klass.readKlassPointer(klassSuperKlassOffset(), KLASS_SUPER_KLASS_LOCATION);
084                    if (superKlass.isNull()) {
085                        return null;
086                    } else {
087                        return readJavaMirror(superKlass);
088                    }
089                }
090            }
091        } else {
092            // Class for primitive type
093        }
094        return null;
095    }
096
097    public static Class<?> readJavaMirror(KlassPointer klass) {
098        return PiNode.asNonNullClass(klass.readObject(classMirrorOffset(), CLASS_MIRROR_LOCATION));
099    }
100
101    public static Class<?> getComponentType(final Class<?> thisObj) {
102        KlassPointer klass = ClassGetHubNode.readClass(thisObj);
103        if (!klass.isNull()) {
104            if (klassIsArray(klass)) {
105                return PiNode.asNonNullClass(klass.readObject(arrayKlassComponentMirrorOffset(), ARRAY_KLASS_COMPONENT_MIRROR));
106            }
107        } else {
108            // Class for primitive type
109        }
110        return null;
111    }
112}