Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Ensure that ConversionService.canConvert(Enum) no longer throws an exception #34532

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

Open
moonfruit wants to merge 1 commit into spring-projects:main
base: main
Choose a base branch
Loading
from moonfruit:to-enum-convert-factory

Conversation

Copy link

@moonfruit moonfruit commented Mar 4, 2025
edited
Loading

In my project, I use the following code to find an appropriate method to execute for a given class. It throws an IllegalArgumentException: The target type java.lang.Enum does not refer to an enum when there is a more appropriate "method": asString(CharSequence).

public class MyTest {
 public static void main(String[] args) throws AccessException {
 EvaluationContext evaluationContext = SimpleEvaluationContext.forReadOnlyDataBinding().build();
 ReflectiveMethodResolver methodResolver = new ReflectiveMethodResolver();
 MethodExecutor asString = methodResolver.resolve(evaluationContext, MyTest.class, "asString",
 Collections.singletonList(TypeDescriptor.valueOf(String.class)));
 TypedValue result = asString.execute(evaluationContext, MyTest.class, "value");
 System.out.println(result);
 }
 public static String asString(Enum<?> left) {
 return left.name();
 }
 public static String asString(CharSequence left) {
 return left.toString();
 }
}

After doing some research I realized that the root cause was that StringToEnumConverterFactory.getConverter(Enum.class) threw an exception when it couldn't convert and resolve the concrete Enum. This directly causes ConversionService.canConvert(String.class,Enum.class) to throw an exception. I don't think the method canConvert() should throw an exception, it returns true if it can convert and false if it can't. That's what this method is for, to avoid throwing an exception when calling the convert method directly.

So I've modified StringToEnumConverterFactory and IntegerToEnumConverterFactory a little and hopefully this PR will be accepted.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 4, 2025
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Mar 5, 2025
Copy link
Author

Hi, @sbrannen. Is there something else I should change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /