io.vertx.codegen.annotations.VertxGen Java Examples

The following examples show how to use io.vertx.codegen.annotations.VertxGen. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClassTest.java    From vertx-codegen with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotated() throws Exception {
  ClassModel model = new GeneratorHelper().generateClass(Annotated.class);
  assertFalse(model.getAnnotations().isEmpty());
  assertEquals(2, model.getAnnotations().size());
  assertEquals(VertxGen.class.getName(), model.getAnnotations().get(0).getName());
  assertEquals(EmptyAnnotation.class.getName(), model.getAnnotations().get(1).getName());
  assertFalse(model.getMethodAnnotations().values().isEmpty());
  assertEquals(1, model.getMethodAnnotations().get("stringAnnotated").size());
  assertEquals(3, model.getMethodAnnotations().get("stringAnnotated").get(0).getMembersNames().size());
  assertNotNull(model.getMethodAnnotations().get("stringAnnotated").get(0).getMember("value"));
  assertNotNull(model.getMethodAnnotations().get("stringAnnotated").get(0).getMember("array"));
  assertNotNull(model.getMethodAnnotations().get("stringAnnotated").get(0).getMember("defaultValue"));
  assertEquals("aString", model.getMethodAnnotations().get("stringAnnotated").get(0).getMember("value"));
  assertArrayEquals(new String[]{"one", "two"}, ((List) model.getMethodAnnotations().get("stringAnnotated").get(0).getMember("array")).toArray());
  assertEquals("defaultString", model.getMethodAnnotations().get("stringAnnotated").get(0).getMember("defaultValue"));
}
 
Example #2
Source File: GeneratorHelper.java    From vertx-codegen with Apache License 2.0 5 votes vote down vote up
private MyProcessor(Function<CodeGen, R> f, Set<String> otherSupportedAnnotations) {
  this.f = f;
  this.supportedAnnotations = new HashSet<>();
  this.supportedAnnotations.add(ProxyGen.class.getCanonicalName());
  this.supportedAnnotations.add(VertxGen.class.getCanonicalName());
  this.supportedAnnotations.add(DataObject.class.getCanonicalName());
  this.supportedAnnotations.add(ModuleGen.class.getCanonicalName());
  this.supportedAnnotations.addAll(otherSupportedAnnotations);
}
 
Example #3
Source File: ModuleModel.java    From vertx-codegen with Apache License 2.0 5 votes vote down vote up
private static boolean isLegalJsonType(TypeMirror type) {
  ClassKind kind = ClassKind.getKind(type.toString(), type.getAnnotation(VertxGen.class) != null);
  return kind.json ||
    type.toString().equals("java.lang.Boolean") ||
    type.toString().equals("java.lang.Number") ||
    kind == ClassKind.STRING;
}
 
Example #4
Source File: EnumCheatsheetGen.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Arrays.asList(VertxGen.class, ModuleGen.class);
}
 
Example #5
Source File: ClassTest.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Test
public void testJsonMapper() throws Exception {
  ClassModel model = new GeneratorHelper()
    .registerConverter(MyPojo.class, WithMyPojo.class.getName(), "serializeMyPojo")
    .registerConverter(MyPojo.class, WithMyPojo.class.getName(), "deserializeMyPojo")
    .generateClass(WithMyPojo.class);
  assertFalse(model.getAnnotations().isEmpty());
  assertEquals(1, model.getAnnotations().size());
  assertEquals(VertxGen.class.getName(), model.getAnnotations().get(0).getName());

  assertEquals(1, model.getReferencedDataObjectTypes().size());
  assertEquals("MyPojo", model.getReferencedDataObjectTypes().iterator().next().getSimpleName());

  checkMethod(model.getMethodMap().get("returnMyPojo").get(0), "returnMyPojo", 0, new TypeLiteral<MyPojo>() {}, MethodKind.OTHER);

  MethodInfo returnMyPojoList = model.getMethodMap().get("returnMyPojoList").get(0);
  checkMethod(returnMyPojoList, "returnMyPojoList", 0, new TypeLiteral<List<MyPojo>>() {}, MethodKind.OTHER);
  assertEquals(ClassKind.LIST, returnMyPojoList.getReturnType().getKind());
  assertEquals(ClassKind.OTHER, ((ParameterizedTypeInfo)returnMyPojoList.getReturnType()).getArg(0).getKind());
  assertEquals(MyPojo.class.getName(), ((ParameterizedTypeInfo)returnMyPojoList.getReturnType()).getArg(0).getName());

  MethodInfo returnMyPojoSet = model.getMethodMap().get("returnMyPojoSet").get(0);
  checkMethod(returnMyPojoSet, "returnMyPojoSet", 0, new TypeLiteral<Set<MyPojo>>() {}, MethodKind.OTHER);
  assertEquals(ClassKind.SET, returnMyPojoSet.getReturnType().getKind());
  assertEquals(ClassKind.OTHER, ((ParameterizedTypeInfo)returnMyPojoSet.getReturnType()).getArg(0).getKind());
  assertEquals(MyPojo.class.getName(), ((ParameterizedTypeInfo)returnMyPojoSet.getReturnType()).getArg(0).getName());

  MethodInfo returnMyPojoMap = model.getMethodMap().get("returnMyPojoMap").get(0);
  checkMethod(returnMyPojoMap, "returnMyPojoMap", 0, new TypeLiteral<Map<String, MyPojo>>() {}, MethodKind.OTHER);
  assertEquals(ClassKind.MAP, returnMyPojoMap.getReturnType().getKind());
  assertEquals(ClassKind.OTHER, ((ParameterizedTypeInfo)returnMyPojoMap.getReturnType()).getArg(1).getKind());
  assertEquals(MyPojo.class.getName(), ((ParameterizedTypeInfo)returnMyPojoMap.getReturnType()).getArg(1).getName());

  MethodInfo myPojoParam = model.getMethodMap().get("myPojoParam").get(0);
  checkMethod(myPojoParam, "myPojoParam", 1, "void", MethodKind.OTHER);
  checkParam(myPojoParam.getParam(0), "p", MyPojo.class.getName(), ClassKind.OTHER);

  MethodInfo myPojoListParam = model.getMethodMap().get("myPojoListParam").get(0);
  checkMethod(myPojoListParam, "myPojoListParam", 1, "void", MethodKind.OTHER);
  checkParam(myPojoListParam.getParam(0), "p", new TypeLiteral<List<MyPojo>>() {}.type.getTypeName(), ClassKind.LIST);
  assertEquals(ClassKind.OTHER, ((ParameterizedTypeInfo)myPojoListParam.getParam(0).getType()).getArg(0).getKind());

  MethodInfo myPojoSetParam = model.getMethodMap().get("myPojoSetParam").get(0);
  checkMethod(myPojoSetParam, "myPojoSetParam", 1, "void", MethodKind.OTHER);
  checkParam(myPojoSetParam.getParam(0), "p", new TypeLiteral<Set<MyPojo>>() {}.type.getTypeName(), ClassKind.SET);
  assertEquals(ClassKind.OTHER, ((ParameterizedTypeInfo)myPojoSetParam.getParam(0).getType()).getArg(0).getKind());

  MethodInfo myPojoMapParam = model.getMethodMap().get("myPojoMapParam").get(0);
  checkMethod(myPojoMapParam, "myPojoMapParam", 1, "void", MethodKind.OTHER);
  checkParam(myPojoMapParam.getParam(0), "p", new TypeLiteral<Map<String, MyPojo>>() {}.type.getTypeName(), ClassKind.MAP);
  assertEquals(ClassKind.OTHER, ((ParameterizedTypeInfo)myPojoMapParam.getParam(0).getType()).getArg(1).getKind());

  MethodInfo myPojoHandler = model.getMethodMap().get("myPojoHandler").get(0);
  checkMethod(myPojoHandler, "myPojoHandler", 1, "void", MethodKind.HANDLER);
  checkParam(myPojoHandler.getParam(0), "handler", new TypeLiteral<Handler<MyPojo>>() {}.type.getTypeName(), ClassKind.HANDLER);
  assertEquals(ClassKind.OTHER, ((ParameterizedTypeInfo)myPojoHandler.getParam(0).getType()).getArg(0).getKind());

  MethodInfo myPojoAsyncResultHandler = model.getMethodMap().get("myPojoAsyncResultHandler").get(0);
  checkMethod(myPojoAsyncResultHandler, "myPojoAsyncResultHandler", 1, "void", MethodKind.FUTURE);
  checkParam(myPojoAsyncResultHandler.getParam(0), "handler", new TypeLiteral<Handler<AsyncResult<MyPojo>>>() {}.type.getTypeName(), ClassKind.HANDLER);
  assertEquals(ClassKind.OTHER, ((ParameterizedTypeInfo)((ParameterizedTypeInfo)myPojoAsyncResultHandler.getParam(0).getType()).getArg(0)).getArg(0).getKind());

}
 
Example #6
Source File: TestGenerator.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Collections.singletonList(VertxGen.class);
}
 
Example #7
Source File: TestGenerator.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Arrays.asList(VertxGen.class);
}
 
Example #8
Source File: TestGenerator.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Arrays.asList(DataObject.class, ModuleGen.class, VertxGen.class);
}
 
Example #9
Source File: TestGenerator.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Arrays.asList(DataObject.class, VertxGen.class);
}
 
Example #10
Source File: TestGenerator.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Collections.singletonList(VertxGen.class);
}
 
Example #11
Source File: TestGenerator1.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Collections.singletonList(VertxGen.class);
}
 
Example #12
Source File: TestGenerator2.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Collections.singletonList(VertxGen.class);
}
 
Example #13
Source File: TypeReflectionFactory.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
public static TypeInfo create(Type type) {
  if (type == void.class) {
    return VoidTypeInfo.INSTANCE;
  } else if (type instanceof Class) {
    String fqcn = type.getTypeName();
    Class<?> classType = (Class<?>) type;
    if (classType.isPrimitive()) {
      return PrimitiveTypeInfo.PRIMITIVES.get(classType.getName());
    } else {
      Package pkg = classType.getPackage();
      ModuleInfo module = null;
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader(classType.getClassLoader());
      try {
        while (pkg != null) {
          ModuleGen annotation = pkg.getAnnotation(ModuleGen.class);
          if (annotation != null) {
            module = new ModuleInfo(pkg.getName(), annotation.name(), annotation.groupPackage());
            break;
          } else {
            int pos = pkg.getName().lastIndexOf('.');
            if (pos == -1) {
              break;
            } else {
              pkg = Package.getPackage(pkg.getName().substring(0, pos));
            }
          }
        }
      } finally {
        Thread.currentThread().setContextClassLoader(loader);
      }
      if (classType.isEnum()) {
        return new EnumTypeInfo(
          fqcn,
          classType.getDeclaredAnnotation(VertxGen.class) != null,
          Stream.of(classType.getEnumConstants()).map(Object::toString).collect(Collectors.toList()),
          module,
          false
        );
      } else {
        ClassKind kind = ClassKind.getKind(fqcn, classType.getAnnotation(VertxGen.class) != null);
        List<TypeParamInfo.Class> typeParams = new ArrayList<>();
        int index = 0;
        for (java.lang.reflect.TypeVariable<? extends Class<?>> var : classType.getTypeParameters()) {
          typeParams.add(new TypeParamInfo.Class(classType.getName(), index++, var.getName()));
        }
        if (kind == ClassKind.API) {
          java.lang.reflect.TypeVariable<Class<Handler>> classTypeVariable = Handler.class.getTypeParameters()[0];
          Type handlerArg = Helper.resolveTypeParameter(type, classTypeVariable);
          return new ApiTypeInfo(fqcn, true, typeParams, handlerArg != null ? create(handlerArg) : null, module, false, false, null);
        } else {

          boolean serializable = isDataObjectAnnotatedSerializable(classType);
          boolean deserializable = isDataObjectAnnotatedDeserializable(classType);
          MapperInfo serializer = null;
          if (serializable) {
            serializer = new MapperInfo();
            serializer.setQualifiedName(fqcn);
            serializer.setKind(MapperKind.SELF);
          }
          MapperInfo deserializer = null;
          if (deserializable) {
            deserializer = new MapperInfo();
            deserializer.setQualifiedName(fqcn);
            deserializer.setKind(MapperKind.SELF);
          }
          DataObjectInfo dataObject = null;
          if (serializable || serializable) {
            dataObject = new DataObjectInfo(serializer, deserializer);
          }
          return new ClassTypeInfo(kind, fqcn, module, false, typeParams, dataObject);
        }
      }
    }
  } else if (type instanceof ParameterizedType) {
    ParameterizedType parameterizedType = (ParameterizedType) type;
    List<TypeInfo> args = Arrays.asList(parameterizedType.getActualTypeArguments()).
      stream().
      map(TypeReflectionFactory::create).
      collect(Collectors.toList());
    Type raw = parameterizedType.getRawType();
    return new ParameterizedTypeInfo((ClassTypeInfo) create(raw), false, args);
  } else if (type instanceof java.lang.reflect.TypeVariable) {
    java.lang.reflect.TypeVariable typeVar = (java.lang.reflect.TypeVariable) type;
    TypeParamInfo param = TypeParamInfo.create(typeVar);
    return new TypeVariableInfo(param, false, ((java.lang.reflect.TypeVariable) type).getName());
  } else {
    throw new IllegalArgumentException("Unsupported type " + type);
  }
}
 
Example #14
Source File: AbstractRxGenerator.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Arrays.asList(VertxGen.class, ModuleGen.class);
}
 
Example #15
Source File: InvalidParams4.java    From vertx-service-proxy with Apache License 2.0 votes vote down vote up
void someMethod(String str, Handler<AsyncResult<VertxGen>> wrongResultType); 
Example #16
Source File: InvalidParams1.java    From vertx-service-proxy with Apache License 2.0 votes vote down vote up
void someMethod(VertxGen vertxGen); 
Example #17
Source File: InvalidReturn2.java    From vertx-service-proxy with Apache License 2.0 votes vote down vote up
VertxGen someMethod();