adl icon indicating copy to clipboard operation
adl copied to clipboard

java backend should generate factories/jsonbindings for type aliases

Open timbod7 opened this issue 6 years ago • 0 comments

There are circumstances where we need the type expression for the alias (eg inside dynamics). Possibly emit a java class for the type alias, but which only has static factory and jsonbinding methods.

For reference the workaround is a helper function like:

  public static <T> JsonBinding<T> typeAliasJsonBinding(String moduleName, String typeAliasName, JsonBinding<T> jb) {
    return new JsonBinding<T>() {
      public Factory<T> factory() {
        return new Factory<T>() {
          @Override
          public T create() {
            return jb.factory().create();
          }
          @Override
          public T create(T other) {
            return jb.factory().create(other);
          }
          @Override
          public TypeExpr typeExpr() {
            return new TypeExpr(TypeRef.reference(new ScopedName(moduleName, typeAliasName)), new ArrayList<>(); }
        };
      }

      @Override
      public JsonElement toJson(T value) {
        return jb.toJson(value);
      }

      @Override
      public T fromJson(JsonElement json) {
        return jb.fromJson(json);
      }
    };
  }

timbod7 avatar Aug 28 '18 04:08 timbod7