Skip to content

Commit ab281df

Browse files
authored
4.x: Check supertypes during validation of runtime types. Added test (helidon-io#10273)
* Check supertypes during validation of runtime types. Added test * Removed unnecessary local variable.
1 parent 5cdff9e commit ab281df

File tree

9 files changed

+295
-3
lines changed

9 files changed

+295
-3
lines changed

builder/codegen/src/main/java/io/helidon/builder/codegen/ValidationTask.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2023, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,11 +39,21 @@ private static void validateImplements(Errors.Collector errors,
3939
TypeInfo validatedType,
4040
TypeName implementedInterface,
4141
String message) {
42+
43+
if (!doesImplement(validatedType, implementedInterface)) {
44+
errors.fatal(validatedType.typeName(), message);
45+
}
46+
}
47+
48+
private static boolean doesImplement(TypeInfo validatedType, TypeName implementedInterface) {
4249
if (validatedType.interfaceTypeInfo()
4350
.stream()
44-
.noneMatch(it -> it.typeName().equals(implementedInterface))) {
45-
errors.fatal(validatedType.typeName(), message);
51+
.anyMatch(it -> it.typeName().equals(implementedInterface))) {
52+
return true;
4653
}
54+
return validatedType.superTypeInfo()
55+
.map(it -> doesImplement(it, implementedInterface))
56+
.orElse(false);
4757
}
4858

4959
private static void validateFactoryMethod(Errors.Collector errors,

builder/tests/inheritance/pom.xml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2025 Oracle and/or its affiliates.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<parent>
24+
<groupId>io.helidon.builder.tests</groupId>
25+
<artifactId>helidon-builder-tests-project</artifactId>
26+
<version>4.3.0-SNAPSHOT</version>
27+
</parent>
28+
<modelVersion>4.0.0</modelVersion>
29+
30+
<artifactId>helidon-builder-tests-inheritance</artifactId>
31+
<name>Helidon Builder Tests Inheritance</name>
32+
<description>Tests inheritance when using runtime types</description>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>io.helidon.builder</groupId>
37+
<artifactId>helidon-builder-api</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.hamcrest</groupId>
41+
<artifactId>hamcrest-all</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.junit.jupiter</groupId>
46+
<artifactId>junit-jupiter-api</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>io.helidon.common.testing</groupId>
51+
<artifactId>helidon-common-testing-junit5</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<configuration>
62+
<annotationProcessorPaths>
63+
<path>
64+
<groupId>io.helidon.builder</groupId>
65+
<artifactId>helidon-builder-codegen</artifactId>
66+
<version>${helidon.version}</version>
67+
</path>
68+
<path>
69+
<groupId>io.helidon.codegen</groupId>
70+
<artifactId>helidon-codegen-apt</artifactId>
71+
<version>${helidon.version}</version>
72+
</path>
73+
<path>
74+
<groupId>io.helidon.codegen</groupId>
75+
<artifactId>helidon-codegen-helidon-copyright</artifactId>
76+
<version>${helidon.version}</version>
77+
</path>
78+
</annotationProcessorPaths>
79+
</configuration>
80+
<dependencies>
81+
<dependency>
82+
<groupId>io.helidon.builder</groupId>
83+
<artifactId>helidon-builder-codegen</artifactId>
84+
<version>${helidon.version}</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>io.helidon.codegen</groupId>
88+
<artifactId>helidon-codegen-apt</artifactId>
89+
<version>${helidon.version}</version>
90+
</dependency>
91+
<dependency>
92+
<groupId>io.helidon.codegen</groupId>
93+
<artifactId>helidon-codegen-helidon-copyright</artifactId>
94+
<version>${helidon.version}</version>
95+
</dependency>
96+
</dependencies>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
101+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.helidon.builder.tests.inheritance;
18+
19+
import java.util.function.Consumer;
20+
21+
import io.helidon.builder.api.RuntimeType;
22+
23+
@RuntimeType.PrototypedBy(InheritanceConfig.class)
24+
class Inheritance extends InheritanceBase<InheritanceConfig> {
25+
private final InheritanceConfig config;
26+
27+
Inheritance(InheritanceConfig config) {
28+
this.config = config;
29+
}
30+
31+
static Inheritance create(InheritanceConfig config) {
32+
return new Inheritance(config);
33+
}
34+
35+
static Inheritance create(Consumer<InheritanceConfig.Builder> consumer) {
36+
return builder().update(consumer).build();
37+
}
38+
39+
static InheritanceConfig.Builder builder() {
40+
return InheritanceConfig.builder();
41+
}
42+
43+
@Override
44+
public InheritanceConfig prototype() {
45+
return config;
46+
}
47+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.helidon.builder.tests.inheritance;
18+
19+
import io.helidon.builder.api.RuntimeType;
20+
21+
abstract class InheritanceBase<CONFIG extends InheritanceBaseConfig> implements RuntimeType.Api<CONFIG> {
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.helidon.builder.tests.inheritance;
18+
19+
import io.helidon.builder.api.Prototype;
20+
21+
@Prototype.Blueprint
22+
interface InheritanceBaseConfigBlueprint {
23+
String name();
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.helidon.builder.tests.inheritance;
18+
19+
import io.helidon.builder.api.Prototype;
20+
21+
@Prototype.Blueprint
22+
interface InheritanceConfigBlueprint extends InheritanceBaseConfigBlueprint, Prototype.Factory<Inheritance> {
23+
String description();
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Helidon Builder Test module for inheritance of runtime types.
19+
*/
20+
module io.helidon.builder.tests.inheritance {
21+
requires io.helidon.common;
22+
requires io.helidon.builder.api;
23+
24+
exports io.helidon.builder.tests.inheritance;
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.helidon.builder.tests.inheritance;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.hamcrest.CoreMatchers.is;
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
24+
class InheritanceTest {
25+
@Test
26+
void testApi() {
27+
// just a sanity check to make sure the api is created as expected
28+
Inheritance inheritance = Inheritance.builder()
29+
.name("name")
30+
.description("description")
31+
.build();
32+
33+
InheritanceConfig config = inheritance.prototype();
34+
35+
assertThat(config.name(), is("name"));
36+
assertThat(config.description(), is("description"));
37+
}
38+
}

builder/tests/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<module>builder</module>
4949
<module>codegen</module>
5050
<module>wildcard</module>
51+
<module>inheritance</module>
5152
</modules>
5253

5354
</project>

0 commit comments

Comments
 (0)