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

Misleading error message when cloning fails to create a new instance #271

Closed
marcotama opened this issue Sep 14, 2023 · 3 comments · Fixed by #308
Closed

Misleading error message when cloning fails to create a new instance #271

marcotama opened this issue Sep 14, 2023 · 3 comments · Fixed by #308
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@marcotama
Copy link

Describe the bug
When the FieldAccessingSolutionCloner fails to construct an object, it throws an exception with message "The class $CLAZZ should have a no-arg constructor to create a planning clone.". However, construction can fail for other reasons, e.g. in my case, I initialize some fields based on other fields, and this failed when a certain collection was empty.

Expected behavior
A better error message, e.g. "Failed to create a new instance of class $CLAZZ for the purpose of solution-cloning. This could be due to a missing no-arg constructor or some other error inside the logic of class $CLAZZ triggered by the no-arg constructor."

Actual behavior
Describe clearly and concisely what actually happened.

To Reproduce
N/A

Environment

Timefold Solver Version or Git ref: 1.1.0

Output of java -version: N/A

Output of uname -a or ver: N/A

Additional information

N/A

@marcotama marcotama added bug Something isn't working process/needs triage Requires initial assessment of validity, priority etc. labels Sep 14, 2023
@triceo
Copy link
Contributor

triceo commented Sep 14, 2023

Thanks for reporting, @marcotama!
Would you have an example source code of a class that failed to clone in this way?

@marcotama
Copy link
Author

marcotama commented Sep 14, 2023

Hello!

Something like this (Kotlin).
When you create a new TimeBlock using the default constructor, the list of Timeslots is empty and so next() throws a NoSuchElementException.

import java.time.DayOfWeek

class Timeslot(
    @PlanningId
    val id: Int? = null,
    val name: String? = null,
    val dayOfWeek: DayOfWeek? = null,
    val startTime: LocalTime? = null,
    val endTime: LocalTime? = null,
)

class TimeBlock(
    @PlanningId
    val id: Int? = null,
    val name: String? = null,
    val allTimeslots: Set<Timeslot> = mutableSetOf(),
) {
    val day: DayOfWeek? = allTimeslots.map { it.dayOfWeek }.iterator().next()
}

Of course there are problems with my classes, but I think the error message could be improved - I ended up spending 10 minutes figuring out why my no-arg constructor was not getting picked up.

@triceo
Copy link
Contributor

triceo commented Sep 14, 2023

Thank you for the example. You are right, the message could be improved.

@triceo triceo removed the process/needs triage Requires initial assessment of validity, priority etc. label Sep 14, 2023
@triceo triceo self-assigned this Sep 14, 2023
@triceo triceo added enhancement New feature or request and removed bug Something isn't working labels Sep 14, 2023
@triceo triceo added this to the 1.3.0 milestone Sep 14, 2023
@triceo triceo linked a pull request Oct 8, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants