Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.1.5: Update to static content #9558

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.helidon.codegen.classmodel.InnerClass;
import io.helidon.codegen.classmodel.Javadoc;
import io.helidon.codegen.classmodel.Method;
import io.helidon.common.Size;
import io.helidon.common.types.AccessModifier;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypeNames;
Expand Down Expand Up @@ -178,8 +179,14 @@ Consumer<ContentBuilder<?>> toDefaultValue(String defaultValue) {
.addContent(defaultValue)
.addContent("\"");
}
if (TypeNames.SIZE.equals(typeName)) {
CodegenValidator.validateSize(enclosingType, annotatedMethod, OPTION_DEFAULT, "value", defaultValue);
return content -> content.addContent(Size.class)
.addContent(".parse(\"")
.addContent(defaultValue)
.addContent("\")");
}
if (TypeNames.DURATION.equals(typeName)) {

CodegenValidator.validateDuration(enclosingType, annotatedMethod, OPTION_DEFAULT, "value", defaultValue);
return content -> content.addContent(Duration.class)
.addContent(".parse(\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.URI;
import java.time.Duration;

import io.helidon.common.Size;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypedElementInfo;

Expand Down Expand Up @@ -88,4 +89,35 @@ public static String validateDuration(TypeName enclosingType,
+ element.elementName()));
}
}

/**
* Validate a {@link io.helidon.common.Size} annotation on a method, field, or constructor.
*
* @param enclosingType type that owns the element
* @param element annotated element
* @param annotationType type of annotation
* @param property property of annotation
* @param value actual value read from the annotation property
* @return the value
* @throws io.helidon.codegen.CodegenException with correct source element describing the problem
*/
public static String validateSize(TypeName enclosingType,
TypedElementInfo element,
TypeName annotationType,
String property,
String value) {
try {
Size.parse(value);
return value;
} catch (Exception e) {
throw new CodegenException("Size expression of annotation " + annotationType.fqName() + "."
+ property + "(): "
+ "\"" + value + "\" cannot be parsed. Size expects an"
+ " expression such as '120 KB' (120 * 1024 * 1024), "
+ "'120 kB' (120 * 1000 * 1000), or '120 KiB' (same as KB)"
+ " Please check javadoc of " + Size.class.getName() + " class.",
e,
element.originatingElementValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public abstract class TypeInfoFactoryBase {
TypeName.create(Target.class),
TypeName.create(Retention.class),
TypeName.create(Repeatable.class));
private static final Set<String> ACCESS_MODIFIERS = Set.of("PUBLIC", "PRIVATE", "PROTECTED");

/**
* There are no side effects of this constructor.
Expand Down Expand Up @@ -144,10 +145,15 @@ protected static Set<io.helidon.common.types.Modifier> modifiers(CodegenContext
Set<io.helidon.common.types.Modifier> result = new HashSet<>();

for (String stringModifier : stringModifiers) {
String upperCased = stringModifier.toUpperCase(Locale.ROOT);
if (ACCESS_MODIFIERS.contains(upperCased)) {
// ignore access modifiers, as they are handled elsewhere
continue;
}
try {
result.add(io.helidon.common.types.Modifier.valueOf(stringModifier.toUpperCase(Locale.ROOT)));
result.add(io.helidon.common.types.Modifier.valueOf(upperCased));
} catch (Exception ignored) {
// we do not care about modifiers we do not understand - either access modifier, or something new
// we do not care about modifiers we do not understand
ctx.logger().log(System.Logger.Level.TRACE,
"Modifier " + stringModifier + " not understood by type info factory.");
}
Expand Down
Loading
Loading