Skip to content

Commit

Permalink
Render ConfigMapping elements properly in the Configuration editor
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Nov 19, 2024
1 parent 2b2dbe4 commit 93dd39d
Showing 1 changed file with 53 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void accept(Container node) {
String defaultDefault;
final Class<?> valueClass = field.getType();

EffectiveConfigTypeAndValues effectiveConfigTypeAndValues = getTypeName(field);
EffectiveConfigTypeAndValues effectiveConfigTypeAndValues = getTypeName(valueClass, field.getGenericType());

if (valueClass == boolean.class) {
defaultDefault = "false";
Expand All @@ -112,8 +112,8 @@ public void accept(Container node) {
ret.add(new ConfigDescriptionBuildItem(name,
defVal,
javadoc.getProperty(javadocKey),
effectiveConfigTypeAndValues.getTypeName(),
effectiveConfigTypeAndValues.getAllowedValues(),
effectiveConfigTypeAndValues.typeName(),
effectiveConfigTypeAndValues.allowedValues(),
configPhase));
}
});
Expand Down Expand Up @@ -147,136 +147,81 @@ private void processMappings(List<ConfigClass> mappings, List<ConfigDescriptionB
}

String javadocKey = method.getDeclaringClass().getName().replace('$', '.') + '.' + method.getName();
// TODO - radcortez - Fix nulls
EffectiveConfigTypeAndValues typeName = getTypeName(method.getReturnType(), method.getGenericReturnType());
descriptionBuildItems.add(new ConfigDescriptionBuildItem(propertyName, defaultValue,
javaDocProperties.getProperty(javadocKey), null, null, configPhase));
javaDocProperties.getProperty(javadocKey), typeName.typeName(), typeName.allowedValues(), configPhase));
}
}
}

private EffectiveConfigTypeAndValues getTypeName(Field field) {
final Class<?> valueClass = field.getType();
return getTypeName(field, valueClass);
}

private EffectiveConfigTypeAndValues getTypeName(Field field, Class<?> valueClass) {
EffectiveConfigTypeAndValues typeAndValues = new EffectiveConfigTypeAndValues();
String name = valueClass.getName();
private EffectiveConfigTypeAndValues getTypeName(Class<?> valueClass, Type genericType) {
final String name;
final List<String> allowedValues = new ArrayList<>();

// Extract Optionals, Lists and Sets
if ((valueClass.equals(Optional.class) || valueClass.equals(List.class) || valueClass.equals(Set.class))) {

if (field != null) {
Type genericType = field.getGenericType();
name = genericType.getTypeName();
if (valueClass.equals(OptionalInt.class)) {
name = Integer.class.getName();
} else if (valueClass.equals(OptionalDouble.class)) {
name = Double.class.getName();
} else if (valueClass.equals(OptionalLong.class)) {
name = Long.class.getName();
} else if ((valueClass.equals(Optional.class) || valueClass.equals(List.class) || valueClass.equals(Set.class))) {
String thisName = valueClass.getName();
if (genericType != null) {
thisName = genericType.getTypeName();
}

if (name.contains("<") && name.contains(">")) {
name = name.substring(name.lastIndexOf("<") + 1, name.indexOf(">"));
if (thisName.contains("<") && thisName.contains(">")) {
thisName = thisName.substring(thisName.lastIndexOf("<") + 1, thisName.indexOf(">"));
}

try {
Class<?> c = Class.forName(name);
return getTypeName(null, c);
Class<?> c = Class.forName(thisName);
return getTypeName(c, null);
} catch (ClassNotFoundException ex) {
// Then we use the name as is.
}
}

// Check other optionals
if (valueClass.equals(OptionalInt.class)) {
name = Integer.class.getName();
} else if (valueClass.equals(OptionalDouble.class)) {
name = Double.class.getName();
} else if (valueClass.equals(OptionalLong.class)) {
name = Long.class.getName();
}

// Check if this is an enum
if (Enum.class.isAssignableFrom(valueClass)) {
name = thisName;
} else if (Enum.class.isAssignableFrom(valueClass)) {
// Check if this is an enum
name = Enum.class.getName();

Object[] values = valueClass.getEnumConstants();
for (Object v : values) {
Enum casted = (Enum) valueClass.cast(v);
typeAndValues.addAllowedValue(casted.name());
Enum<?> casted = (Enum<?>) valueClass.cast(v);
allowedValues.add(casted.name());
}
} else {
// Map all primitives
name = switch (valueClass.getName()) {
case "int" -> Integer.class.getName();
case "boolean" -> Boolean.class.getName();
case "float" -> Float.class.getName();
case "double" -> Double.class.getName();
case "long" -> Long.class.getName();
case "byte" -> Byte.class.getName();
case "short" -> Short.class.getName();
case "char" -> Character.class.getName();
default -> valueClass.getName();
};
}

// Special case for Log level
if (valueClass.isAssignableFrom(Level.class)) {
typeAndValues.addAllowedValue(Level.ALL.getName());
typeAndValues.addAllowedValue(Level.CONFIG.getName());
typeAndValues.addAllowedValue(Level.FINE.getName());
typeAndValues.addAllowedValue(Level.FINER.getName());
typeAndValues.addAllowedValue(Level.FINEST.getName());
typeAndValues.addAllowedValue(Level.INFO.getName());
typeAndValues.addAllowedValue(Level.OFF.getName());
typeAndValues.addAllowedValue(Level.SEVERE.getName());
typeAndValues.addAllowedValue(Level.WARNING.getName());
}

// Map all primitives
if (name.equals("int")) {
name = Integer.class.getName();
} else if (name.equals("boolean")) {
name = Boolean.class.getName();
} else if (name.equals("float")) {
name = Float.class.getName();
} else if (name.equals("double")) {
name = Double.class.getName();
} else if (name.equals("long")) {
name = Long.class.getName();
} else if (name.equals("byte")) {
name = Byte.class.getName();
} else if (name.equals("short")) {
name = Short.class.getName();
} else if (name.equals("char")) {
name = Character.class.getName();
}

typeAndValues.setTypeName(name);
return typeAndValues;
allowedValues.add(Level.ALL.getName());
allowedValues.add(Level.CONFIG.getName());
allowedValues.add(Level.FINE.getName());
allowedValues.add(Level.FINER.getName());
allowedValues.add(Level.FINEST.getName());
allowedValues.add(Level.INFO.getName());
allowedValues.add(Level.OFF.getName());
allowedValues.add(Level.SEVERE.getName());
allowedValues.add(Level.WARNING.getName());
}

return new EffectiveConfigTypeAndValues(name, allowedValues);
}

static class EffectiveConfigTypeAndValues {
private String typeName;
private List<String> allowedValues;

public EffectiveConfigTypeAndValues() {

}

public EffectiveConfigTypeAndValues(String typeName) {
this.typeName = typeName;
}

public EffectiveConfigTypeAndValues(String typeName, List<String> allowedValues) {
this.typeName = typeName;
this.allowedValues = allowedValues;
}

public String getTypeName() {
return typeName;
}

public void setTypeName(String typeName) {
this.typeName = typeName;
}

public List<String> getAllowedValues() {
return allowedValues;
}

public void setAllowedValues(List<String> allowedValues) {
this.allowedValues = allowedValues;
}

public void addAllowedValue(String v) {
if (allowedValues == null) {
allowedValues = new ArrayList<>();
}
allowedValues.add(v);
}
private record EffectiveConfigTypeAndValues(String typeName, List<String> allowedValues) {
}
}

0 comments on commit 93dd39d

Please sign in to comment.