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

Renaming the package name, is creating a compilation issue #1522

Closed
sourabhsparkala opened this issue Sep 6, 2017 · 3 comments
Closed

Renaming the package name, is creating a compilation issue #1522

sourabhsparkala opened this issue Sep 6, 2017 · 3 comments
Labels

Comments

@sourabhsparkala
Copy link

sourabhsparkala commented Sep 6, 2017

Hello,

Please find the below code

package app;

Class Test{
        public static void main(String[] args){
                  System.out.println("test");
        }
}

I am parsing the code using spoon, and I am trying to change the name of the package app

(element instanceof CtPackage){
         ((CtPackage) element).setSimpleName("newtest");
}

The output of the code is occurring as:

package .newtest;

Class Test{
        public static void main(String[] args){
                  System.out.println("test");
        }
}

There is PACKAGE_SEPERATOR '.' added before the package. I am not sure if this is the correct way of renaming the package for a class.

Regards
Sourabh

surli added a commit to surli/spoon that referenced this issue Sep 7, 2017
@surli
Copy link
Collaborator

surli commented Sep 7, 2017

Hi @sourabhsparkala

Could you give us more insight on the way you're renaming the package: what is the exact context of this piece of code:

(element instanceof CtPackage){
         ((CtPackage) element).setSimpleName("newtest");
}

I tried to reproduce your issue but without success yet. What version of Spoon do you use?

@sourabhsparkala
Copy link
Author

sourabhsparkala commented Sep 7, 2017

Hello @surli

I am using this maven repository core

<dependency>
    <groupId>fr.inria.gforge.spoon</groupId>
    <artifactId>spoon-core</artifactId>
    <version>5.9.0</version>
</dependency>

I am parsing the file, and i wanted to change the package name for example

Src Input file
src/main/java/app/App.java--> the package name is 'app'

package app;

Class Test{
        public static void main(String[] args){
                  System.out.println("test");
        }
}

The processor which I wrote to process the .java file is:

public class ElementProcessor extends AbstractProcessor<CtElement>{
       public void process(CtElement element) {
              if(element instanceof CtPackage){
                      ((CtPackage) element).setSimpleName("newtest");
              }
       }
}

The output after the process should be
if the output is give as src/main/java

The final output of the folder package should be src/main/java/newtest/App.java --> package newtest

But the output in the App.java file is occurring as

package .newtest;

Class Test{
        public static void main(String[] args){
                  System.out.println("test");
        }
}

With PACKAGE_SEPERATOR '.' before as shown above. Which is creating the compilation issue.

I hope I was able to explain my requirement. Your insights would help in understanding if I am missing something here.

Regards
Sourabh

surli added a commit to surli/spoon that referenced this issue Sep 11, 2017
@surli
Copy link
Collaborator

surli commented Sep 11, 2017

Ok it might be a bug in Spoon but you can already fix it in ElementProcessor: the bug appears because you also rename the "root package" of Spoon, which should never be renamed.

So in ElementProcessor instead of:

if(element instanceof CtPackage){

use:

if(element instanceof CtPackage && element != getFactory().Package().getRootPackage()){

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

No branches or pull requests

2 participants