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    
037        /**
038         * Warning: use DEFAULT_IMPLS_CLASS_NAME and TypeMappingConfiguration.innerClassNameFactory when possible.
039         * This is false for KAPT3 mode.
040         */
041        public static final String DEFAULT_IMPLS_SUFFIX = "$" + DEFAULT_IMPLS_CLASS_NAME;
042    
043        public static final String DEFAULT_PARAMS_IMPL_SUFFIX = "$default";
044    
045        private static final String GET_PREFIX = "get";
046        private static final String IS_PREFIX = "is";
047        private static final String SET_PREFIX = "set";
048    
049        public static final String DELEGATED_PROPERTY_NAME_SUFFIX = "$delegate";
050        public static final String DELEGATED_PROPERTIES_ARRAY_NAME = "$$delegatedProperties";
051        public static final String DELEGATE_SUPER_FIELD_PREFIX = "$$delegate_";
052        public static final String ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = "$annotations";
053    
054        public static final String INSTANCE_FIELD = "INSTANCE";
055    
056        public static final String DEFAULT_MODULE_NAME = "main";
057        public static final ClassId REFLECTION_FACTORY_IMPL = ClassId.topLevel(new FqName("kotlin.reflect.jvm.internal.ReflectionFactoryImpl"));
058    
059        public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_ARGUMENT = "$i$a$";
060        public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_FUNCTION = "$i$f$";
061    
062        private static final Regex SANITIZE_AS_JAVA_INVALID_CHARACTERS = new Regex("[^\\p{L}\\p{Digit}]");
063    
064        @NotNull
065        public static String getSyntheticMethodNameForAnnotatedProperty(@NotNull Name propertyName) {
066            return propertyName.asString() + ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX;
067        }
068    
069        public static boolean isGetterName(@NotNull String name) {
070            return name.startsWith(GET_PREFIX) || name.startsWith(IS_PREFIX);
071        }
072    
073        public static boolean isSetterName(@NotNull String name) {
074            return name.startsWith(SET_PREFIX);
075        }
076    
077        @NotNull
078        public static String getterName(@NotNull String propertyName) {
079            return startsWithIsPrefix(propertyName)
080                   ? propertyName
081                   : GET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
082    
083        }
084    
085        @NotNull
086        public static String setterName(@NotNull String propertyName) {
087            return startsWithIsPrefix(propertyName)
088                   ? SET_PREFIX + propertyName.substring(IS_PREFIX.length())
089                   : SET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
090        }
091    
092        public static boolean startsWithIsPrefix(String name) {
093            if (!name.startsWith(IS_PREFIX)) return false;
094            if (name.length() == IS_PREFIX.length()) return false;
095            char c = name.charAt(IS_PREFIX.length());
096            return !('a' <= c && c <= 'z');
097        }
098    
099        @NotNull
100        public static String sanitizeAsJavaIdentifier(@NotNull String str) {
101            return SANITIZE_AS_JAVA_INVALID_CHARACTERS.replace(str, "_");
102        }
103    
104        public static boolean isPropertyWithBackingFieldInOuterClass(@NotNull PropertyDescriptor propertyDescriptor) {
105            return propertyDescriptor.getKind() != CallableMemberDescriptor.Kind.FAKE_OVERRIDE &&
106                   isCompanionObjectWithBackingFieldsInOuter(propertyDescriptor.getContainingDeclaration());
107        }
108    
109        public static boolean isCompanionObjectWithBackingFieldsInOuter(@NotNull DeclarationDescriptor companionObject) {
110            return isCompanionObject(companionObject) &&
111                   isClassOrEnumClass(companionObject.getContainingDeclaration()) &&
112                   !CompanionObjectMapping.INSTANCE.isMappedIntrinsicCompanionObject((ClassDescriptor) companionObject);
113        }
114    }