@Test void serialization() { MethodBehavior isInstancePrecomputed = cache.get(IS_INSTANCE_SIGNATURE); assertThat(isInstancePrecomputed).isNotNull(); String isInstanceSerialized = gson.toJson(isInstancePrecomputed); assertThat(isInstanceSerialized).isEqualTo(IS_INSTANCE_METHOD_BEHAVIOR); }
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);  }