comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeFieldAccessor.java @ 22368:0d4b0e4263ee

Convert NodeClass and NodeFieldAccessor to interfaces
author Christian Wimmer <christian.wimmer@oracle.com>
date Fri, 13 Nov 2015 10:29:52 -0800
parents cd7ee4a382e4
children 6598b9b7aafd
comparison
equal deleted inserted replaced
22367:1b48778cee21 22368:0d4b0e4263ee
29 import sun.misc.Unsafe; 29 import sun.misc.Unsafe;
30 30
31 import com.oracle.truffle.api.nodes.Node.Child; 31 import com.oracle.truffle.api.nodes.Node.Child;
32 import com.oracle.truffle.api.nodes.Node.Children; 32 import com.oracle.truffle.api.nodes.Node.Children;
33 33
34 /** 34 public interface NodeFieldAccessor {
35 * Information about a field in a {@link Node} class.
36 */
37 public abstract class NodeFieldAccessor {
38 35
39 public static enum NodeFieldKind { 36 public static enum NodeFieldKind {
40 /** The reference to the {@link NodeClass}. */ 37 /** The reference to the {@link NodeClass}. */
41 NODE_CLASS, 38 NODE_CLASS,
42 /** The single {@link Node#getParent() parent} field. */ 39 /** The single {@link Node#getParent() parent} field. */
47 CHILDREN, 44 CHILDREN,
48 /** A normal non-child data field of the node. */ 45 /** A normal non-child data field of the node. */
49 DATA 46 DATA
50 } 47 }
51 48
52 private static final boolean USE_UNSAFE = Boolean.getBoolean("truffle.unsafe"); 49 NodeFieldKind getKind();
53 50
54 private final NodeFieldKind kind; 51 Class<?> getFieldType();
55 private final Class<?> declaringClass; 52
56 private final String name; 53 Class<?> getFieldDeclaringClass();
57 protected final Class<?> type; 54
58 55 String getName();
59 protected NodeFieldAccessor(NodeFieldKind kind, Class<?> declaringClass, String name, Class<?> type) { 56
60 this.kind = kind; 57 void putObject(Node receiver, Object value);
61 this.declaringClass = declaringClass; 58
62 this.name = name; 59 Object getObject(Node receiver);
63 this.type = type; 60
64 } 61 Object loadValue(Node node);
65 62
66 protected static NodeFieldAccessor create(NodeFieldKind kind, Field field) { 63 /**
67 if (USE_UNSAFE) { 64 * Information about a field in a {@link Node} class.
68 return new UnsafeNodeField(kind, field); 65 */
69 } else { 66 public abstract static class NodeFieldAccessorImpl implements NodeFieldAccessor {
70 return new ReflectionNodeField(kind, field); 67 private static final boolean USE_UNSAFE = Boolean.getBoolean("truffle.unsafe");
71 } 68
72 } 69 private final NodeFieldKind kind;
73 70 private final Class<?> declaringClass;
74 public NodeFieldKind getKind() { 71 private final String name;
75 return kind; 72 protected final Class<?> type;
76 } 73
77 74 protected NodeFieldAccessorImpl(NodeFieldKind kind, Class<?> declaringClass, String name, Class<?> type) {
78 public Class<?> getType() { 75 this.kind = kind;
79 return type; 76 this.declaringClass = declaringClass;
80 } 77 this.name = name;
81 78 this.type = type;
82 public Class<?> getDeclaringClass() { 79 }
83 return declaringClass; 80
84 } 81 protected static NodeFieldAccessor create(NodeFieldKind kind, Field field) {
85 82 if (USE_UNSAFE) {
86 public String getName() { 83 return new UnsafeNodeField(kind, field);
87 return name; 84 } else {
88 } 85 return new ReflectionNodeField(kind, field);
89 86 }
90 public abstract void putObject(Node receiver, Object value); 87 }
91 88
92 public abstract Object getObject(Node receiver); 89 public NodeFieldKind getKind() {
93 90 return kind;
94 public abstract Object loadValue(Node node); 91 }
95 92
96 public abstract static class AbstractUnsafeNodeFieldAccessor extends NodeFieldAccessor { 93 public Class<?> getFieldType() {
94 return type;
95 }
96
97 public Class<?> getFieldDeclaringClass() {
98 return declaringClass;
99 }
100
101 public String getName() {
102 return name;
103 }
104 }
105
106 public abstract static class AbstractUnsafeNodeFieldAccessor extends NodeFieldAccessorImpl {
97 107
98 protected AbstractUnsafeNodeFieldAccessor(NodeFieldKind kind, Class<?> declaringClass, String name, Class<?> type) { 108 protected AbstractUnsafeNodeFieldAccessor(NodeFieldKind kind, Class<?> declaringClass, String name, Class<?> type) {
99 super(kind, declaringClass, name, type); 109 super(kind, declaringClass, name, type);
100 } 110 }
101 111
157 throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e); 167 throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e);
158 } 168 }
159 } 169 }
160 } 170 }
161 171
162 private static final class UnsafeNodeField extends AbstractUnsafeNodeFieldAccessor { 172 public static final class UnsafeNodeField extends AbstractUnsafeNodeFieldAccessor {
163 private final long offset; 173 private final long offset;
164 174
165 protected UnsafeNodeField(NodeFieldKind kind, Field field) { 175 protected UnsafeNodeField(NodeFieldKind kind, Field field) {
166 super(kind, field.getDeclaringClass(), field.getName(), field.getType()); 176 super(kind, field.getDeclaringClass(), field.getName(), field.getType());
167 this.offset = AbstractUnsafeNodeFieldAccessor.unsafe.objectFieldOffset(field); 177 this.offset = AbstractUnsafeNodeFieldAccessor.unsafe.objectFieldOffset(field);
171 public long getOffset() { 181 public long getOffset() {
172 return offset; 182 return offset;
173 } 183 }
174 } 184 }
175 185
176 private static final class ReflectionNodeField extends NodeFieldAccessor { 186 public static final class ReflectionNodeField extends NodeFieldAccessorImpl {
177 private final Field field; 187 private final Field field;
178 188
179 protected ReflectionNodeField(NodeFieldKind kind, Field field) { 189 protected ReflectionNodeField(NodeFieldKind kind, Field field) {
180 super(kind, field.getDeclaringClass(), field.getName(), field.getType()); 190 super(kind, field.getDeclaringClass(), field.getName(), field.getType());
181 this.field = field; 191 this.field = field;
227 } catch (IllegalAccessException e) { 237 } catch (IllegalAccessException e) {
228 throw new AssertionError(e); 238 throw new AssertionError(e);
229 } 239 }
230 } 240 }
231 } 241 }
232
233 } 242 }