Skip to content

Commit

Permalink
Scheduler - detect scheduled methods of the same name on a class
Browse files Browse the repository at this point in the history
- fix #31547
  • Loading branch information
mkouba committed Mar 2, 2023
1 parent 9207c6b commit e1f819d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public boolean isNonBlocking() {
return nonBlocking;
}

public String getMethodDescription() {
return method.declaringClass().name() + "#" + method.name() + "()";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
Expand Down Expand Up @@ -192,17 +194,23 @@ void validateScheduledBusinessMethods(SchedulerConfig config, List<ScheduledBusi
ValidationPhaseBuildItem validationPhase, BuildProducer<ValidationErrorBuildItem> validationErrors) {
List<Throwable> errors = new ArrayList<>();
Map<String, AnnotationInstance> encounteredIdentities = new HashMap<>();
Set<String> methodDescriptions = new HashSet<>();

for (ScheduledBusinessMethodItem scheduledMethod : scheduledMethods) {
if (!methodDescriptions.add(scheduledMethod.getMethodDescription())) {
errors.add(new IllegalStateException("Multiple @Scheduled methods of the same name declared on the same class: "
+ scheduledMethod.getMethodDescription()));
continue;
}
MethodInfo method = scheduledMethod.getMethod();
if (Modifier.isAbstract(method.flags())) {
errors.add(new IllegalStateException("@Scheduled method must not be abstract: "
+ method.declaringClass().name() + "#" + method.name() + "()"));
+ scheduledMethod.getMethodDescription()));
continue;
}
if (Modifier.isPrivate(method.flags())) {
errors.add(new IllegalStateException("@Scheduled method must not be private: "
+ method.declaringClass().name() + "#" + method.name() + "()"));
+ scheduledMethod.getMethodDescription()));
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.scheduler.test;

import jakarta.enterprise.inject.spi.DeploymentException;

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

import io.quarkus.scheduler.Scheduled;
import io.quarkus.scheduler.ScheduledExecution;
import io.quarkus.test.QuarkusUnitTest;

public class MultipleScheduledMethodsWithTheSameNameTest {

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest()
.setExpectedException(DeploymentException.class)
.withApplicationRoot((jar) -> jar
.addClasses(BeanWithInvalidScheduledMethods.class));

@Test
public void test() throws InterruptedException {
}

static class BeanWithInvalidScheduledMethods {

@Scheduled(cron = "0/1 * * * * ?")
void foo() {
}

@Scheduled(every = "10s")
void foo(ScheduledExecution execution) {
}

}

}

0 comments on commit e1f819d

Please sign in to comment.