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

How could I disable static imports in the v55 output? #1152

Closed
yanOnGithub opened this issue Jan 29, 2017 · 7 comments
Closed

How could I disable static imports in the v55 output? #1152

yanOnGithub opened this issue Jan 29, 2017 · 7 comments

Comments

@yanOnGithub
Copy link

I found v53+ are producing static imports.

# input/Constants.java
package com.company.framework;
public class Constants {
	public static int READY = 0x1;
	public static int RUNNING = 0x2;
}

# input/Main.java
package com.company;
import com.company.framework.Constants;
public class Main {
	public static void main(String[] args) throws Exception {
		System.out.println(Constants.READY);
	}
}

# output/v52/Main.java
package com.company;
import com.company.framework.Constants;
public class Main {
	public static void main(String[] args) throws Exception {
		System.out.println(Constants.READY);
	}
}

# output/v52/Constants.java
package com.company;
public class Constants {
	public static int READY = 1;
	public static int RUNNING = 2;
}

# output/v55/Main.java
package com.company;
import com.company.framework.Constants;
import static com.company.framework.Constants.READY;
import static java.lang.System.out;
public class Main {
	public static void main(String[] args) throws Exception {
		out.println(READY);
	}
}

@surli
Copy link
Collaborator

surli commented Jan 30, 2017

We introduced static imports in Spoon 5.5 when you use the --with-imports argument. You can disable all imports if you don't pass the argument.
The choice to add static imports has been made to fix some bugs related with name conflicts when dealing with imports. Then you can't disable only static imports.

@yanOnGithub
Copy link
Author

yanOnGithub commented Jan 30, 2017

Can I have this behavior?

// import doesnt work. should be fully-qualified in the code
import java.util.Date
import java.sql.Date
import com.company.framework1.Constants
import com.company.framework2.Constants

// ok, no fully-qualified class names
import java.util.ArrayList;
import org.apache.commons.logging.Log

@surli
Copy link
Collaborator

surli commented Jan 30, 2017

Not sure to understand. Without auto-import (--with-importsarg), Spoon will use fully-qualified name and use imports only if there is name conflict between package names and variables name, i.e:

import spoon.Launcher;

public class Toto {
   int spoon;
   Launcher launcher; // using spoon.Launcher here is wrong
}

On the contrary, in auto-import mode, Spoon try to import types and static types if there is no name conflicts:

import java.util.Date;

public class Toto {
  Date date1;
  java.sql.Date date2; // can't be imported
}

Do you spot an incorrect behaviour here? Could you create a unit test showing it?

@yanOnGithub
Copy link
Author

Thx for the explanation. I think you mean

"Then you can't disable only static imports"

while enabling auto imports.

What I want is auto imports w/o static import w/ fully qualified class names on conflict. Here is a related discussion on stackoverflow:

http://stackoverflow.com/a/2644534

One way is to produce all the fully qualified class names and press ctrl+shift+M to produce all the non-static imports, but it is kind of inefficient. Maybe I should stick with v52 --with-import at the moment.

@surli
Copy link
Collaborator

surli commented Jan 30, 2017

Reading again the produced output:

package com.company;
import com.company.framework.Constants;
import static com.company.framework.Constants.READY;
import static java.lang.System.out;
public class Main {
	public static void main(String[] args) throws Exception {
		out.println(READY);
	}
}

shows that we need indeed improve the pretty print of Spoon with static imports. The constant READY should not have been imported especially since Constants is already imported. I will have a look on that. I keep the issue open.

What I want is auto imports w/o static import w/ fully qualified class names on conflict.

Is it for readability reason or is there another reason behind?

@yanOnGithub
Copy link
Author

yanOnGithub commented Jan 30, 2017

Yes, it is for readability reason. The code base here was developed during the jdk1.4-1.6 era w/o static imports and should be kept that way. The transpiled code has to work on jdk1.6. There are a lot of constants like the following.

com.company.framework.Module1Constants.READY = 0x1; // need fqcn on conflict
com.company.framework.Module1Constants.RUNNING = 0x2; // need fqcn on conflict
com.company.framework.Module2Constants.READY = 0x3; // short name ok
com.company.framework.Module2Constants.RUNNING = 0x4; // short name ok
com.company.framework.dept3.Module1Constants.READY = 0x5; // need fqcn on conflict
com.company.framework.dept3.Module1Constants.RUNNING = 0x6; // need fqcn on conflict

When there are no conflicts, short names like Module1Constants.READY should be used, but never just READY. System.out.println should be used rather than just out.println.

I normally pick a handful of java files, put them in a temp directory, and run Spoon with jdk1.8 on the java files. I turn on -x|--noclasspath since Spoon cannot see the full code base. It would be great if Spoon could keep the original imports.

But #774 mentioned that

spoon computes the imports instead of using the import written by the developer.

@yanOnGithub
Copy link
Author

Meanwhile, eclipse Ctrl+shift+M requires a human to pick from a list of candidates. I came to Spoon for automation.

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

No branches or pull requests

2 participants