comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 7530:5e3d1a68664e

applied mx eclipseformat to all Java files
author Doug Simon <doug.simon@oracle.com>
date Wed, 23 Jan 2013 16:34:57 +0100
parents a4b84ba6dc2e
children 07f8d136a05e
comparison
equal deleted inserted replaced
7529:4a11124a3563 7530:5e3d1a68664e
41 private final Class[] nodeFieldClasses; 41 private final Class[] nodeFieldClasses;
42 private final long[] nodeArrayFieldOffsets; 42 private final long[] nodeArrayFieldOffsets;
43 private final Class[] nodeArrayFieldClasses; 43 private final Class[] nodeArrayFieldClasses;
44 private final long parentOffset; 44 private final long parentOffset;
45 private final long[] nodeDataFieldOffsets; 45 private final long[] nodeDataFieldOffsets;
46 private final Class< ? >[] nodeDataFieldClasses; 46 private final Class<?>[] nodeDataFieldClasses;
47 47
48 private static final Map<Class< ? >, NodeClass> nodeClasses = new IdentityHashMap<>(); 48 private static final Map<Class<?>, NodeClass> nodeClasses = new IdentityHashMap<>();
49 49
50 public static NodeClass get(Class< ? > clazz) { 50 public static NodeClass get(Class<?> clazz) {
51 NodeClass nodeClass = nodeClasses.get(clazz); 51 NodeClass nodeClass = nodeClasses.get(clazz);
52 if (nodeClass == null) { 52 if (nodeClass == null) {
53 nodeClass = new NodeClass(clazz); 53 nodeClass = new NodeClass(clazz);
54 nodeClasses.put(clazz, nodeClass); 54 nodeClasses.put(clazz, nodeClass);
55 } 55 }
56 return nodeClass; 56 return nodeClass;
57 } 57 }
58 58
59 private NodeClass(Class< ? > clazz) { 59 private NodeClass(Class<?> clazz) {
60 // scan object fields 60 // scan object fields
61 Class< ? > parentClassTmp = null; 61 Class<?> parentClassTmp = null;
62 List<Long> nodeFieldOffsetsList = new ArrayList<>(); 62 List<Long> nodeFieldOffsetsList = new ArrayList<>();
63 List<Class< ? >> nodeFieldClassesList = new ArrayList<>(); 63 List<Class<?>> nodeFieldClassesList = new ArrayList<>();
64 List<Long> nodeArrayFieldOffsetsList = new ArrayList<>(); 64 List<Long> nodeArrayFieldOffsetsList = new ArrayList<>();
65 List<Class< ? >> nodeArrayFieldClassesList = new ArrayList<>(); 65 List<Class<?>> nodeArrayFieldClassesList = new ArrayList<>();
66 List<Long> nodeDataFieldOffsetList = new ArrayList<>(); 66 List<Long> nodeDataFieldOffsetList = new ArrayList<>();
67 List<Class< ? >> nodeDataFieldClassList = new ArrayList<>(); 67 List<Class<?>> nodeDataFieldClassList = new ArrayList<>();
68 Field[] fields = getAllFields(clazz); 68 Field[] fields = getAllFields(clazz);
69 long parentOffsetTemp = -1; 69 long parentOffsetTemp = -1;
70 for (Field field : fields) { 70 for (Field field : fields) {
71 if (Modifier.isStatic(field.getModifiers()) || field.isSynthetic()) { 71 if (Modifier.isStatic(field.getModifiers()) || field.isSynthetic()) {
72 continue; 72 continue;
93 this.nodeFieldOffsets = toLongArray(nodeFieldOffsetsList); 93 this.nodeFieldOffsets = toLongArray(nodeFieldOffsetsList);
94 this.nodeFieldClasses = nodeFieldClassesList.toArray(new Class[nodeFieldClassesList.size()]); 94 this.nodeFieldClasses = nodeFieldClassesList.toArray(new Class[nodeFieldClassesList.size()]);
95 this.nodeArrayFieldOffsets = toLongArray(nodeArrayFieldOffsetsList); 95 this.nodeArrayFieldOffsets = toLongArray(nodeArrayFieldOffsetsList);
96 this.nodeArrayFieldClasses = nodeArrayFieldClassesList.toArray(new Class[nodeArrayFieldClassesList.size()]); 96 this.nodeArrayFieldClasses = nodeArrayFieldClassesList.toArray(new Class[nodeArrayFieldClassesList.size()]);
97 this.nodeDataFieldOffsets = toLongArray(nodeDataFieldOffsetList); 97 this.nodeDataFieldOffsets = toLongArray(nodeDataFieldOffsetList);
98 this.nodeDataFieldClasses = nodeDataFieldClassList.toArray(new Class< ? >[nodeDataFieldClassList.size()]); 98 this.nodeDataFieldClasses = nodeDataFieldClassList.toArray(new Class<?>[nodeDataFieldClassList.size()]);
99 99
100 this.parentOffset = parentOffsetTemp; 100 this.parentOffset = parentOffsetTemp;
101 } 101 }
102 } 102 }
103 103
196 } 196 }
197 } 197 }
198 198
199 @SuppressWarnings("unchecked") 199 @SuppressWarnings("unchecked")
200 public static <T extends Node> T cloneNode(T orig) { 200 public static <T extends Node> T cloneNode(T orig) {
201 Class< ? extends Node> clazz = orig.getClass(); 201 Class<? extends Node> clazz = orig.getClass();
202 NodeClass nodeClass = NodeClass.get(clazz); 202 NodeClass nodeClass = NodeClass.get(clazz);
203 Node clone = orig.copy(); 203 Node clone = orig.copy();
204 if (clone == null) { 204 if (clone == null) {
205 return null; 205 return null;
206 } 206 }
277 } 277 }
278 } 278 }
279 } 279 }
280 } 280 }
281 281
282 public static long[] getNodeDataFieldOffsets(Class< ? > nodeClass) { 282 public static long[] getNodeDataFieldOffsets(Class<?> nodeClass) {
283 NodeClass clazz = NodeClass.get(nodeClass); 283 NodeClass clazz = NodeClass.get(nodeClass);
284 return Arrays.copyOf(clazz.nodeDataFieldOffsets, clazz.nodeDataFieldClasses.length); 284 return Arrays.copyOf(clazz.nodeDataFieldOffsets, clazz.nodeDataFieldClasses.length);
285 } 285 }
286 286
287 public static Class[] getNodeDataFieldClasses(Class< ? > nodeClass) { 287 public static Class[] getNodeDataFieldClasses(Class<?> nodeClass) {
288 NodeClass clazz = NodeClass.get(nodeClass); 288 NodeClass clazz = NodeClass.get(nodeClass);
289 return Arrays.copyOf(clazz.nodeDataFieldClasses, clazz.nodeDataFieldClasses.length); 289 return Arrays.copyOf(clazz.nodeDataFieldClasses, clazz.nodeDataFieldClasses.length);
290 } 290 }
291 291
292 public static long getNodeParentOffset(Class< ? > nodeClass) { 292 public static long getNodeParentOffset(Class<?> nodeClass) {
293 NodeClass clazz = NodeClass.get(nodeClass); 293 NodeClass clazz = NodeClass.get(nodeClass);
294 return clazz.parentOffset; 294 return clazz.parentOffset;
295 } 295 }
296 296
297 /** Returns the number of Node field declarations in the class hierarchy. */ 297 /** Returns the number of Node field declarations in the class hierarchy. */
298 public static long[] getNodeFieldOffsets(Class< ? > nodeClass) { 298 public static long[] getNodeFieldOffsets(Class<?> nodeClass) {
299 NodeClass clazz = NodeClass.get(nodeClass); 299 NodeClass clazz = NodeClass.get(nodeClass);
300 return Arrays.copyOf(clazz.nodeFieldOffsets, clazz.nodeFieldOffsets.length); 300 return Arrays.copyOf(clazz.nodeFieldOffsets, clazz.nodeFieldOffsets.length);
301 } 301 }
302 302
303 /** Returns the number of Node[] declaration in the class hierarchy. */ 303 /** Returns the number of Node[] declaration in the class hierarchy. */
304 public static long[] getNodeFieldArrayOffsets(Class< ? > nodeClass) { 304 public static long[] getNodeFieldArrayOffsets(Class<?> nodeClass) {
305 NodeClass clazz = NodeClass.get(nodeClass); 305 NodeClass clazz = NodeClass.get(nodeClass);
306 return Arrays.copyOf(clazz.nodeArrayFieldOffsets, clazz.nodeArrayFieldOffsets.length); 306 return Arrays.copyOf(clazz.nodeArrayFieldOffsets, clazz.nodeArrayFieldOffsets.length);
307 } 307 }
308 308
309 public static Class[] getNodeFieldArrayClasses(Class< ? > nodeClass) { 309 public static Class[] getNodeFieldArrayClasses(Class<?> nodeClass) {
310 NodeClass clazz = NodeClass.get(nodeClass); 310 NodeClass clazz = NodeClass.get(nodeClass);
311 return Arrays.copyOf(clazz.nodeArrayFieldClasses, clazz.nodeArrayFieldClasses.length); 311 return Arrays.copyOf(clazz.nodeArrayFieldClasses, clazz.nodeArrayFieldClasses.length);
312 } 312 }
313 313
314 public static Class getNodeParentClass(Class< ? > nodeClass) { 314 public static Class getNodeParentClass(Class<?> nodeClass) {
315 return NodeClass.get(nodeClass).parentClass; 315 return NodeClass.get(nodeClass).parentClass;
316 } 316 }
317 317
318 public static Class[] getNodeFieldClasses(Class< ? > nodeClass) { 318 public static Class[] getNodeFieldClasses(Class<?> nodeClass) {
319 NodeClass clazz = NodeClass.get(nodeClass); 319 NodeClass clazz = NodeClass.get(nodeClass);
320 return Arrays.copyOf(clazz.nodeFieldClasses, clazz.nodeFieldClasses.length); 320 return Arrays.copyOf(clazz.nodeFieldClasses, clazz.nodeFieldClasses.length);
321 } 321 }
322 322
323 /** Returns all declared fields in the class hierarchy. */ 323 /** Returns all declared fields in the class hierarchy. */
324 public static Field[] getAllFields(Class< ? extends Object> clazz) { 324 public static Field[] getAllFields(Class<? extends Object> clazz) {
325 Field[] declaredFields = clazz.getDeclaredFields(); 325 Field[] declaredFields = clazz.getDeclaredFields();
326 if (clazz.getSuperclass() != null) { 326 if (clazz.getSuperclass() != null) {
327 return concat(getAllFields(clazz.getSuperclass()), declaredFields); 327 return concat(getAllFields(clazz.getSuperclass()), declaredFields);
328 } 328 }
329 return declaredFields; 329 return declaredFields;
334 System.arraycopy(second, 0, result, first.length, second.length); 334 System.arraycopy(second, 0, result, first.length, second.length);
335 return result; 335 return result;
336 } 336 }
337 337
338 /** find annotation in class/interface hierarchy. */ 338 /** find annotation in class/interface hierarchy. */
339 public static <T extends Annotation> T findAnnotation(Class< ? > clazz, Class<T> annotationClass) { 339 public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) {
340 if (clazz.getAnnotation(annotationClass) != null) { 340 if (clazz.getAnnotation(annotationClass) != null) {
341 return clazz.getAnnotation(annotationClass); 341 return clazz.getAnnotation(annotationClass);
342 } else { 342 } else {
343 for (Class< ? > intf : clazz.getInterfaces()) { 343 for (Class<?> intf : clazz.getInterfaces()) {
344 if (intf.getAnnotation(annotationClass) != null) { 344 if (intf.getAnnotation(annotationClass) != null) {
345 return intf.getAnnotation(annotationClass); 345 return intf.getAnnotation(annotationClass);
346 } 346 }
347 } 347 }
348 if (clazz.getSuperclass() != null) { 348 if (clazz.getSuperclass() != null) {
466 } 466 }
467 return new String(byteOut.toByteArray()); 467 return new String(byteOut.toByteArray());
468 } 468 }
469 469
470 /** 470 /**
471 * Prints a human readable form of a {@link Node} AST to the given {@link PrintStream}. This print method does not 471 * Prints a human readable form of a {@link Node} AST to the given {@link PrintStream}. This
472 * check for cycles in the node structure. 472 * print method does not check for cycles in the node structure.
473 * 473 *
474 * @param p the {@link PrintStream} to print to. 474 * @param p the {@link PrintStream} to print to.
475 * @param node the root node to write 475 * @param node the root node to write
476 */ 476 */
477 public static void printTree(PrintStream p, Node node) { 477 public static void printTree(PrintStream p, Node node) {
478 printTree(p, node, new NodeTreeResolver()); 478 printTree(p, node, new NodeTreeResolver());
479 } 479 }
480 480
481 /** 481 /**
482 * Prints a human readable form of a tree to the given {@link PrintStream}. The {@link TreeResolver} interface needs 482 * Prints a human readable form of a tree to the given {@link PrintStream}. The
483 * to be implemented to specify how the method can read the tree from plain a object. 483 * {@link TreeResolver} interface needs to be implemented to specify how the method can read the
484 * 484 * tree from plain a object.
485 *
485 * @param p the {@link PrintStream} to print to. 486 * @param p the {@link PrintStream} to print to.
486 * @param o the root object to be printed. 487 * @param o the root object to be printed.
487 * @param resolver an implementation of a tree resolver 488 * @param resolver an implementation of a tree resolver
488 */ 489 */
489 public static void printTree(PrintStream p, Object o, TreeResolver resolver) { 490 public static void printTree(PrintStream p, Object o, TreeResolver resolver) {
535 } 536 }
536 537
537 // I refetch the fields to get declaration order. 538 // I refetch the fields to get declaration order.
538 for (int i = 0; i < childFields.size(); i++) { 539 for (int i = 0; i < childFields.size(); i++) {
539 Field field = childFields.get(i); 540 Field field = childFields.get(i);
540 Class< ? > fieldClass = field.getType(); 541 Class<?> fieldClass = field.getType();
541 String name = field.getName(); 542 String name = field.getName();
542 543
543 long offset = unsafe.objectFieldOffset(field); 544 long offset = unsafe.objectFieldOffset(field);
544 545
545 Object value = getObjectValue(node, fieldClass, offset); 546 Object value = getObjectValue(node, fieldClass, offset);
574 printNewLine(p, level - 1); 575 printNewLine(p, level - 1);
575 p.print("}"); 576 p.print("}");
576 } 577 }
577 } 578 }
578 579
579 private static Object getObjectValue(Object base, Class< ? > fieldClass, long offset) { 580 private static Object getObjectValue(Object base, Class<?> fieldClass, long offset) {
580 if (fieldClass == boolean.class) { 581 if (fieldClass == boolean.class) {
581 return unsafe.getBoolean(base, offset); 582 return unsafe.getBoolean(base, offset);
582 } else if (fieldClass == byte.class) { 583 } else if (fieldClass == byte.class) {
583 return unsafe.getByte(base, offset); 584 return unsafe.getByte(base, offset);
584 } else if (fieldClass == short.class) { 585 } else if (fieldClass == short.class) {
646 */ 647 */
647 public interface TreeResolver { 648 public interface TreeResolver {
648 649
649 /** 650 /**
650 * Returns true if a {@link Field} is filtered from the tree. 651 * Returns true if a {@link Field} is filtered from the tree.
651 * 652 *
652 * @param f the field to check 653 * @param f the field to check
653 */ 654 */
654 boolean isFiltered(Field f); 655 boolean isFiltered(Field f);
655 656
656 /** 657 /**
657 * Returns true if a {@link Field} is a field that contains a data value which should not be traversed 658 * Returns true if a {@link Field} is a field that contains a data value which should not be
658 * recursively. 659 * traversed recursively.
659 * 660 *
660 * @param f the field to check 661 * @param f the field to check
661 * @return true if a the given field is a data field else false. 662 * @return true if a the given field is a data field else false.
662 */ 663 */
663 boolean isDataField(Field f); 664 boolean isDataField(Field f);
664 665
665 /** 666 /**
666 * Returns true if a {@link Field} is a field that contains an {@link Object} which should be recursively 667 * Returns true if a {@link Field} is a field that contains an {@link Object} which should
667 * visited. 668 * be recursively visited.
668 * 669 *
669 * @param f the field to check 670 * @param f the field to check
670 * @return true if a the given field is a child field else false. 671 * @return true if a the given field is a child field else false.
671 */ 672 */
672 boolean isChildObject(Field f); 673 boolean isChildObject(Field f);
673 674
674 /** 675 /**
675 * Returns true if a {@link Field} is a field that contains any kind of list/array structure which itself holds 676 * Returns true if a {@link Field} is a field that contains any kind of list/array structure
676 * values that should be recursively visited. 677 * which itself holds values that should be recursively visited.
677 * 678 *
678 * @param f the field to check 679 * @param f the field to check
679 * @return true if a the given field is a child array/list field else false. 680 * @return true if a the given field is a child array/list field else false.
680 */ 681 */
681 boolean isChildArrayObject(Field f); 682 boolean isChildArrayObject(Field f);
682 683
683 /** 684 /**
684 * Converts an given child array object to array which can be traversed. This is especially useful to convert 685 * Converts an given child array object to array which can be traversed. This is especially
685 * any kind of list structure to a traversable array. 686 * useful to convert any kind of list structure to a traversable array.
686 * 687 *
687 * @param f the field for meta data needed to convert the data {@link Object}. 688 * @param f the field for meta data needed to convert the data {@link Object}.
688 * @param value the actual value of the child array/list object. 689 * @param value the actual value of the child array/list object.
689 * @return the converted {@link Object} array. 690 * @return the converted {@link Object} array.
690 */ 691 */
691 Object[] convertToArray(Field f, Object value); 692 Object[] convertToArray(Field f, Object value);
692 693
693 /** 694 /**
694 * Returns a human readable string for any data field object in the tree. 695 * Returns a human readable string for any data field object in the tree.
695 * 696 *
696 * @param o the object to convert to string. 697 * @param o the object to convert to string.
697 * @return the converted string 698 * @return the converted string
698 */ 699 */
699 String toString(Object o); 700 String toString(Object o);
700 } 701 }