Modules.xml: add Java sources right after Kotlin sources

Java sources should appear before classpath dependencies because they should
have higher priority if the same symbol is located in the module source and in
the classpath. This is so because by default module source has the highest
priority in IDEA except the JDK. Ideally this will be handled later by full
support of modules in the compiler
This commit is contained in:
Alexander Udalov
2015-01-22 16:40:01 +03:00
parent eb3588bd0a
commit fa2116040e
8 changed files with 47 additions and 22 deletions
@@ -27,6 +27,7 @@ public interface KotlinModuleDescriptionBuilder {
String outputDir, String outputDir,
DependencyProvider dependencyProvider, DependencyProvider dependencyProvider,
List<File> sourceFiles, List<File> sourceFiles,
List<File> javaSourceRoots,
boolean tests, boolean tests,
Set<File> directoriesToFilterOut Set<File> directoriesToFilterOut
); );
@@ -56,6 +56,7 @@ public class KotlinModuleScriptBuilderFactory implements KotlinModuleDescription
String outputDir, String outputDir,
DependencyProvider dependencyProvider, DependencyProvider dependencyProvider,
List<File> sourceFiles, List<File> sourceFiles,
List<File> javaSourceRoots,
boolean tests, boolean tests,
final Set<File> directoriesToFilterOut final Set<File> directoriesToFilterOut
) { ) {
@@ -74,7 +75,7 @@ public class KotlinModuleScriptBuilderFactory implements KotlinModuleDescription
script.append(" sources += \"" + toSystemIndependentName(sourceFile.getPath()) + "\"\n"); script.append(" sources += \"" + toSystemIndependentName(sourceFile.getPath()) + "\"\n");
} }
dependencyProvider.processClassPath(new DependencyProcessor() { DependencyProcessor processor = new DependencyProcessor() {
@Override @Override
public void processClassPathSection(@NotNull String sectionDescription, @NotNull Collection<File> files) { public void processClassPathSection(@NotNull String sectionDescription, @NotNull Collection<File> files) {
script.append(" // " + sectionDescription + "\n"); script.append(" // " + sectionDescription + "\n");
@@ -97,7 +98,13 @@ public class KotlinModuleScriptBuilderFactory implements KotlinModuleDescription
script.append(" annotationsPath += \"").append(toSystemIndependentName(file.getPath())).append("\"\n"); script.append(" annotationsPath += \"").append(toSystemIndependentName(file.getPath())).append("\"\n");
} }
} }
}); };
if (!javaSourceRoots.isEmpty()) {
processor.processClassPathSection("Java source roots", javaSourceRoots);
}
dependencyProvider.processClassPath(processor);
script.append(" }\n"); script.append(" }\n");
return this; return this;
@@ -60,6 +60,7 @@ public class KotlinModuleXmlBuilderFactory implements KotlinModuleDescriptionBui
String outputDir, String outputDir,
DependencyProvider dependencyProvider, DependencyProvider dependencyProvider,
List<File> sourceFiles, List<File> sourceFiles,
List<File> javaSourceRoots,
boolean tests, boolean tests,
final Set<File> directoriesToFilterOut final Set<File> directoriesToFilterOut
) { ) {
@@ -82,7 +83,7 @@ public class KotlinModuleXmlBuilderFactory implements KotlinModuleDescriptionBui
p.println("<", SOURCES, " ", PATH, "=\"", getEscapedPath(sourceFile), "\"/>"); p.println("<", SOURCES, " ", PATH, "=\"", getEscapedPath(sourceFile), "\"/>");
} }
dependencyProvider.processClassPath(new DependencyProcessor() { DependencyProcessor processor = new DependencyProcessor() {
@Override @Override
public void processClassPathSection(@NotNull String sectionDescription, @NotNull Collection<File> files) { public void processClassPathSection(@NotNull String sectionDescription, @NotNull Collection<File> files) {
p.println("<!-- ", sectionDescription, " -->"); p.println("<!-- ", sectionDescription, " -->");
@@ -110,10 +111,16 @@ public class KotlinModuleXmlBuilderFactory implements KotlinModuleDescriptionBui
public void processAnnotationRoots(@NotNull List<File> files) { public void processAnnotationRoots(@NotNull List<File> files) {
p.println("<!-- External annotations -->"); p.println("<!-- External annotations -->");
for (File file : files) { for (File file : files) {
p.println("<", EXTERNAL_ANNOTATIONS, " ", PATH, "= \"", getEscapedPath(file), "\"/>"); p.println("<", EXTERNAL_ANNOTATIONS, " ", PATH, "=\"", getEscapedPath(file), "\"/>");
} }
} }
}); };
if (!javaSourceRoots.isEmpty()) {
processor.processClassPathSection("Java source roots", javaSourceRoots);
}
dependencyProvider.processClassPath(processor);
closeTag(p, MODULE); closeTag(p, MODULE);
return this; return this;
+4 -2
View File
@@ -3,9 +3,11 @@
<module name="name" outputDir="output"> <module name="name" outputDir="output">
<sources path="s1"/> <sources path="s1"/>
<sources path="s2"/> <sources path="s2"/>
<!-- Java source roots -->
<classpath path="java"/>
<!-- External annotations --> <!-- External annotations -->
<externalAnnotations path= "a1/f1"/> <externalAnnotations path="a1/f1"/>
<externalAnnotations path= "a2"/> <externalAnnotations path="a2"/>
<!-- s1 --> <!-- s1 -->
<classpath path="cp1"/> <classpath path="cp1"/>
<classpath path="cp2"/> <classpath path="cp2"/>
+2 -2
View File
@@ -4,8 +4,8 @@
<sources path="s1"/> <sources path="s1"/>
<sources path="s2"/> <sources path="s2"/>
<!-- External annotations --> <!-- External annotations -->
<externalAnnotations path= "a1/f1"/> <externalAnnotations path="a1/f1"/>
<externalAnnotations path= "a2"/> <externalAnnotations path="a2"/>
<!-- s1 --> <!-- s1 -->
<!-- Output directory, commented out --> <!-- Output directory, commented out -->
<!-- <!--
+4 -4
View File
@@ -4,8 +4,8 @@
<sources path="s1"/> <sources path="s1"/>
<sources path="s2"/> <sources path="s2"/>
<!-- External annotations --> <!-- External annotations -->
<externalAnnotations path= "a1/f1"/> <externalAnnotations path="a1/f1"/>
<externalAnnotations path= "a2"/> <externalAnnotations path="a2"/>
<!-- s1 --> <!-- s1 -->
<!-- Output directory, commented out --> <!-- Output directory, commented out -->
<!-- <!--
@@ -18,8 +18,8 @@
<sources path="s12"/> <sources path="s12"/>
<sources path="s22"/> <sources path="s22"/>
<!-- External annotations --> <!-- External annotations -->
<externalAnnotations path= "a12/f12"/> <externalAnnotations path="a12/f12"/>
<externalAnnotations path= "a22"/> <externalAnnotations path="a22"/>
<!-- s12 --> <!-- s12 -->
<!-- Output directory, commented out --> <!-- Output directory, commented out -->
<!-- <!--
@@ -37,8 +37,10 @@ public class KotlinModuleXmlGeneratorTest extends TestCase {
} }
}, },
Arrays.asList(new File("s1"), new File("s2")), Arrays.asList(new File("s1"), new File("s2")),
Collections.singletonList(new File("java")),
false, false,
Collections.<File>emptySet()).asText().toString(); Collections.<File>emptySet()
).asText().toString();
JetTestUtils.assertEqualsToFile(new File("idea/testData/modules.xml/basic.xml"), actual); JetTestUtils.assertEqualsToFile(new File("idea/testData/modules.xml/basic.xml"), actual);
} }
@@ -54,8 +56,10 @@ public class KotlinModuleXmlGeneratorTest extends TestCase {
} }
}, },
Arrays.asList(new File("s1"), new File("s2")), Arrays.asList(new File("s1"), new File("s2")),
Collections.<File>emptyList(),
false, false,
Collections.singleton(new File("cp1"))).asText().toString(); Collections.singleton(new File("cp1"))
).asText().toString();
JetTestUtils.assertEqualsToFile(new File("idea/testData/modules.xml/filtered.xml"), actual); JetTestUtils.assertEqualsToFile(new File("idea/testData/modules.xml/filtered.xml"), actual);
} }
@@ -72,8 +76,10 @@ public class KotlinModuleXmlGeneratorTest extends TestCase {
} }
}, },
Arrays.asList(new File("s1"), new File("s2")), Arrays.asList(new File("s1"), new File("s2")),
Collections.<File>emptyList(),
false, false,
Collections.singleton(new File("cp1"))); Collections.singleton(new File("cp1"))
);
builder.addModule( builder.addModule(
"name2", "name2",
"output2", "output2",
@@ -85,8 +91,10 @@ public class KotlinModuleXmlGeneratorTest extends TestCase {
} }
}, },
Arrays.asList(new File("s12"), new File("s22")), Arrays.asList(new File("s12"), new File("s22")),
Collections.<File>emptyList(),
true, true,
Collections.singleton(new File("cp12"))); Collections.singleton(new File("cp12"))
);
String actual = builder.asText().toString(); String actual = builder.asText().toString();
JetTestUtils.assertEqualsToFile(new File("idea/testData/modules.xml/multiple.xml"), actual); JetTestUtils.assertEqualsToFile(new File("idea/testData/modules.xml/multiple.xml"), actual);
} }
@@ -89,8 +89,9 @@ public class KotlinBuilderModuleScriptGenerator {
builder.addModule( builder.addModule(
target.getId(), target.getId(),
outputDir.getAbsolutePath(), outputDir.getAbsolutePath(),
getKotlinModuleDependencies(context, target), getKotlinModuleDependencies(target),
moduleSources, moduleSources,
findSourceRoots(context, target),
target.isTests(), target.isTests(),
// this excludes the output directories from the class path, to be removed for true incremental compilation // this excludes the output directories from the class path, to be removed for true incremental compilation
outputDirs outputDirs
@@ -115,12 +116,11 @@ public class KotlinBuilderModuleScriptGenerator {
return outputDir; return outputDir;
} }
private static DependencyProvider getKotlinModuleDependencies(final CompileContext context, final ModuleBuildTarget target) { private static DependencyProvider getKotlinModuleDependencies(final ModuleBuildTarget target) {
return new DependencyProvider() { return new DependencyProvider() {
@Override @Override
public void processClassPath(@NotNull DependencyProcessor processor) { public void processClassPath(@NotNull DependencyProcessor processor) {
processor.processClassPathSection("Classpath", findClassPathRoots(target)); processor.processClassPathSection("Classpath", findClassPathRoots(target));
processor.processClassPathSection("Java Source Roots", findSourceRoots(context, target));
processor.processAnnotationRoots(findAnnotationRoots(target)); processor.processAnnotationRoots(findAnnotationRoots(target));
} }
}; };
@@ -132,9 +132,9 @@ public class KotlinBuilderModuleScriptGenerator {
} }
@NotNull @NotNull
private static Collection<File> findSourceRoots(@NotNull CompileContext context, @NotNull ModuleBuildTarget target) { private static List<File> findSourceRoots(@NotNull CompileContext context, @NotNull ModuleBuildTarget target) {
List<JavaSourceRootDescriptor> roots = context.getProjectDescriptor().getBuildRootIndex().getTargetRoots(target, context); List<JavaSourceRootDescriptor> roots = context.getProjectDescriptor().getBuildRootIndex().getTargetRoots(target, context);
Collection<File> result = ContainerUtil.newArrayList(); List<File> result = ContainerUtil.newArrayList();
for (JavaSourceRootDescriptor root : roots) { for (JavaSourceRootDescriptor root : roots) {
File file = root.getRootFile(); File file = root.getRootFile();
if (file.exists()) { if (file.exists()) {