view graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaField.java @ 6539:2463eb24b644

Cleanup of Graal API: Rename methods so that it follows the getXxx naming convention and so that they are similar to the names of the java.lang.reflect classes. Remove unused methods.
author Christian Wimmer <christian.wimmer@oracle.com>
date Tue, 09 Oct 2012 15:23:38 -0700
parents 2c913b643422
children 41938af2b3d8
line wrap: on
line source

/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.oracle.graal.hotspot.meta;

import java.lang.annotation.*;
import java.lang.reflect.*;

import com.oracle.graal.api.meta.*;
import com.oracle.graal.api.meta.ResolvedJavaType.*;
import com.oracle.graal.hotspot.*;
import com.oracle.graal.phases.*;

/**
 * Represents a field in a HotSpot type.
 */
public class HotSpotResolvedJavaField extends CompilerObject implements ResolvedJavaField {

    private static final long serialVersionUID = 7692985878836955683L;
    private final ResolvedJavaType holder;
    private final String name;
    private final JavaType type;
    private final int offset;
    private final int accessFlags;
    private Constant constant;                // Constant part only valid for static fields.

    public HotSpotResolvedJavaField(ResolvedJavaType holder, String name, JavaType type, int offset, int accessFlags) {
        this.holder = holder;
        this.name = name;
        this.type = type;
        assert offset != -1;
        this.offset = offset;
        this.accessFlags = accessFlags;
    }

    @Override
    public int getModifiers() {
        return accessFlags;
    }

    @Override
    public Constant readConstantValue(Constant receiver) {
        if (receiver == null) {
            assert Modifier.isStatic(accessFlags);
            if (constant == null) {
                if (holder.isInitialized() && holder.toJava() != System.class) {
                    if (Modifier.isFinal(getModifiers()) || assumeStaticFieldsFinal(holder.toJava())) {
                        constant = readValue(receiver);
                    }
                }
            }
            return constant;
        } else {
            assert !Modifier.isStatic(accessFlags);
            // TODO (chaeubl) HotSpot does not trust final non-static fields (see ciField.cpp)
            if (Modifier.isFinal(getModifiers())) {
                return readValue(receiver);
            }
        }
        return null;
    }

    @Override
    public Constant readValue(Constant receiver) {
        if (receiver == null) {
            assert Modifier.isStatic(accessFlags);
            if (holder.isInitialized()) {
                Constant encoding = holder.getEncoding(getKind() == Kind.Object ? Representation.StaticObjectFields : Representation.StaticPrimitiveFields);
                return this.getKind().readUnsafeConstant(encoding.asObject(), offset);
            }
            return null;
        } else {
            assert !Modifier.isStatic(accessFlags);
            return this.getKind().readUnsafeConstant(receiver.asObject(), offset);
        }
    }

    private static boolean assumeStaticFieldsFinal(Class< ? > clazz) {
        return clazz == GraalOptions.class;
    }

    @Override
    public ResolvedJavaType getDeclaringClass() {
        return holder;
    }

    @Override
    public Kind getKind() {
        return getType().getKind();
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public JavaType getType() {
        return type;
    }

    public int offset() {
        return offset;
    }

    @Override
    public String toString() {
        return "HotSpotField<" + MetaUtil.format("%h.%n", this) + ":" + offset + ">";
    }

    @Override
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
        Field javaField = toJava();
        if (javaField != null) {
            return javaField.getAnnotation(annotationClass);
        }
        return null;
    }

    private Field toJava() {
        try {
            return holder.toJava().getDeclaredField(name);
        } catch (NoSuchFieldException e) {
            return null;
        }
    }
}