Skip to content

Commit

Permalink
Config Doc - Avoid annotations in primitive type name
Browse files Browse the repository at this point in the history
While this was already correctly handled for declared types, primitive
types don't have an API to get the name without the annotations being
present (toString() contains the annotations and there is no other way
to get a string representation of the type...).

Fixing it by making sure we get to the type name.

This avoids having things like:
@io.smallrye.config.WithConverter(io.quarkus.runtime.init.InitRuntimeConfig.BooleanConverter.class) boolean
in the doc.
Or Bean Validation constraints when they are used.
  • Loading branch information
gsmet committed Aug 31, 2024
1 parent 7d1ea42 commit 74a4268
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ public final String toString() {
return unwrappedType.toString();
}

public static ResolvedType ofPrimitive(TypeMirror unwrappedType) {
String primitiveName = unwrappedType.toString();

return new ResolvedType(unwrappedType, unwrappedType, primitiveName, primitiveName, primitiveName, true, false, false,
public static ResolvedType ofPrimitive(TypeMirror unwrappedType, String typeName) {
return new ResolvedType(unwrappedType, unwrappedType, typeName, typeName, typeName, true, false, false,
false, false, false, false, false, false, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ private void resolveProperty(ConfigRoot configRoot, Map<String, ConfigSection> e
if (discoveryConfigProperty.getType().isMap()) {
// it is a leaf pass through map, it is always optional
optional = true;
typeQualifiedName = discoveryConfigProperty.getType().wrapperType().toString();
typeQualifiedName = utils.element().getQualifiedName(discoveryConfigProperty.getType().wrapperType());
typeSimplifiedName = utils.element().simplifyGenericType(discoveryConfigProperty.getType().wrapperType());

potentiallyMappedPath += ConfigNamingUtil.getMapKey(discoveryConfigProperty.getMapKey());
additionalPaths = additionalPaths.stream()
.map(p -> p + ConfigNamingUtil.getMapKey(discoveryConfigProperty.getMapKey()))
.collect(Collectors.toCollection(ArrayList::new));
} else if (discoveryConfigProperty.getType().isList()) {
typeQualifiedName = discoveryConfigProperty.getType().wrapperType().toString();
typeQualifiedName = utils.element().getQualifiedName(discoveryConfigProperty.getType().wrapperType());
}

PropertyPath propertyPath = new PropertyPath(potentiallyMappedPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ private void scanElement(List<ConfigAnnotationListener> listeners, DiscoveryRoot
}
} else {
TypeMirror superclass = clazz.getSuperclass();
if (superclass.getKind() != TypeKind.NONE && !superclass.toString().equals(Object.class.getName())) {
if (superclass.getKind() != TypeKind.NONE
&& !utils.element().getQualifiedName(superclass).equals(Object.class.getName())) {
TypeElement superclassTypeElement = (TypeElement) ((DeclaredType) superclass).asElement();

debug("Detected superclass: " + superclassTypeElement, clazz);
Expand Down Expand Up @@ -324,7 +325,7 @@ private boolean isEnumAlreadyHandled(TypeElement clazz) {

private ResolvedType resolveType(TypeMirror typeMirror) {
if (typeMirror.getKind().isPrimitive()) {
return ResolvedType.ofPrimitive(typeMirror);
return ResolvedType.ofPrimitive(typeMirror, utils.element().getQualifiedName(typeMirror));
}
if (typeMirror.getKind() == TypeKind.ARRAY) {
ResolvedType resolvedType = resolveType(((ArrayType) typeMirror).getComponentType());
Expand Down Expand Up @@ -369,7 +370,7 @@ private ResolvedType resolveType(TypeMirror typeMirror) {
isConfigGroup = utils.element().isAnnotationPresent(typeElement, Types.ANNOTATION_CONFIG_GROUP);
} else if (typeElement.getKind() == ElementKind.CLASS) {
isClass = true;
isDuration = typeMirror.toString().equals(Duration.class.getName());
isDuration = utils.element().getQualifiedName(typeMirror).equals(Duration.class.getName());
isConfigGroup = utils.element().isAnnotationPresent(typeElement, Types.ANNOTATION_CONFIG_GROUP);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Optional<DiscoveryConfigRoot> onConfigRoot(TypeElement configRoot) {
AnnotationMirror configDocFileNameAnnotation = null;

for (AnnotationMirror annotationMirror : configRoot.getAnnotationMirrors()) {
String annotationName = annotationMirror.getAnnotationType().toString();
String annotationName = utils.element().getQualifiedName(annotationMirror.getAnnotationType());

if (annotationName.equals(Types.ANNOTATION_CONFIG_ROOT)) {
configRootAnnotation = annotationMirror;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Optional<DiscoveryConfigRoot> onConfigRoot(TypeElement configRoot) {
AnnotationMirror configDocFileNameAnnotation = null;

for (AnnotationMirror annotationMirror : configRoot.getAnnotationMirrors()) {
String annotationName = annotationMirror.getAnnotationType().toString();
String annotationName = utils.element().getQualifiedName(annotationMirror.getAnnotationType());

if (annotationName.equals(Types.ANNOTATION_CONFIG_ROOT)) {
configRootAnnotation = annotationMirror;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ public class ElementUtil {
this.processingEnv = processingEnv;
}

public String getQualifiedName(TypeMirror type) {
switch (type.getKind()) {
case BOOLEAN:
return "boolean";
case BYTE:
return "byte";
case CHAR:
return "char";
case DOUBLE:
return "double";
case FLOAT:
return "float";
case INT:
return "int";
case LONG:
return "long";
case SHORT:
return "short";
case DECLARED:
return ((TypeElement) ((DeclaredType) type).asElement()).getQualifiedName().toString();
default:
// note that it includes annotations, which is something we don't want
// thus why all this additional work...
return type.toString();
}
}

public String getBinaryName(TypeElement clazz) {
return processingEnv.getElementUtils().getBinaryName(clazz).toString();
}
Expand Down

0 comments on commit 74a4268

Please sign in to comment.