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

New feature: allow spoon to do static imports for methods and fields #1040

Merged
merged 38 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9694ff9
bug: no fully qualified name if top package is shadowed by a local va…
monperrus Nov 16, 2016
2609284
Merge branch 'bug-static-import' of https://github.com/monperrus/spoo…
Dec 1, 2016
30363db
Proposal to fix #972: apparently a condition was improperly checked w…
surli Dec 5, 2016
c05c638
WiP #980: create two tests to check case when the name is shadowed by…
surli Dec 5, 2016
5bf26f5
WiP #980: first naive fix when the shadowed variable is a field, by u…
surli Dec 5, 2016
a0a1329
WiP #980 New tests and scanner for variables
surli Dec 6, 2016
0b5371d
Revert "Merge branch 'bug-static-import' of https://github.com/monper…
surli Dec 6, 2016
c9fe205
Fix #972 and subsequent tests. Add a condition to ignore unsettablePr…
surli Dec 6, 2016
7f504d5
Merge remote-tracking branch 'inria/master' into pr#972
surli Dec 6, 2016
a30611b
Fix unused import
surli Dec 6, 2016
2c7b4d3
Proposal to fix #980: a different scanner is used when autoimports is…
surli Dec 6, 2016
5614297
Two more tests added to check if #980 is really fixed. Works fine.
surli Dec 6, 2016
013d5d9
Merge branch 'pr#972' into issue980
surli Dec 6, 2016
e7c9b0d
Static import are not supported yet. Test case to show it.
surli Dec 6, 2016
3155d12
Merge branch 'master' of https://github.com/INRIA/spoon
surli Dec 7, 2016
3b0a416
Fix locally checkstyle for ParentExiter
surli Dec 7, 2016
e81c26b
Merge branch 'master' of https://github.com/INRIA/spoon
surli Dec 8, 2016
cae9260
Merge branch 'master' of https://github.com/INRIA/spoon
surli Dec 9, 2016
e0b23ab
Merge branch 'master' into bugStaticImport
surli Dec 9, 2016
0ee080f
Create new tests to show issue #1027
surli Dec 9, 2016
4ae8cf7
Was renamed MinimalImportScanner in previous commit but not deleted i…
surli Dec 9, 2016
6561ee7
Begin of the refactoring to import static fields and methods in reada…
surli Dec 9, 2016
cc962a0
Fix compilation issues with the change of signature of computeImports
surli Dec 9, 2016
2d57b50
Fix lot of things to manage correctly readable mode with statics impo…
surli Dec 9, 2016
73d0ee9
Fix some tests and avoid redundancy in imports. #1027
surli Dec 9, 2016
bdeef55
WiP #1027 Fix some errors by doing more tests when adding imports
surli Dec 10, 2016
99855a0
WiP #1027 Fix ambiguity between getXXImports and addXXImports in Impo…
surli Dec 12, 2016
350920a
WiP #1027 Fix some tests regarding class import
surli Dec 12, 2016
512e32c
WiP #1027: still working to pass all tests for this feature
surli Dec 12, 2016
7800b56
WiP #1027 Change the behaviour to write static fields, now it uses th…
surli Dec 12, 2016
715b62e
WiP #1027 One more test to manage java.lang when printing FQN
surli Dec 12, 2016
6c5a5b2
Fix last tests for #1027. Fix checkstyle issues as well.
Dec 13, 2016
f37484f
Merge branch 'master' of https://github.com/INRIA/spoon
Dec 13, 2016
3f5f475
Merge branch 'master' into bugStaticImport
Dec 13, 2016
bb70bb5
Clear all lists for #1027
surli Dec 14, 2016
3b1bf4c
Feature #1027: delete mentions of classImports in comments. Change th…
surli Dec 14, 2016
cc0ec5b
Also have to clear all list in computeImports #1027
surli Dec 14, 2016
859928d
Revert some changes to force the add of classtype before calling a st…
surli Dec 15, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ protected void exitCtExpression(CtExpression<?> e) {
/**
* Make the imports for a given type.
*/
public Collection<CtTypeReference<?>> computeImports(CtType<?> type) {
public Collection<CtReference> computeImports(CtType<?> type) {
context.currentTopLevel = type;
return importsContext.computeImports(context.currentTopLevel);
return importsContext.computeAllImports(context.currentTopLevel);
}

/**
Expand Down Expand Up @@ -682,7 +682,11 @@ private <T> void printCtFieldAccess(CtFieldAccess<T> f) {
_context.ignoreGenerics(true);
}
if (f.getTarget() != null) {
if (!isInitializeStaticFinalField(f.getTarget())) {
boolean isInitializeStaticFinalField = isInitializeStaticFinalField(f.getTarget());
boolean isStaticField = f.getVariable().isStatic();
boolean isImportedField = importsContext.isImported(f.getVariable());

if (!isInitializeStaticFinalField && !(isStaticField && isImportedField)) {
printer.snapshotLength();
scan(f.getTarget());
if (printer.hasNewContent()) {
Expand Down Expand Up @@ -1526,7 +1530,7 @@ public void visitCtWildcardReference(CtWildcardReference wildcardReference) {
}

private boolean printQualified(CtTypeReference<?> ref) {
if (importsContext.isImported(ref)) {
if (importsContext.isImported(ref) || (this.env.isAutoImports() && ref.getPackage() != null && ref.getPackage().getSimpleName().equals("java.lang"))) {
// If my.pkg.Something is imported, but
//A) we are in the context of a class which is also called "Something",
//B) we are in the context of a class which defines field which is also called "Something",
Expand Down Expand Up @@ -1722,7 +1726,7 @@ public void reset() {
@Override
public void calculate(CompilationUnit sourceCompilationUnit, List<CtType<?>> types) {
this.sourceCompilationUnit = sourceCompilationUnit;
Set<CtTypeReference<?>> imports = new HashSet<>();
Set<CtReference> imports = new HashSet<>();
for (CtType<?> t : types) {
imports.addAll(computeImports(t));
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/spoon/reflect/visitor/ImportScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeReference;

import java.util.Collection;
Expand All @@ -26,22 +27,31 @@
* Used to compute the imports required to write readable code with no fully qualified names.
*/
public interface ImportScanner {

/**
* Computes import of a {@link spoon.reflect.declaration.CtType}
* (represent a class).
*
* @return Imports computes by Spoon, not original imports.
* @return class imports computed by Spoon, it does not contain static imports
*/
Collection<CtTypeReference<?>> computeImports(CtType<?> simpleType);

/**
* Computes imports for all elements.
* Computes import of a {@link spoon.reflect.declaration.CtType}
* (represent a class).
*
* @return imports computed by Spoon, it can be CtTypeReference (for classes), but also CtFieldReference (static field) or CtExecutableReference (static methods)
*/
Collection<CtReference> computeAllImports(CtType<?> simpleType);

/**
* Computes imports for an element.
*/
@Deprecated
void computeImports(CtElement element);

/**
* Checks if the type is already imported.
*/
boolean isImported(CtTypeReference<?> ref);
boolean isImported(CtReference ref);
}
Loading