Skip to content

Commit

Permalink
Support for validation of Duration and URI default values. (#9166)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-langer authored Aug 20, 2024
1 parent 18b995b commit d8fc80c
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ static PrototypeProperty create(CodegenContext ctx,

boolean sameGeneric = element.hasAnnotation(Types.OPTION_SAME_GENERIC);
// to help with defaults, setters, config mapping etc.
TypeHandler typeHandler = TypeHandler.create(name, getterName, setterName, returnType, sameGeneric);
TypeHandler typeHandler = TypeHandler.create(blueprint.typeName(),
element,
name,
getterName,
setterName,
returnType,
sameGeneric);

// all information from @ConfiguredOption annotation
AnnotationDataOption configuredOption = AnnotationDataOption.create(typeHandler, element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Optional;
import java.util.function.Consumer;

import io.helidon.codegen.CodegenValidator;
import io.helidon.codegen.classmodel.ContentBuilder;
import io.helidon.codegen.classmodel.Field;
import io.helidon.codegen.classmodel.InnerClass;
Expand All @@ -34,39 +35,57 @@
import io.helidon.common.types.AccessModifier;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypeNames;
import io.helidon.common.types.TypedElementInfo;

import static io.helidon.builder.codegen.Types.OPTION_DEFAULT;

class TypeHandler {
private final TypeName enclosingType;
private final TypedElementInfo annotatedMethod;
private final String name;
private final String getterName;
private final String setterName;
private final TypeName declaredType;

TypeHandler(String name, String getterName, String setterName, TypeName declaredType) {
TypeHandler(TypeName enclosingType,
TypedElementInfo annotatedMethod,
String name,
String getterName,
String setterName,
TypeName declaredType) {
this.enclosingType = enclosingType;
this.annotatedMethod = annotatedMethod;
this.name = name;
this.getterName = getterName;
this.setterName = setterName;
this.declaredType = declaredType;
}

static TypeHandler create(String name, String getterName, String setterName, TypeName returnType, boolean sameGeneric) {
static TypeHandler create(TypeName blueprintType,
TypedElementInfo annotatedMethod,
String name,
String getterName,
String setterName,
TypeName returnType,
boolean sameGeneric) {
if (TypeNames.OPTIONAL.equals(returnType)) {
return new TypeHandlerOptional(name, getterName, setterName, returnType);
return new TypeHandlerOptional(blueprintType, annotatedMethod, name, getterName, setterName, returnType);
}
if (TypeNames.SUPPLIER.equals(returnType)) {
return new TypeHandlerSupplier(name, getterName, setterName, returnType);
return new TypeHandlerSupplier(blueprintType, annotatedMethod, name, getterName, setterName, returnType);
}
if (TypeNames.SET.equals(returnType)) {
return new TypeHandlerSet(name, getterName, setterName, returnType);
return new TypeHandlerSet(blueprintType, annotatedMethod, name, getterName, setterName, returnType);
}

if (TypeNames.LIST.equals(returnType)) {
return new TypeHandlerList(name, getterName, setterName, returnType);
return new TypeHandlerList(blueprintType, annotatedMethod, name, getterName, setterName, returnType);
}
if (TypeNames.MAP.equals(returnType)) {
return new TypeHandlerMap(name, getterName, setterName, returnType, sameGeneric);
return new TypeHandlerMap(blueprintType, annotatedMethod, name, getterName, setterName, returnType, sameGeneric);
}

return new TypeHandler(name, getterName, setterName, returnType);
return new TypeHandler(blueprintType, annotatedMethod, name, getterName, setterName, returnType);
}

static AccessModifier setterAccessModifier(AnnotationDataOption configured) {
Expand Down Expand Up @@ -160,6 +179,8 @@ Consumer<ContentBuilder<?>> toDefaultValue(String defaultValue) {
.addContent("\"");
}
if (TypeNames.DURATION.equals(typeName)) {

CodegenValidator.validateDuration(enclosingType, annotatedMethod, OPTION_DEFAULT, "value", defaultValue);
return content -> content.addContent(Duration.class)
.addContent(".parse(\"")
.addContent(defaultValue)
Expand All @@ -177,6 +198,7 @@ Consumer<ContentBuilder<?>> toDefaultValue(String defaultValue) {
.addContent("\")");
}
if (Types.URI.equals(typeName)) {
CodegenValidator.validateUri(enclosingType, annotatedMethod, OPTION_DEFAULT, "value", defaultValue);
return content -> content.addContent(URI.class)
.addContent(".create(\"")
.addContent(defaultValue)
Expand Down Expand Up @@ -628,8 +650,13 @@ private void factorySetter(InnerClass.Builder classBuilder,
static class OneTypeHandler extends TypeHandler {
private final TypeName actualType;

OneTypeHandler(String name, String getterName, String setterName, TypeName declaredType) {
super(name, getterName, setterName, declaredType);
OneTypeHandler(TypeName enclosingType,
TypedElementInfo annotatedMethod,
String name,
String getterName,
String setterName,
TypeName declaredType) {
super(enclosingType, annotatedMethod, name, getterName, setterName, declaredType);

if (declaredType.typeArguments().isEmpty()) {
this.actualType = TypeNames.STRING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.helidon.codegen.classmodel.Method;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypeNames;
import io.helidon.common.types.TypedElementInfo;

import static io.helidon.builder.codegen.Types.COMMON_CONFIG;
import static io.helidon.codegen.CodegenUtil.capitalize;
Expand Down Expand Up @@ -94,14 +95,16 @@ abstract class TypeHandlerCollection extends TypeHandler.OneTypeHandler {
private final String collector;
private final Optional<String> configMapper;

TypeHandlerCollection(String name,
TypeHandlerCollection(TypeName blueprintType,
TypedElementInfo annotatedMethod,
String name,
String getterName,
String setterName,
TypeName declaredType,
TypeName collectionType,
String collector,
Optional<String> configMapper) {
super(name, getterName, setterName, declaredType);
super(blueprintType, annotatedMethod, name, getterName, setterName, declaredType);
this.collectionType = collectionType;
this.collectionImplType = collectionImplType(collectionType);
this.collector = collector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
import io.helidon.codegen.CodegenUtil;
import io.helidon.codegen.classmodel.Method;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypedElementInfo;

import static io.helidon.common.types.TypeNames.LIST;

class TypeHandlerList extends TypeHandlerCollection {

TypeHandlerList(String name, String getterName, String setterName, TypeName declaredType) {
super(name, getterName, setterName, declaredType, LIST, "toList()", Optional.empty());
TypeHandlerList(TypeName blueprintType,
TypedElementInfo annotatedMethod,
String name, String getterName, String setterName, TypeName declaredType) {
super(blueprintType, annotatedMethod, name, getterName, setterName, declaredType, LIST, "toList()", Optional.empty());
}

static String isMutatedField(String propertyName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.helidon.codegen.classmodel.TypeArgument;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypeNames;
import io.helidon.common.types.TypedElementInfo;

import static io.helidon.codegen.CodegenUtil.capitalize;
import static io.helidon.common.types.TypeNames.LIST;
Expand All @@ -43,8 +44,10 @@ class TypeHandlerMap extends TypeHandler {
private final TypeName implTypeName;
private final boolean sameGeneric;

TypeHandlerMap(String name, String getterName, String setterName, TypeName declaredType, boolean sameGeneric) {
super(name, getterName, setterName, declaredType);
TypeHandlerMap(TypeName blueprintType,
TypedElementInfo annotatedMethod,
String name, String getterName, String setterName, TypeName declaredType, boolean sameGeneric) {
super(blueprintType, annotatedMethod, name, getterName, setterName, declaredType);
this.sameGeneric = sameGeneric;

this.implTypeName = collectionImplType(MAP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.helidon.common.types.AccessModifier;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypeNames;
import io.helidon.common.types.TypedElementInfo;

import static io.helidon.builder.codegen.Types.CHAR_ARRAY;
import static io.helidon.codegen.CodegenUtil.capitalize;
Expand Down Expand Up @@ -67,8 +68,10 @@ class TypeHandlerOptional extends TypeHandler.OneTypeHandler {
BOXED_VOID, PRIMITIVE_VOID
);

TypeHandlerOptional(String name, String getterName, String setterName, TypeName declaredType) {
super(name, getterName, setterName, declaredType);
TypeHandlerOptional(TypeName blueprintType,
TypedElementInfo annotatedMethod,
String name, String getterName, String setterName, TypeName declaredType) {
super(blueprintType, annotatedMethod, name, getterName, setterName, declaredType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
import java.util.Optional;

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

import static io.helidon.common.types.TypeNames.SET;

class TypeHandlerSet extends TypeHandlerCollection {

TypeHandlerSet(String name, String getterName, String setterName, TypeName declaredType) {
super(name,
TypeHandlerSet(TypeName blueprintType,
TypedElementInfo annotatedMethod,
String name, String getterName, String setterName, TypeName declaredType) {
super(blueprintType,
annotatedMethod,
name,
getterName,
setterName,
declaredType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
import io.helidon.codegen.classmodel.Method;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypeNames;
import io.helidon.common.types.TypedElementInfo;

import static io.helidon.builder.codegen.Types.CHAR_ARRAY;
import static io.helidon.common.types.TypeNames.SUPPLIER;

class TypeHandlerSupplier extends TypeHandler.OneTypeHandler {

TypeHandlerSupplier(String name, String getterName, String setterName, TypeName declaredType) {
super(name, getterName, setterName, declaredType);
TypeHandlerSupplier(TypeName blueprintType,
TypedElementInfo annotatedMethod,
String name, String getterName, String setterName, TypeName declaredType) {
super(blueprintType, annotatedMethod, name, getterName, setterName, declaredType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.codegen;

import java.net.URI;
import java.time.Duration;

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

/**
* Validation utilities.
*/
public final class CodegenValidator {
private CodegenValidator() {
}

/**
* Validate a URI value in an annotation.
*
* @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 validateUri(TypeName enclosingType,
TypedElementInfo element,
TypeName annotationType,
String property,
String value) {
try {
URI.create(value);
return value;
} catch (Exception e) {
throw new CodegenException("URI expression of annotation " + annotationType.fqName() + "."
+ property + "(): "
+ "\"" + value + "\" cannot be parsed. Invalid URI.",
e,
element.originatingElement().orElseGet(() -> enclosingType.fqName() + "."
+ element.elementName()));
}
}

/**
* Validate a duration 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 validateDuration(TypeName enclosingType,
TypedElementInfo element,
TypeName annotationType,
String property,
String value) {
try {
Duration.parse(value);
return value;
} catch (Exception e) {
throw new CodegenException("Duration expression of annotation " + annotationType.fqName() + "."
+ property + "(): "
+ "\"" + value + "\" cannot be parsed. Duration expects an"
+ " expression such as 'PT1S' (1 second), 'PT0.1S' (tenth of a second)."
+ " Please check javadoc of " + Duration.class.getName() + " class.",
e,
element.originatingElement().orElseGet(() -> enclosingType.fqName() + "."
+ element.elementName()));
}
}
}
Loading

0 comments on commit d8fc80c

Please sign in to comment.