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.descriptors.CallableMemberDescriptor;
022    import org.jetbrains.kotlin.descriptors.ClassDescriptor;
023    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
024    import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
025    import org.jetbrains.kotlin.name.ClassId;
026    import org.jetbrains.kotlin.name.FqName;
027    import org.jetbrains.kotlin.name.Name;
028    import org.jetbrains.kotlin.platform.JavaToKotlinClassMap;
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 ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = "$annotations";
047    
048        public static final String INSTANCE_FIELD = "INSTANCE";
049    
050        public static final String DEFAULT_MODULE_NAME = "main";
051        public static final ClassId REFLECTION_FACTORY_IMPL = ClassId.topLevel(new FqName("kotlin.reflect.jvm.internal.ReflectionFactoryImpl"));
052    
053        public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_ARGUMENT = "$i$a$";
054        public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_FUNCTION = "$i$f$";
055    
056        private static final Regex SANITIZE_AS_JAVA_INVALID_CHARACTERS = new Regex("[^\\p{L}\\p{Digit}]");
057    
058        @NotNull
059        public static String getSyntheticMethodNameForAnnotatedProperty(@NotNull Name propertyName) {
060            return propertyName.asString() + ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX;
061        }
062    
063        public static boolean isGetterName(@NotNull String name) {
064            return name.startsWith(GET_PREFIX) || name.startsWith(IS_PREFIX);
065        }
066    
067        public static boolean isSetterName(@NotNull String name) {
068            return name.startsWith(SET_PREFIX);
069        }
070    
071        @NotNull
072        public static String getterName(@NotNull String propertyName) {
073            return startsWithIsPrefix(propertyName)
074                   ? propertyName
075                   : GET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
076    
077        }
078    
079        @NotNull
080        public static String setterName(@NotNull String propertyName) {
081            return startsWithIsPrefix(propertyName)
082                   ? SET_PREFIX + propertyName.substring(IS_PREFIX.length())
083                   : SET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
084        }
085    
086        public static boolean startsWithIsPrefix(String name) {
087            if (!name.startsWith(IS_PREFIX)) return false;
088            if (name.length() == IS_PREFIX.length()) return false;
089            char c = name.charAt(IS_PREFIX.length());
090            return !('a' <= c && c <= 'z');
091        }
092    
093        @NotNull
094        public static String sanitizeAsJavaIdentifier(@NotNull String str) {
095            return SANITIZE_AS_JAVA_INVALID_CHARACTERS.replace(str, "_");
096        }
097    
098        public static boolean isPropertyWithBackingFieldInOuterClass(@NotNull PropertyDescriptor propertyDescriptor) {
099            return propertyDescriptor.getKind() != CallableMemberDescriptor.Kind.FAKE_OVERRIDE &&
100                   isCompanionObjectWithBackingFieldsInOuter(propertyDescriptor.getContainingDeclaration());
101        }
102    
103        public static boolean isCompanionObjectWithBackingFieldsInOuter(@NotNull DeclarationDescriptor companionObject) {
104            return isCompanionObject(companionObject) &&
105                   isClassOrEnumClass(companionObject.getContainingDeclaration()) &&
106                   !JavaToKotlinClassMap.INSTANCE.isMappedCompanion((ClassDescriptor) companionObject);
107        }
108    }
109