From 592969bad1bd16fb4e53b33958e788d2b78da74c Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 18 Feb 2014 18:06:03 +0400 Subject: [PATCH] Rewrite utils method for qualified names to Kotlin --- .../resolve/lazy/ResolveSessionUtils.java | 6 +- .../FileBasedDeclarationProviderFactory.java | 6 +- .../jet/util/QualifiedNamesUtil.java | 202 ------------------ .../jet/asJava/JavaElementFinder.java | 8 +- .../tests/org/jetbrains/jet/FqNameTest.java | 36 ++-- .../jet/lang/resolve/name/FqNameUnsafe.java | 4 +- .../jet/lang/resolve/name/FqNamesUtil.kt | 123 +++++++++++ .../caches/JetFromJavaDescriptorHelper.java | 6 +- .../IDELightClassGenerationSupport.java | 17 +- .../JetDeclarationRemotenessWeigher.java | 6 +- .../importOptimizer/JetImportOptimizer.java | 12 +- .../presentation/JetFunctionPresenter.java | 5 +- .../plugin/quickfix/ImportInsertHelper.java | 12 +- .../StubPackageMemberDeclarationProvider.java | 6 +- .../jet/plugin/util/JetPsiHeuristicsUtil.java | 7 +- j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt | 6 +- 16 files changed, 192 insertions(+), 270 deletions(-) delete mode 100644 compiler/frontend/src/org/jetbrains/jet/util/QualifiedNamesUtil.java create mode 100644 core/descriptors/src/org/jetbrains/jet/lang/resolve/name/FqNamesUtil.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/ResolveSessionUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/ResolveSessionUtils.java index fecb5bc12ed..9daae950242 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/ResolveSessionUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/ResolveSessionUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -26,8 +26,8 @@ import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor; import org.jetbrains.jet.lang.psi.JetNamed; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import org.jetbrains.jet.lang.resolve.scopes.JetScope; -import org.jetbrains.jet.util.QualifiedNamesUtil; import java.util.Arrays; import java.util.Collection; @@ -80,7 +80,7 @@ public class ResolveSessionUtils { while (true) { PackageViewDescriptor packageDescriptor = analyzer.getModuleDescriptor().getPackage(packageFqName); if (packageDescriptor != null) { - FqName classInPackagePath = new FqName(QualifiedNamesUtil.tail(packageFqName, fqName)); + FqName classInPackagePath = NamePackage.tail(fqName, packageFqName); Collection descriptors = getClassOrObjectDescriptorsByFqName(packageDescriptor, classInPackagePath, filter); classDescriptors.addAll(descriptors); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/declarations/FileBasedDeclarationProviderFactory.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/declarations/FileBasedDeclarationProviderFactory.java index 5d6c39243a3..5d553f204ab 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/declarations/FileBasedDeclarationProviderFactory.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/declarations/FileBasedDeclarationProviderFactory.java @@ -33,7 +33,7 @@ import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.storage.MemoizedFunctionToNullable; import org.jetbrains.jet.storage.NotNullLazyValue; import org.jetbrains.jet.storage.StorageManager; -import org.jetbrains.jet.util.QualifiedNamesUtil; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import java.util.Collection; import java.util.Collections; @@ -110,12 +110,12 @@ public class FileBasedDeclarationProviderFactory implements DeclarationProviderF Collection resultElements = Lists.newArrayList(); for (FqName declaredPackage : index.invoke().filesByPackage.keys()) { - if (QualifiedNamesUtil.isSubpackageOf(declaredPackage, fqName)) { + if (NamePackage.isSubpackageOf(declaredPackage, fqName)) { Collection files = index.invoke().filesByPackage.get(declaredPackage); resultElements.addAll(ContainerUtil.map(files, new Function() { @Override public NavigatablePsiElement fun(JetFile file) { - return JetPsiUtil.getPackageReference(file, QualifiedNamesUtil.numberOfSegments(fqName) - 1); + return JetPsiUtil.getPackageReference(file, NamePackage.numberOfSegments(fqName) - 1); } })); } diff --git a/compiler/frontend/src/org/jetbrains/jet/util/QualifiedNamesUtil.java b/compiler/frontend/src/org/jetbrains/jet/util/QualifiedNamesUtil.java deleted file mode 100644 index 5c169fde9f8..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/util/QualifiedNamesUtil.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * 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. - * 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.util; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.resolve.ImportPath; -import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.lang.resolve.name.Name; - -/** - * Common methods for working with qualified names. - */ -public final class QualifiedNamesUtil { - - private QualifiedNamesUtil() { - } - - public static boolean isSubpackageOf(@NotNull FqName subpackageName, @NotNull FqName packageName) { - if (subpackageName.equals(packageName)) { - return true; - } - - if (packageName.isRoot()) { - return true; - } - - String subpackageNameStr = subpackageName.asString(); - String packageNameStr = packageName.asString(); - - return isSubpackageOf(subpackageNameStr, packageNameStr); - } - - public static boolean isOneSegmentFQN(@NotNull String fqn) { - if (fqn.isEmpty()) { - return false; - } - - return fqn.indexOf('.') < 0; - } - - public static boolean isOneSegmentFQN(@NotNull FqName fqn) { - return isOneSegmentFQN(fqn.asString()); - } - - @NotNull - public static String getFirstSegment(@NotNull String fqn) { - int dotIndex = fqn.indexOf('.'); - return (dotIndex != -1) ? fqn.substring(0, dotIndex) : fqn; - } - - @NotNull - public static FqName withoutLastSegment(@NotNull FqName fqName) { - return fqName.parent(); - } - - @NotNull - public static FqName withoutFirstSegment(@NotNull FqName fqName) { - if (fqName.isRoot() || fqName.parent().isRoot()) { - return FqName.ROOT; - } - - String fqNameStr = fqName.asString(); - return new FqName(fqNameStr.substring(fqNameStr.indexOf('.'), fqNameStr.length())); - } - - public static int numberOfSegments(@NotNull FqName fqName) { - if (fqName.isRoot()) { - return 0; - } - - return 1 + numberOfSegments(fqName.parent()); - } - - @NotNull - public static FqName combine(@NotNull FqName first, @NotNull Name second) { - return first.child(second); - } - - /** - * Get tail part of the full fqn by subtracting head part. - * - * @param headFQN - * @param fullFQN - * @return tail fqn. If first part is not a begging of the full fqn, fullFQN will be returned. - */ - @NotNull - public static String tail(@NotNull FqName headFQN, @NotNull FqName fullFQN) { - if (!isSubpackageOf(fullFQN, headFQN) || headFQN.isRoot()) { - return fullFQN.asString(); - } - - return fullFQN.equals(headFQN) ? - "" : - fullFQN.asString().substring(headFQN.asString().length() + 1); // (headFQN + '.').length - } - - /** - * Add one segment of nesting to given qualified name according to the full qualified name. - * - * @param fqn - * @param fullFQN - * @return qualified name with one more segment or null if fqn is not head part of fullFQN or there's no additional segment. - */ - @Nullable - public static FqName plusOneSegment(@NotNull FqName fqn, @NotNull FqName fullFQN) { - if (!isSubpackageOf(fullFQN, fqn)) { - return null; - } - - String nextSegment = getFirstSegment(tail(fqn, fullFQN)); - - if (isOneSegmentFQN(nextSegment)) { - return combine(fqn, Name.guess(nextSegment)); - } - - return null; - } - - public static boolean isImported(@NotNull ImportPath alreadyImported, @NotNull FqName fqName) { - if (alreadyImported.hasAlias()) { - return false; - } - - if (alreadyImported.isAllUnder() && !fqName.isRoot()) { - return alreadyImported.fqnPart().equals(fqName.parent()); - } - - return alreadyImported.fqnPart().equals(fqName); - } - - public static boolean isImported(@NotNull ImportPath alreadyImported, @NotNull ImportPath newImport) { - if (newImport.isAllUnder() || newImport.hasAlias()) { - return alreadyImported.equals(newImport); - } - - return isImported(alreadyImported, newImport.fqnPart()); - } - - public static boolean isImported(@NotNull Iterable imports, @NotNull ImportPath newImport) { - for (ImportPath alreadyImported : imports) { - if (isImported(alreadyImported, newImport)) { - return true; - } - } - - return false; - } - - public static boolean isValidJavaFqName(@Nullable String qualifiedName) { - if (qualifiedName == null) return false; - - // Check that it is javaName(\.javaName)* or an empty string - - class State {} - State BEGINNING = new State(); - State MIDDLE = new State(); - State AFTER_DOT = new State(); - - State state = BEGINNING; - - int length = qualifiedName.length(); - for (int i = 0; i < length; i++) { - char c = qualifiedName.charAt(i); - if (state == BEGINNING || state == AFTER_DOT) { - if (!Character.isJavaIdentifierPart(c)) return false; - state = MIDDLE; - } - - //noinspection ConstantConditions - assert state == MIDDLE; - - if (c == '.') { - state = AFTER_DOT; - } - else if (!Character.isJavaIdentifierPart(c)) { - return false; - } - } - - return state != AFTER_DOT; - } - - public static boolean isSubpackageOf(String subpackageNameStr, String packageNameStr) { - return subpackageNameStr.equals(packageNameStr) || - (subpackageNameStr.startsWith(packageNameStr) && subpackageNameStr.charAt(packageNameStr.length()) == '.'); - } -} diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java index 0eba90d878d..58df4ccf360 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -41,7 +41,7 @@ import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.resolve.java.JavaPsiFacadeKotlinHacks; import org.jetbrains.jet.lang.resolve.java.PackageClassUtils; import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.util.QualifiedNamesUtil; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import java.util.Collection; import java.util.List; @@ -110,7 +110,7 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade } private PsiClass[] doFindClasses(String qualifiedNameString, GlobalSearchScope scope) { - if (!QualifiedNamesUtil.isValidJavaFqName(qualifiedNameString)) { + if (!NamePackage.isValidJavaFqName(qualifiedNameString)) { return PsiClass.EMPTY_ARRAY; } @@ -182,7 +182,7 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade @Override public PsiPackage findPackage(@NotNull String qualifiedNameString) { - if (!QualifiedNamesUtil.isValidJavaFqName(qualifiedNameString)) { + if (!NamePackage.isValidJavaFqName(qualifiedNameString)) { return null; } diff --git a/compiler/tests/org/jetbrains/jet/FqNameTest.java b/compiler/tests/org/jetbrains/jet/FqNameTest.java index bcd2b1d8414..cbdb0474839 100644 --- a/compiler/tests/org/jetbrains/jet/FqNameTest.java +++ b/compiler/tests/org/jetbrains/jet/FqNameTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -20,7 +20,7 @@ import com.google.common.collect.Lists; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe; import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.util.QualifiedNamesUtil; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import org.junit.Assert; import org.junit.Test; @@ -104,22 +104,22 @@ public class FqNameTest { @Test public void isValidJavaFqName() { - Assert.assertTrue(QualifiedNamesUtil.isValidJavaFqName("")); - Assert.assertTrue(QualifiedNamesUtil.isValidJavaFqName("a")); - Assert.assertTrue(QualifiedNamesUtil.isValidJavaFqName("1")); - Assert.assertTrue(QualifiedNamesUtil.isValidJavaFqName("a.a")); - Assert.assertTrue(QualifiedNamesUtil.isValidJavaFqName("org.jetbrains")); - Assert.assertTrue(QualifiedNamesUtil.isValidJavaFqName("$")); - Assert.assertTrue(QualifiedNamesUtil.isValidJavaFqName("org.A$B")); + Assert.assertTrue(NamePackage.isValidJavaFqName("")); + Assert.assertTrue(NamePackage.isValidJavaFqName("a")); + Assert.assertTrue(NamePackage.isValidJavaFqName("1")); + Assert.assertTrue(NamePackage.isValidJavaFqName("a.a")); + Assert.assertTrue(NamePackage.isValidJavaFqName("org.jetbrains")); + Assert.assertTrue(NamePackage.isValidJavaFqName("$")); + Assert.assertTrue(NamePackage.isValidJavaFqName("org.A$B")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName(".")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName("..")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName("a.")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName(".a")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName("a..b")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName("a.b..")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName("a.b.")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName("a.b...)")); - Assert.assertFalse(QualifiedNamesUtil.isValidJavaFqName("a.b.")); + Assert.assertFalse(NamePackage.isValidJavaFqName(".")); + Assert.assertFalse(NamePackage.isValidJavaFqName("..")); + Assert.assertFalse(NamePackage.isValidJavaFqName("a.")); + Assert.assertFalse(NamePackage.isValidJavaFqName(".a")); + Assert.assertFalse(NamePackage.isValidJavaFqName("a..b")); + Assert.assertFalse(NamePackage.isValidJavaFqName("a.b..")); + Assert.assertFalse(NamePackage.isValidJavaFqName("a.b.")); + Assert.assertFalse(NamePackage.isValidJavaFqName("a.b...)")); + Assert.assertFalse(NamePackage.isValidJavaFqName("a.b.")); } } diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/name/FqNameUnsafe.java b/core/descriptors/src/org/jetbrains/jet/lang/resolve/name/FqNameUnsafe.java index 2825825aa18..dca26fd0757 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/name/FqNameUnsafe.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/name/FqNameUnsafe.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -108,7 +108,7 @@ public final class FqNameUnsafe extends FqNameBase { } public boolean isRoot() { - return fqName.equals(""); + return fqName.isEmpty(); } @NotNull diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/name/FqNamesUtil.kt b/core/descriptors/src/org/jetbrains/jet/lang/resolve/name/FqNamesUtil.kt new file mode 100644 index 00000000000..ead93e84775 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/name/FqNamesUtil.kt @@ -0,0 +1,123 @@ +/* + * 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.lang.resolve.name + +import org.jetbrains.jet.lang.resolve.name.FqName +import org.jetbrains.jet.lang.resolve.ImportPath + +public fun FqName.isSubpackageOf(packageName: FqName): Boolean { + return when { + this == packageName -> true + packageName.isRoot() -> true + else -> isSubpackageOf(this.asString(), packageName.asString()) + } +} + +public fun FqName.isParent(child: FqName): Boolean = child.isSubpackageOf(this) + +public fun FqName.isOneSegmentFQN(): Boolean = !isRoot() && parent().isRoot() + +public fun FqName.withoutFirstSegment(): FqName { + if (isRoot() || parent().isRoot()) return FqName.ROOT + + val fqNameStr = asString() + return FqName(fqNameStr.substring(fqNameStr.indexOf('.'), fqNameStr.length())) +} + +public fun FqName.numberOfSegments(): Int { + return if (isRoot()) 0 else 1 + parent().numberOfSegments() +} + +/** + * Get tail part of the full fqn by subtracting head part. + * + * @param headFQN + * @return tail fqn. If first part is not a begging of the full fqn, fullFQN will be returned. + */ +public fun FqName.tail(headFQN: FqName): FqName { + return when { + !isSubpackageOf(headFQN) || headFQN.isRoot() -> this + this == headFQN -> FqName.ROOT + else -> FqName(asString().substring(headFQN.asString().length + 1)) + } +} + +/** + * Add one segment of nesting to given qualified name according to the full qualified name. + * + * @param fullFQN + * @return qualified name with one more segment or null if fqn is not head part of fullFQN or there's no additional segment. + */ +public fun FqName.plusOneSegment(fullFQN: FqName): FqName? { + if (!isParent(fullFQN) || fullFQN == this) return null + + return child(fullFQN.tail(this).pathSegments().first!!) +} + +public fun FqName.isImported(importPath: ImportPath): Boolean { + return when { + importPath.hasAlias() -> false + importPath.isAllUnder() && !isRoot() -> importPath.fqnPart() == this.parent() + else -> importPath.fqnPart() == this + } +} + +public fun ImportPath.isImported(alreadyImported: ImportPath): Boolean { + return if (isAllUnder() || hasAlias()) this == alreadyImported else fqnPart().isImported(alreadyImported) +} + +public fun ImportPath.isImported(imports: Iterable): Boolean = imports.any { isImported(it) } + +public fun isValidJavaFqName(qualifiedName: String?): Boolean { + if (qualifiedName == null) return false + + // Check that it is javaName(\.javaName)* or an empty string + enum class State { + BEGINNING + MIDDLE + AFTER_DOT + } + + var state = State.BEGINNING + + for (c in qualifiedName) { + when (state) { + State.BEGINNING, State.AFTER_DOT -> { + if (!Character.isJavaIdentifierPart(c)) return false + state = State.MIDDLE + } + State.MIDDLE -> { + if (c == '.') { + state = State.AFTER_DOT + } + else if (!Character.isJavaIdentifierPart(c)) return false + } + } + } + + return state != State.AFTER_DOT +} + +private fun isSubpackageOf(subpackageNameStr: String, packageNameStr: String): Boolean { + return subpackageNameStr == packageNameStr || + (subpackageNameStr.startsWith(packageNameStr) && subpackageNameStr[packageNameStr.length()] == '.') +} + +private fun getFirstSegment(fqn: String): String { + val dotIndex = fqn.indexOf('.') + return if ((dotIndex != -1)) fqn.substring(0, dotIndex) else fqn +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/caches/JetFromJavaDescriptorHelper.java b/idea/src/org/jetbrains/jet/plugin/caches/JetFromJavaDescriptorHelper.java index b0f949a2ae4..e3ed6f9cd80 100644 --- a/idea/src/org/jetbrains/jet/plugin/caches/JetFromJavaDescriptorHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/caches/JetFromJavaDescriptorHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -34,7 +34,6 @@ import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder; import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.util.QualifiedNamesUtil; import java.util.ArrayList; import java.util.Collection; @@ -145,8 +144,7 @@ public class JetFromJavaDescriptorHelper { FqName classFQN = new FqName(qualifiedName); if (JavaResolverPsiUtils.isCompiledKotlinPackageClass(containingClass)) { - FqName classParentFQN = QualifiedNamesUtil.withoutLastSegment(classFQN); - return QualifiedNamesUtil.combine(classParentFQN, Name.identifier(method.getName())); + return classFQN.parent().child(Name.identifier(method.getName())); } } diff --git a/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java b/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java index 3c47b2dc206..15e07a425db 100644 --- a/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java +++ b/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -32,7 +32,10 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.asJava.KotlinLightClassForExplicitDeclaration; import org.jetbrains.jet.asJava.LightClassConstructionContext; import org.jetbrains.jet.asJava.LightClassGenerationSupport; -import org.jetbrains.jet.lang.descriptors.*; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; +import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor; +import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.java.PackageClassUtils; @@ -40,13 +43,13 @@ import org.jetbrains.jet.lang.resolve.lazy.ForceResolveUtil; import org.jetbrains.jet.lang.resolve.lazy.KotlinCodeAnalyzer; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import org.jetbrains.jet.plugin.libraries.JetSourceNavigationHelper; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.plugin.project.ResolveSessionForBodies; import org.jetbrains.jet.plugin.stubindex.JetAllPackagesIndex; import org.jetbrains.jet.plugin.stubindex.JetClassByPackageIndex; import org.jetbrains.jet.plugin.stubindex.JetFullClassNameIndex; -import org.jetbrains.jet.util.QualifiedNamesUtil; import org.jetbrains.jet.utils.Profiler; import java.util.*; @@ -231,10 +234,10 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport for (JetFile file : files) { FqName fqName = JetPsiUtil.getFQName(file); - assert QualifiedNamesUtil.isSubpackageOf(fqName, fqn) : "Registered package is not a subpackage of actually declared package:\n" + - "in index: " + fqn + "\n" + - "declared: " + fqName; - FqName subpackage = QualifiedNamesUtil.plusOneSegment(fqn, fqName); + assert NamePackage.isSubpackageOf(fqName, fqn) : "Registered package is not a subpackage of actually declared package:\n" + + "in index: " + fqn + "\n" + + "declared: " + fqName; + FqName subpackage = NamePackage.plusOneSegment(fqn, fqName); if (subpackage != null) { result.add(subpackage); } diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java index 9ef921386d0..16d4236a58f 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -26,9 +26,9 @@ import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.ImportPath; import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import org.jetbrains.jet.plugin.completion.JetLookupObject; import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper; -import org.jetbrains.jet.util.QualifiedNamesUtil; public class JetDeclarationRemotenessWeigher extends LookupElementWeigher { private final JetFile file; @@ -65,7 +65,7 @@ public class JetDeclarationRemotenessWeigher extends LookupElementWeigher { if (descriptor != null) { FqNameUnsafe fqName = DescriptorUtils.getFqName(descriptor); // Invalid name can be met for class object descriptor: Test.MyTest.A..testOther - if (QualifiedNamesUtil.isValidJavaFqName(fqName.toString())) { + if (NamePackage.isValidJavaFqName(fqName.toString())) { ImportPath importPath = new ImportPath(fqName.toString()); if (ImportInsertHelper.needImport(importPath, file)) { return MyResult.notImported; diff --git a/idea/src/org/jetbrains/jet/plugin/editor/importOptimizer/JetImportOptimizer.java b/idea/src/org/jetbrains/jet/plugin/editor/importOptimizer/JetImportOptimizer.java index 30bbf4a5996..afda892adb1 100644 --- a/idea/src/org/jetbrains/jet/plugin/editor/importOptimizer/JetImportOptimizer.java +++ b/idea/src/org/jetbrains/jet/plugin/editor/importOptimizer/JetImportOptimizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -35,7 +35,7 @@ import org.jetbrains.jet.lang.resolve.java.JavaResolverPsiUtils; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; -import org.jetbrains.jet.util.QualifiedNamesUtil; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import java.util.*; @@ -96,7 +96,7 @@ public class JetImportOptimizer implements ImportOptimizer { } for (FqName usedName : usedNames) { - if (QualifiedNamesUtil.isImported(importPath, usedName)) { + if (NamePackage.isImported(usedName, importPath)) { return true; } } @@ -207,7 +207,7 @@ public class JetImportOptimizer implements ImportOptimizer { if (fqName == null) { fqName = new FqName(referencedName.asString()); } else { - fqName = QualifiedNamesUtil.combine(fqName, referencedName); + fqName = fqName.child(referencedName); } if (nameExpression.equals(element)) { return fqName; @@ -266,10 +266,10 @@ public class JetImportOptimizer implements ImportOptimizer { return null; } if (JavaResolverPsiUtils.isCompiledKotlinPackageClass(containingClass)) { - return QualifiedNamesUtil.combine(classFQN.parent(), Name.identifier(memberName)); + return classFQN.parent().child(Name.identifier(memberName)); } else { - return QualifiedNamesUtil.combine(classFQN, Name.identifier(memberName)); + return classFQN.child(Name.identifier(memberName)); } } diff --git a/idea/src/org/jetbrains/jet/plugin/presentation/JetFunctionPresenter.java b/idea/src/org/jetbrains/jet/plugin/presentation/JetFunctionPresenter.java index be2d65ee633..230d3d743b3 100644 --- a/idea/src/org/jetbrains/jet/plugin/presentation/JetFunctionPresenter.java +++ b/idea/src/org/jetbrains/jet/plugin/presentation/JetFunctionPresenter.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -26,7 +26,6 @@ import org.jetbrains.jet.lang.psi.JetParameter; import org.jetbrains.jet.lang.psi.JetPsiUtil; import org.jetbrains.jet.lang.psi.JetTypeReference; import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.util.QualifiedNamesUtil; import java.util.Collection; @@ -65,7 +64,7 @@ public class JetFunctionPresenter implements ItemPresentationProvider defaultImports = ProjectStructureUtil.isJsKotlinModule(contextFile) ? AnalyzerFacadeForJS.DEFAULT_IMPORTS : AnalyzerFacadeForJVM.DEFAULT_IMPORTS; - return QualifiedNamesUtil.isImported(defaultImports, importPath); + return NamePackage.isImported(importPath, defaultImports); } public static boolean needImport(@NotNull FqName fqName, @NotNull JetFile file) { @@ -152,7 +152,7 @@ public class ImportInsertHelper { public static boolean needImport(@NotNull ImportPath importPath, @NotNull JetFile file, List importDirectives) { if (importPath.fqnPart().firstSegmentIs(JavaDescriptorResolver.JAVA_ROOT)) { - FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart()); + FqName withoutJavaRoot = NamePackage.withoutFirstSegment(importPath.fqnPart()); importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder(), importPath.getAlias()); } @@ -164,7 +164,7 @@ public class ImportInsertHelper { // Check if import is already present for (JetImportDirective directive : importDirectives) { ImportPath existentImportPath = JetPsiUtil.getImportPath(directive); - if (existentImportPath != null && QualifiedNamesUtil.isImported(existentImportPath, importPath)) { + if (existentImportPath != null && NamePackage.isImported(importPath, existentImportPath)) { return false; } } diff --git a/idea/src/org/jetbrains/jet/plugin/stubindex/resolve/StubPackageMemberDeclarationProvider.java b/idea/src/org/jetbrains/jet/plugin/stubindex/resolve/StubPackageMemberDeclarationProvider.java index 79082d43212..60709084b69 100644 --- a/idea/src/org/jetbrains/jet/plugin/stubindex/resolve/StubPackageMemberDeclarationProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/stubindex/resolve/StubPackageMemberDeclarationProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -32,7 +32,7 @@ import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.plugin.stubindex.JetAllPackagesIndex; import org.jetbrains.jet.plugin.stubindex.JetTopLevelFunctionsFqnNameIndex; import org.jetbrains.jet.plugin.stubindex.JetTopLevelPropertiesFqnNameIndex; -import org.jetbrains.jet.util.QualifiedNamesUtil; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import java.util.Collection; import java.util.Collections; @@ -86,7 +86,7 @@ public class StubPackageMemberDeclarationProvider extends AbstractStubDeclaratio return ContainerUtil.map(files, new Function() { @Override public NavigatablePsiElement fun(JetFile file) { - return JetPsiUtil.getPackageReference(file, QualifiedNamesUtil.numberOfSegments(fqName) - 1); + return JetPsiUtil.getPackageReference(file, NamePackage.numberOfSegments(fqName) - 1); } }); } diff --git a/idea/src/org/jetbrains/jet/plugin/util/JetPsiHeuristicsUtil.java b/idea/src/org/jetbrains/jet/plugin/util/JetPsiHeuristicsUtil.java index 0a061ad4a8c..6884af5af87 100644 --- a/idea/src/org/jetbrains/jet/plugin/util/JetPsiHeuristicsUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/util/JetPsiHeuristicsUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -25,8 +25,8 @@ import org.jetbrains.jet.lang.psi.JetClassOrObject; import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetPsiUtil; import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.lang.resolve.name.NamePackage; import org.jetbrains.jet.lexer.JetTokens; -import org.jetbrains.jet.util.QualifiedNamesUtil; public class JetPsiHeuristicsUtil { private JetPsiHeuristicsUtil() {} @@ -48,7 +48,8 @@ public class JetPsiHeuristicsUtil { JetFile targetFile = (JetFile) classOrObject.getContainingFile(); FqName targetPackage = JetPsiUtil.getFQName(targetFile); FqName fromPackage = JetPsiUtil.getFQName(fromFile); - return QualifiedNamesUtil.isSubpackageOf(fromPackage, targetPackage); + + return NamePackage.isSubpackageOf(fromPackage, targetPackage); } } return member.hasModifierProperty(PsiModifier.PUBLIC) || member.hasModifierProperty(PsiModifier.PROTECTED); diff --git a/j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt b/j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt index a90ae6416a5..3275689a062 100644 --- a/j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt +++ b/j2k/src/org/jetbrains/jet/j2k/ast/Imports.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -20,8 +20,8 @@ import com.intellij.psi.PsiImportStatementBase import org.jetbrains.jet.j2k.* import com.intellij.psi.PsiImportList import org.jetbrains.jet.lang.resolve.name.FqName -import org.jetbrains.jet.util.QualifiedNamesUtil import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap +import org.jetbrains.jet.lang.resolve.name.isValidJavaFqName class Import(val name: String) : Element { @@ -33,7 +33,7 @@ class ImportList(val imports: List) : Element { !it.name.isEmpty() && it.name !in NOT_NULL_ANNOTATIONS }.filter { // If name is invalid, like with star imports, don't try to filter - if (!QualifiedNamesUtil.isValidJavaFqName(it.name)) + if (!isValidJavaFqName(it.name)) true else { // If imported class has a kotlin analog, drop the import