2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.util.json;
21 import gov.nasa.jpf.vm.ElementInfo;
22 import gov.nasa.jpf.vm.MJIEnv;
24 import java.util.HashMap;
26 public class CreatorsFactory {
28 static private final HashMap<String, Creator> creatorsTable = new HashMap<String, Creator>();
31 creatorsTable.put("java.lang.Boolean", new BoxedBoolCreator());
32 creatorsTable.put("java.lang.Byte", new BoxedByteCreator());
33 creatorsTable.put("java.lang.Short", new BoxedShortCreator());
34 creatorsTable.put("java.lang.Integer", new BoxedIntCreator());
35 creatorsTable.put("java.lang.Long", new BoxedLongCreator());
36 creatorsTable.put("java.lang.Float", new BoxedFloatCreator());
37 creatorsTable.put("java.lang.Double", new BoxedDoubleCreator());
38 creatorsTable.put("java.lang.String", new StringCreator());
41 public static Creator getCreator(String typeName) {
43 return creatorsTable.get(typeName);
48 class BoxedBoolCreator implements Creator {
50 public int create(MJIEnv env, String typeName, Value value) {
51 Boolean read = value.getBoolean();
52 int boolRef = MJIEnv.NULL;
55 boolRef = env.newObject("java.lang.Boolean");
56 ElementInfo ei = env.getModifiableElementInfo(boolRef);
57 ei.setBooleanField("value", (read == true));
64 class BoxedByteCreator implements Creator {
66 public int create(MJIEnv env, String typeName, Value value) {
67 Double read = value.getDouble();
68 int byteRef = MJIEnv.NULL;
71 byteRef = env.newObject("java.lang.Byte");
72 ElementInfo ei = env.getModifiableElementInfo(byteRef);
73 ei.setByteField("value", read.byteValue());
80 class BoxedShortCreator implements Creator {
82 public int create(MJIEnv env, String typeName, Value value) {
83 Double read = value.getDouble();
84 int shortRef = MJIEnv.NULL;
87 shortRef = env.newObject("java.lang.Short");
88 ElementInfo ei = env.getModifiableElementInfo(shortRef);
89 ei.setShortField("value", read.shortValue());
96 class BoxedIntCreator implements Creator {
98 public int create(MJIEnv env, String typeName, Value value) {
99 Double read = value.getDouble();
100 int intRef = MJIEnv.NULL;
103 intRef = env.newObject("java.lang.Integer");
104 ElementInfo ei = env.getModifiableElementInfo(intRef);
105 ei.setIntField("value", read.intValue());
112 class BoxedLongCreator implements Creator {
114 public int create(MJIEnv env, String typeName, Value value) {
115 Double read = value.getDouble();
116 int longRef = MJIEnv.NULL;
119 longRef = env.newObject("java.lang.Long");
120 ElementInfo ei = env.getModifiableElementInfo(longRef);
121 ei.setLongField("value", read.longValue());
128 class BoxedFloatCreator implements Creator {
130 public int create(MJIEnv env, String typeName, Value value) {
131 Double read = value.getDouble();
132 int floatRef = MJIEnv.NULL;
135 floatRef = env.newObject("java.lang.Float");
136 ElementInfo ei = env.getModifiableElementInfo(floatRef);
137 ei.setFloatField("value", read.floatValue());
144 class BoxedDoubleCreator implements Creator {
146 public int create(MJIEnv env, String typeName, Value value) {
147 Double read = value.getDouble();
148 int doubleRef = MJIEnv.NULL;
151 doubleRef = env.newObject("java.lang.Double");
152 ElementInfo ei = env.getModifiableElementInfo(doubleRef);
153 ei.setDoubleField("value", read.doubleValue());
160 class StringCreator implements Creator {
163 public int create(MJIEnv env, String typeName, Value value) {
164 String strVal = value.getString();
165 int stringRef = MJIEnv.NULL;
167 if (strVal != null) {
168 stringRef = env.newString(strVal);