1 /*
2 * Copyright (c) 2020, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package pseudoScriptEngineCompilable;
27
28 import javax.script.Bindings;
29 import javax.script.ScriptContext;
30 import java.time.Instant;
31 import java.util.ArrayList;
32 import java.util.Set;
33 import java.util.TreeMap;
34
35
36 /** Stores PseudoScriptEngine related invocation information for asserting and debugging. */
37 public class InvocationInfos {
38 public String script;
39 public TreeMap<Integer,TreeMap<String,Object>> bindings;
40 public Instant dateTime;
41
42 InvocationInfos(String script, ScriptContext context) {
43 this.dateTime = Instant.now();
44 this.script = script;
45 this.bindings = new TreeMap();
46 // get and save each Bindings
47 for (Integer scope : context.getScopes()) {
48 Bindings binding = context.getBindings(scope);
49 bindings.put(scope, binding == null ? new TreeMap<String,Object>() : new TreeMap<String,Object>(binding));
50 }
51 }
52
53 /**
54 * Creates and returns a string having all information formatted to ease debugging.
55 * @return string formatted to ease debugging
56 */
57 public String toDebugFormat(String indentation) {
58 StringBuilder sb = new StringBuilder();
59 String indent = (indentation == null ? "\t\t" : indentation);
60 sb.append(indent).append("at: [").append(dateTime.toString()).append("]\n");
61 sb.append(indent).append("script: [").append(script) .append("]\n");
62
63 for (Integer scope : (Set<Integer>) bindings.keySet()) {
64 sb.append(indent).append("Bindings for scope # ").append(scope);
65 if (scope == 100) sb.append(" (ENGINE_SCOPE):");
66 else if (scope == 200) sb.append(" (GLOBAL_SCOPE):");
67 else sb.append(':');
68 sb.append('\n');
69
70 TreeMap<String,Object> treeMap = bindings.get(scope);
71 for (String k : (Set<String>) treeMap.keySet()) {
72 sb.append(indent).append("\t[").append(k).append("]:\t[").append(treeMap.get(k)).append("]\n");
73 }
74 }
75 return sb.toString();
76 }
77 }