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

Fix bug with import java.lang.subpackage #1327

Merged
merged 3 commits into from
May 29, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ public void writeActualTypeArguments(CtActualTypeContainer ctGenericElementRefer
}
}

private boolean isJavaLangClasses(String importType) {
return importType.matches("^(java\\.lang\\.)[^.]*$");
}

/**
* Write the compilation unit header.
*/
Expand Down Expand Up @@ -283,7 +287,7 @@ public void writeHeader(List<CtType<?>> types, Collection<CtReference> imports)
importTypeStr = this.removeInnerTypeSeparator(fieldRef.getDeclaringType().getQualifiedName()) + "." + fieldRef.getSimpleName();
}

if (!importTypeStr.equals("") && !importTypeStr.startsWith("java.lang")) {
if (!importTypeStr.equals("") && !isJavaLangClasses(importTypeStr)) {
printer.write(importStr + " " + importTypeStr + ";").writeln().writeTabs();
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/spoon/test/imports/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -929,4 +929,16 @@ public void testJavaLangIsConsideredAsImported() {

canBeBuilt(outputDir, 7);
}

@Test
public void testJavaLangIsConsideredAsImportedButNotForSubPackages() {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
String outputDir = "./target/spooned-javalang";
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/Reflection.java");
launcher.setSourceOutputDirectory(outputDir);
launcher.run();

canBeBuilt(outputDir, 7);
}
}
10 changes: 10 additions & 0 deletions src/test/java/spoon/test/imports/testclasses/Reflection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package spoon.test.imports.testclasses;

import java.lang.reflect.Field;

/**
* Created by urli on 29/05/2017.
*/
public class Reflection {
Field field;
}