001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.load.java;
018
019 import kotlin.text.Regex;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.kotlin.builtins.CompanionObjectMapping;
022 import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor;
023 import org.jetbrains.kotlin.descriptors.ClassDescriptor;
024 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
025 import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
026 import org.jetbrains.kotlin.name.ClassId;
027 import org.jetbrains.kotlin.name.FqName;
028 import org.jetbrains.kotlin.name.Name;
029 import org.jetbrains.kotlin.util.capitalizeDecapitalize.CapitalizeDecapitalizeKt;
030
031 import static org.jetbrains.kotlin.resolve.DescriptorUtils.isClassOrEnumClass;
032 import static org.jetbrains.kotlin.resolve.DescriptorUtils.isCompanionObject;
033
034 public final class JvmAbi {
035 public static final String DEFAULT_IMPLS_CLASS_NAME = "DefaultImpls";
036 public static final String DEFAULT_IMPLS_SUFFIX = "$" + DEFAULT_IMPLS_CLASS_NAME;
037
038 public static final String DEFAULT_PARAMS_IMPL_SUFFIX = "$default";
039
040 private static final String GET_PREFIX = "get";
041 private static final String IS_PREFIX = "is";
042 private static final String SET_PREFIX = "set";
043
044 public static final String DELEGATED_PROPERTY_NAME_SUFFIX = "$delegate";
045 public static final String DELEGATED_PROPERTIES_ARRAY_NAME = "$$delegatedProperties";
046 public static final String DELEGATE_SUPER_FIELD_PREFIX = "$$delegate_";
047 public static final String ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = "$annotations";
048
049 public static final String INSTANCE_FIELD = "INSTANCE";
050
051 public static final String DEFAULT_MODULE_NAME = "main";
052 public static final ClassId REFLECTION_FACTORY_IMPL = ClassId.topLevel(new FqName("kotlin.reflect.jvm.internal.ReflectionFactoryImpl"));
053
054 public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_ARGUMENT = "$i$a$";
055 public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_FUNCTION = "$i$f$";
056
057 private static final Regex SANITIZE_AS_JAVA_INVALID_CHARACTERS = new Regex("[^\\p{L}\\p{Digit}]");
058
059 @NotNull
060 public static String getSyntheticMethodNameForAnnotatedProperty(@NotNull Name propertyName) {
061 return propertyName.asString() + ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX;
062 }
063
064 public static boolean isGetterName(@NotNull String name) {
065 return name.startsWith(GET_PREFIX) || name.startsWith(IS_PREFIX);
066 }
067
068 public static boolean isSetterName(@NotNull String name) {
069 return name.startsWith(SET_PREFIX);
070 }
071
072 @NotNull
073 public static String getterName(@NotNull String propertyName) {
074 return startsWithIsPrefix(propertyName)
075 ? propertyName
076 : GET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
077
078 }
079
080 @NotNull
081 public static String setterName(@NotNull String propertyName) {
082 return startsWithIsPrefix(propertyName)
083 ? SET_PREFIX + propertyName.substring(IS_PREFIX.length())
084 : SET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
085 }
086
087 public static boolean startsWithIsPrefix(String name) {
088 if (!name.startsWith(IS_PREFIX)) return false;
089 if (name.length() == IS_PREFIX.length()) return false;
090 char c = name.charAt(IS_PREFIX.length());
091 return !('a' <= c && c <= 'z');
092 }
093
094 @NotNull
095 public static String sanitizeAsJavaIdentifier(@NotNull String str) {
096 return SANITIZE_AS_JAVA_INVALID_CHARACTERS.replace(str, "_");
097 }
098
099 public static boolean isPropertyWithBackingFieldInOuterClass(@NotNull PropertyDescriptor propertyDescriptor) {
100 return propertyDescriptor.getKind() != CallableMemberDescriptor.Kind.FAKE_OVERRIDE &&
101 isCompanionObjectWithBackingFieldsInOuter(propertyDescriptor.getContainingDeclaration());
102 }
103
104 public static boolean isCompanionObjectWithBackingFieldsInOuter(@NotNull DeclarationDescriptor companionObject) {
105 return isCompanionObject(companionObject) &&
106 isClassOrEnumClass(companionObject.getContainingDeclaration()) &&
107 !CompanionObjectMapping.INSTANCE.isMappedIntrinsicCompanionObject((ClassDescriptor) companionObject);
108 }
109 }