forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC: add validation for sealed types
- Loading branch information
Showing
9 changed files
with
483 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 95 additions & 104 deletions
199
independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BuiltinBean.java
Large diffs are not rendered by default.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
...ts/arc/tests/src/test/java/io/quarkus/arc/test/records/InterceptedRecordProducerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.quarkus.arc.test.records; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.enterprise.inject.spi.DefinitionException; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.InterceptionProxy; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class InterceptedRecordProducerTest { | ||
@RegisterExtension | ||
public ArcTestContainer container = ArcTestContainer.builder() | ||
.beanClasses(Producer.class, MyInterceptorBinding.class, MyInterceptor.class) | ||
.shouldFail() | ||
.build(); | ||
|
||
@Test | ||
public void trigger() { | ||
Throwable error = container.getFailure(); | ||
assertNotNull(error); | ||
assertInstanceOf(DefinitionException.class, error); | ||
assertTrue(error.getMessage().contains("Cannot build InterceptionProxy for a record")); | ||
} | ||
|
||
@Dependent | ||
static class Producer { | ||
@Produces | ||
@Dependent | ||
DependentRecord produce(InterceptionProxy<DependentRecord> proxy) { | ||
return proxy.create(new DependentRecord()); | ||
} | ||
} | ||
|
||
record DependentRecord() { | ||
@MyInterceptorBinding | ||
String hello() { | ||
return "hello"; | ||
} | ||
} | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
@interface MyInterceptorBinding { | ||
} | ||
|
||
@MyInterceptorBinding | ||
@Interceptor | ||
@Priority(1) | ||
static class MyInterceptor { | ||
@AroundInvoke | ||
Object intercept(InvocationContext ctx) throws Exception { | ||
return "intercepted: " + ctx.proceed(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...dent-projects/arc/tests/src/test/java/io/quarkus/arc/test/sealed/DependentSealedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.quarkus.arc.test.sealed; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import jakarta.enterprise.context.Dependent; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class DependentSealedTest { | ||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(MyDependent.class); | ||
|
||
@Test | ||
public void test() { | ||
assertNotNull(Arc.container().select(MyDependent.class).get()); | ||
} | ||
|
||
@Dependent | ||
static sealed class MyDependent permits MyDependentSubclass { | ||
} | ||
|
||
static final class MyDependentSubclass extends MyDependent { | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...cts/arc/tests/src/test/java/io/quarkus/arc/test/sealed/InterceptedSealedProducerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.quarkus.arc.test.sealed; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.enterprise.inject.spi.DefinitionException; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.InterceptionProxy; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class InterceptedSealedProducerTest { | ||
@RegisterExtension | ||
public ArcTestContainer container = ArcTestContainer.builder() | ||
.beanClasses(Producer.class, MyInterceptorBinding.class, MyInterceptor.class) | ||
.shouldFail() | ||
.build(); | ||
|
||
@Test | ||
public void trigger() { | ||
Throwable error = container.getFailure(); | ||
assertNotNull(error); | ||
assertInstanceOf(DefinitionException.class, error); | ||
assertTrue(error.getMessage().contains("Cannot build InterceptionProxy for a sealed type")); | ||
} | ||
|
||
@Dependent | ||
static class Producer { | ||
@Produces | ||
@Dependent | ||
DependentSealed produce(InterceptionProxy<DependentSealed> proxy) { | ||
return proxy.create(new DependentSealed()); | ||
} | ||
} | ||
|
||
static sealed class DependentSealed permits DependentSealedSubclass { | ||
@MyInterceptorBinding | ||
String hello() { | ||
return "hello"; | ||
} | ||
} | ||
|
||
static final class DependentSealedSubclass extends DependentSealed { | ||
} | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
@interface MyInterceptorBinding { | ||
} | ||
|
||
@MyInterceptorBinding | ||
@Interceptor | ||
@Priority(1) | ||
static class MyInterceptor { | ||
@AroundInvoke | ||
Object intercept(InvocationContext ctx) throws Exception { | ||
return "intercepted: " + ctx.proceed(); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...nt-projects/arc/tests/src/test/java/io/quarkus/arc/test/sealed/InterceptedSealedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.quarkus.arc.test.sealed; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.spi.DeploymentException; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class InterceptedSealedTest { | ||
@RegisterExtension | ||
public ArcTestContainer container = ArcTestContainer.builder() | ||
.beanClasses(DependentSealed.class, MyInterceptorBinding.class, MyInterceptor.class) | ||
.shouldFail() | ||
.build(); | ||
|
||
@Test | ||
public void trigger() { | ||
Throwable error = container.getFailure(); | ||
assertNotNull(error); | ||
assertInstanceOf(DeploymentException.class, error); | ||
assertTrue(error.getMessage().contains("must not be sealed")); | ||
} | ||
|
||
@Dependent | ||
static sealed class DependentSealed permits DependentSealedSubclass { | ||
@MyInterceptorBinding | ||
String hello() { | ||
return "hello"; | ||
} | ||
} | ||
|
||
static final class DependentSealedSubclass extends DependentSealed { | ||
} | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
@interface MyInterceptorBinding { | ||
} | ||
|
||
@MyInterceptorBinding | ||
@Interceptor | ||
@Priority(1) | ||
static class MyInterceptor { | ||
@AroundInvoke | ||
Object intercept(InvocationContext ctx) throws Exception { | ||
return "intercepted: " + ctx.proceed(); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ts/arc/tests/src/test/java/io/quarkus/arc/test/sealed/NormalScopedSealedProducerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.arc.test.sealed; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.enterprise.inject.spi.DeploymentException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class NormalScopedSealedProducerTest { | ||
@RegisterExtension | ||
public ArcTestContainer container = ArcTestContainer.builder() | ||
.beanClasses(Producer.class) | ||
.shouldFail() | ||
.build(); | ||
|
||
@Test | ||
public void trigger() { | ||
Throwable error = container.getFailure(); | ||
assertNotNull(error); | ||
assertInstanceOf(DeploymentException.class, error); | ||
assertTrue(error.getMessage().contains("must not have a return type that is sealed")); | ||
} | ||
|
||
@Dependent | ||
static class Producer { | ||
@Produces | ||
@ApplicationScoped | ||
MySealed produce() { | ||
return new MySealed(); | ||
} | ||
} | ||
|
||
static sealed class MySealed permits MySealedSubclass { | ||
} | ||
|
||
static final class MySealedSubclass extends MySealed { | ||
} | ||
} |
Oops, something went wrong.