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