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.KotlinPackage;
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.resolve.jvm.JvmClassName;
025    
026    import java.util.Arrays;
027    import java.util.HashSet;
028    import java.util.Set;
029    
030    public final class JvmAnnotationNames {
031        public static final FqName KOTLIN_CLASS = KotlinClass.CLASS_NAME.getFqNameForClassNameWithoutDollars();
032        public static final FqName KOTLIN_PACKAGE = new FqName("kotlin.jvm.internal.KotlinPackage");
033    
034        public static final FqName KOTLIN_SIGNATURE = new FqName("kotlin.jvm.KotlinSignature");
035        public static final FqName OLD_KOTLIN_SIGNATURE = new FqName("jet.runtime.typeinfo.KotlinSignature");
036    
037        public static final String ABI_VERSION_FIELD_NAME = "abiVersion";
038        public static final String KIND_FIELD_NAME = "kind";
039        public static final String DATA_FIELD_NAME = "data";
040        public static final Name DEFAULT_ANNOTATION_MEMBER_NAME = Name.identifier("value");
041    
042        public static final FqName JETBRAINS_NOT_NULL_ANNOTATION = new FqName("org.jetbrains.annotations.NotNull");
043        public static final FqName JETBRAINS_NULLABLE_ANNOTATION = new FqName("org.jetbrains.annotations.Nullable");
044        public static final FqName JETBRAINS_MUTABLE_ANNOTATION = new FqName("org.jetbrains.annotations.Mutable");
045        public static final FqName JETBRAINS_READONLY_ANNOTATION = new FqName("org.jetbrains.annotations.ReadOnly");
046    
047        public static class KotlinClass {
048            public static final JvmClassName CLASS_NAME = JvmClassName.byInternalName("kotlin/jvm/internal/KotlinClass");
049            public static final ClassId KIND_CLASS_ID =
050                    ClassId.topLevel(CLASS_NAME.getFqNameForClassNameWithoutDollars()).createNestedClassId(Name.identifier("Kind"));
051            public static final String KIND_INTERNAL_NAME = JvmClassName.byClassId(KIND_CLASS_ID).getInternalName();
052    
053            /**
054             * This enum duplicates {@link kotlin.jvm.internal.KotlinClass.Kind}. Both places should be updated simultaneously.
055             */
056            public enum Kind {
057                CLASS,
058                LOCAL_CLASS,
059                ANONYMOUS_OBJECT,
060                ;
061            }
062        }
063    
064        public static class KotlinSyntheticClass {
065            public static final JvmClassName CLASS_NAME = JvmClassName.byInternalName("kotlin/jvm/internal/KotlinSyntheticClass");
066            public static final ClassId KIND_CLASS_ID =
067                    ClassId.topLevel(CLASS_NAME.getFqNameForClassNameWithoutDollars()).createNestedClassId(Name.identifier("Kind"));
068            public static final String KIND_INTERNAL_NAME = JvmClassName.byClassId(KIND_CLASS_ID).getInternalName();
069    
070            /**
071             * This enum duplicates {@link kotlin.jvm.internal.KotlinSyntheticClass.Kind}. Both places should be updated simultaneously.
072             */
073            public enum Kind {
074                PACKAGE_PART,
075                TRAIT_IMPL,
076                LOCAL_TRAIT_IMPL,
077                SAM_WRAPPER,
078                SAM_LAMBDA,
079                CALLABLE_REFERENCE_WRAPPER,
080                LOCAL_FUNCTION,
081                ANONYMOUS_FUNCTION,
082                WHEN_ON_ENUM_MAPPINGS,
083                ;
084            }
085        }
086    
087        @Deprecated
088        public static final FqName OLD_JET_CLASS_ANNOTATION = new FqName("jet.runtime.typeinfo.JetClass");
089        @Deprecated
090        public static final FqName OLD_JET_PACKAGE_CLASS_ANNOTATION = new FqName("jet.runtime.typeinfo.JetPackageClass");
091        @Deprecated
092        public static final FqName OLD_JET_VALUE_PARAMETER_ANNOTATION = new FqName("jet.runtime.typeinfo.JetValueParameter");
093        @Deprecated
094        public static final FqName OLD_KOTLIN_CLASS = new FqName("jet.KotlinClass");
095        @Deprecated
096        public static final FqName OLD_KOTLIN_PACKAGE = new FqName("jet.KotlinPackage");
097        @Deprecated
098        public static final FqName OLD_KOTLIN_PACKAGE_FRAGMENT = new FqName("jet.KotlinPackageFragment");
099        @Deprecated
100        public static final FqName OLD_KOTLIN_TRAIT_IMPL = new FqName("jet.KotlinTraitImpl");
101    
102        // When these annotations appear on a declaration, they are copied to the _type_ of the declaration, becoming type annotations
103        // See also DescriptorRendererBuilder#excludedTypeAnnotationClasses
104        public static final Set<FqName> ANNOTATIONS_COPIED_TO_TYPES = KotlinPackage.setOf(
105                JETBRAINS_READONLY_ANNOTATION,
106                JETBRAINS_MUTABLE_ANNOTATION,
107                JETBRAINS_NOT_NULL_ANNOTATION,
108                JETBRAINS_NULLABLE_ANNOTATION
109        );
110    
111        private static final Set<JvmClassName> SPECIAL_ANNOTATIONS = new HashSet<JvmClassName>();
112        private static final Set<JvmClassName> NULLABILITY_ANNOTATIONS = new HashSet<JvmClassName>();
113        static {
114            for (FqName fqName : Arrays.asList(KOTLIN_CLASS, KOTLIN_PACKAGE, KOTLIN_SIGNATURE)) {
115                SPECIAL_ANNOTATIONS.add(JvmClassName.byFqNameWithoutInnerClasses(fqName));
116            }
117            SPECIAL_ANNOTATIONS.add(KotlinSyntheticClass.CLASS_NAME);
118    
119            for (FqName fqName : Arrays.asList(JETBRAINS_NOT_NULL_ANNOTATION, JETBRAINS_NULLABLE_ANNOTATION)) {
120                NULLABILITY_ANNOTATIONS.add(JvmClassName.byFqNameWithoutInnerClasses(fqName));
121            }
122        }
123    
124        public static boolean isSpecialAnnotation(@NotNull ClassId classId, boolean javaSpecificAnnotationsAreSpecial) {
125            JvmClassName className = JvmClassName.byClassId(classId);
126            return (javaSpecificAnnotationsAreSpecial && NULLABILITY_ANNOTATIONS.contains(className))
127                   || SPECIAL_ANNOTATIONS.contains(className)
128                   || className.getInternalName().startsWith("jet/runtime/typeinfo/");
129        }
130    
131        private JvmAnnotationNames() {
132        }
133    }