rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

ShortenFullyQualifiedTypeReferences does not shorten java.lang

Open uhoefel opened this issue 1 year ago • 4 comments

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.

uhoefel avatar May 20 '23 22:05 uhoefel