Move name sanitization utilities to module 'descriptors'

This commit is contained in:
Alexander Udalov
2017-01-11 14:22:18 +03:00
parent f4d59304e2
commit 6ffa56b640
7 changed files with 63 additions and 38 deletions
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.load.java;
import kotlin.text.Regex;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.CompanionObjectMapping;
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor;
@@ -51,8 +50,8 @@ public final class JvmAbi {
public static final String DELEGATED_PROPERTIES_ARRAY_NAME = "$$delegatedProperties";
public static final String DELEGATE_SUPER_FIELD_PREFIX = "$$delegate_";
private static final String ANNOTATIONS_SUFFIX = "$annotations";
public static final String ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = ANNOTATIONS_SUFFIX;
public static final String ANNOTATED_TYPEALIAS_METHOD_NAME_SUFFIX = ANNOTATIONS_SUFFIX;
private static final String ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = ANNOTATIONS_SUFFIX;
private static final String ANNOTATED_TYPEALIAS_METHOD_NAME_SUFFIX = ANNOTATIONS_SUFFIX;
public static final String INSTANCE_FIELD = "INSTANCE";
@@ -62,8 +61,6 @@ public final class JvmAbi {
public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_ARGUMENT = "$i$a$";
public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_FUNCTION = "$i$f$";
private static final Regex SANITIZE_AS_JAVA_INVALID_CHARACTERS = new Regex("[^\\p{L}\\p{Digit}]");
@NotNull
public static String getSyntheticMethodNameForAnnotatedProperty(@NotNull Name propertyName) {
return propertyName.asString() + ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX;
@@ -92,9 +89,10 @@ public final class JvmAbi {
@NotNull
public static String setterName(@NotNull String propertyName) {
return startsWithIsPrefix(propertyName)
? SET_PREFIX + propertyName.substring(IS_PREFIX.length())
: SET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
return SET_PREFIX +
(startsWithIsPrefix(propertyName)
? propertyName.substring(IS_PREFIX.length())
: CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName));
}
public static boolean startsWithIsPrefix(String name) {
@@ -104,11 +102,6 @@ public final class JvmAbi {
return !('a' <= c && c <= 'z');
}
@NotNull
public static String sanitizeAsJavaIdentifier(@NotNull String str) {
return SANITIZE_AS_JAVA_INVALID_CHARACTERS.replace(str, "_");
}
public static boolean isPropertyWithBackingFieldInOuterClass(@NotNull PropertyDescriptor propertyDescriptor) {
return propertyDescriptor.getKind() != CallableMemberDescriptor.Kind.FAKE_OVERRIDE &&
isCompanionObjectWithBackingFieldsInOuter(propertyDescriptor.getContainingDeclaration());
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2017 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.name
import java.util.*
object NameUtils {
private val SANITIZE_AS_JAVA_INVALID_CHARACTERS = "[^\\p{L}\\p{Digit}]".toRegex()
@JvmStatic
fun sanitizeAsJavaIdentifier(name: String): String {
return SANITIZE_AS_JAVA_INVALID_CHARACTERS.replace(name, "_")
}
/**
* Capitalizes the short name of the file (without extension) and sanitizes it so that it's a valid Java identifier.
* E.g. "fileName" -> "FileName", "1" -> "_1", "" -> "_"
*/
@JvmStatic
fun getPackagePartClassNamePrefix(shortFileName: String): String =
if (shortFileName.isEmpty())
"_"
else
capitalizeAsJavaClassName(sanitizeAsJavaIdentifier(shortFileName))
@JvmStatic
private fun capitalizeAsJavaClassName(str: String): String =
// NB use Locale.ENGLISH so that build is locale-independent.
// See Javadoc on java.lang.String.toUpperCase() for more details.
if (Character.isJavaIdentifierStart(str[0]))
str.substring(0, 1).toUpperCase(Locale.ENGLISH) + str.substring(1)
else
"_$str"
}
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
import org.jetbrains.kotlin.load.java.structure.reflect.*
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
@@ -126,7 +127,7 @@ internal sealed class JvmPropertySignature {
if (classProto.hasExtension(JvmProtoBuf.classModuleName))
nameResolver.getString(classProto.getExtension(JvmProtoBuf.classModuleName))
else JvmAbi.DEFAULT_MODULE_NAME
return "$" + JvmAbi.sanitizeAsJavaIdentifier(moduleName)
return "$" + NameUtils.sanitizeAsJavaIdentifier(moduleName)
}
if (descriptor.visibility == Visibilities.PRIVATE && containingDeclaration is PackageFragmentDescriptor) {
val packagePartSource = (descriptor as DeserializedPropertyDescriptor).containerSource