Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClass.java @ 20129:5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Thu, 02 Apr 2015 01:22:41 +0200 |
parents | |
children | 8dc73c226c63 |
rev | line source |
---|---|
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
1 /* |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
4 * |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. Oracle designates this |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
8 * particular file as subject to the "Classpath" exception as provided |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
9 * by Oracle in the LICENSE file that accompanied this code. |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
10 * |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
15 * accompanied this code). |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
16 * |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU General Public License version |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
20 * |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
22 * or visit www.oracle.com if you need additional information or have any |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
23 * questions. |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
24 */ |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
25 package com.oracle.truffle.api.nodes; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
26 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
27 import java.lang.reflect.*; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
28 import java.security.*; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
29 import java.util.*; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
30 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
31 import com.oracle.truffle.api.nodes.Node.Child; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
32 import com.oracle.truffle.api.nodes.Node.Children; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
33 import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
34 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
35 /** |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
36 * Information about a {@link Node} class. A single instance of this class is allocated for every |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
37 * subclass of {@link Node} that is used. |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
38 */ |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
39 public final class NodeClass { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
40 private static final ClassValue<NodeClass> nodeClasses = new ClassValue<NodeClass>() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
41 @SuppressWarnings("unchecked") |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
42 @Override |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
43 protected NodeClass computeValue(final Class<?> clazz) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
44 assert Node.class.isAssignableFrom(clazz); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
45 return AccessController.doPrivileged(new PrivilegedAction<NodeClass>() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 public NodeClass run() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 return new NodeClass((Class<? extends Node>) clazz); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 }); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
50 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 }; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
52 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 // The comprehensive list of all fields. |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 private final NodeFieldAccessor[] fields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
55 // Separate arrays for the frequently accessed fields. |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 private final NodeFieldAccessor parentField; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 private final NodeFieldAccessor[] childFields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 private final NodeFieldAccessor[] childrenFields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 private final NodeFieldAccessor[] cloneableFields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
60 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
61 private final Class<? extends Node> clazz; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 public static NodeClass get(Class<? extends Node> clazz) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
64 return nodeClasses.get(clazz); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
65 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 public NodeClass(Class<? extends Node> clazz) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 List<NodeFieldAccessor> fieldsList = new ArrayList<>(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
69 NodeFieldAccessor parentFieldTmp = null; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 List<NodeFieldAccessor> childFieldList = new ArrayList<>(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 List<NodeFieldAccessor> childrenFieldList = new ArrayList<>(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 List<NodeFieldAccessor> cloneableFieldList = new ArrayList<>(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 for (Field field : NodeUtil.getAllFields(clazz)) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 if (Modifier.isStatic(field.getModifiers()) || field.isSynthetic()) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 continue; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 NodeFieldAccessor nodeField; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 if (field.getDeclaringClass() == Node.class && field.getName().equals("parent")) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 assert Node.class.isAssignableFrom(field.getType()); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 nodeField = NodeFieldAccessor.create(NodeFieldKind.PARENT, field); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 parentFieldTmp = nodeField; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 } else if (field.getAnnotation(Child.class) != null) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 checkChildField(field); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 nodeField = NodeFieldAccessor.create(NodeFieldKind.CHILD, field); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
87 childFieldList.add(nodeField); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
88 } else if (field.getAnnotation(Children.class) != null) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
89 checkChildrenField(field); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
90 nodeField = NodeFieldAccessor.create(NodeFieldKind.CHILDREN, field); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 childrenFieldList.add(nodeField); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 } else { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 nodeField = NodeFieldAccessor.create(NodeFieldKind.DATA, field); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
94 if (NodeCloneable.class.isAssignableFrom(field.getType())) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
95 cloneableFieldList.add(nodeField); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
96 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
97 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
98 fieldsList.add(nodeField); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
99 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
100 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
101 if (parentFieldTmp == null) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
102 throw new AssertionError("parent field not found"); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
103 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
104 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
105 this.fields = fieldsList.toArray(new NodeFieldAccessor[fieldsList.size()]); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
106 this.parentField = parentFieldTmp; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
107 this.childFields = childFieldList.toArray(new NodeFieldAccessor[childFieldList.size()]); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
108 this.childrenFields = childrenFieldList.toArray(new NodeFieldAccessor[childrenFieldList.size()]); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
109 this.cloneableFields = cloneableFieldList.toArray(new NodeFieldAccessor[cloneableFieldList.size()]); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
110 this.clazz = clazz; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
111 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
112 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
113 public NodeFieldAccessor[] getCloneableFields() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
114 return cloneableFields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
115 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
116 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
117 private static boolean isNodeType(Class<?> clazz) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
118 return Node.class.isAssignableFrom(clazz) || (clazz.isInterface() && NodeInterface.class.isAssignableFrom(clazz)); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
119 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
120 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
121 private static void checkChildField(Field field) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
122 if (!isNodeType(field.getType())) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
123 throw new AssertionError("@Child field type must be a subclass of Node or an interface extending NodeInterface (" + field + ")"); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
124 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
125 if (Modifier.isFinal(field.getModifiers())) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
126 throw new AssertionError("@Child field must not be final (" + field + ")"); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
127 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
128 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
129 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
130 private static void checkChildrenField(Field field) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
131 if (!(field.getType().isArray() && isNodeType(field.getType().getComponentType()))) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
132 throw new AssertionError("@Children field type must be an array of a subclass of Node or an interface extending NodeInterface (" + field + ")"); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
133 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
134 if (!Modifier.isFinal(field.getModifiers())) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
135 throw new AssertionError("@Children field must be final (" + field + ")"); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
136 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
137 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
138 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
139 public NodeFieldAccessor[] getFields() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
140 return fields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
141 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
142 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
143 public NodeFieldAccessor getParentField() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
144 return parentField; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
145 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
146 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
147 public NodeFieldAccessor[] getChildFields() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
148 return childFields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
149 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
150 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
151 public NodeFieldAccessor[] getChildrenFields() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
152 return childrenFields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
153 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
154 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
155 @Override |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
156 public int hashCode() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
157 return clazz.hashCode(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
158 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
159 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
160 @Override |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
161 public boolean equals(Object obj) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
162 if (obj instanceof NodeClass) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
163 NodeClass other = (NodeClass) obj; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
164 return clazz.equals(other.clazz); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
165 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
166 return false; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
167 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
168 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
169 public Iterator<Node> makeIterator(Node node) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
170 assert clazz.isInstance(node); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
171 return new NodeIterator(this, node); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
172 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
173 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
174 private static final class NodeIterator implements Iterator<Node> { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
175 private final NodeFieldAccessor[] childFields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
176 private final NodeFieldAccessor[] childrenFields; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
177 private final Node node; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
178 private final int childrenCount; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
179 private int index; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
180 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
181 protected NodeIterator(NodeClass nodeClass, Node node) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
182 this.childFields = nodeClass.getChildFields(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
183 this.childrenFields = nodeClass.getChildrenFields(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
184 this.node = node; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
185 this.childrenCount = childrenCount(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
186 this.index = 0; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
187 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
188 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
189 private int childrenCount() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
190 int nodeCount = childFields.length; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
191 for (NodeFieldAccessor childrenField : childrenFields) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
192 Object[] children = ((Object[]) childrenField.getObject(node)); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
193 if (children != null) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
194 nodeCount += children.length; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
195 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
196 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
197 return nodeCount; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
198 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
199 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
200 private Node nodeAt(int idx) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
201 int nodeCount = childFields.length; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
202 if (idx < nodeCount) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
203 return (Node) childFields[idx].getObject(node); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
204 } else { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
205 for (NodeFieldAccessor childrenField : childrenFields) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
206 Object[] nodeArray = (Object[]) childrenField.getObject(node); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
207 if (idx < nodeCount + nodeArray.length) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
208 return (Node) nodeArray[idx - nodeCount]; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
209 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
210 nodeCount += nodeArray.length; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
211 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
212 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
213 return null; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
214 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
215 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
216 private void forward() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
217 if (index < childrenCount) { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
218 index++; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
219 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
220 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
221 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
222 public boolean hasNext() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
223 return index < childrenCount; |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
224 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
225 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
226 public Node next() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
227 try { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
228 return nodeAt(index); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
229 } finally { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
230 forward(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
231 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
232 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
233 |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
234 public void remove() { |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
235 throw new UnsupportedOperationException(); |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
236 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
237 } |
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
238 } |