Skip to content

[3.0.x] Fix ClassCastException when using @With Java annotation#13282

Merged
mkurz merged 2 commits into
playframework:3.0.xfrom
mkurz:fix_classcastexception
May 22, 2025
Merged

[3.0.x] Fix ClassCastException when using @With Java annotation#13282
mkurz merged 2 commits into
playframework:3.0.xfrom
mkurz:fix_classcastexception

Conversation

@mkurz
Copy link
Copy Markdown
Member

@mkurz mkurz commented May 21, 2025

See

This does not fix the problem but at least avoids the ClassCastException

When using @With directly on an action method, there simply is no configuration.
Even the "Simple Action" that is used in the docs when using @With make that clear and uses Void

/** A simple action with no configuration. */
public abstract static class Simple extends Action<Void> {}

However, what the author in #13281 tries, even fails with a ClassCastException when using Action.Simple because also Void can not be cast to the desired type.

@mkurz mkurz changed the title Fix ClassCastException when using @with Java Annotation Fix ClassCastException when using @With Java Annotation May 21, 2025
@mkurz mkurz changed the title Fix ClassCastException when using @With Java Annotation Fix ClassCastException when using @With Java annotation May 21, 2025
@mkurz mkurz changed the title Fix ClassCastException when using @With Java annotation [3.0.x] Fix ClassCastException when using @With Java annotation May 22, 2025
@mkurz
Copy link
Copy Markdown
Member Author

mkurz commented May 22, 2025

So I had a look again. I am very sure this "fix" is correct.
In the end it does not change anything. Also no one was relying on it because you currently get a class cast exception anyway.

More explanation with this example:

import play.mvc.*;

import java.util.concurrent.CompletionStage;

public final class AnnotationAction extends play.mvc.Action.Simple {

    @Override
    public CompletionStage<Result> call(Http.Request request) {
    
            // Both of these line give a ClassCastException currently.
            // After this PR it will be a NullPointerException
            // So nothing really changes, but the exception is more accurate
            configuration.getClass();
            configuration.toString();
            
            // The only thing that changes is that after this PR we can at least check
            // for null (null = @With was used), but before this PR it was always "not null"
            if(configuration == null) {
                // @With was used - impossible to find out before
            }
            return delegate.call(request);
    }
}
    @With(AnnotationAction.class)
    public Result myAction { ... }

@mkurz mkurz marked this pull request as ready for review May 22, 2025 10:23
@mkurz mkurz merged commit 6551272 into playframework:3.0.x May 22, 2025
25 checks passed
@mkurz mkurz deleted the fix_classcastexception branch May 22, 2025 10:23
@mkurz
Copy link
Copy Markdown
Member Author

mkurz commented May 22, 2025

@Mergifyio backport 2.9.x main

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 22, 2025

backport 2.9.x main

✅ Backports have been created

Details

mergify Bot added a commit that referenced this pull request May 22, 2025
[2.9.x] Fix `ClassCastException` when using `@With` Java annotation (backport #13282) by @mkurz
mergify Bot added a commit that referenced this pull request May 22, 2025
Fix `ClassCastException` when using `@With` Java annotation (backport #13282) by @mkurz
@mkurz mkurz added this to the 3.0.8 milestone Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant