openrewrite / openrewrite/rewrite-static-analysis

ShortenFullyQualifiedTypeReferences does not shorten java.lang

Open
#113 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.