Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

package statements are missing #2944

Closed
lzfgnu opened this issue Apr 12, 2019 · 6 comments · Fixed by #2945
Closed

package statements are missing #2944

lzfgnu opened this issue Apr 12, 2019 · 6 comments · Fixed by #2945

Comments

@lzfgnu
Copy link

lzfgnu commented Apr 12, 2019

I'm writing a program to format java code(variable renaming,function renaming,class renaming). the second screenshot is the input code to format, while the first screen is the output. you can see that the input contains package import statement. I want to remain those statement in the output code. Because I am just doing renaming. How can I remain those package statements ?

Screenshot from 2019-04-12 16-26-00
Screenshot from 2019-04-12 16-26-36

`
Launcher launcher = new Launcher();
launcher.addInputResource(projectPath);
launcher.getEnvironment().setAutoImports(true);
CtModel model = launcher.buildModel();
model.processWith(new FieldProcessor());
model.processWith(new FieldAccessProcessor(table));
model.processWith(new VariableProcessor());
model.processWith(new ParamProcessor());
Collection<CtType<?>> types = model.getAllTypes();

    List<CtType<?>> typeList = new ArrayList<>();
    for (CtType<?> type : types) {

        String newClassName = NameTransform.transform(Type.CLASS, type.getSimpleName());
        if (!type.getSimpleName().equals(newClassName)) {
            Refactoring.changeTypeName(type, newClassName);
        }

        Set<CtMethod<?>> methods = type.getMethods();
        if (methods != null) {
            for (CtMethod<?> method : methods) {
                String newName =
                        NameTransform.transform(Type.METHOD, method.getSimpleName());
                if (!method.getSimpleName().equals(newName)) {
                    Refactoring.changeMethodName(method, newName);
                }
            }
        }

        Set<CtType<?>> nestTypes = type.getNestedTypes();
        Queue<CtType<?>> nestTypesQ = new ArrayDeque<>();
        if (!nestTypes.isEmpty()) {
            nestTypesQ.addAll(nestTypes);
        }
        while (!nestTypesQ.isEmpty()) {
            CtType<?> nestType = nestTypesQ.poll();
            String newName = NameTransform.transform(Type.CLASS, nestType.getSimpleName());
            if (!nestType.getSimpleName().equals(newName)) {
                Refactoring.changeTypeName(nestType, newName);
            }
            nestTypes = nestType.getNestedTypes();
            if (nestTypes != null) {
                nestTypesQ.addAll(nestTypes);
            }
        }
        typeList.add(type);

    }
    for (CtType<?> type : typeList) {
        System.out.println(type);
    }
}`

The same problem in other issue
Screenshot from 2019-04-12 22-38-07

@surli
Copy link
Collaborator

surli commented Apr 12, 2019

Not sure to understand the issue here.
According to your screenshot you're talking about an "unused import statement", which means that one of the import is not used in your class.
Basically in your example TEST2.testT4 is not used anymore so IntelliJ informs you that this imports is useless. I guess it's related to the transformation you performed.

So in a nutshell there's nothing wrong here.

@lzfgnu
Copy link
Author

lzfgnu commented Apr 12, 2019

Not sure to understand the issue here.
According to your screenshot you're talking about an "unused import statement", which means that one of the import is not used in your class.
Basically in your example TEST2.testT4 is not used anymore so IntelliJ informs you that this imports is useless. I guess it's related to the transformation you performed.

So in a nutshell there's nothing wrong here.

I'm writing a program to format java code(variable renaming,function renaming,class renaming). the second screenshot is the input code to format, while the first screen is the output. you can see that the input contains package import statement. I want to remain those statement in the output code. Because I am just doing renaming. How can I remain those package statements ?

@surli
Copy link
Collaborator

surli commented Apr 12, 2019

The problem is that your original input contains several classes: by default, Spoon process the file to display only one class by compilation unit.
You can change this behaviour by setting it in the environment:

launcher.getEnvironment().setOutputType(OutputType.COMPILATION_UNITS);

@lzfgnu
Copy link
Author

lzfgnu commented Apr 12, 2019

The problem is that your original input contains several classes: by default, Spoon process the file to display only one class by compilation unit.
You can change this behaviour by setting it in the environment:

launcher.getEnvironment().setOutputType(OutputType.COMPILATION_UNITS);

Thanks for replying! I tried your method just now. but how can I get the output codes.
for (CtType<?> type : typeList) { System.out.println(type); }
the code above doesn't work.

@lzfgnu
Copy link
Author

lzfgnu commented Apr 12, 2019

I saw the same problem in other issue
Screenshot from 2019-04-12 22-38-07

@monperrus
Copy link
Collaborator

See #2945

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants