diff --git a/simple-processor/src/main/java/org/javahelpers/codegen/processing/AbstractCodegenProcessor.java b/simple-processor/src/main/java/org/javahelpers/codegen/processing/AbstractCodegenProcessor.java index ec72fe80..83390a74 100644 --- a/simple-processor/src/main/java/org/javahelpers/codegen/processing/AbstractCodegenProcessor.java +++ b/simple-processor/src/main/java/org/javahelpers/codegen/processing/AbstractCodegenProcessor.java @@ -329,6 +329,7 @@ public SourceVersion getSupportedSourceVersion() { protected final void generateTarget(GenerationTargetClassDto target) throws CodeGenerationException { assertTargetDoesNotExist(target); + GenerationTargetValidator.validate(target); backend.generator().generateClass(target, backend.formatterFor(target.getFormattingMode())); } diff --git a/simple-processor/src/main/java/org/javahelpers/codegen/processing/GenerationTargetValidator.java b/simple-processor/src/main/java/org/javahelpers/codegen/processing/GenerationTargetValidator.java new file mode 100644 index 00000000..972deb27 --- /dev/null +++ b/simple-processor/src/main/java/org/javahelpers/codegen/processing/GenerationTargetValidator.java @@ -0,0 +1,74 @@ +/* + * MIT License + * + * Copyright (c) 2025-2026 Andreas Igel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.javahelpers.codegen.processing; + +import org.apache.commons.lang3.StringUtils; +import org.javahelpers.codegen.CodeGenerationException; +import org.javahelpers.codegen.model.core.GenerationTargetClassDto; +import org.javahelpers.codegen.model.method.MethodDto; +import org.javahelpers.codegen.model.method.MethodParameterDto; +import org.javahelpers.codegen.model.type.NestedTypeDto; + +/** Validates generation-target DTOs before a backend-specific generator is invoked. */ +public final class GenerationTargetValidator { + + private GenerationTargetValidator() {} + + /** + * Validates a generation target. + * + * @param target target class definition + * @throws CodeGenerationException if the target is invalid + */ + public static void validate(GenerationTargetClassDto target) throws CodeGenerationException { + for (var constructor : target.getConstructors()) { + validateParameters(target, constructor.getParameters(), "constructor"); + } + for (MethodDto method : target.getMethods()) { + validateMethod(target, method); + } + for (NestedTypeDto nestedType : target.getNestedTypes()) { + for (MethodDto method : nestedType.getMethods()) { + validateMethod(target, method); + } + } + } + + private static void validateMethod(GenerationTargetClassDto target, MethodDto method) + throws CodeGenerationException { + validateParameters(target, method.getParameters(), "method '" + method.getMethodName() + "'"); + } + + private static void validateParameters( + GenerationTargetClassDto target, Iterable parameters, String owner) + throws CodeGenerationException { + for (MethodParameterDto parameter : parameters) { + if (StringUtils.isBlank(parameter.getParameterName())) { + throw new CodeGenerationException( + "Unnamed parameter in " + owner + " of " + target.getTypeName().getFullQualifiedName()); + } + } + } +} diff --git a/simple-processor/src/test/java/org/javahelpers/codegen/processing/GenerationTargetValidatorTest.java b/simple-processor/src/test/java/org/javahelpers/codegen/processing/GenerationTargetValidatorTest.java new file mode 100644 index 00000000..04cadaac --- /dev/null +++ b/simple-processor/src/test/java/org/javahelpers/codegen/processing/GenerationTargetValidatorTest.java @@ -0,0 +1,82 @@ +/* + * MIT License + * + * Copyright (c) 2025-2026 Andreas Igel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.javahelpers.codegen.processing; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.javahelpers.codegen.CodeGenerationException; +import org.javahelpers.codegen.model.core.GenerationTargetClassDtoBuilder; +import org.javahelpers.codegen.model.method.MethodDto; +import org.javahelpers.codegen.model.method.MethodParameterDto; +import org.javahelpers.codegen.model.type.TypeName; +import org.junit.jupiter.api.Test; + +/** Tests for backend-independent generation-target validation. */ +class GenerationTargetValidatorTest { + + /** Blank method parameter names are rejected before backend generation. */ + @Test + void blankMethodParameterFails() { + MethodDto method = new MethodDto(); + method.setMethodName("run"); + method.addParameter(unnamedParameter()); + + CodeGenerationException thrown = + assertThrows( + CodeGenerationException.class, + () -> + GenerationTargetValidator.validate( + GenerationTargetClassDtoBuilder.create() + .typeName("com.example", "Target") + .method(method) + .build())); + + assertEquals("Unnamed parameter in method 'run' of com.example.Target", thrown.getMessage()); + } + + /** Blank constructor parameter names are rejected before backend generation. */ + @Test + void blankConstructorParameterFails() { + CodeGenerationException thrown = + assertThrows( + CodeGenerationException.class, + () -> + GenerationTargetValidator.validate( + GenerationTargetClassDtoBuilder.create() + .typeName("com.example", "Target") + .constructor(c -> c.parameter(unnamedParameter())) + .build())); + + assertEquals("Unnamed parameter in constructor of com.example.Target", thrown.getMessage()); + } + + private static MethodParameterDto unnamedParameter() { + MethodParameterDto parameter = new MethodParameterDto(); + parameter.setParameterTypeName(TypeName.of(String.class)); + parameter.setParameterName(" "); + return parameter; + } +}