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 org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.builtins.CompanionObjectMapping;
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.util.capitalizeDecapitalize.CapitalizeDecapitalizeKt;
029    
030    import static org.jetbrains.kotlin.resolve.DescriptorUtils.isClassOrEnumClass;
031    import static org.jetbrains.kotlin.resolve.DescriptorUtils.isCompanionObject;
032    
033    public final class JvmAbi {
034        public static final String DEFAULT_IMPLS_CLASS_NAME = "DefaultImpls";
035    
036        /**
037         * Warning: use DEFAULT_IMPLS_CLASS_NAME and TypeMappingConfiguration.innerClassNameFactory when possible.
038         * This is false for KAPT3 mode.
039         */
040        public static final String DEFAULT_IMPLS_SUFFIX = "$" + DEFAULT_IMPLS_CLASS_NAME;
041        public static final String DEFAULT_IMPLS_DELEGATE_SUFFIX = "$defaultImpl";
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        private static final String ANNOTATIONS_SUFFIX = "$annotations";
053        private static final String ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = ANNOTATIONS_SUFFIX;
054        private static final String ANNOTATED_TYPEALIAS_METHOD_NAME_SUFFIX = ANNOTATIONS_SUFFIX;
055    
056        public static final String INSTANCE_FIELD = "INSTANCE";
057    
058        public static final String DEFAULT_MODULE_NAME = "main";
059        public static final ClassId REFLECTION_FACTORY_IMPL = ClassId.topLevel(new FqName("kotlin.reflect.jvm.internal.ReflectionFactoryImpl"));
060    
061        public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_ARGUMENT = "$i$a$";
062        public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_FUNCTION = "$i$f$";
063    
064        @NotNull
065        public static String getSyntheticMethodNameForAnnotatedProperty(@NotNull Name propertyName) {
066            return propertyName.asString() + ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX;
067        }
068    
069        @NotNull
070        public static String getSyntheticMethodNameForAnnotatedTypeAlias(@NotNull Name typeAliasName) {
071            return typeAliasName.asString() + ANNOTATED_TYPEALIAS_METHOD_NAME_SUFFIX;
072        }
073    
074        public static boolean isGetterName(@NotNull String name) {
075            return name.startsWith(GET_PREFIX) || name.startsWith(IS_PREFIX);
076        }
077    
078        public static boolean isSetterName(@NotNull String name) {
079            return name.startsWith(SET_PREFIX);
080        }
081    
082        @NotNull
083        public static String getterName(@NotNull String propertyName) {
084            return startsWithIsPrefix(propertyName)
085                   ? propertyName
086                   : GET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
087    
088        }
089    
090        @NotNull
091        public static String setterName(@NotNull String propertyName) {
092            return SET_PREFIX +
093                   (startsWithIsPrefix(propertyName)
094                    ? propertyName.substring(IS_PREFIX.length())
095                    : CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName));
096        }
097    
098        public static boolean startsWithIsPrefix(String name) {
099            if (!name.startsWith(IS_PREFIX)) return false;
100            if (name.length() == IS_PREFIX.length()) return false;
101            char c = name.charAt(IS_PREFIX.length());
102            return !('a' <= c && c <= 'z');
103        }
104    
105        public static boolean isPropertyWithBackingFieldInOuterClass(@NotNull PropertyDescriptor propertyDescriptor) {
106            return propertyDescriptor.getKind() != CallableMemberDescriptor.Kind.FAKE_OVERRIDE &&
107                   isCompanionObjectWithBackingFieldsInOuter(propertyDescriptor.getContainingDeclaration());
108        }
109    
110        public static boolean isCompanionObjectWithBackingFieldsInOuter(@NotNull DeclarationDescriptor companionObject) {
111            return isCompanionObject(companionObject) &&
112                   isClassOrEnumClass(companionObject.getContainingDeclaration()) &&
113                   !CompanionObjectMapping.INSTANCE.isMappedIntrinsicCompanionObject((ClassDescriptor) companionObject);
114        }
115    }