001/*
002 * Copyright (c) 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.compiler.common;
024
025import static jdk.internal.jvmci.common.UnsafeAccess.*;
026
027import java.lang.reflect.*;
028import java.util.*;
029
030import sun.misc.*;
031
032/**
033 * Scans the fields in a class hierarchy.
034 */
035public class FieldsScanner {
036
037    /**
038     * Determines the offset (in bytes) of a field.
039     */
040    public interface CalcOffset {
041
042        long getOffset(Field field);
043    }
044
045    /**
046     * Determines the offset (in bytes) of a field using {@link Unsafe#objectFieldOffset(Field)}.
047     */
048    public static class DefaultCalcOffset implements CalcOffset {
049
050        @Override
051        public long getOffset(Field field) {
052            return unsafe.objectFieldOffset(field);
053        }
054    }
055
056    /**
057     * Describes a field in a class during {@linkplain FieldsScanner scanning}.
058     */
059    public static class FieldInfo implements Comparable<FieldInfo> {
060        public final long offset;
061        public final String name;
062        public final Class<?> type;
063        public final Class<?> declaringClass;
064
065        public FieldInfo(long offset, String name, Class<?> type, Class<?> declaringClass) {
066            this.offset = offset;
067            this.name = name;
068            this.type = type;
069            this.declaringClass = declaringClass;
070        }
071
072        /**
073         * Sorts fields in ascending order by their {@link #offset}s.
074         */
075        public int compareTo(FieldInfo o) {
076            return offset < o.offset ? -1 : (offset > o.offset ? 1 : 0);
077        }
078
079        @Override
080        public String toString() {
081            return "[" + offset + "]" + name + ":" + type.getSimpleName();
082        }
083    }
084
085    private final FieldsScanner.CalcOffset calc;
086
087    /**
088     * Fields not belonging to a more specific category defined by scanner subclasses are added to
089     * this list.
090     */
091    public final ArrayList<FieldsScanner.FieldInfo> data = new ArrayList<>();
092
093    public FieldsScanner(FieldsScanner.CalcOffset calc) {
094        this.calc = calc;
095    }
096
097    /**
098     * Scans the fields in a class hierarchy.
099     *
100     * @param clazz the class at which to start scanning
101     * @param endClazz scanning stops when this class is encountered (i.e. {@code endClazz} is not
102     *            scanned)
103     */
104    public void scan(Class<?> clazz, Class<?> endClazz, boolean includeTransient) {
105        Class<?> currentClazz = clazz;
106        while (currentClazz != endClazz) {
107            for (Field field : currentClazz.getDeclaredFields()) {
108                if (Modifier.isStatic(field.getModifiers())) {
109                    continue;
110                }
111                if (!includeTransient && Modifier.isTransient(field.getModifiers())) {
112                    continue;
113                }
114                long offset = calc.getOffset(field);
115                scanField(field, offset);
116            }
117            currentClazz = currentClazz.getSuperclass();
118        }
119    }
120
121    protected void scanField(Field field, long offset) {
122        data.add(new FieldsScanner.FieldInfo(offset, field.getName(), field.getType(), field.getDeclaringClass()));
123    }
124}