Skip to content

review refactor: prepare import API for future development #2110

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

Merged
merged 1 commit into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 9 additions & 4 deletions src/main/java/spoon/reflect/cu/CompilationUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtImport;
import spoon.support.Experimental;

import java.io.File;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
* Defines a compilation unit. In Java, a compilation unit can contain only one
Expand Down Expand Up @@ -139,15 +140,19 @@ enum UNIT_TYPE {
int getTabCount(int index);

/**
* Get the imports computed for this CU
* Get the imports computed for this CU.
* WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.
* @return All the imports from the original source code
*/
Collection<CtImport> getImports();
@Experimental
Set<CtImport> getImports();

/**
* Set the imports of this CU
* WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.
* @param imports All the imports of the original source code
*/
void setImports(Collection<CtImport> imports);
@Experimental
void setImports(Set<CtImport> imports);

}
9 changes: 6 additions & 3 deletions src/main/java/spoon/reflect/visitor/ImportScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtImport;
import spoon.reflect.reference.CtReference;
import spoon.support.Experimental;

import java.util.Collection;
import java.util.Set;

/**
* Used to compute the imports required to write readable code with no fully qualified names.
* The import scanner API might still change in future release, that's why it is marked as experimental.
*/
@Experimental
public interface ImportScanner {

/**
Expand All @@ -37,7 +40,7 @@ public interface ImportScanner {
*
* @return the list of computed imports or an empty collection if not imports has been computed.
*/
Collection<CtImport> getAllImports();
Set<CtImport> getAllImports();

/**
* Checks if the type is already imported.
Expand All @@ -47,5 +50,5 @@ public interface ImportScanner {
/**
* Specify the original imports to use before computing new imports.
*/
void initWithImports(Collection<CtImport> importCollection);
void initWithImports(Set<CtImport> importCollection);
}
7 changes: 3 additions & 4 deletions src/main/java/spoon/reflect/visitor/ImportScannerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import spoon.support.SpoonClassNotFoundException;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -235,8 +234,8 @@ public void visitCtInvocation(CtInvocation invocation) {
}

@Override
public Collection<CtImport> getAllImports() {
Collection<CtImport> listallImports = new ArrayList<>();
public Set<CtImport> getAllImports() {
Set<CtImport> listallImports = new HashSet<>();

for (Map.Entry<CtImport, Boolean> entry : this.usedImport.entrySet()) {
if (entry.getValue()) {
Expand Down Expand Up @@ -287,7 +286,7 @@ public boolean isImported(CtReference ref) {
}

@Override
public void initWithImports(Collection<CtImport> importCollection) {
public void initWithImports(Set<CtImport> importCollection) {
for (CtImport ctImport : importCollection) {
this.usedImport.put(ctImport, Boolean.FALSE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import java.io.FileInputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static spoon.reflect.ModelElementContainerDefaultCapacities.COMPILATION_UNIT_DECLARED_TYPES_CONTAINER_DEFAULT_CAPACITY;

Expand All @@ -48,7 +48,7 @@ public class CompilationUnitImpl implements CompilationUnit, FactoryAccessor {

CtPackage ctPackage;

Collection<CtImport> imports = new HashSet<>();
Set<CtImport> imports = new HashSet<>();

CtModule ctModule;

Expand Down Expand Up @@ -245,12 +245,12 @@ public int getTabCount(int index) {
}

@Override
public Collection<CtImport> getImports() {
public Set<CtImport> getImports() {
return this.imports;
}

@Override
public void setImports(Collection<CtImport> imports) {
public void setImports(Set<CtImport> imports) {
this.imports = imports;
}

Expand Down