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 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 @NotNull 063 public static String getSyntheticMethodNameForAnnotatedProperty(@NotNull Name propertyName) { 064 return propertyName.asString() + ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX; 065 } 066 067 public static boolean isGetterName(@NotNull String name) { 068 return name.startsWith(GET_PREFIX) || name.startsWith(IS_PREFIX); 069 } 070 071 public static boolean isSetterName(@NotNull String name) { 072 return name.startsWith(SET_PREFIX); 073 } 074 075 @NotNull 076 public static String getterName(@NotNull String propertyName) { 077 return startsWithIsPrefix(propertyName) 078 ? propertyName 079 : GET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName); 080 081 } 082 083 @NotNull 084 public static String setterName(@NotNull String propertyName) { 085 return startsWithIsPrefix(propertyName) 086 ? SET_PREFIX + propertyName.substring(IS_PREFIX.length()) 087 : SET_PREFIX + CapitalizeDecapitalizeKt.capitalizeAsciiOnly(propertyName); 088 } 089 090 public static boolean startsWithIsPrefix(String name) { 091 if (!name.startsWith(IS_PREFIX)) return false; 092 if (name.length() == IS_PREFIX.length()) return false; 093 char c = name.charAt(IS_PREFIX.length()); 094 return !('a' <= c && c <= 'z'); 095 } 096 097 @NotNull 098 public static String sanitizeAsJavaIdentifier(@NotNull String str) { 099 return StringsKt.replace(str, StringsKt.toRegex("[^\\p{L}\\p{Digit}]"), "_"); 100 } 101 } 102