Skip to content

Commit

Permalink
fix: disallow non-static inner classes (#2780)
Browse files Browse the repository at this point in the history
Also add a Java example


fixes: #2746
  • Loading branch information
stuartwdouglas authored Sep 24, 2024
1 parent 093902a commit 4cdc96f
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/java/echo/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "echo"
language = "kotlin"
14 changes: 14 additions & 0 deletions examples/java/echo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.block.ftl.examples</groupId>
<artifactId>echo</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>xyz.block.ftl</groupId>
<artifactId>ftl-build-parent-java</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

</project>
15 changes: 15 additions & 0 deletions examples/java/echo/src/main/java/ftl/echo/Echo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ftl.echo;

import ftl.time.TimeClient;
import xyz.block.ftl.Export;
import xyz.block.ftl.Verb;

public class Echo {

@Export
@Verb
public EchoResponse echo(EchoRequest req, TimeClient time) {
var response = time.call();
return new EchoResponse("Hello, " + req.name().orElse("anonymous") + "! The time is " + response.toString() + ".");
}
}
6 changes: 6 additions & 0 deletions examples/java/echo/src/main/java/ftl/echo/EchoRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ftl.echo;

import java.util.Optional;

public record EchoRequest(Optional<String> name) {
}
4 changes: 4 additions & 0 deletions examples/java/echo/src/main/java/ftl/echo/EchoResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ftl.echo;

public record EchoResponse(String message) {
}
2 changes: 2 additions & 0 deletions examples/java/time/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "time"
language = "kotlin"
14 changes: 14 additions & 0 deletions examples/java/time/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.block.ftl.examples</groupId>
<artifactId>time</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>xyz.block.ftl</groupId>
<artifactId>ftl-build-parent-java</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

</project>
15 changes: 15 additions & 0 deletions examples/java/time/src/main/java/ftl/time/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ftl.time;

import java.time.OffsetDateTime;

import xyz.block.ftl.Export;
import xyz.block.ftl.Verb;

public class Time {

@Verb
@Export
public static TimeResponse time() {
return new TimeResponse(OffsetDateTime.now());
}
}
6 changes: 6 additions & 0 deletions examples/java/time/src/main/java/ftl/time/TimeResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ftl.time;

import java.time.OffsetDateTime;

public record TimeResponse(OffsetDateTime time) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class ModuleBuilder {
private final Map<DotName, VerbClientBuildItem.DiscoveredClients> verbClients;
private final FTLRecorder recorder;
private final Map<String, Iterable<String>> comments;
private final List<ValidationFailure> validationFailures = new ArrayList<>();

public ModuleBuilder(IndexView index, String moduleName, Map<DotName, TopicsBuildItem.DiscoveredTopic> knownTopics,
Map<DotName, VerbClientBuildItem.DiscoveredClients> verbClients, FTLRecorder recorder,
Expand Down Expand Up @@ -301,6 +302,11 @@ public Type buildType(org.jboss.jandex.Type type, boolean export) {
case CLASS -> {
var clazz = type.asClassType();
var info = index.getClassByName(clazz.name());
if (info.enclosingClass() != null && !Modifier.isStatic(info.flags())) {
// proceed as normal, we fail at the end
validationFailures.add(new ValidationFailure(clazz.name().toString(),
"Inner classes must be static"));
}

PrimitiveType unboxed = PrimitiveType.unbox(clazz);
if (unboxed != null) {
Expand Down Expand Up @@ -445,6 +451,14 @@ public ModuleBuilder addDecls(Decl decl) {
}

public void writeTo(OutputStream out) throws IOException {
if (!validationFailures.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (var failure : validationFailures) {
sb.append("Validation failure: ").append(failure.className).append(": ").append(failure.message)
.append("\n");
}
throw new RuntimeException(sb.toString());
}
moduleBuilder.build().writeTo(out);
}

Expand All @@ -466,4 +480,7 @@ public enum BodyType {
ALLOWED,
REQUIRED
}

record ValidationFailure(String className, String message) {
}
}

0 comments on commit 4cdc96f

Please sign in to comment.