Skip to content

Commit

Permalink
Merge pull request #31012 from mkouba/arc-detect-unsupported-inject
Browse files Browse the repository at this point in the history
ArC: detect unsupported Inject annotations
  • Loading branch information
mkouba authored Feb 9, 2023
2 parents 5e03f9f + 01396ac commit d9b0770
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
Expand Down Expand Up @@ -38,24 +37,25 @@ void detect(ArcConfig config, ApplicationIndexBuildItem applicationIndex, Custom

// Detect unsupported annotations
List<UnsupportedAnnotation> unsupported = new ArrayList<>();
Function<AnnotationInstance, String> singletonFun = new Function<AnnotationInstance, String>() {

@Override
public String apply(AnnotationInstance annotationInstance) {
return String.format("%s declared on %s, use @jakarta.inject.Singleton instead",
annotationInstance.toString(false), getTargetInfo(annotationInstance));
}
};
unsupported.add(new UnsupportedAnnotation(DotName.createSimple("com.google.inject.Singleton"), singletonFun));
unsupported.add(new UnsupportedAnnotation(DotName.createSimple("jakarta.ejb.Singleton"), singletonFun));
unsupported.add(new UnsupportedAnnotation(DotName.createSimple("groovy.lang.Singleton"), singletonFun));
unsupported.add(new UnsupportedAnnotation(DotName.createSimple("jakarta.ejb.Singleton"), singletonFun));
String correctSingleton = "@jakarta.inject.Singleton";
unsupported.add(new UnsupportedAnnotation("com.google.inject.Singleton", correctSingleton));
unsupported.add(new UnsupportedAnnotation("jakarta.ejb.Singleton", correctSingleton));
unsupported.add(new UnsupportedAnnotation("groovy.lang.Singleton", correctSingleton));

String correctInject = "@jakarta.inject.Inject";
unsupported.add(new UnsupportedAnnotation("javax.inject.Inject", correctInject));
unsupported.add(new UnsupportedAnnotation("com.google.inject.Inject", correctInject));
unsupported.add(new UnsupportedAnnotation("com.oracle.svm.core.annotate.Inject", correctInject));
unsupported.add(new UnsupportedAnnotation("org.gradle.internal.impldep.javax.inject.Inject",
correctInject));

Map<AnnotationInstance, String> wrongUsages = new HashMap<>();

for (UnsupportedAnnotation annotation : unsupported) {
for (AnnotationInstance annotationInstance : index.getAnnotations(annotation.name)) {
wrongUsages.put(annotationInstance, annotation.messageFun.apply(annotationInstance));
wrongUsages.put(annotationInstance, String.format("%s declared on %s, use %s instead",
annotationInstance.toString(false), getTargetInfo(annotationInstance), annotation.correctAnnotation));
}
}

Expand Down Expand Up @@ -108,11 +108,11 @@ public String apply(AnnotationInstance annotationInstance) {
private static class UnsupportedAnnotation {

final DotName name;
final Function<AnnotationInstance, String> messageFun;
final String correctAnnotation;

UnsupportedAnnotation(DotName name, Function<AnnotationInstance, String> messageFun) {
this.name = name;
this.messageFun = messageFun;
UnsupportedAnnotation(String name, String correctAnnotation) {
this.name = DotName.createSimple(name);
this.correctAnnotation = correctAnnotation;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import jakarta.enterprise.event.Event;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.inject.Qualifier;
import jakarta.inject.Singleton;

Expand All @@ -26,8 +27,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.google.inject.Inject;

import io.quarkus.arc.deployment.ObserverTransformerBuildItem;
import io.quarkus.arc.processor.ObserverTransformer;
import io.quarkus.builder.BuildChainBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.arc.test.wrongannotations;

import jakarta.enterprise.inject.spi.BeanManager;

import com.google.inject.Inject;

public class BeanWithIncorrectInject {

@Inject
BeanManager bm1;

@javax.inject.Inject
BeanManager bm2;

@com.oracle.svm.core.annotate.Inject
BeanManager bm3;

@org.gradle.internal.impldep.javax.inject.Inject
BeanManager bm4;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkus.arc.test.wrongannotations;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import javax.inject.Inject;

import jakarta.enterprise.inject.spi.BeanManager;

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

import io.quarkus.runtime.util.ExceptionUtil;
import io.quarkus.test.QuarkusUnitTest;

public class WrongInjectTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(BeanWithIncorrectInject.class))
.assertException(t -> {
Throwable rootCause = ExceptionUtil.getRootCause(t);
assertTrue(rootCause.getMessage().contains(
"@com.google.inject.Inject declared on io.quarkus.arc.test.wrongannotations.BeanWithIncorrectInject.bm1, use @jakarta.inject.Inject instead"),
t.toString());
assertTrue(rootCause.getMessage().contains(
"@javax.inject.Inject declared on io.quarkus.arc.test.wrongannotations.BeanWithIncorrectInject.bm2, use @jakarta.inject.Inject instead"),
t.toString());
assertTrue(rootCause.getMessage().contains(
"@com.oracle.svm.core.annotate.Inject declared on io.quarkus.arc.test.wrongannotations.BeanWithIncorrectInject.bm3, use @jakarta.inject.Inject instead"),
t.toString());
assertTrue(rootCause.getMessage().contains(
"@org.gradle.internal.impldep.javax.inject.Inject declared on io.quarkus.arc.test.wrongannotations.BeanWithIncorrectInject.bm4, use @jakarta.inject.Inject instead"),
t.toString());
assertTrue(rootCause.getMessage().contains(
"@javax.inject.Inject declared on io.quarkus.arc.test.wrongannotations.WrongInjectTest.beanManager, use @jakarta.inject.Inject instead"),
t.toString());
});

@Inject
BeanManager beanManager;

@Test
public void testValidationFailed() {
// This method should not be invoked
fail();
}

}

0 comments on commit d9b0770

Please sign in to comment.