Java源码示例:io.vertx.codegen.type.ClassTypeInfo

示例1
@Override
protected void genRxMethod(ClassModel model, MethodInfo method, List<String> cacheDecls, boolean genBody, PrintWriter writer) {
  ClassTypeInfo type = model.getType();
  String packageName = type.getPackageName();
  writer.print("  ");
  MethodInfo futMethod = genFutureMethod(method);
  startMethodTemplate(type, futMethod, "", writer);
  if (genBody) {
    writer.println(" { ");
    writer.println("    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {");
    writer.print("      ");
    writer.print(method.getName());
    writer.print("(");
    writer.print(futMethod.getParams().stream().map(ParamInfo::getName).collect(Collectors.joining(", ")));
    if (futMethod.getParams().size() > 0) {
      writer.print(", ");
    }
    writer.println("fut);");
    writer.println("    }));");
    writer.println("  }");
  } else {
    writer.println(";");
  }
  writer.println();
}
 
示例2
@Override
public void renderJavaType(ClassTypeInfo javaType) {
  switch (javaType.getKind()) {
    case STRING:
      append("String");
      break;
    case VOID:
      append("Unit");
      break;
    case BOXED_PRIMITIVE:
      renderBasicType(javaType);
      break;
    default:
      append(javaType.getName());
  }
}
 
示例3
public MethodInfo(Set<ClassTypeInfo> ownerTypes, String name,
                  TypeInfo returnType, Text returnDescription, boolean fluent,  boolean cacheReturn,
                  List<ParamInfo> params, String comment, Doc doc, boolean staticMethod, boolean defaultMethod,
                  List<TypeParamInfo.Method> typeParams, boolean deprecated, Text deprecatedDesc) {


  this.comment = comment;
  this.name = name;
  this.returnType = returnType;
  this.returnDescription = returnDescription;
  this.fluent = fluent;
  this.cacheReturn = cacheReturn;
  this.doc = doc;
  this.staticMethod = staticMethod;
  this.defaultMethod = defaultMethod;
  this.params = params;
  this.typeParams = typeParams;
  this.ownerTypes = ownerTypes;
  this.deprecated = deprecated;
  this.deprecatedDesc = deprecatedDesc;
}
 
示例4
private Case getCase(DataObjectModel model) {
  AnnotationValueInfo abc = model
    .getAnnotations()
    .stream().filter(ann -> ann.getName().equals(DataObject.class.getName()))
    .findFirst().get();
  ClassTypeInfo cti = (ClassTypeInfo) abc.getMember("jsonPropertyNameFormatter");
  switch (cti.getName()) {
    case "io.vertx.codegen.format.CamelCase":
      return CamelCase.INSTANCE;
    case "io.vertx.codegen.format.SnakeCase":
      return SnakeCase.INSTANCE;
    case "io.vertx.codegen.format.LowerCamelCase":
      return LowerCamelCase.INSTANCE;
    default:
      throw new UnsupportedOperationException("Todo");
  }
}
 
示例5
public static String generateDeserializeDataObject(String stmt, ClassTypeInfo doTypeInfo) {
  MapperInfo deserializer = doTypeInfo.getDataObject().getDeserializer();
  String s;
  switch (deserializer.getKind()) {
    case SELF:
      s = String.format("new %s((%s)%s)", doTypeInfo.getName(), doTypeInfo.getDataObject().getJsonType().getSimpleName(), stmt);
      break;
    case STATIC_METHOD:
      StringBuilder sb = new StringBuilder(deserializer.getQualifiedName());
      deserializer.getSelectors().forEach(selector -> {
        sb.append('.').append(selector);
      });
      sb.append("((").append(deserializer.getJsonType().getSimpleName()).append(')').append(stmt).append(')');
      s =  sb.toString();
      break;
    default:
      throw new AssertionError();
  }
  return String.format("%s != null ? %s : null", stmt, s);
}
 
示例6
public static String generateSerializeDataObject(String stmt, ClassTypeInfo doTypeInfo) {
  MapperInfo serializer = doTypeInfo.getDataObject().getSerializer();
  StringBuilder sb = new StringBuilder();
  serializer.getSelectors().forEach(selector -> {
    sb.append('.').append(selector);
  });
  String s;
  switch (serializer.getKind()) {
    case SELF:
      s = stmt + sb + "()";
      break;
    case STATIC_METHOD:
      s = serializer.getQualifiedName() + sb + "(" + stmt + ")";
      break;
    default:
      throw new AssertionError();
  }
  return String.format("%s != null ? %s : null", stmt, s);
}
 
示例7
private Case getCase(DataObjectModel model, String name) {
  AnnotationValueInfo abc = getAnnotation(model).get();
  ClassTypeInfo cti = (ClassTypeInfo) abc.getMember(name);
  switch (cti.getName()) {
    case "io.vertx.codegen.format.CamelCase":
      return CamelCase.INSTANCE;
    case "io.vertx.codegen.format.SnakeCase":
      return SnakeCase.INSTANCE;
    case "io.vertx.codegen.format.LowerCamelCase":
      return LowerCamelCase.INSTANCE;
    default:
      throw new UnsupportedOperationException();
  }
}
 
示例8
private static Function<String, String> getter_(TypeInfo type, boolean isArray) {
  String getter = getter(type);
  if (getter != null) {
    return col -> "row." + getter + (isArray ? "Array" : "") + "(\"" + col + "\")";
  }
  if (type.getKind() == ClassKind.ENUM || type instanceof ClassTypeInfo) {
    if (isArray) {
      return col -> "row.get(" + type.getName() + "[].class, \"" + col + "\")";
    } else {
      return col -> "row.get(" + type.getName() + ".class, \"" + col + "\")";
    }
  }
  return null;
}
 
示例9
@Override
protected void genRxMethod(ClassModel model, MethodInfo method, List<String> cacheDecls, boolean genBody, PrintWriter writer) {
  MethodInfo futMethod = genFutureMethod(method);
  ClassTypeInfo raw = futMethod.getReturnType().getRaw();
  String methodSimpleName = raw.getSimpleName();
  String adapterType = "AsyncResult" + methodSimpleName + ".to" + methodSimpleName;
  String rxType = raw.getName();
  startMethodTemplate(model.getType(), futMethod, "", writer);
  if (genBody) {
    writer.println(" { ");
    writer.print("    return ");
    writer.print(adapterType);
    writer.println("($handler -> {");
    writer.print("      ");
    writer.print(method.getName());
    writer.print("(");
    List<ParamInfo> params = futMethod.getParams();
    writer.print(params.stream().map(ParamInfo::getName).collect(Collectors.joining(", ")));
    if (params.size() > 0) {
      writer.print(", ");
    }
    writer.println("$handler);");
    writer.println("    });");
    writer.println("  }");
  } else {
    writer.println(";");
  }
  writer.println();
}
 
示例10
@Override
protected MethodInfo createMethodInfo(Set<ClassTypeInfo> ownerTypes, String methodName, String comment, Doc doc, TypeInfo returnType, Text returnDescription, boolean isFluent, boolean isCacheReturn, List<ParamInfo> mParams, ExecutableElement methodElt, boolean isStatic, boolean isDefault, ArrayList<TypeParamInfo.Method> typeParams, TypeElement declaringElt, boolean methodDeprecated, Text deprecatedDesc) {
  ProxyMethodInfo baseInfo = (ProxyMethodInfo) super.createMethodInfo(ownerTypes, methodName, comment, doc, returnType, returnDescription, isFluent, isCacheReturn, mParams, methodElt, isStatic, isDefault, typeParams, declaringElt, methodDeprecated, deprecatedDesc);
  if (!isStatic && !baseInfo.isProxyClose()) {
    // Check signature constraints

    TypeInfo ret;
    if (baseInfo.getKind() == MethodKind.FUTURE) {
      ret = ((ParameterizedTypeInfo) ((ParameterizedTypeInfo) mParams.get(mParams.size() - 1).getType()).getArg(0)).getArg(0);
    } else {
      throw new GenException(methodElt, SIGNATURE_CONSTRAINT_ERROR);
    }
    if (!ServiceResponse.class.getName().equals(ret.getName())) {
      throw new GenException(methodElt, SIGNATURE_CONSTRAINT_ERROR);
    }

    TypeInfo shouldBeServiceRequest;
    if (baseInfo.getKind() == MethodKind.FUTURE) {
      if (mParams.size() <= 1) {
        throw new GenException(methodElt, SIGNATURE_CONSTRAINT_ERROR);
      }
      shouldBeServiceRequest = mParams.get(mParams.size() - 2).getType();
    } else {
      if (mParams.size() == 0) {
        throw new GenException(methodElt, SIGNATURE_CONSTRAINT_ERROR);
      }
      shouldBeServiceRequest = mParams.get(mParams.size() - 1).getType();
    }
    if (!ServiceRequest.class.getName().equals(shouldBeServiceRequest.getName())) {
      throw new GenException(methodElt, SIGNATURE_CONSTRAINT_ERROR);
    }

    return new WebApiProxyMethodInfo(baseInfo);
  } else {
    return baseInfo;
  }
}
 
示例11
@Override
public String render(RunnableCompilationUnit unit, RenderMode renderMode) {
  GroovyWriter writer = newWriter();
  if (renderMode != RenderMode.SNIPPET) {
    if (unit.getFields().size() > 0) {
      writer.append("import groovy.transform.Field\n");
    }
    for (ClassTypeInfo importedType : imports) {
      String fqn = importedType.getName();
      writer.append("import ").append(fqn).append('\n');
    }
    for (Map.Entry<String, StatementModel> field : unit.getFields().entrySet()) {
      writer.append("@Field ");
      field.getValue().render(writer);
      writer.append("\n");
    }
    for (Map.Entry<String, MethodModel> method : unit.getMethods().entrySet()) {
      writer.append("def ").append(method.getKey()).append("(");
      for (Iterator<String> it = method.getValue().getParameterNames().iterator(); it.hasNext(); ) {
        String paramName = it.next();
        writer.append(paramName);
        if (it.hasNext()) {
          writer.append(", ");
        }
      }
      writer.append(") {\n");
      writer.indent();
      method.getValue().render(writer);
      writer.unindent();
      writer.append("}\n");
    }
  }
  unit.getMain().render(writer);
  return writer.getBuffer().toString();
}
 
示例12
@Override
public void renderToDataObject(JsonObjectModel model, ClassTypeInfo type) {
  append(type.getSimpleName());
  append("(");
  model.render(this);
  append(")");

}
 
示例13
private void renderType(TypeInfo type, KotlinCodeWriter renderer) {
  if (type instanceof ApiTypeInfo) {
    renderer.renderApiType((ApiTypeInfo) type);
  } else if (type instanceof ClassTypeInfo) {
    renderer.renderJavaType((ClassTypeInfo) type);
  } else if (type instanceof PrimitiveTypeInfo) {
    renderer.renderBasicType(type);
  } else {
    renderer.append(type.getName());
  }
}
 
示例14
private void addImport(ClassTypeInfo importedType) {
    String fqn = importedType.getName();
/*
    if (importedType instanceof ApiTypeInfo) {
      fqn = importedType.translateName("kotlin");
    }
*/
    imports.add(fqn);
  }
 
示例15
public Stream<String> additionalImports(ProxyModel model) {
  return Stream
    .concat(
      model.getImportedTypes().stream(),
      model.getReferencedDataObjectTypes()
        .stream()
        .filter(t -> t.isDataObjectHolder() && t.getDataObject().getJsonType() instanceof ClassTypeInfo)
        .map(t -> (ClassTypeInfo) t.getDataObject().getJsonType())
    )
    .filter(c -> !c.getPackageName().equals("java.lang") && !c.getPackageName().equals("io.vertx.core.json"))
    .map(ClassTypeInfo::toString)
    .distinct();
}
 
示例16
public ProxyMethodInfo(Set<ClassTypeInfo> ownerTypes, String name, TypeInfo returnType, Text returnDescription, boolean fluent,
                       boolean cacheReturn, List<ParamInfo> params, String comment, Doc doc, boolean staticMethod, boolean defaultMethod,
                       List<TypeParamInfo.Method> typeParams, boolean proxyIgnore, boolean proxyClose, boolean deprecated, Text deprecatedDesc) {


  super(ownerTypes, name, returnType, returnDescription, fluent, cacheReturn, params, comment, doc, staticMethod, defaultMethod, typeParams, deprecated, deprecatedDesc);
  this.proxyIgnore = proxyIgnore;
  this.proxyClose = proxyClose;
}
 
示例17
private static String getter(TypeInfo type) {
  switch (type.getKind()) {
    case PRIMITIVE:
      PrimitiveTypeInfo pt = (PrimitiveTypeInfo) type;
      return getter(pt.getBoxed());
    case BOXED_PRIMITIVE:
      return "get" + type.getSimpleName();
    case STRING:
      return "getString";
    case JSON_OBJECT:
      return "getJsonObject";
    case JSON_ARRAY:
      return "getJsonArray";
  }
  if (type instanceof ClassTypeInfo) {
    DataObjectInfo dataObject = type.getDataObject();
    if (dataObject != null) {
      return getter(dataObject.getJsonType());
    }
    ClassTypeInfo ct = (ClassTypeInfo) type;
    switch (ct.getName()) {
      case "java.time.LocalDateTime":
        return "getLocalDateTime";
      case "java.time.LocalDate":
        return "getLocalDate";
      case "java.time.LocalTime":
        return "getLocalTime";
      case "java.time.OffsetTime":
        return "getOffsetTime";
      case "java.time.OffsetDateTime":
        return "getOffsetDateTime";
      case "java.time.temporal.Temporal":
        return "getTemporal";
      case "java.util.UUID":
        return "getUUID";
      case "io.vertx.core.buffer.Buffer":
        return "getBuffer";
    }
  }
  return null;
}
 
示例18
public WebApiProxyMethodInfo(Set<ClassTypeInfo> ownerTypes, String name, TypeInfo returnType, Text returnDescription, boolean fluent, boolean cacheReturn, List<ParamInfo> params, String comment, Doc doc, boolean staticMethod, boolean defaultMethod, List<TypeParamInfo.Method> typeParams, boolean proxyIgnore, boolean proxyClose, boolean deprecated, Text deprecatedDesc) {
  super(ownerTypes, name, returnType, returnDescription, fluent, cacheReturn, params, comment, doc, staticMethod, defaultMethod, typeParams, proxyIgnore, proxyClose, deprecated, deprecatedDesc);
  paramsToExtract = params.subList(0, params.size() - 2);
  requestContextName = params.get(params.size() - 2).getName();
}
 
示例19
default DataObjectClassModel dataObjectClass(ClassTypeInfo type) {
  return new DataObjectClassModel(this, type);
}
 
示例20
@Override
public void renderJavaType(ClassTypeInfo javaType) {
  append(javaType.getName());
}
 
示例21
@Override
public void renderToDataObject(JsonObjectModel model, ClassTypeInfo type) {
  model.render(this);
}
 
示例22
@Override
public DataObjectClassModel dataObjectClass(ClassTypeInfo type) {
  addImport(type);
  imports.add(type.translatePackageName("kotlin") + ".*");
  return CodeBuilder.super.dataObjectClass(type);
}
 
示例23
public DataObjectLiteralModel(CodeBuilder builder, ClassTypeInfo type) {
  this(builder, type, Collections.emptyMap());
}
 
示例24
public DataObjectLiteralModel(CodeBuilder builder, ClassTypeInfo type, Map<String, Member> members) {
  super(builder);
  this.type = type;
  this.members = members;
}
 
示例25
public ClassTypeInfo getType() {
  return type;
}
 
示例26
public ThrowableClassModel(CodeBuilder builder, ClassTypeInfo type) {
  super(builder);
  this.type = type;
}
 
示例27
public JavaClassModel(CodeBuilder builder, ClassTypeInfo type) {
  super(builder);
  this.type = type;
}
 
示例28
public DataObjectClassModel(CodeBuilder builder, ClassTypeInfo type) {
  super(builder);
  this.type = type;
}
 
示例29
public Set<ClassTypeInfo> getOwnerTypes() {
  return ownerTypes;
}
 
示例30
public MethodInfo setOwnerTypes(Set<ClassTypeInfo> ownerTypes) {
  this.ownerTypes = ownerTypes;
  return this;
}