Skip to content

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
reckart wants to merge 3 commits into
mainfrom
bugfix/384-Selecting-a-class-from-a-CAS-can-yield-a-classcast-exception-if-the-CAS-contains-subclasses-of-the-type-not-locally-known
Open

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
reckart wants to merge 3 commits into
mainfrom
bugfix/384-Selecting-a-class-from-a-CAS-can-yield-a-classcast-exception-if-the-CAS-contains-subclasses-of-the-type-not-locally-known

Conversation

@reckart
Copy link
Copy Markdown
Member

@reckart reckart commented May 25, 2026

What's in the PR

  • Added reproducer test

How to test manually

  • ...

Automatic testing

  • PR adds/updates unit tests

Documentation

  • PR adds/updates documentation

Organizational

  • PR adds/updates dependencies.
    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.

…ion if the CAS contains subclasses of the type not locally known

- Added reproducer test
@reckart reckart added this to the 3.7.0 milestone May 25, 2026
@reckart reckart self-assigned this May 25, 2026
@reckart reckart added the 🦟 Bug Something isn't working label May 25, 2026
reckart added 2 commits May 25, 2026 18:38
…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`.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🦟 Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants