io.vertx.codegen.annotations.ModuleGen Java Examples

The following examples show how to use io.vertx.codegen.annotations.ModuleGen. 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: ModuleInfo.java    From vertx-codegen with Apache License 2.0 6 votes vote down vote up
public static DeclaredType resolveJsonMapper(Elements elementUtils, Types typeUtils, PackageElement pkgElt, DeclaredType javaType) {
  PackageElement result = resolveFirstModuleGenAnnotatedPackageElement(elementUtils, pkgElt);
  if (result != null) {
    TypeElement jsonMapperElt = elementUtils.getTypeElement("io.vertx.core.spi.json.JsonMapper");
    TypeParameterElement typeParamElt = jsonMapperElt.getTypeParameters().get(0);
    return elementUtils
      .getAllAnnotationMirrors(pkgElt)
      .stream()
      .filter(am -> am.getAnnotationType().toString().equals(ModuleGen.class.getName()))
      .flatMap(am -> am.getElementValues().entrySet().stream())
      .filter(e -> e.getKey().getSimpleName().toString().equals("mappers"))
      .flatMap(e -> ((List<AnnotationValue>) e.getValue().getValue()).stream())
      .map(annotationValue -> (DeclaredType) annotationValue.getValue())
      .filter(dt -> {
        TypeMirror mapperType = Helper.resolveTypeParameter(typeUtils, dt, typeParamElt);
        return mapperType != null && mapperType.getKind() == TypeKind.DECLARED && typeUtils.isSameType(mapperType, javaType);
      })
      .findFirst()
      .orElse(null);
  }
  return null;
}
 
Example #2
Source File: ModuleInfo.java    From vertx-codegen with Apache License 2.0 6 votes vote down vote up
public static PackageElement resolveFirstModuleGenAnnotatedPackageElement(Elements elementUtils, PackageElement pkgElt) {
  if (pkgElt == null) return null;
  String pkgQN = pkgElt.getQualifiedName().toString();
  while (true) {
    if (pkgElt != null) {
      ModuleGen annotation = pkgElt.getAnnotation(ModuleGen.class);
      if (annotation != null) {
        return pkgElt;
      }
    }
    int pos = pkgQN.lastIndexOf('.');
    if (pos == -1) {
      break;
    } else {
      pkgQN = pkgQN.substring(0, pos);
      Set<PackageElement> pkgElts = getPackageElement.apply(elementUtils, pkgQN);
      pkgElt = pkgElts.isEmpty() ? null : pkgElts.iterator().next();
    }
  }
  return null;
}
 
Example #3
Source File: ModuleInfo.java    From vertx-codegen with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve a module info for the specified {@code pkgElt} argument, returns null for undertermined.
 *
 * @param elementUtils the element utils
 * @param pkgElt the package element
 * @return the module info
 */
public static ModuleInfo resolve(Elements elementUtils, PackageElement pkgElt) {
  PackageElement result = resolveFirstModuleGenAnnotatedPackageElement(elementUtils, pkgElt);
  if (result != null) {
    ModuleGen annotation = result.getAnnotation(ModuleGen.class);
    return new ModuleInfo(result.getQualifiedName().toString(), annotation.name(), annotation.groupPackage());
  } else return null;
}
 
Example #4
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 #5
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 #6
Source File: WebApiProxyHandlerGen.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Arrays.asList(WebApiServiceGen.class, ModuleGen.class);
}
 
Example #7
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 #8
Source File: DataObjectCheatsheetGen.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);
}
 
Example #9
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 #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 Arrays.asList(DataObject.class, ModuleGen.class);
}
 
Example #11
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 #12
Source File: ServiceProxyHandlerGen.java    From vertx-service-proxy with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Arrays.asList(ProxyGen.class, ModuleGen.class);
}
 
Example #13
Source File: ServiceProxyGen.java    From vertx-service-proxy with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Class<? extends Annotation>> annotations() {
  return Arrays.asList(ProxyGen.class, ModuleGen.class);
}