Skip to content

expandWildcardImports: necessary imports in method call expressions removed #2833

@awa-xima

Description

@awa-xima

Issue

The new expandWildcardImports step removes necessary imports in method call expressions, e.g.:

import com.alibaba.fastjson.*;
public class Demo {
  public static void main() {
    final JSONObject jsonObject = new JSONObject();
    final Script inlineScript = JSON.toJSONString(jsonObject);
  }
}

becomes

import com.alibaba.fastjson.JSONObject;
public class Demo {
  public static void main() {
    final JSONObject jsonObject = new JSONObject();
    final Script inlineScript = JSON.toJSONString(jsonObject);
  }
}

The import import com.alibaba.fastjson.JSON is missing and the code fails to compile.

Java version

openjdk 25.0.1 2025-10-21
OpenJDK Runtime Environment (build 25.0.1+8-Ubuntu-125.10)
OpenJDK 64-Bit Server VM (build 25.0.1+8-Ubuntu-125.10, mixed mode, sharing)

Solution

Code like JSON.toJSONString(jsonObject) seems to be a method call expression:

public void visit(final MethodCallExpr n, final Map<ImportDeclaration, Set<ImportDeclaration>> importMap) {
// static imports
ResolvedMethodDeclaration resolved = wrapUnsolvedSymbolException(n, MethodCallExpr::resolve);
if (resolved.isStatic()) {
matchTypeName(importMap, resolved.getQualifiedName(), true);
}
super.visit(n, importMap);
}

The current code above only checks the method (toJSONString), but not the scope of the method. Adding this resolved the issue for me:

			n.getScope().ifPresent(s -> {
				ResolvedType type = n.getSymbolResolver().calculateType(n.getScope().get());
				if (type != null && type.isReference()) {
					matchTypeName(importMap, type.asReferenceType().getQualifiedName(), false);
				}
			});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions