Skip to content

Commit

Permalink
Funqy - fix constructor injection for normal scoped beans
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvavrik authored and evanchooly committed Sep 8, 2022
1 parent da3036e commit e9926a2
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
5 changes: 5 additions & 0 deletions extensions/funqy/funqy-server-common/deployment/pom.xml
Expand Up @@ -25,6 +25,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -172,6 +172,12 @@ private static void generateDefaultConstructors(BuildProducer<BytecodeTransforme
Set<ClassInfo> withoutDefaultCtor) {

for (ClassInfo classInfo : withoutDefaultCtor) {
// don't generate constructor for normal scoped beans as the Quarkus Arc does that for us
final BuiltinScope scope = BuiltinScope.from(classInfo);
if (scope != null && scope.getInfo().isNormal()) {
continue;
}

// keep it super simple - only generate default constructor is the object is a direct descendant of Object
if (!(classInfo.superClassType() != null && classInfo.superClassType().name().equals(DotNames.OBJECT))) {
return;
Expand Down
@@ -0,0 +1,127 @@
package io.quarkus.funqy.deployment;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.funqy.Funq;
import io.quarkus.test.QuarkusUnitTest;

public class DependencyInjectionTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(
(jar) -> jar.addClasses(GreetingFunction.class, GreetingService.class, FarewellFunction.class,
HiFunction.class, ByeFunction.class));

@Inject
GreetingFunction greetingFunction;

@Inject
FarewellFunction farewellFunction;

@Inject
ByeFunction byeFunction;

@Inject
HiFunction hiFunction;

@Test
public void testFieldInjection() {
// bean with a pseudo-scope
Assertions.assertEquals("Hello World!", greetingFunction.greeting());
// normal scoped bean
Assertions.assertEquals("Bye!", byeFunction.bye());
}

@Test
public void testConstructorInjection() {
// bean with a pseudo-scope
Assertions.assertEquals("Goodbye!", farewellFunction.farewell());
// normal scoped bean
Assertions.assertEquals("Hi!", hiFunction.hi());
}

public static class GreetingFunction {

@Inject
GreetingService greetingService;

@Funq
String greeting() {
return greetingService.sayHello();
}

}

public static class FarewellFunction {

private final GreetingService greetingService;

public FarewellFunction(GreetingService greetingService) {
this.greetingService = greetingService;
}

@Funq
String farewell() {
return greetingService.sayGoodbye();
}

}

@ApplicationScoped
public static class ByeFunction {

@Inject
GreetingService greetingService;

@Funq
String bye() {
return greetingService.sayBye();
}

}

@ApplicationScoped
public static class HiFunction {

private final GreetingService greetingService;

public HiFunction(GreetingService greetingService) {
this.greetingService = greetingService;
}

@Funq
String hi() {
return greetingService.sayHi();
}

}

@Singleton
public static class GreetingService {

public String sayHello() {
return "Hello World!";
}

public String sayGoodbye() {
return "Goodbye!";
}

public String sayHi() {
return "Hi!";
}

public String sayBye() {
return "Bye!";
}

}

}

0 comments on commit e9926a2

Please sign in to comment.