XenoAmess opened a new pull request, #1820:
URL: https://github.com/apache/commons-lang/pull/1820
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
Thanks for your contribution to [Apache
Commons](https://commons.apache.org/)! Your help is appreciated!
Before you push a pull request, review this list:
- [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
- [x] Read the [ASF Generative Tooling
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use
Artificial Intelligence (AI).
- [x] I used AI to create any part of, or all of, this pull request. Which
AI tool was used to create this pull request, and to what extent did it
contribute?
- OpenCode (AI coding assistant) assisted with regression analysis,
implementation, unit tests, running validation commands, and drafting this pull
request description.
- [x] Run a successful build using the default
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command
line by itself.
- [x] Write unit tests that match behavioral changes, where the tests fail
if the changes to the runtime are not applied. This may not always be possible,
but it is a best practice.
- [x] Write a pull request description that is detailed enough to understand
what the pull request does, how, and why.
- [x] Each commit in the pull request should have a meaningful subject line
and body. Note that a maintainer may squash commits during the merge process.
## Summary
Fix a regression introduced by #1548 (LANG-1749), released in Commons Lang
3.21.0: `TypeUtils.isAssignable` rejects legal assignments such as
`Class<Integer>` to `Class<?>` because the `java.lang.Class` check requires
exact type-argument equality, including when the target argument is a wildcard.
## Reproducer
```java
import java.lang.reflect.Type;
import org.apache.commons.lang3.reflect.TypeUtils;
class Reproducer {
public static void main(String[] args) {
final Type from = TypeUtils.parameterize(Class.class, Integer.class);
final Type to = TypeUtils.parameterize(Class.class,
TypeUtils.wildcardType().build());
System.out.println(TypeUtils.isAssignable(from, to));
}
}
```
Output:
| Version | Result |
| --- | --- |
| 3.20.0 | `true` |
| 3.21.0 | `false` |
| With this fix | `true` |
The corresponding assignment is accepted by `javac`:
```java
Class<Integer> source = Integer.class;
Class<?> target = source;
```
## Downstream evidence: EvoSuite
This regression caused a real downstream build failure when EvoSuite
upgraded Commons Lang:
1. **Dependency-update PR:**
[XenoAmess/evosuite#363](https://github.com/XenoAmess/evosuite/pull/363),
upgrading `org.apache.commons:commons-lang3` from 3.20.0 to 3.21.0.
2. **Failing Java CI:** [Ubuntu / Java 21
build](https://github.com/XenoAmess/evosuite/actions/runs/37233026734/job/111526535103),
at the dependency-update commit `8cc82c7ebe4c22952e4bbab46401ba0ceccc4e78`.
The affected downstream test is `TestGenericClassImpl.testAssignableClass`.
3. **Downstream workaround:** [EvoSuite commit
`6fd000e4a2934afa91397306f80f8101e33d8741`](https://github.com/XenoAmess/evosuite/commit/6fd000e4a2934afa91397306f80f8101e33d8741),
which adds a fallback to `GenericClassUtils.isAssignable` for parameterized
`Class` types. For example, when Commons Lang rejects `Class<Integer>` to
`Class<?>`, the workaround checks the target wildcard's bounds explicitly
instead of requiring exact argument equality.
4. **Java CI after the workaround:** [Successful run at
`6fd000e4a`](https://github.com/XenoAmess/evosuite/actions/runs/38734916786),
with both Ubuntu and Windows Java 21 builds passing.
## Implementation
- Allow a target `Class` wildcard argument when the source argument
satisfies its bounds, using the existing wildcard assignability logic. This
also handles wildcard source arguments such as `Class<? extends Integer>` to
`Class<? extends Number>`.
- Preserve an unresolved source type variable for the wildcard check instead
of losing it during variable-assignment unrolling. This allows `Class<T>` to
`Class<?>` when `T` is declared by another class.
- Continue rejecting raw generic source arguments against parameterized
upper bounds, since unchecked conversion does not apply inside a type argument.
The three regression tests introduced by #1548 continue to pass.
- Check lower bounds in the correct direction, including the legal
assignment from `Class<List>` to `Class<? super List<String>>`.
## Validation
- Added five regression test methods covering unbounded, upper-bounded,
lower-bounded, and parameterized wildcard bounds, as well as source wildcards
and type variables.
- With the original upstream `Class` check restored, all five new test
methods fail; the three existing #1548 tests pass.
- With the fix applied, the default `mvn` command succeeds: **96,027 tests,
0 failures, 0 errors, 16 skipped**. Its default goals also run Apache RAT,
Checkstyle, japicmp, SpotBugs, PMD, and Javadoc.
- Checked representative positive assignments against `javac
-Xlint:unchecked -Werror`.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]