@Test void exceptional_yield_serialization_deserialaization() { MethodBehavior mb = newMethodBehavior("org.foo.A.bar(Ljava/lang/Object;)Z"); ExceptionalYield yield = new ExceptionalYield(mb); yield.setExceptionType("java.lang.Exception"); yield.parametersConstraints.add(ConstraintsByDomain.empty().put(ObjectConstraint.NULL)); mb.addYield(yield); mb.completed(); String serialized = gson.toJson(mb); assertThat(serialized).contains("\"yields\": [\n" + " {\n" + " \"parametersConstraints\": [\n" + " [\n" + " \"NULL\"\n" + " ]\n" + " ],\n" + " \"exception\": \"java.lang.Exception\"\n" + " }\n" + " ]"); MethodBehavior deserialized = gson.fromJson(serialized, MethodBehavior.class); assertThat(deserialized).isEqualTo(mb); }
private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; }
MethodBehaviorJsonAdapter implements JsonSerializer<MethodBehavior>, JsonDeserializer<MethodBehavior> { private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; } }
MethodBehaviorJsonAdapter implements JsonSerializer<MethodBehavior>, JsonDeserializer<MethodBehavior> { private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; } private  MethodBehaviorJsonAdapter(); }
MethodBehaviorJsonAdapter implements JsonSerializer<MethodBehavior>, JsonDeserializer<MethodBehavior> { private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; } private  MethodBehaviorJsonAdapter(); static Gson gson(); @Override MethodBehavior deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(MethodBehavior src, Type typeOfSrc, JsonSerializationContext context); }
MethodBehaviorJsonAdapter implements JsonSerializer<MethodBehavior>, JsonDeserializer<MethodBehavior> { private static JsonElement toJson(MethodYield methodYield, int arity) { JsonObject yield = new JsonObject(); JsonArray jsonParameterConstraints = new JsonArray(); for (int i = 0; i < arity; i++) { jsonParameterConstraints.add(toJson(methodYield.parametersConstraints.get(i))); } yield.add(JSON_PARAMETERS_CONSTRAINTS, jsonParameterConstraints); if (methodYield instanceof HappyPathYield) { HappyPathYield happyPathYield = (HappyPathYield) methodYield; yield.addProperty(JSON_RESULT_INDEX, happyPathYield.resultIndex()); yield.add(JSON_RESULT_CONSTRAINTS, toJson(happyPathYield.resultConstraint())); } else if (methodYield instanceof ExceptionalYield) { ExceptionalYield exceptionalYield = (ExceptionalYield) methodYield; yield.addProperty(JSON_THROWN_EXCEPTION, exceptionalYield.getExceptionType()); } else { throw new IllegalStateException("Hardcoded yields should only be HappyPathYield or ExceptionalYield."); } return yield; } private  MethodBehaviorJsonAdapter(); static Gson gson(); @Override MethodBehavior deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(MethodBehavior src, Type typeOfSrc, JsonSerializationContext context);  }