-
-
Notifications
You must be signed in to change notification settings - Fork 353
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
Comments
Hi @schakko the feature you propose is interesting, but actually it would be more explicit to put it in 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. |
@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. // 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. |
Since #2936 , |
I'd be happy if the API would provide something like
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
where I would have previously statically imported assertThat by using
The text was updated successfully, but these errors were encountered: