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

feat(CtUnresolvedImport): keep all imports in noClassPath mode #2936

Merged
merged 23 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
10 changes: 6 additions & 4 deletions spoon-pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,14 @@
<configuration>
<header>LICENSE-short.txt</header>
<includes>
<include>src/main/java/**</include>
nharrand marked this conversation as resolved.
Show resolved Hide resolved
<include>main/java/**</include>
<!-- We also want to check the license in templates to generate files with the proper header -->
<include>src/test/java/spoon/generating/clone/*</include>
<!-- excluding the code coming from Javaparser -->
<exclude>src/main/java/spoon/javadoc/internal/*</exclude>
<include>test/java/spoon/generating/clone/*</include>
</includes>
<excludes>
<!-- excluding the code coming from Javaparser -->
<exclude>main/java/spoon/javadoc/internal/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/spoon/experimental/CtUnresolvedImport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
nharrand marked this conversation as resolved.
Show resolved Hide resolved
* Copyright (C) 2006-2018 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.experimental;

import spoon.SpoonException;
import spoon.reflect.declaration.CtImport;
import spoon.reflect.declaration.CtImportKind;
import spoon.reflect.reference.CtReference;
import spoon.reflect.visitor.CtImportVisitor;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.reflect.declaration.CtElementImpl;

public class CtUnresolvedImport extends CtElementImpl implements CtImport {

public CtUnresolvedImport() {
}

private boolean isStatic;

public void setStatic(boolean isStatic) {
this.isStatic = isStatic;
}

public boolean isStatic() {
return isStatic;
}

private String unresolvedReference;

public void setUnresolvedReference(String reference) {
this.unresolvedReference = reference;
}

public String getUnresolvedReference() {
return unresolvedReference;
}

@Override
public CtImportKind getImportKind() {
return CtImportKind.UNRESOLVED;
}

@Override
public CtReference getReference() {
return null;
}

@Override
public <T extends CtImport> T setReference(CtReference reference) {
throw new SpoonException("UnrseolvedImport reference cannot be set. Use CtImportImpl instead");
}

@Override
public void accept(CtImportVisitor visitor) {

}

@Override
public void accept(CtVisitor visitor) {
visitor.visitCtImport(this);
}

@Override
public CtUnresolvedImport clone() {
return (CtUnresolvedImport) super.clone();
}
}
3 changes: 2 additions & 1 deletion src/main/java/spoon/reflect/declaration/CtImportKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public enum CtImportKind {
ALL_TYPES,
ALL_STATIC_MEMBERS,
FIELD,
METHOD
METHOD,
UNRESOLVED
nharrand marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/CoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ BodyHolderSourcePosition createBodyHolderSourcePosition(
*/
CtImport createImport();

/**
* Creates an unresolved import.
*/
CtImport createUnresolvedImport();
nharrand marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates a package declaration.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,11 @@ public interface Factory {
*/
CtImport createImport(CtReference reference);

/**
* @see TypeFactory#createUnresolvedImport(String,boolean)
*/
CtImport createUnresolvedImport(String reference, boolean isStatic);

/**
* @see TypeFactory#createTypeMemberWildcardImportReference(CtTypeReference)
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/FactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,11 @@ public CtImport createImport(CtReference reference) {
return Type().createImport(reference);
}

@Override
public CtImport createUnresolvedImport(String reference, boolean isStatic) {
return Type().createUnresolvedImport(reference, isStatic);
}

@Override
public CtTypeMemberWildcardImportReference createTypeMemberWildcardImportReference(CtTypeReference typeReference) {
return Type().createTypeMemberWildcardImportReference(typeReference);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.experimental.CtUnresolvedImport;
import spoon.reflect.reference.CtActualTypeContainer;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.declaration.CtImport;
Expand Down Expand Up @@ -715,4 +716,11 @@ protected void enter(CtElement e) {
}.scan(importRef);
return ctImport.setReference(importRef);
}

public CtImport createUnresolvedImport(String reference, boolean isStatic) {
CtUnresolvedImport ctUnresolvedImport = (CtUnresolvedImport) factory.Core().createUnresolvedImport();
ctUnresolvedImport.setUnresolvedReference(reference);
ctUnresolvedImport.setStatic(isStatic);
return ctUnresolvedImport;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.experimental.CtUnresolvedImport;
import spoon.reflect.declaration.CtUsedService;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.declaration.ModifierKind;
Expand Down Expand Up @@ -1008,6 +1009,13 @@ public void visitCtImport(CtImport ctImport) {
printer.writeSeparator(".");
printer.writeIdentifier("*");
break;
case UNRESOLVED:
CtUnresolvedImport ctUnresolvedImport = (CtUnresolvedImport) ctImport;
if (ctUnresolvedImport.isStatic()) {
printer.writeKeyword("static");
printer.writeSpace();
}
printer.writeCodeSnippet(ctUnresolvedImport.getUnresolvedReference());
}
printer.writeSeparator(";");
printer.writeln();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**

* Copyright (C) 2006-2019 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/spoon/support/DefaultCoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtProvidedService;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.experimental.CtUnresolvedImport;
import spoon.reflect.declaration.CtUsedService;
import spoon.reflect.factory.CoreFactory;
import spoon.reflect.factory.Factory;
Expand Down Expand Up @@ -729,6 +730,13 @@ public CtImport createImport() {
return e;
}

@Override
public CtImport createUnresolvedImport() {
CtImport e = new CtUnresolvedImport();
e.setFactory(getMainFactory());
return e;
}

@Override
public CtPackageDeclaration createPackageDeclaration() {
CtPackageDeclaration e = new CtPackageDeclarationImpl();
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/spoon/support/compiler/jdt/JDTImportBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ void build() {

if (ctPackage != null) {
this.imports.add(createImportWithPosition(ctPackage.getReference(), importRef));
} else {
if (factory.getEnvironment().getNoClasspath()) {
this.imports.add(createUnresolvedImportWithPosition(importName, false, importRef));
}
}

} else {
CtType klass = this.getOrLoadClass(importName);
if (klass != null) {
this.imports.add(createImportWithPosition(klass.getReference(), importRef));
} else {
if (factory.getEnvironment().getNoClasspath()) {
this.imports.add(createUnresolvedImportWithPosition(importName, false, importRef));
}
}
}
} else {
Expand All @@ -91,6 +99,10 @@ void build() {
this.imports.add(createImportWithPosition(methodOrField.getReference(), importRef));
}
}
} else {
if (factory.getEnvironment().getNoClasspath()) {
this.imports.add(createUnresolvedImportWithPosition(importName, true, importRef));
}
}
}
}
Expand All @@ -109,6 +121,16 @@ private CtImport createImportWithPosition(CtReference ref, ImportReference impor
return imprt;
}

private CtImport createUnresolvedImportWithPosition(String ref, boolean isStatic, ImportReference importRef) {
char[] content = sourceUnit.getContents();
CtImport imprt = factory.Type().createUnresolvedImport(ref, isStatic);
//include comment before import
int declStart = importRef.declarationSourceStart;
int commentStart = PositionBuilder.findNextNonWhitespace(false, content, declStart, PositionBuilder.findPrevNonWhitespace(content, 0, declStart - 1) + 1);
imprt.setPosition(factory.Core().createCompoundSourcePosition(spoonUnit, importRef.sourceStart(), importRef.sourceEnd(), commentStart, importRef.declarationEnd, spoonUnit.getLineSeparatorPositions()));
return imprt;
}

private CtType getOrLoadClass(String className) {
CtType klass = this.factory.Type().get(className);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import spoon.Launcher;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.declaration.CtClass;
import spoon.experimental.CtUnresolvedImport;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.declaration.CtImport;
import spoon.reflect.reference.CtPackageReference;
Expand All @@ -33,6 +34,7 @@

import java.util.Collection;
import java.util.Iterator;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -94,7 +96,7 @@ public void testWithSimpleImportNoAutoimport() {

@Test
public void testInternalImportWhenNoClasspath() {
// contract: in no-classpath anything which is not loaded cannot be imported, even if original source code has imports
// contract: in no-classpath anything which is not loaded becomes CtUnresolvedImport, even if original source code has imports
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/resources/noclasspath/Attachment.java");
spoon.getEnvironment().setAutoImports(true);
Expand All @@ -103,7 +105,8 @@ public void testInternalImportWhenNoClasspath() {

CtClass classA = spoon.getFactory().Class().get("it.feio.android.omninotes.models.Attachment");
CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
assertTrue(unitA.getImports().isEmpty());
assertTrue(unitA.getImports().stream().filter(i -> !(i instanceof CtUnresolvedImport)).collect(Collectors.toList()).isEmpty());
assertEquals(1, unitA.getImports().size());
nharrand marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand Down