diff --git a/.idea/artifacts/KotlinPlugin.xml b/.idea/artifacts/KotlinPlugin.xml index 83d30f7cbd7..53cac94caf7 100644 --- a/.idea/artifacts/KotlinPlugin.xml +++ b/.idea/artifacts/KotlinPlugin.xml @@ -17,6 +17,9 @@ + + + diff --git a/build.xml b/build.xml index 183e49cbbe1..53df90313ce 100644 --- a/build.xml +++ b/build.xml @@ -104,7 +104,7 @@ - + @@ -267,7 +267,7 @@ - + diff --git a/compiler/frontend/src/jet/Any.jet b/compiler/frontend/builtins/jet/Any.jet similarity index 100% rename from compiler/frontend/src/jet/Any.jet rename to compiler/frontend/builtins/jet/Any.jet diff --git a/compiler/frontend/src/jet/Arrays.jet b/compiler/frontend/builtins/jet/Arrays.jet similarity index 100% rename from compiler/frontend/src/jet/Arrays.jet rename to compiler/frontend/builtins/jet/Arrays.jet diff --git a/compiler/frontend/src/jet/Collections.jet b/compiler/frontend/builtins/jet/Collections.jet similarity index 100% rename from compiler/frontend/src/jet/Collections.jet rename to compiler/frontend/builtins/jet/Collections.jet diff --git a/compiler/frontend/src/jet/Enum.jet b/compiler/frontend/builtins/jet/Enum.jet similarity index 100% rename from compiler/frontend/src/jet/Enum.jet rename to compiler/frontend/builtins/jet/Enum.jet diff --git a/compiler/frontend/src/jet/ExtensionFunctions.jet b/compiler/frontend/builtins/jet/ExtensionFunctions.jet similarity index 100% rename from compiler/frontend/src/jet/ExtensionFunctions.jet rename to compiler/frontend/builtins/jet/ExtensionFunctions.jet diff --git a/compiler/frontend/src/jet/Functions.jet b/compiler/frontend/builtins/jet/Functions.jet similarity index 100% rename from compiler/frontend/src/jet/Functions.jet rename to compiler/frontend/builtins/jet/Functions.jet diff --git a/compiler/frontend/src/jet/Iterators.jet b/compiler/frontend/builtins/jet/Iterators.jet similarity index 100% rename from compiler/frontend/src/jet/Iterators.jet rename to compiler/frontend/builtins/jet/Iterators.jet diff --git a/compiler/frontend/src/jet/KExtensionFunctions.jet b/compiler/frontend/builtins/jet/KExtensionFunctions.jet similarity index 100% rename from compiler/frontend/src/jet/KExtensionFunctions.jet rename to compiler/frontend/builtins/jet/KExtensionFunctions.jet diff --git a/compiler/frontend/src/jet/KFunctions.jet b/compiler/frontend/builtins/jet/KFunctions.jet similarity index 100% rename from compiler/frontend/src/jet/KFunctions.jet rename to compiler/frontend/builtins/jet/KFunctions.jet diff --git a/compiler/frontend/src/jet/KMemberFunctions.jet b/compiler/frontend/builtins/jet/KMemberFunctions.jet similarity index 100% rename from compiler/frontend/src/jet/KMemberFunctions.jet rename to compiler/frontend/builtins/jet/KMemberFunctions.jet diff --git a/compiler/frontend/src/jet/Library.jet b/compiler/frontend/builtins/jet/Library.jet similarity index 100% rename from compiler/frontend/src/jet/Library.jet rename to compiler/frontend/builtins/jet/Library.jet diff --git a/compiler/frontend/src/jet/Nothing.jet b/compiler/frontend/builtins/jet/Nothing.jet similarity index 100% rename from compiler/frontend/src/jet/Nothing.jet rename to compiler/frontend/builtins/jet/Nothing.jet diff --git a/compiler/frontend/src/jet/Numbers.jet b/compiler/frontend/builtins/jet/Numbers.jet similarity index 100% rename from compiler/frontend/src/jet/Numbers.jet rename to compiler/frontend/builtins/jet/Numbers.jet diff --git a/compiler/frontend/src/jet/Progressions.jet b/compiler/frontend/builtins/jet/Progressions.jet similarity index 100% rename from compiler/frontend/src/jet/Progressions.jet rename to compiler/frontend/builtins/jet/Progressions.jet diff --git a/compiler/frontend/src/jet/Ranges.jet b/compiler/frontend/builtins/jet/Ranges.jet similarity index 100% rename from compiler/frontend/src/jet/Ranges.jet rename to compiler/frontend/builtins/jet/Ranges.jet diff --git a/compiler/frontend/src/jet/Unit.jet b/compiler/frontend/builtins/jet/Unit.jet similarity index 100% rename from compiler/frontend/src/jet/Unit.jet rename to compiler/frontend/builtins/jet/Unit.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/lang/KotlinBuiltIns.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/lang/KotlinBuiltIns.java index 601c8a69a18..bd99858b55a 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/lang/KotlinBuiltIns.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/lang/KotlinBuiltIns.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 JetBrains s.r.o. + * Copyright 2010-2013 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. @@ -20,14 +20,17 @@ import com.google.common.base.Predicates; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.StandardFileSystems; import com.intellij.psi.PsiFileFactory; import com.intellij.util.LocalTimeCounter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; import org.jetbrains.jet.lang.DefaultModuleConfiguration; import org.jetbrains.jet.lang.ModuleConfiguration; import org.jetbrains.jet.lang.PlatformToKotlinClassMap; @@ -48,10 +51,14 @@ import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.plugin.JetFileType; +import org.jetbrains.jet.utils.ExceptionUtils; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; import java.util.*; import static org.jetbrains.jet.lang.types.lang.PrimitiveType.*; @@ -247,16 +254,11 @@ public class KotlinBuiltIns { { List files = new LinkedList(); for(String path : libraryFiles) { - InputStream stream = KotlinBuiltIns.class.getClassLoader().getResourceAsStream(path); + String text = loadBuiltInFileText(path); - if (stream == null) { - throw new IllegalStateException("Resource not found in classpath: " + path); - } + JetFile file = (JetFile) PsiFileFactory.getInstance(project).createFileFromText( + path, JetFileType.INSTANCE, StringUtil.convertLineSeparators(text), LocalTimeCounter.currentTime(), true, false); - //noinspection IOResourceOpenedButNotSafelyClosed - String text = FileUtil.loadTextAndClose(new InputStreamReader(stream)); - JetFile file = (JetFile) PsiFileFactory.getInstance(project).createFileFromText(path, - JetFileType.INSTANCE, StringUtil.convertLineSeparators(text), LocalTimeCounter.currentTime(), true, false); files.add(file); } return files; @@ -277,7 +279,57 @@ public class KotlinBuiltIns { jetArrayTypeToPrimitiveJetType.put(arrayType, type); } + private static String loadBuiltInFileText(String path) throws IOException { + InputStream stream = KotlinBuiltIns.class.getClassLoader().getResourceAsStream(path); + if (stream == null) { + if (ApplicationManager.getApplication().isUnitTestMode()) { + //noinspection TestOnlyProblems + return FileUtil.loadFile(getInTestBuiltInPath(path)); + } + + throw new IllegalStateException("Resource not found in classpath: " + path); + } + + //noinspection IOResourceOpenedButNotSafelyClosed + return FileUtil.loadTextAndClose(new InputStreamReader(stream)); + } + + @TestOnly + private static File getInTestBuiltInPath(String path) { + return new File("compiler/frontend/builtins", path); + } + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @NotNull + public static URL getBuiltInsDirUrl() { + String builtInFilePath = "/" + LIBRARY_FILES.get(0); + String dirPath = "/" + BUILT_INS_DIR; + + URL url = KotlinBuiltIns.class.getResource(builtInFilePath); + URL dirUrl = KotlinBuiltIns.class.getResource(dirPath); + + if (url == null || dirUrl == null) { + if (ApplicationManager.getApplication().isUnitTestMode()) { + // HACK: Temp code. Get built-in files from the sources when running from test. + try { + @SuppressWarnings("TestOnlyProblems") File builtInDir = getInTestBuiltInPath(BUILT_INS_DIR); + return new URL(StandardFileSystems.FILE_PROTOCOL, "", FileUtil.toSystemIndependentName(builtInDir.getAbsolutePath())); + } + catch (MalformedURLException e) { + throw ExceptionUtils.rethrow(e); + } + } + + if (url == null) { + throw new IllegalStateException("Built-ins file wasn't found at url: " + builtInFilePath); + } + else { + throw new IllegalStateException("Built-ins directory wasn't found at url: " + dirPath); + } + } + + return dirUrl; + } @NotNull public ModuleDescriptorImpl getBuiltInsModule() { diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinJavaFileStubProvider.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinJavaFileStubProvider.java index 2a4a9da49ee..45f6c33386b 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinJavaFileStubProvider.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinJavaFileStubProvider.java @@ -48,6 +48,7 @@ import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetPsiUtil; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import java.util.Collection; import java.util.Collections; @@ -234,7 +235,7 @@ public class KotlinJavaFileStubProvider implements CachedValueProvider" : virtualFile.getPath(); LOG.error( "Could not generate LightClass for " + fqName + " declared in " + path + "\n" + - "built-ins dir URL is " + LightClassUtil.getBuiltInsDirResourceUrl() + "\n" + + "built-ins dir URL is " + KotlinBuiltIns.getBuiltInsDirUrl() + "\n" + "System: " + SystemInfo.OS_NAME + " " + SystemInfo.OS_VERSION + " Java Runtime: " + SystemInfo.JAVA_RUNTIME_VERSION, cause); } diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassUtil.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassUtil.java index 2d49b5af072..5b8b3afa28c 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassUtil.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/LightClassUtil.java @@ -43,56 +43,46 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.utils.KotlinVfsUtil; import java.net.MalformedURLException; -import java.net.URL; import java.util.*; public class LightClassUtil { private static final Logger LOG = Logger.getInstance(LightClassUtil.class); - private static final String DEFINITION_OF_ANY = "Any.jet"; /** * Checks whether the given file is loaded from the location where Kotlin's built-in classes are defined. - * As of today, this is compiler/frontend/src/jet directory and files such as Any.jet, Nothing.jet etc. + * As of today, this is compiler/frontend/builtins/jet directory and files such as Any.jet, Nothing.jet etc. * * Used to skip JetLightClass creation for built-ins, because built-in classes have no Java counterparts */ public static boolean belongsToKotlinBuiltIns(@NotNull JetFile file) { - try { - String jetVfsPathUrl = KotlinVfsUtil.convertFromUrl(getBuiltInsDirResourceUrl()); - VirtualFile virtualFile = file.getVirtualFile(); - if (virtualFile != null) { - VirtualFile parent = virtualFile.getParent(); - if (parent != null) { - String fileDirVfsUrl = parent.getUrl() + "/" + DEFINITION_OF_ANY; + VirtualFile virtualFile = file.getVirtualFile(); + if (virtualFile != null) { + VirtualFile parent = virtualFile.getParent(); + if (parent != null) { + try { + String jetVfsPathUrl = KotlinVfsUtil.convertFromUrl(KotlinBuiltIns.getBuiltInsDirUrl()); + + String fileDirVfsUrl = parent.getUrl(); if (jetVfsPathUrl.equals(fileDirVfsUrl)) { return true; } } + catch (MalformedURLException e) { + LOG.error(e); + } } } - catch (MalformedURLException e) { - LOG.error(e); - } + // We deliberately return false on error: who knows what weird URLs we might come across out there // it would be a pity if no light classes would be created in such cases return false; } - @NotNull - public static URL getBuiltInsDirResourceUrl() { - String pathToAny = "/" + KotlinBuiltIns.BUILT_INS_DIR + "/" + DEFINITION_OF_ANY; - URL url = KotlinBuiltIns.class.getResource(pathToAny); - if (url == null) { - throw new IllegalStateException("Built-ins not found in the classpath: " + pathToAny); - } - return url; - } - /*package*/ static void logErrorWithOSInfo(@Nullable Throwable cause, @NotNull FqName fqName, @Nullable VirtualFile virtualFile) { String path = virtualFile == null ? "" : virtualFile.getPath(); LOG.error( "Could not generate LightClass for " + fqName + " declared in " + path + "\n" + - "built-ins dir URL is " + getBuiltInsDirResourceUrl() + "\n" + + "built-ins dir URL is " + KotlinBuiltIns.getBuiltInsDirUrl() + "\n" + "System: " + SystemInfo.OS_NAME + " " + SystemInfo.OS_VERSION + " Java Runtime: " + SystemInfo.JAVA_RUNTIME_VERSION, cause); } diff --git a/generators/org/jetbrains/jet/generators/runtime/GenerateFunctions.java b/generators/org/jetbrains/jet/generators/runtime/GenerateFunctions.java index 13b1e2ccb12..f93b3fc06e8 100644 --- a/generators/org/jetbrains/jet/generators/runtime/GenerateFunctions.java +++ b/generators/org/jetbrains/jet/generators/runtime/GenerateFunctions.java @@ -27,7 +27,7 @@ import java.io.PrintWriter; public class GenerateFunctions { public static final int MAX_PARAM_COUNT = 22; - public static final File JET_SRC_DIR = new File("compiler/frontend/src/jet/"); + public static final File JET_SRC_DIR = new File("compiler/frontend/builtins/jet/"); public static final File RUNTIME_SRC_DIR = new File("runtime/src/jet/"); private final PrintWriter out; diff --git a/idea/src/org/jetbrains/jet/plugin/references/BuiltInsReferenceResolver.java b/idea/src/org/jetbrains/jet/plugin/references/BuiltInsReferenceResolver.java index 72dff8d5f13..2cad0a1b1ab 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/BuiltInsReferenceResolver.java +++ b/idea/src/org/jetbrains/jet/plugin/references/BuiltInsReferenceResolver.java @@ -16,8 +16,6 @@ package org.jetbrains.jet.plugin.references; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import com.google.common.collect.Sets; import com.intellij.openapi.components.AbstractProjectComponent; import com.intellij.openapi.project.DumbService; @@ -91,15 +89,15 @@ public class BuiltInsReferenceResolver extends AbstractProjectComponent { scope.changeLockLevel(WritableScope.LockLevel.BOTH); jetNamespace.setMemberScope(scope); - List jetBuiltInsFiles = getJetFiles("jet", Predicates.alwaysTrue()); + List jetBuiltInsFiles = getJetBuiltinsFiles(); TopDownAnalyzer.processStandardLibraryNamespace(myProject, context, scope, jetNamespace, jetBuiltInsFiles); builtInsSources = Sets.newHashSet(jetBuiltInsFiles); bindingContext = context.getBindingContext(); } - private List getJetFiles(String dir, final Predicate filter) { - URL url = BuiltInsReferenceResolver.class.getResource("/" + dir + "/"); + private List getJetBuiltinsFiles() { + URL url = KotlinBuiltIns.getBuiltInsDirUrl(); VirtualFile vf = VfsUtil.findFileByURL(url); assert vf != null : "Virtual file not found by URL: " + url; @@ -116,11 +114,7 @@ public class BuiltInsReferenceResolver extends AbstractProjectComponent { return ContainerUtil.mapNotNull(psiDirectory.getFiles(), new Function() { @Override public JetFile fun(PsiFile file) { - if (file instanceof JetFile) { - JetFile jetFile = (JetFile) file; - return filter.apply(jetFile) ? jetFile : null; - } - return null; + return file instanceof JetFile ? (JetFile) file : null; } }); }