You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "jerinraj5555 (via GitHub)" <gi...@apache.org> on 2026/04/20 07:19:07 UTC

[PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

jerinraj5555 opened a new pull request, #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35

   ## Problem
   
   `ContextBvpCollector.addingService()` crashes with a `ClassCastException` on Java 21 (Linux) when a `BindingsValuesProvider` service does not set the `ScriptEngine.NAME` property.
   
   The root cause is in this line:
   
   ```java
   final String[] engineNames = Converters.standardConverter()
           .convert(ref.getProperty(ScriptEngine.NAME))
           .to(String[].class);
   ```
   
   When `ref.getProperty(ScriptEngine.NAME)` returns `null`, the OSGi Converters API (`org.osgi.util.converter:1.0.9`) returns `Object[]` instead of `String[]` on Java 21, causing:
   
   ```
   java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String;
   ```
   
   This affects all versions of `scripting.core` (2.4.x and 3.0.x) and causes widespread test failures when running under Java 21.
   
   ## Fix
   
   Add a null-check before invoking the Converters API:
   
   ```java
   final Object prop = ref.getProperty(ScriptEngine.NAME);
   final String[] engineNames = (prop == null) ? new String[0]
           : Converters.standardConverter().convert(prop).to(String[].class);
   ```
   
   This avoids the Converters API entirely when the property is null, which is a valid and common case for generic `BindingsValuesProvider` services that apply to all script engines.
   
   ## Impact
   
   - Fixes 100+ test failures when running under Java 21
   - No behaviour change for services that do set `ScriptEngine.NAME`
   - Fully backwards compatible


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "cziegeler (via GitHub)" <gi...@apache.org>.
cziegeler commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4589867235

   I'll close this PR as we cant reproduce the problem, especially as the osgi converter 1.0.9 is the latest version available and is used in these tests.
   If this issue still arises, please: a) create a SLING issue first and b) come up with an exact step by step plan on how to reproduce, a test case would even be better.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4560363481

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "cziegeler (via GitHub)" <gi...@apache.org>.
cziegeler closed pull request #35: Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "jerinraj5555 (via GitHub)" <gi...@apache.org>.
jerinraj5555 commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4341448277

   Thank you for testing this — the difference comes down to which converter implementation is on the classpath.
                                                                                                                                                                                              
     The failure is version-specific to org.osgi.util.converter:1.0.9         
                                                                                                                                                                                              
     When you run locally or on Jenkins, org.apache.felix.converter:1.0.14 (or a newer osgi.util.converter) is resolved, which handles the null → String[] conversion correctly. The bug only 
     manifests with org.osgi.util.converter:1.0.9.                                                                                                                                            
                                                                                                                                                                                              
     To reproduce the exact failure, force org.osgi.util.converter:1.0.9 onto the classpath (not felix.converter) and run:                                                                    
     
   `  final String[] engineNames = Converters.standardConverter().convert(null).to(String[].class);                                                                                            
     // → ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; (Java 21)    `                                                                                         
                                                                                                                                                                                              
     **Our environment: AEM 6.5 LTS SP2**                                                                                                                                                         
                                                                                                                                                                                              
     AEM 6.5 LTS SP2 ships org.osgi.util.converter:1.0.9 baked directly inside uber-jar:6.6.2 as embedded classes. In that container 1.0.9 always wins over any test-scoped felix.converter   
     because it is inside the fat JAR — not a separate resolvable artifact. That is the exact environment where this causes 100+ test failures on Java 21 — every test that initialises a
     Sling mock context hits this code path.                                                                                                                                                  
                                                                              
     Why the null-check still makes sense                                                                                                                                                     
      
     The fix bypasses the Converters API entirely when the property is null, which is the valid and common case for generic BindingsValuesProvider services that apply to all script engines. 
     This makes ContextBvpCollector safe regardless of which converter version is present at runtime — important for AEM and any other container that embeds 1.0.9 and cannot easily replace
     it.                                                                                                                                                                                      
              


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "rombert (via GitHub)" <gi...@apache.org>.
rombert commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4341944904

   Thank you for the added context @jerinraj5555 . Can you please explain how to force-trigger the failure in this project? Or maybe in the Sling Starter?
   
   Looking at the dependency tree for scripting.core I only see the osgi.converter in the version you indicated as problematic
   
   ```
   $ mvn dependency:tree -Dscope=test | grep -C 3 converter
   [INFO] +- org.apache.sling:org.apache.sling.api:jar:3.0.0:provided
   [INFO] +- org.apache.felix:org.apache.felix.http.wrappers:jar:6.1.0:provided
   [INFO] +- org.apache.sling:org.apache.sling.commons.mime:jar:2.2.4:provided
   [INFO] +- org.osgi:org.osgi.util.converter:jar:1.0.9:provided
   [INFO] |  \- org.osgi:org.osgi.util.function:jar:1.1.0:provided
   [INFO] +- org.apache.sling:org.apache.sling.scripting.api:jar:2.1.12:provided
   [INFO] +- org.apache.sling:org.apache.sling.scripting.spi:jar:2.0.0:provided
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "cziegeler (via GitHub)" <gi...@apache.org>.
cziegeler commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4280939565

   converters support a default value (via defaultValue) which can be used - this avoids the extra null check


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "jerinraj5555 (via GitHub)" <gi...@apache.org>.
jerinraj5555 commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4311031026

   Thank you both for the feedback!
   
   **@cziegeler** — Applied your suggestion. Instead of the manual null-check, the fix now uses `.defaultValue(new String[0])` on the Converters chain:
   
   ```java
   final String[] engineNames = Converters.standardConverter()
           .convert(ref.getProperty(ScriptEngine.NAME))
           .defaultValue(new String[0])
           .to(String[].class);
   ```
   
   This is cleaner and more idiomatic with the OSGi Converters API.
   
   **@rombert** — The Spotless formatting violation has also been fixed (the previous ternary line break was the cause of the build failure). A Sling JIRA issue will be created as requested — I'll link it here once done.
   
   To clarify the problem statement: this `ClassCastException` is reproducible when a `BindingsValuesProvider` OSGi service does **not** set the `ScriptEngine.NAME` property (which is a valid and documented use case). On Java 21, `Converters.standardConverter().convert(null).to(String[].class)` returns `Object[]` instead of `String[]`, causing the cast to fail. The existing Jenkins CI passes because the upstream test suite does not cover this null-property scenario — the fix prevents the crash for services that legitimately omit `ScriptEngine.NAME`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "cziegeler (via GitHub)" <gi...@apache.org>.
cziegeler commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4311234076

   Sorry that I didnt check earlier, but when I include the below and run it on Java 21, I dont get a ClassCastException:
   ``
           final String[] engineNames =
                   Converters.standardConverter().convert(null).to(String[].class);
   ``


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4311055189

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix ClassCastException in ContextBvpCollector on Java 21 when ScriptEngine.NAME is null [sling-org-apache-sling-scripting-core]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #35:
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/35#issuecomment-4311076533

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-scripting-core&pullRequest=35)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org