From 1ce24b10cc3f5d5a72080c3e75568518f1eb3a99 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Sun, 17 Jun 2012 00:27:19 +0400 Subject: [PATCH] FqName*.equalsTo, type safe unlike equals --- .../resolve/java/JavaDescriptorResolver.java | 2 +- .../jet/lang/DefaultModuleConfiguration.java | 2 +- .../resolve/QualifiedExpressionResolver.java | 2 +- .../jet/lang/resolve/name/FqName.java | 2 +- .../jet/lang/resolve/name/FqNameBase.java | 59 +++++++++++++++++++ .../jet/lang/resolve/name/FqNameUnsafe.java | 2 +- .../jet/resolve/DescriptorRenderer.java | 2 +- .../plugin/highlighter/IdeErrorMessages.java | 2 +- 8 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqNameBase.java diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java index 362faef8010..bf2698fbd48 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java @@ -603,7 +603,7 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes private boolean isJavaLangObject(JetType type) { ClassifierDescriptor classifierDescriptor = type.getConstructor().getDeclarationDescriptor(); return classifierDescriptor instanceof ClassDescriptor && - DescriptorUtils.getFQName(classifierDescriptor).equals(JL_OBJECT.toUnsafe()); + DescriptorUtils.getFQName(classifierDescriptor).equalsTo(JL_OBJECT); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/DefaultModuleConfiguration.java b/compiler/frontend/src/org/jetbrains/jet/lang/DefaultModuleConfiguration.java index 1316afabb60..330d2674b66 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/DefaultModuleConfiguration.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/DefaultModuleConfiguration.java @@ -59,7 +59,7 @@ public class DefaultModuleConfiguration implements ModuleConfiguration { @Override public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) { - if (DescriptorUtils.getFQName(namespaceDescriptor).equals(JetStandardClasses.STANDARD_CLASSES_FQNAME.toUnsafe())) { + if (DescriptorUtils.getFQName(namespaceDescriptor).equalsTo(JetStandardClasses.STANDARD_CLASSES_FQNAME)) { if (!extendBuiltins) { namespaceMemberScope.importScope(JetStandardClasses.STANDARD_CLASSES); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/QualifiedExpressionResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/QualifiedExpressionResolver.java index db042c7c414..2993f86870c 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/QualifiedExpressionResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/QualifiedExpressionResolver.java @@ -392,7 +392,7 @@ public class QualifiedExpressionResolver { } if (namespaceDescriptor != null && classDescriptor != null) { - if (DescriptorUtils.getFQName(namespaceDescriptor).equals(DescriptorUtils.getFQName(classDescriptor))) { + if (DescriptorUtils.getFQName(namespaceDescriptor).equalsTo(DescriptorUtils.getFQName(classDescriptor))) { trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, classDescriptor); trace.record(BindingContext.RESOLUTION_SCOPE, referenceExpression, resolutionScope); checkVisibility(classDescriptor, trace, referenceExpression, scopeToCheckVisibility); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqName.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqName.java index 3554f894de8..295170ed043 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqName.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqName.java @@ -25,7 +25,7 @@ import java.util.List; /** * @author Stepan Koltsov */ -public class FqName { +public class FqName extends FqNameBase { public static final FqName ROOT = new FqName(""); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqNameBase.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqNameBase.java new file mode 100644 index 00000000000..8168f5b04db --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqNameBase.java @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2012 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.annotations.NotNull; + +/** + * @author Stepan Koltsov + */ +public abstract class FqNameBase { + + protected FqNameBase() { + if (!(this instanceof FqName || this instanceof FqNameUnsafe)) { + throw new AssertionError("do not use this class directly"); + } + } + + @NotNull + protected abstract String getFqName(); + + @NotNull + private FqNameUnsafe toFqNameUnsafe() { + if (this instanceof FqName) { + return ((FqName) this).toUnsafe(); + } + else if (this instanceof FqNameUnsafe) { + return ((FqNameUnsafe) this); + } + else { + throw new AssertionError(); + } + } + + public final boolean equalsTo(@NotNull FqName that) { + return equalsTo(that.toUnsafe()); + } + + public final boolean equalsTo(@NotNull FqNameUnsafe that) { + return toFqNameUnsafe().equals(that); + } + + public final boolean equalsTo(@NotNull String that) { + return getFqName().equals(that); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqNameUnsafe.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqNameUnsafe.java index dd6107d7d2d..c14128d31b5 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqNameUnsafe.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/name/FqNameUnsafe.java @@ -27,7 +27,7 @@ import java.util.List; * * @author Stepan Koltsov */ -public class FqNameUnsafe { +public class FqNameUnsafe extends FqNameBase { public static final Name ROOT_NAME = Name.special(""); diff --git a/compiler/frontend/src/org/jetbrains/jet/resolve/DescriptorRenderer.java b/compiler/frontend/src/org/jetbrains/jet/resolve/DescriptorRenderer.java index dea858ba5bf..9e11fb9f07e 100644 --- a/compiler/frontend/src/org/jetbrains/jet/resolve/DescriptorRenderer.java +++ b/compiler/frontend/src/org/jetbrains/jet/resolve/DescriptorRenderer.java @@ -274,7 +274,7 @@ public class DescriptorRenderer implements Renderer { final DeclarationDescriptor containingDeclaration = declarationDescriptor.getContainingDeclaration(); if (containingDeclaration != null) { FqNameUnsafe fqName = DescriptorUtils.getFQName(containingDeclaration); - stringBuilder.append(FqName.ROOT.toUnsafe().equals(fqName) ? "root package" : escape(fqName.getFqName())); + stringBuilder.append(FqName.ROOT.equalsTo(fqName) ? "root package" : escape(fqName.getFqName())); } } diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/IdeErrorMessages.java b/idea/src/org/jetbrains/jet/plugin/highlighter/IdeErrorMessages.java index f2822898e88..814d8179e9e 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/IdeErrorMessages.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/IdeErrorMessages.java @@ -222,7 +222,7 @@ public class IdeErrorMessages { DeclarationDescriptor containingDeclaration = funDescriptor.getContainingDeclaration(); if (containingDeclaration != null) { FqNameUnsafe fqName = DescriptorUtils.getFQName(containingDeclaration); - stringBuilder.append(FqName.ROOT.toUnsafe().equals(fqName) ? "root package" : fqName.getFqName()); + stringBuilder.append(FqName.ROOT.equalsTo(fqName) ? "root package" : fqName.getFqName()); } stringBuilder.append(""); }