Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning message if using Kotlin default parameter values (see #857) #860

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

import static java.util.Arrays.asList;
import static org.jobrunr.utils.reflection.ReflectionUtils.cast;
import static org.jobrunr.utils.reflection.ReflectionUtils.classExists;

public class AllJVMInstructions {

private static final Map<Integer, Function<JobDetailsBuilder, AbstractJVMInstruction>> instructions = new HashMap<>();
private static final Map<Integer, String> unsupportedInstructions = new HashMap<>();
private static final Map<Integer, String> kotlinUnsupportedInstructions = new HashMap<>();

static {
instructions.put(Opcodes.AASTORE, AAStoreInstruction::new);
Expand Down Expand Up @@ -71,6 +73,8 @@ public class AllJVMInstructions {
.forEach(instr -> unsupportedInstructions.put(instr, "You are dividing two numbers while enqueueing/scheduling jobs" + mathematicalPerformanceSuffix));
asList(Opcodes.IREM, Opcodes.LREM, Opcodes.FREM, Opcodes.DREM)
.forEach(instr -> unsupportedInstructions.put(instr, "You are calculating the remainder (%) for two numbers while enqueueing/scheduling jobs" + mathematicalPerformanceSuffix));

kotlinUnsupportedInstructions.put(Opcodes.ACONST_NULL, "You are (probably) using Kotlin default parameter values which is not supported by JobRunr.");
}

private AllJVMInstructions() {
Expand All @@ -82,6 +86,8 @@ public static <T extends AbstractJVMInstruction> T get(int opcode, JobDetailsBui
if (instructionBuilder == null) {
if (unsupportedInstructions.containsKey(opcode)) {
throw new IllegalArgumentException("Unsupported lambda", new UnsupportedOperationException(unsupportedInstructions.get(opcode)));
} else if (classExists("kotlin.KotlinVersion") && kotlinUnsupportedInstructions.containsKey(opcode)) {
throw new IllegalArgumentException("Unsupported lambda", new UnsupportedOperationException(kotlinUnsupportedInstructions.get(opcode)));
}
throw JobRunrException.shouldNotHappenException(new IllegalArgumentException("Instruction " + opcode + " not found"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonValue
import org.awaitility.Awaitility.await
import org.awaitility.Durations
import org.jobrunr.JobRunrAssertions.assertThat
import org.jobrunr.JobRunrAssertions.assertThatCode
import org.jobrunr.configuration.JobRunr
import org.jobrunr.jobs.mappers.JobMapper
import org.jobrunr.jobs.states.StateName.*
Expand Down Expand Up @@ -57,6 +58,15 @@ class JobSchedulerTest {
assertThat(job).hasStates(ENQUEUED, PROCESSING, SUCCEEDED)
}

@Test
fun `test enqueue lambda with default parameter throws exception`() {
val testService = TestService()
assertThatCode { jobScheduler.enqueue { testService.doWorkWithDefaultParameter() } }
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessage("Unsupported lambda")
.hasRootCauseMessage("You are (probably) using Kotlin default parameter values which is not supported by JobRunr.")
}

@Test
fun `test enqueue lambda with service dependency`() {
val testService = TestService()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.jobrunr.scheduling

import org.jobrunr.jobs.annotations.Job
import java.util.*

class TestService {

fun doWork(s: String) {
println(s)
}

fun doWorkWithDefaultParameter(id: UUID = UUID.randomUUID()) {
println("id: $id")
}

@Job(name = "Some neat Job Display Name")
fun doWorkWithJobName(s: String, jobName: String) {
println("$s $jobName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
import org.awaitility.Awaitility.await
import org.awaitility.Durations
import org.jobrunr.JobRunrAssertions
import org.jobrunr.JobRunrAssertions.assertThat
import org.jobrunr.configuration.JobRunr
import org.jobrunr.jobs.mappers.JobMapper
Expand Down Expand Up @@ -57,6 +58,15 @@ class JobSchedulerTest {
assertThat(job).hasStates(ENQUEUED, PROCESSING, SUCCEEDED)
}

@Test
fun `test enqueue lambda with default parameter throws exception`() {
val testService = TestService()
JobRunrAssertions.assertThatCode { jobScheduler.enqueue { testService.doWorkWithDefaultParameter() } }
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessage("Unsupported lambda")
.hasRootCauseMessage("You are (probably) using Kotlin default parameter values which is not supported by JobRunr.")
}

@Test
fun `test enqueue lambda with service dependency`() {
val testService = TestService()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jobrunr.scheduling

import org.jobrunr.jobs.annotations.Job
import java.util.*

class TestService {

Expand All @@ -10,6 +11,10 @@ class TestService {
println(s)
}

fun doWorkWithDefaultParameter(id: UUID = UUID.randomUUID()) {
println("id: $id")
}

fun doWorkWithPair(s: Pair<String, String>) {
println("Pair: " + s.first + "; " + s.second)
}
Expand Down