INRIA / INRIA/spoon

Auto Import not working: Output not showing simple names

Open
#4,070 15 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

I am trying to change the type of the field from **org.apache.log4j.Logger** to **org.apache.logging.log4j.Logger**. I am also creating an assignment expression to set that in the field.

The code for this is -

```
public void process(CtField element) {
final CtTypeReference loggerRef = getFactory().Code().createCtTypeReference(Logger.class);
final CtField loggerField = getFactory()
.createCtField("LOGGER", loggerRef, null, ModifierKind.PRIVATE, ModifierKind.FINAL, ModifierKind.STATIC);
CtExpression assignment = element.getAssignment();
CtTypeAccess typeAccess = getFactory().createTypeAccess(getFactory().Code().createCtTypeReference(LogManager.class));
((CtInvocation) assignment).setTarget(typeAccess);
loggerField.setAssignment(assignment);
element.replace(loggerField);
}
```

I have set the **autoimport as true** for the launcher and the code for running the processor is -

```
Launcher launcher = new Launcher();
launcher.addInputResource("./src/main/java/");
launcher.setSourceOutputDirectory("./src/main/java/");
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setPrettyPrinterCreator(() -> new SniperJavaPrettyPrinter(launcher.getEnvironment()));
launcher.buildModel();
```

But my output is still getting created in this way -

`private static final org.apache.logging.log4j.Logger LOGGER = org.apache.logging.log4j.LogManager.getLogger(spoon.process.classes.InstrumentUtils.class);`

The simple names are not being used.
Also, **org.apache.log4j.Logger** import is still there in the file.

Is there something I am missing in my code?

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the provided Launcher setup against ./src/main/java/ and inspect the generated InstrumentUtils output first. Trace how setAutoImports(true) interacts with SniperJavaPrettyPrinter and the replacement code. Done means the generated field uses simple Logger and LogManager names and the obsolete org.apache.log4j.Logger import is removed.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.