Move NameShortness to a separate file and refactor it to be an interface

This commit is contained in:
Pavel V. Talanov
2016-02-12 13:23:54 +03:00
parent 77f74a929a
commit 40d538731b
7 changed files with 83 additions and 51 deletions
@@ -61,7 +61,7 @@ public abstract class AbstractAnnotationDescriptorResolveTest extends KotlinLite
@Override
public Unit invoke(DescriptorRendererOptions options) {
options.setVerbose(true);
options.setNameShortness(NameShortness.SHORT);
options.setNameShortness(NameShortness.SHORT.INSTANCE);
options.setModifiers(DescriptorRendererModifier.ALL);
return Unit.INSTANCE;
}
@@ -54,7 +54,7 @@ public class RecursiveDescriptorComparator {
options.setExcludedAnnotationClasses(Collections.singleton(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME)));
options.setOverrideRenderingPolicy(OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE);
options.setIncludePropertyConstant(true);
options.setNameShortness(NameShortness.FULLY_QUALIFIED);
options.setNameShortness(NameShortness.FULLY_QUALIFIED.INSTANCE);
options.setVerbose(true);
options.setModifiers(DescriptorRendererModifier.ALL);
return Unit.INSTANCE;
@@ -226,12 +226,6 @@ enum class RenderingFormat {
HTML
}
enum class NameShortness {
SHORT,
FULLY_QUALIFIED,
SOURCE_CODE_QUALIFIED // for local declarations qualified up to function scope
}
enum class OverrideRenderingPolicy {
RENDER_OVERRIDE,
RENDER_OPEN,
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.ErrorUtils.UninferredParameterTypeConstructor
import org.jetbrains.kotlin.types.TypeUtils.CANT_INFER_FUNCTION_PARAM_TYPE
import org.jetbrains.kotlin.types.error.MissingDependencyErrorClass
import org.jetbrains.kotlin.types.typeUtil.builtIns
import java.util.*
internal class DescriptorRendererImpl(
@@ -122,27 +123,7 @@ internal class DescriptorRendererImpl(
if (ErrorUtils.isError(klass)) {
return klass.typeConstructor.toString()
}
when (nameShortness) {
NameShortness.SHORT -> {
val qualifiedNameElements = ArrayList<Name>()
// for nested classes qualified name should be used
var current: DeclarationDescriptor? = klass
do {
qualifiedNameElements.add(current!!.name)
current = current.containingDeclaration
}
while (current is ClassDescriptor)
return renderFqName(qualifiedNameElements.asReversed())
}
NameShortness.FULLY_QUALIFIED -> return renderFqName(DescriptorUtils.getFqName(klass))
NameShortness.SOURCE_CODE_QUALIFIED -> return qualifiedNameForSourceCode(klass)
else -> throw IllegalArgumentException()
}
return nameShortness.renderClassifier(klass, this)
}
/* TYPES RENDERING */
@@ -221,7 +202,8 @@ internal class DescriptorRendererImpl(
return lowerRendered + "!"
}
val kotlinCollectionsPrefix = if (nameShortness != NameShortness.SHORT) "kotlin.collections." else ""
val kotlinCollectionsPrefix = nameShortness.renderClassifier(type.builtIns.collection, this).substringBefore("Collection")
val mutablePrefix = "Mutable"
// java.util.List<Foo> -> (Mutable)List<Foo!>!
val simpleCollection = replacePrefixes(lowerRendered, kotlinCollectionsPrefix + mutablePrefix, upperRendered, kotlinCollectionsPrefix, kotlinCollectionsPrefix + "(" + mutablePrefix + ")")
@@ -230,7 +212,7 @@ internal class DescriptorRendererImpl(
val mutableEntry = replacePrefixes(lowerRendered, kotlinCollectionsPrefix + "MutableMap.MutableEntry", upperRendered, kotlinCollectionsPrefix + "Map.Entry", kotlinCollectionsPrefix + "(Mutable)Map.(Mutable)Entry")
if (mutableEntry != null) return mutableEntry
val kotlinPrefix = if (nameShortness != NameShortness.SHORT) "kotlin." else ""
val kotlinPrefix = nameShortness.renderClassifier(type.builtIns.array, this).substringBefore("Array")
// Foo[] -> Array<(out) Foo!>!
val array = replacePrefixes(lowerRendered, kotlinPrefix + escape("Array<"), upperRendered, kotlinPrefix + escape("Array<out "), kotlinPrefix + escape("Array<(out) "))
if (array != null) return array
@@ -297,8 +279,7 @@ internal class DescriptorRendererImpl(
override fun renderTypeConstructor(typeConstructor: TypeConstructor): String {
val cd = typeConstructor.declarationDescriptor
return when (cd) {
is TypeParameterDescriptor -> renderName(cd.getName())
is ClassDescriptor -> renderClassifierName(cd)
is TypeParameterDescriptor, is ClassDescriptor -> renderClassifierName(cd)
null -> typeConstructor.toString()
else -> error("Unexpected classifier: " + cd.javaClass)
}
@@ -63,7 +63,7 @@ internal class DescriptorRendererOptionsImpl : DescriptorRendererOptions {
}
}
override var nameShortness by property(NameShortness.SOURCE_CODE_QUALIFIED)
override var nameShortness: NameShortness by property(NameShortness.SOURCE_CODE_QUALIFIED)
override var withDefinedIn by property(true)
override var modifiers: Set<DescriptorRendererModifier> by property(DescriptorRendererModifier.DEFAULTS)
override var startFromName by property(false)
@@ -0,0 +1,74 @@
/*
* Copyright 2010-2016 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.kotlin.renderer
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.DescriptorUtils
import java.util.*
interface NameShortness {
object SHORT : NameShortness {
override fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String {
if (classifier is TypeParameterDescriptor) return renderer.renderName(classifier.name)
val qualifiedNameElements = ArrayList<Name>()
// for nested classes qualified name should be used
var current: DeclarationDescriptor? = classifier
do {
qualifiedNameElements.add(current!!.name)
current = current.containingDeclaration
}
while (current is ClassDescriptor)
return renderFqName(qualifiedNameElements.asReversed())
}
}
object FULLY_QUALIFIED : NameShortness {
override fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String {
if (classifier is TypeParameterDescriptor) return renderer.renderName(classifier.name)
return renderer.renderFqName(DescriptorUtils.getFqName(classifier))
}
}
// for local declarations qualified up to function scope
object SOURCE_CODE_QUALIFIED : NameShortness {
override fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String {
return qualifiedNameForSourceCode(classifier)
}
fun qualifiedNameForSourceCode(descriptor: ClassifierDescriptor): String {
val nameString = descriptor.name.render()
if (descriptor is TypeParameterDescriptor) {
return nameString
}
val qualifier = qualifierName(descriptor.containingDeclaration)
return if (qualifier != null && qualifier != "") qualifier + "." + nameString else nameString
}
private fun qualifierName(descriptor: DeclarationDescriptor): String? = when (descriptor) {
is ClassDescriptor -> qualifiedNameForSourceCode(descriptor)
is PackageFragmentDescriptor -> descriptor.fqName.toUnsafe().render()
else -> null
}
}
fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String
}
@@ -16,27 +16,10 @@
package org.jetbrains.kotlin.renderer
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.name.Name
fun qualifiedNameForSourceCode(descriptor: ClassifierDescriptor): String {
val nameString = descriptor.name.render()
if (descriptor is TypeParameterDescriptor) {
return nameString
}
val qualifier = qualifierName(descriptor.containingDeclaration)
return if (qualifier != null && qualifier != "") qualifier + "." + nameString else nameString
}
private fun qualifierName(descriptor: DeclarationDescriptor): String? = when (descriptor) {
is ClassDescriptor -> qualifiedNameForSourceCode(descriptor)
is PackageFragmentDescriptor -> descriptor.fqName.toUnsafe().render()
else -> null
}
fun Name.render(): String {
return if (this.shouldBeEscaped()) '`' + asString() + '`' else asString()
}