comparison graal/com.oracle.truffle.object/src/com/oracle/truffle/object/debug/JSONShapeVisitor.java @ 18408:2c3666f44855

Truffle: initial commit of object API implementation
author Andreas Woess <andreas.woess@jku.at>
date Tue, 18 Nov 2014 23:19:43 +0100
parents
children b1530a6cce8c
comparison
equal deleted inserted replaced
18407:f439fdb137a3 18408:2c3666f44855
1 /*
2 * Copyright (c) 2014, 2014, 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 package com.oracle.truffle.object.debug;
24
25 import java.util.*;
26 import java.util.Map.Entry;
27
28 import com.oracle.truffle.api.object.*;
29 import com.oracle.truffle.api.utilities.*;
30 import com.oracle.truffle.api.utilities.JSONHelper.JSONArrayBuilder;
31 import com.oracle.truffle.api.utilities.JSONHelper.JSONObjectBuilder;
32 import com.oracle.truffle.object.*;
33 import com.oracle.truffle.object.Transition.*;
34
35 public class JSONShapeVisitor extends DebugShapeVisitor<JSONObjectBuilder> {
36 @Override
37 public JSONObjectBuilder visitShape(Shape shape, Map<? extends Transition, ? extends Shape> transitions) {
38 JSONObjectBuilder sb = JSONHelper.object();
39 JSONArrayBuilder transitionarray = JSONHelper.array();
40 for (Entry<? extends Transition, ? extends Shape> entry : transitions.entrySet()) {
41 transitionarray.add(JSONHelper.object().add("transition", dumpTransition(entry.getKey())).add("successor", getId(entry.getValue())));
42 }
43 JSONArrayBuilder propertiesarray = JSONHelper.array();
44 for (Property p : shape.getPropertyList()) {
45 propertiesarray.add(dumpProperty(p));
46 }
47 sb.add("id", getId(shape));
48 sb.add("properties", propertiesarray);
49 sb.add("transitions", transitionarray);
50 sb.add("predecessor", shape.getParent() != null ? getId(shape.getParent()) : null);
51 sb.add("valid", shape.isValid());
52 return sb;
53 }
54
55 public JSONObjectBuilder dumpProperty(Property property) {
56 return JSONHelper.object().add("id", property.getKey().toString()).add("location", dumpLocation(property.getLocation())).add("flags", property.getFlags());
57 }
58
59 public JSONObjectBuilder dumpTransition(Transition transition) {
60 JSONObjectBuilder sb = JSONHelper.object().add("type", transition.getShortName());
61 if (transition instanceof PropertyTransition) {
62 sb.add("property", dumpProperty(((PropertyTransition) transition).getProperty()));
63 }
64 return sb;
65 }
66
67 public JSONObjectBuilder dumpLocation(Location location) {
68 JSONObjectBuilder obj = JSONHelper.object();
69 obj.add("type", (location instanceof TypedLocation ? ((TypedLocation) location).getType() : Object.class).getName());
70 // if (location instanceof Locations.FieldLocation) {
71 // obj.add("offset", ((Locations.FieldLocation) location).getOffset());
72 // }
73 // if (location instanceof Locations.ArrayLocation) {
74 // obj.add("index", ((Locations.ArrayLocation) location).getIndex());
75 // }
76 if (location instanceof Locations.ValueLocation) {
77 obj.add("value", String.valueOf(((Locations.ValueLocation) location).get(null, false)));
78 }
79 return obj;
80 }
81 }