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 34 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
24 changes: 17 additions & 7 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class DefaultJavaPrettyPrinter implements CtVisitor, PrettyPrinter {
public PrintingContext context = new PrintingContext();

/**
* Handle imports of classes.
* Handle classImports of classes.
*/
private ImportScanner importsContext;

Expand Down Expand Up @@ -225,15 +225,15 @@ protected void exitCtExpression(CtExpression<?> e) {
}

/**
* Make the imports for a given type.
* Make the classImports for a given type.
*/
public Collection<CtTypeReference<?>> computeImports(CtType<?> type) {
public Collection<CtReference> computeImports(CtType<?> type) {
context.currentTopLevel = type;
return importsContext.computeImports(context.currentTopLevel);
}

/**
* Make the imports for all elements.
* Make the classImports for all elements.
*/
public void computeImports(CtElement element) {
if (env.isAutoImports()) {
Expand Down Expand Up @@ -682,12 +682,22 @@ 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()) {
printer.write(".");
}
} else if (isStaticField && !isImportedField) {
CtTypeReference declaringType = f.getVariable().getDeclaringType();
if (declaringType != null) {
printer.write(declaringType.getSimpleName());
printer.write(".");
}
}
_context.ignoreStaticAccess(true);
}
Expand Down Expand Up @@ -1526,7 +1536,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 +1732,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
12 changes: 6 additions & 6 deletions src/main/java/spoon/reflect/visitor/ImportScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@

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

import java.util.Collection;

/**
* Used to compute the imports required to write readable code with no fully qualified names.
* Used to compute the classImports 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 Imports computes by Spoon, it can be CtTypeReference (for classes), but also CtFieldReference (static field) or CtExecutableReference (static methods)
*/
Collection<CtTypeReference<?>> computeImports(CtType<?> simpleType);
Collection<CtReference> computeImports(CtType<?> simpleType);

/**
* Computes imports for all elements.
* Computes classImports for all elements.
*/
@Deprecated
void computeImports(CtElement element);

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