openrewrite / openrewrite/rewrite-static-analysis
ShortenFullyQualifiedTypeReferences does not shorten java.lang
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 62
- Forks
- 112
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 40
Description
Currently, ShortenFullyQualifiedTypeReferences does not shorten all types:
class TestClass {
java.util.List<java.lang.Enum> foo;
}
in combination with ShortenFullyQualifiedTypeReferences leads to
class TestClass {
List<java.lang.Enum> foo;
}
A change like the following to the visitor in ShortenFullyQualifiedTypeReferences,
@Override
public J.Identifier visitIdentifier(J.Identifier identifier, Map<String, JavaType> types) {
JavaType type = identifier.getType();
if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) {
types.put(identifier.getSimpleName(), type);
}
// ==========
if (type instanceof JavaType.Parameterized param) {
types.put(param.getClassName(), param.getType());
for (var typeParams : param.getTypeParameters()) {
if (typeParams instanceof JavaType.FullyQualified fq) {
types.put(fq.getClassName(), fq);
}
}
}
// ==========
return identifier;
}
fixes the issue for me.
Note that it already works if there is an explicit field of type Enum.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the ShortenFullyQualifiedTypeReferences visitor and its visitIdentifier method. Reproduce the issue with the Java snippet in the report, then inspect how parameterized type arguments such as java.lang.Enum are collected. Done means the recipe shortens both java.util.List and java.lang.Enum in the example without regressing explicit Enum fields.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100