Issue #384: Selecting a class from a CAS can yield a classcast exception if the CAS contains subclasses of the type not locally known#448
Open
Conversation
…ion if the CAS contains subclasses of the type not locally known - Added reproducer test
…ion if the CAS contains subclasses of the type not locally known - Fix `select(SuperType.class)` in PEAR contexts so subtype FSes whose JCas wrapper was loaded by the parent classloader get re-wrapped using the PEAR's override of the nearest ancestor type instead of being returned as parent-classloader instances (which would `ClassCastException` at the caller). - In `FSClassRegistry.getGeneratorsForTypeAndSubtypes`, when in PEAR mode and the resolved `JCasClassInfo` is not actually a PEAR override, walk further up the type hierarchy for an ancestor whose JCas class *is* PEAR-overridden, and use that ancestor's generator.
…ion if the CAS contains subclasses of the type not locally known - Update PEAR integration test `testScenario3` to reflect the corrected behavior: PEAR processing must succeed instead of throwing a `ClassCastException`, with the sub-type FS remaining in the CAS after the PEAR exits. - In `Scenario3TestAnnotator`, replace the comment describing the old broken behavior and add assertions that the FS returned by `select(ComplexAnnotation.class)` is a PEAR-local `ComplexAnnotation` instance while its UIMA type is still `ComplexAnnotationSubtype`. - Drop now-unused imports of `assertThatExceptionOfType` and `AnalysisEngineProcessException` in `PearIT`.
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #384 where select(SuperType.class) in a PEAR context can trigger ClassCastException when the CAS contains subtype FSes whose JCas wrappers are not locally (PEAR) compatible due to classloader isolation.
Changes:
- Adjust PEAR generator selection to wrap subtype FSes using the nearest PEAR-overridden ancestor wrapper, avoiding incompatible subtype wrappers.
- Update the PEAR integration scenario 3 to assert successful processing and verify the subtype FS remains in the CAS.
- Add a core regression test for PEAR-classloader
select(Class)behavior.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
uimaj-it-pear-with-typesystem/src/test/java/org/apache/uima/it/pear_with_typesystem/PearIT.java |
Updates scenario 3 test to expect successful processing (no ClassCastException) and verify the subtype FS persists. |
uimaj-it-pear-with-typesystem/src/main/java/org/apache/uima/it/pear_with_typesystem/Scenario3TestAnnotator.java |
Strengthens in-PEAR assertions to verify returned wrapper type and underlying UIMA type for the subtype FS. |
uimaj-core/src/test/java/org/apache/uima/cas/test/SelectByClassInPearContextTest.java |
Adds a core-level regression test exercising PEAR classloader selection behavior. |
uimaj-core/src/main/java/org/apache/uima/cas/impl/FSClassRegistry.java |
Implements the PEAR-specific ancestor-wrapper fallback to prevent classloader-incompatible subtype wrappers from causing ClassCastException. |
.gitignore |
Normalizes ignoring of VS Code folder to .vscode/. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+83
to
+90
| // The actual contract under test: select(SuperType.class) must only return FSes that are | ||
| // assignable to the requested class. The Level_2 instance's JCas wrapper is loaded by the | ||
| // parent classloader and is not assignable to the PEAR's Level_1, so it must be filtered out. | ||
| assertThat(casImpl.select(pearLevel1Class).asList()) | ||
| .as("select(PEAR_Level_1.class) must only return instances assignable to " | ||
| + "the PEAR's copy of Level_1") | ||
| .allMatch(pearLevel1Class::isInstance); | ||
| } |
Comment on lines
+1592
to
+1599
| // PEAR-only: if the JCas class found for this type is not actually overridden by the PEAR | ||
| // (the PEAR classloader delegated to a parent), keep walking up the type hierarchy for an | ||
| // ancestor whose JCas class *is* PEAR-overridden. Without this, subtype FSes would be wrapped | ||
| // with the parent-loaded JCas class and would not be assignable to the PEAR's copy of the | ||
| // super-type, causing ClassCastException in idiomatic PEAR code such as | ||
| // `for (T t : cas.select(T.class))`. See https://github.com/apache/uima-uimaj/issues/384. | ||
| if (isPear && !jcci.isPearOverride(tsi)) { | ||
| var ancestor = typeInfo; |
Comment on lines
+1592
to
+1596
| // PEAR-only: if the JCas class found for this type is not actually overridden by the PEAR | ||
| // (the PEAR classloader delegated to a parent), keep walking up the type hierarchy for an | ||
| // ancestor whose JCas class *is* PEAR-overridden. Without this, subtype FSes would be wrapped | ||
| // with the parent-loaded JCas class and would not be assignable to the PEAR's copy of the | ||
| // super-type, causing ClassCastException in idiomatic PEAR code such as |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's in the PR
How to test manually
Automatic testing
Documentation
Organizational
Only dependencies under approved licenses are allowed. LICENSE and NOTICE files in the respective modules where dependencies have been added as well as in the project root have been updated.