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

Feature request: Allow to flag in the model an import as static import #1267

Closed
schakko opened this issue Apr 20, 2017 · 4 comments
Closed
Labels

Comments

@schakko
Copy link

schakko commented Apr 20, 2017

I'd be happy if the API would provide something like

getFactory().Class().addStaticImport("fqdn.to.static.method");

Use case

I'd like to generate unit tests by using Hamcrester matcher. I don't want to use either the AST to accomplish this but also don't want to use the full FQDN. A real usage would be

addStatement(new CodeSnippert("assertThat(" + variableName + ", is(1))"));

where I would have previously statically imported assertThat by using

getFactory().Class().addStaticImport("org.hamcrest.MatcherAssert.assertThat");
@surli surli changed the title Feature request: Allow static imports Feature request: Allow to flag in the model an import as static import Apr 20, 2017
@surli surli added the feature label Apr 20, 2017
@surli
Copy link
Collaborator

surli commented Apr 24, 2017

Hi @schakko

the feature you propose is interesting, but actually it would be more explicit to put it in ImportScanner interface, and to make it available from the Environment class.

If you're ok I let you propose a PR to implement that kind of behaviour, and I'll try to help you on it.

@monperrus
Copy link
Collaborator

@surli does the new CtImport support this?

@surli
Copy link
Collaborator

surli commented Nov 27, 2017

@surli does the new CtImport support this?

It's partly supported: it's doable but I did not provide an easy API to do it, yet.
In the meantime, @schakko you still can try this kind of trick to have what you want:

// put whatever you need to build your model
Launcher launcher = new Launcher();
launcher.setInputResources(/your/path);
launcher.getEnvironment().setAutoImport(true);
launcher.buildModel();

// we get a reference to the method assertThat, that you want to import statically
CtType matcherType = launcher.getFactory().Type().get("org.hamcrest.MatcherAssert");
CtMethod assertMethod = matcherType.getMethodsByName("assertThat").get(0);

// we create the proper import
CtImport myStaticImport = launcher.getFactory().Type().createImport(assertMethod.getReference());

// we put the import in the compilation unit
CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate("/path/of/the/input/file/to/put/import");
Collection<CtImport> cuImports = new ArrayList<CtImport>();
cuImports.addAll(cu.getImports());
cuImports.add(myStaticImport);
cu.setImports(cuImports);

// print the sources
launcher.prettyprint();

Please note that you have to use Spoon 6.0.0-SNAPSHOT and it's for now still experimental.
I keep this issue open as I'll trial to improve the API to make this kind of job easier.

@nharrand
Copy link
Collaborator

Since #2936 , CtUnresolvedImport can be used for this problem. (And it is possible to mark them as static.)

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

Successfully merging a pull request may close this issue.

4 participants