diff --git a/compiler/tests/org/jetbrains/jet/MockLibraryUtil.java b/compiler/tests/org/jetbrains/jet/MockLibraryUtil.java index 4eaca823538..14b3f438c28 100644 --- a/compiler/tests/org/jetbrains/jet/MockLibraryUtil.java +++ b/compiler/tests/org/jetbrains/jet/MockLibraryUtil.java @@ -18,7 +18,6 @@ package org.jetbrains.jet; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.util.containers.ContainerUtil; import com.intellij.util.io.ZipUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.cli.common.ExitCode; @@ -32,7 +31,9 @@ import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.regex.Pattern; import java.util.zip.ZipOutputStream; @@ -43,35 +44,43 @@ public class MockLibraryUtil { private static Class compilerClass = null; - public static File compileLibraryToJar(String sourcesPath, boolean addSources) { + @NotNull + public static File compileLibraryToJar( + @NotNull String sourcesPath, + @NotNull String jarName, + boolean addSources, + @NotNull String... extraClasspath + ) { try { - File contentDir = JetTestUtils.tmpDir("lib-content"); + File contentDir = JetTestUtils.tmpDir("testLibrary-" + jarName); File classesDir = new File(contentDir, "classes"); - compileKotlin(sourcesPath, classesDir); + compileKotlin(sourcesPath, classesDir, extraClasspath); List javaFiles = FileUtil.findFilesByMask(Pattern.compile(".*\\.java"), new File(sourcesPath)); if (!javaFiles.isEmpty()) { - List classPath = ContainerUtil.list(ForTestCompileRuntime.runtimeJarForTests().getPath(), - JetTestUtils.getAnnotationsJar().getPath()); + List classpath = new ArrayList(); + classpath.add(ForTestCompileRuntime.runtimeJarForTests().getPath()); + classpath.add(JetTestUtils.getAnnotationsJar().getPath()); + Collections.addAll(classpath, extraClasspath); // Probably no kotlin files were present, so dir might not have been created after kotlin compiler if (classesDir.exists()) { - classPath.add(classesDir.getPath()); + classpath.add(classesDir.getPath()); } else { FileUtil.createDirectory(classesDir); } List options = Arrays.asList( - "-classpath", StringUtil.join(classPath, File.pathSeparator), + "-classpath", StringUtil.join(classpath, File.pathSeparator), "-d", classesDir.getPath() ); JetTestUtils.compileJavaFiles(javaFiles, options); } - File jarFile = new File(contentDir, "library.jar"); + File jarFile = new File(contentDir, jarName + ".jar"); ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(jarFile)); ZipUtil.addDirToZipRecursively(zip, jarFile, classesDir, "", null, null); @@ -88,18 +97,17 @@ public class MockLibraryUtil { } // Runs compiler in custom class loader to avoid effects caused by replacing Application with another one created in compiler. - public static void compileKotlin(@NotNull String sourcesPath, @NotNull File outDir) { - compileKotlin(sourcesPath, outDir, sourcesPath); - } - - public static void compileKotlin(@NotNull String sourcesPath, @NotNull File outDir, @NotNull String classpath) { + public static void compileKotlin(@NotNull String sourcesPath, @NotNull File outDir, @NotNull String... extraClasspath) { try { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Class compilerClass = getCompilerClass(); Object compilerObject = compilerClass.newInstance(); Method execMethod = compilerClass.getMethod("exec", PrintStream.class, String[].class); - String newClasspath = classpath.contains(sourcesPath) ? classpath : classpath + File.pathSeparator + sourcesPath; + List classpath = new ArrayList(); + classpath.add(sourcesPath); + Collections.addAll(classpath, extraClasspath); + String newClasspath = StringUtil.join(classpath, File.pathSeparator); //noinspection IOResourceOpenedButNotSafelyClosed Enum invocationResult = (Enum) execMethod.invoke( diff --git a/compiler/tests/org/jetbrains/jet/codegen/AbstractTopLevelMembersInvocationTest.java b/compiler/tests/org/jetbrains/jet/codegen/AbstractTopLevelMembersInvocationTest.java index 4765d3f5c63..1fcd967995e 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/AbstractTopLevelMembersInvocationTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/AbstractTopLevelMembersInvocationTest.java @@ -56,7 +56,7 @@ public abstract class AbstractTopLevelMembersInvocationTest extends AbstractByte File library = new File(root, LIBRARY); List classPath = library.exists() ? - Collections.singletonList(MockLibraryUtil.compileLibraryToJar(library.getPath(), false)) : + Collections.singletonList(MockLibraryUtil.compileLibraryToJar(library.getPath(), LIBRARY, false)) : Collections.emptyList(); assert !sourceFiles.isEmpty() : getTestName(true) + " should contain at least one .kt file"; diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java index 59b74d4188e..3c4f13300b6 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java @@ -66,7 +66,7 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir { @NotNull private File compileLibrary(@NotNull String sourcePath) { - return MockLibraryUtil.compileLibraryToJar(new File(getTestDataDirectory(), sourcePath).getPath(), false); + return MockLibraryUtil.compileLibraryToJar(new File(getTestDataDirectory(), sourcePath).getPath(), "customKotlinLib", false); } private void doTestWithTxt(@NotNull File... extraClassPath) throws Exception { diff --git a/idea/testData/highlightingWithDependentLibraries/lib1/lib1.kt b/idea/testData/highlightingWithDependentLibraries/lib1/lib1.kt new file mode 100644 index 00000000000..b4f07997613 --- /dev/null +++ b/idea/testData/highlightingWithDependentLibraries/lib1/lib1.kt @@ -0,0 +1,9 @@ +package lib1 + +public open class Base { + public open fun baseFun() { + } +} + +public fun acceptBase(b: Base) { +} \ No newline at end of file diff --git a/idea/testData/highlightingWithDependentLibraries/lib2/lib2.kt b/idea/testData/highlightingWithDependentLibraries/lib2/lib2.kt new file mode 100644 index 00000000000..723a4481103 --- /dev/null +++ b/idea/testData/highlightingWithDependentLibraries/lib2/lib2.kt @@ -0,0 +1,17 @@ +package lib2 + +import lib1.* + +public class Derived(): Base() { + public fun derivedFun() { + } +} + +public fun acceptBase(b: Base) { +} + +public fun returnBase(): Base = Base() + +public fun Base.extendBase(): Unit { + +} \ No newline at end of file diff --git a/idea/testData/highlightingWithDependentLibraries/module/usingLibs.kt b/idea/testData/highlightingWithDependentLibraries/module/usingLibs.kt new file mode 100644 index 00000000000..a325bd7822a --- /dev/null +++ b/idea/testData/highlightingWithDependentLibraries/module/usingLibs.kt @@ -0,0 +1,16 @@ +package using.libs + +import lib2.extendBase + +//check that references to lib1 entities obtained through lib2 are valid +fun main() { + lib1.acceptBase(lib1.Base()) + lib2.acceptBase(lib1.Base()) + lib2.acceptBase(lib2.Derived()) + lib1.acceptBase(lib2.Derived()) + lib1.acceptBase(lib2.returnBase()) + lib1.Base().extendBase() + lib1.Base().baseFun() + lib2.Derived().baseFun() + lib2.Derived().derivedFun() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/JdkAndMockLibraryProjectDescriptor.java b/idea/tests/org/jetbrains/jet/plugin/JdkAndMockLibraryProjectDescriptor.java index 1db616f1f38..1da204523bf 100644 --- a/idea/tests/org/jetbrains/jet/plugin/JdkAndMockLibraryProjectDescriptor.java +++ b/idea/tests/org/jetbrains/jet/plugin/JdkAndMockLibraryProjectDescriptor.java @@ -37,7 +37,7 @@ public class JdkAndMockLibraryProjectDescriptor extends JetLightProjectDescripto @Override public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) { - File libraryJar = MockLibraryUtil.compileLibraryToJar(sourcesPath, withSources); + File libraryJar = MockLibraryUtil.compileLibraryToJar(sourcesPath, "myKotlinLib,", withSources); String jarUrl = "jar://" + FileUtilRt.toSystemIndependentName(libraryJar.getAbsolutePath()) + "!/"; Library.ModifiableModel libraryModel = model.getModuleLibraryTable().getModifiableModel().createLibrary("myKotlinLib").getModifiableModel(); diff --git a/idea/tests/org/jetbrains/jet/plugin/caches/resolve/HighlightingWithDependentLibrariesTest.kt b/idea/tests/org/jetbrains/jet/plugin/caches/resolve/HighlightingWithDependentLibrariesTest.kt new file mode 100644 index 00000000000..df4777a9e3b --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/caches/resolve/HighlightingWithDependentLibrariesTest.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.caches.resolve + +import org.jetbrains.jet.plugin.PluginTestCaseBase +import org.jetbrains.jet.MockLibraryUtil +import com.intellij.openapi.roots.libraries.Library +import com.intellij.openapi.command.WriteCommandAction +import java.io.File +import com.intellij.openapi.roots.OrderRootType +import com.intellij.openapi.vfs.VfsUtil +import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase +import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar +import org.jetbrains.jet.plugin.JetLightProjectDescriptor +import com.intellij.openapi.module.Module +import com.intellij.openapi.roots.ModifiableRootModel +import com.intellij.openapi.roots.ContentEntry + +public class HighlightingWithDependentLibrariesTest : JetLightCodeInsightFixtureTestCase() { + private val TEST_DATA_PATH = PluginTestCaseBase.getTestDataPathBase() + "/highlightingWithDependentLibraries" + + override fun getProjectDescriptor() = object : JetLightProjectDescriptor() { + override fun configureModule(module: Module, model: ModifiableRootModel, contentEntry: ContentEntry?) { + val compiledJar1 = MockLibraryUtil.compileLibraryToJar("$TEST_DATA_PATH/lib1", "lib1", false) + val compiledJar2 = MockLibraryUtil.compileLibraryToJar("$TEST_DATA_PATH/lib2", "lib2", false, compiledJar1.canonicalPath) + + model.addLibraryEntry(createLibrary(compiledJar1, "baseLibrary")) + model.addLibraryEntry(createLibrary(compiledJar2, "dependentLibrary")) + } + + private fun createLibrary(jarFile: File, name: String): Library { + val library = LibraryTablesRegistrar.getInstance()!!.getLibraryTable(getProject()).createLibrary(name)!! + val model = library.getModifiableModel() + model.addRoot(VfsUtil.getUrlForLibraryRoot(jarFile), OrderRootType.CLASSES) + model.commit() + return library + } + } + + public fun testHighlightingWithDependentLibraries() { + myFixture.configureByFile("$TEST_DATA_PATH/module/usingLibs.kt") + myFixture.checkHighlighting(false, false, false) + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/debugger/KotlinDebuggerTestCase.java b/idea/tests/org/jetbrains/jet/plugin/debugger/KotlinDebuggerTestCase.java index 83e706365e9..976560d49d1 100644 --- a/idea/tests/org/jetbrains/jet/plugin/debugger/KotlinDebuggerTestCase.java +++ b/idea/tests/org/jetbrains/jet/plugin/debugger/KotlinDebuggerTestCase.java @@ -124,7 +124,7 @@ public abstract class KotlinDebuggerTestCase extends DescriptorTestCase { if (!IS_TINY_APP_COMPILED) { String modulePath = getTestAppPath(); - CUSTOM_LIBRARY_JAR = MockLibraryUtil.compileLibraryToJar(CUSTOM_LIBRARY_SOURCES.getPath(), true); + CUSTOM_LIBRARY_JAR = MockLibraryUtil.compileLibraryToJar(CUSTOM_LIBRARY_SOURCES.getPath(), "debuggerCustomLibrary", true); String outputDir = modulePath + File.separator + "classes"; String sourcesDir = modulePath + File.separator + "src";