diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/PackageCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/PackageCodegen.java index f29e36f67ba..ffea59c1810 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/PackageCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/PackageCodegen.java @@ -20,9 +20,11 @@ import com.google.common.collect.Lists; import com.google.common.collect.Ordering; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProcessCanceledException; +import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ArrayUtil; import com.intellij.util.SmartList; +import com.intellij.util.containers.ContainerUtil; import kotlin.Function0; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -82,8 +84,7 @@ public class PackageCodegen { @Override public ClassBuilder invoke() { Collection files = PackageCodegen.this.files; - JetFile sourceFile = files.size() == 1 && previouslyCompiledCallables.isEmpty() - ? files.iterator().next() : null; + JetFile sourceFile = getRepresentativePackageFile(files); String className = AsmUtil.internalNameByFqNameWithoutInnerClasses(getPackageClassFqName(fqName)); ClassBuilder v = PackageCodegen.this.state.getFactory().newVisitor(Type.getObjectType(className), files); @@ -103,6 +104,28 @@ public class PackageCodegen { }); } + // Returns null if file has callables in several files + @Nullable + private JetFile getRepresentativePackageFile(@NotNull Collection packageFiles) { + if (!previouslyCompiledCallables.isEmpty()) { + return null; + } + + List packageFilesWithCallables = ContainerUtil.filter(packageFiles, new Condition() { + @Override + public boolean value(JetFile packageFile) { + for (JetDeclaration declaration : packageFile.getDeclarations()) { + if (declaration instanceof JetProperty || declaration instanceof JetNamedFunction) { + return true; + } + } + return false; + } + }); + + return packageFilesWithCallables.size() == 1 ? packageFilesWithCallables.get(0) : null; + } + @Nullable private static PackageFragmentDescriptor getCompiledPackageFragment(@NotNull PackageFragmentDescriptor packageFragment) { if (!IncrementalCompilation.ENABLED) {