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.StringsKt;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.name.ClassId;
022    import org.jetbrains.kotlin.name.FqName;
023    import org.jetbrains.kotlin.name.Name;
024    import org.jetbrains.kotlin.serialization.deserialization.BinaryVersion;
025    import org.jetbrains.kotlin.util.capitalizeDecapitalize.CapitalizeDecapitalizeKt;
026    
027    public final class JvmAbi {
028        /**
029         * This constant is used to identify binary format (class file) versions
030         * If you change class file metadata format and/or naming conventions, please change this version.
031         * - Major version should be increased only when the new binary format is neither forward- nor backward compatible.
032         *   This shouldn't really ever happen at all.
033         * - Minor version should be increased when the new format is backward compatible,
034         *   i.e. the new compiler can process old class files, but the old compiler will not be able to process new class files.
035         * - Patch version can be increased freely and is only supposed to be used for debugging. Increase the patch version when you
036         *   make a change to the metadata format or the bytecode which is both forward- and backward compatible.
037         */
038        public static final BinaryVersion VERSION = BinaryVersion.create(1, 0, 0);
039    
040        public static final String DEFAULT_IMPLS_CLASS_NAME = "DefaultImpls";
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 ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = "$annotations";
052    
053        public static final String INSTANCE_FIELD = "INSTANCE";
054        public static final String DEPRECATED_INSTANCE_FIELD = "INSTANCE$";
055    
056        public static final String MODULE_NAME_FIELD = "$moduleName";
057        public static final String DEFAULT_MODULE_NAME = "main";
058        public static final ClassId REFLECTION_FACTORY_IMPL = ClassId.topLevel(new FqName("kotlin.reflect.jvm.internal.ReflectionFactoryImpl"));
059    
060        public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_ARGUMENT = "$i$a$";
061        public static final String LOCAL_VARIABLE_NAME_PREFIX_INLINE_FUNCTION = "$i$f$";
062    
063        @NotNull
064        public static String getSyntheticMethodNameForAnnotatedProperty(@NotNull Name propertyName) {
065            return propertyName.asString() + ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX;
066        }
067    
068        public static boolean isGetterName(@NotNull String name) {
069            return name.startsWith(GET_PREFIX) || name.startsWith(IS_PREFIX);
070        }
071    
072        public static boolean isSetterName(@NotNull String name) {
073            return name.startsWith(SET_PREFIX);
074        }
075    
076        @NotNull
077        public static String getterName(@NotNull String propertyName) {
078            return startsWithIsPrefix(propertyName)
079                   ? propertyName
080                   : GET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
081    
082        }
083    
084        @NotNull
085        public static String setterName(@NotNull String propertyName) {
086            return startsWithIsPrefix(propertyName)
087                   ? SET_PREFIX + propertyName.substring(IS_PREFIX.length())
088                   : SET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName);
089        }
090    
091        public static boolean startsWithIsPrefix(String name) {
092            if (!name.startsWith(IS_PREFIX)) return false;
093            if (name.length() == IS_PREFIX.length()) return false;
094            char c = name.charAt(IS_PREFIX.length());
095            return !('a' <= c && c <= 'z');
096        }
097    
098        @NotNull
099        public static String sanitizeAsJavaIdentifier(@NotNull String str) {
100            return StringsKt.replace(str, StringsKt.toRegex("[^\\p{L}\\p{Digit}]"), "_");
101        }
102    }
103