1/*
2 * Copyright (c) 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 */
23package org.graalvm.compiler.core.common;
24
25import static org.graalvm.compiler.core.common.UnsafeAccess.UNSAFE;
26
27import java.lang.reflect.Field;
28import java.lang.reflect.Modifier;
29import java.util.ArrayList;
30
31import sun.misc.Unsafe;
32
33/**
34 * Scans the fields in a class hierarchy.
35 */
36public class FieldsScanner {
37
38    /**
39     * Determines the offset (in bytes) of a field.
40     */
41    public interface CalcOffset {
42
43        long getOffset(Field field);
44    }
45
46    /**
47     * Determines the offset (in bytes) of a field using {@link Unsafe#objectFieldOffset(Field)}.
48     */
49    public static class DefaultCalcOffset implements CalcOffset {
50
51        @Override
52        public long getOffset(Field field) {
53            return UNSAFE.objectFieldOffset(field);
54        }
55    }
56
57    /**
58     * Describes a field in a class during {@linkplain FieldsScanner scanning}.
59     */
60    public static class FieldInfo implements Comparable<FieldInfo> {
61        public final long offset;
62        public final String name;
63        public final Class<?> type;
64        public final Class<?> declaringClass;
65
66        public FieldInfo(long offset, String name, Class<?> type, Class<?> declaringClass) {
67            this.offset = offset;
68            this.name = name;
69            this.type = type;
70            this.declaringClass = declaringClass;
71        }
72
73        /**
74         * Sorts fields in ascending order by their {@link #offset}s.
75         */
76        @Override
77        public int compareTo(FieldInfo o) {
78            return offset < o.offset ? -1 : (offset > o.offset ? 1 : 0);
79        }
80
81        @Override
82        public String toString() {
83            return "[" + offset + "]" + name + ":" + type.getSimpleName();
84        }
85    }
86
87    private final FieldsScanner.CalcOffset calc;
88
89    /**
90     * Fields not belonging to a more specific category defined by scanner subclasses are added to
91     * this list.
92     */
93    public final ArrayList<FieldsScanner.FieldInfo> data = new ArrayList<>();
94
95    public FieldsScanner(FieldsScanner.CalcOffset calc) {
96        this.calc = calc;
97    }
98
99    /**
100     * Scans the fields in a class hierarchy.
101     *
102     * @param clazz the class at which to start scanning
103     * @param endClazz scanning stops when this class is encountered (i.e. {@code endClazz} is not
104     *            scanned)
105     */
106    public void scan(Class<?> clazz, Class<?> endClazz, boolean includeTransient) {
107        Class<?> currentClazz = clazz;
108        while (currentClazz != endClazz) {
109            for (Field field : currentClazz.getDeclaredFields()) {
110                if (Modifier.isStatic(field.getModifiers())) {
111                    continue;
112                }
113                if (!includeTransient && Modifier.isTransient(field.getModifiers())) {
114                    continue;
115                }
116                long offset = calc.getOffset(field);
117                scanField(field, offset);
118            }
119            currentClazz = currentClazz.getSuperclass();
120        }
121    }
122
123    protected void scanField(Field field, long offset) {
124        data.add(new FieldsScanner.FieldInfo(offset, field.getName(), field.getType(), field.getDeclaringClass()));
125    }
126}
127