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.core.common.UnsafeAccess.UNSAFE; 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 @Override 54 public long getOffset(Field field) { 55 return UNSAFE.objectFieldOffset(field); 56 } 57 } 58 59 /** 60 * Describes a field in a class during {@linkplain FieldsScanner scanning}. 61 */ 62 public static class FieldInfo implements Comparable<FieldInfo> { 63 public final long offset; 64 public final String name; 65 public final Class<?> type; 66 public final Class<?> declaringClass; 67 68 public FieldInfo(long offset, String name, Class<?> type, Class<?> declaringClass) { 69 this.offset = offset; 70 this.name = name; 71 this.type = type; 72 this.declaringClass = declaringClass; 73 } 74 75 /** 76 * Sorts fields in ascending order by their {@link #offset}s. 77 */ 78 @Override 79 public int compareTo(FieldInfo o) { 80 return offset < o.offset ? -1 : (offset > o.offset ? 1 : 0); 81 } 82 83 @Override 84 public String toString() { 85 return "[" + offset + "]" + name + ":" + type.getSimpleName(); 86 } 87 } 88 89 private final FieldsScanner.CalcOffset calc; 90 91 /** 92 * Fields not belonging to a more specific category defined by scanner subclasses are added to 93 * this list. 94 */ 95 public final ArrayList<FieldsScanner.FieldInfo> data = new ArrayList<>(); 96 97 public FieldsScanner(FieldsScanner.CalcOffset calc) { 98 this.calc = calc; 99 } 100 101 /** 102 * Scans the fields in a class hierarchy. 103 * 104 * @param clazz the class at which to start scanning 105 * @param endClazz scanning stops when this class is encountered (i.e. {@code endClazz} is not 106 * scanned) 107 */ 108 public void scan(Class<?> clazz, Class<?> endClazz, boolean includeTransient) { 109 Class<?> currentClazz = clazz; 110 while (currentClazz != endClazz) { 111 for (Field field : currentClazz.getDeclaredFields()) { 112 if (Modifier.isStatic(field.getModifiers())) { 113 continue; 114 } 115 if (!includeTransient && Modifier.isTransient(field.getModifiers())) { 116 continue; 117 } 118 long offset = calc.getOffset(field); 119 scanField(field, offset); 120 } 121 currentClazz = currentClazz.getSuperclass(); 122 } 123 } 124 125 protected void scanField(Field field, long offset) { 126 data.add(new FieldsScanner.FieldInfo(offset, field.getName(), field.getType(), field.getDeclaringClass())); 127 } 128 }