001    /*
002     * Copyright 2010-2013 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.jet.lang.resolve.java;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.resolve.name.FqName;
022    import org.jetbrains.jet.lang.resolve.name.Name;
023    
024    import java.util.Arrays;
025    import java.util.HashSet;
026    import java.util.Set;
027    
028    public final class JvmAnnotationNames {
029        public static final FqName KOTLIN_CLASS = new FqName("kotlin.jvm.internal.KotlinClass");
030        public static final FqName KOTLIN_PACKAGE = new FqName("kotlin.jvm.internal.KotlinPackage");
031    
032        public static final FqName KOTLIN_SIGNATURE = new FqName("kotlin.jvm.KotlinSignature");
033        public static final FqName OLD_KOTLIN_SIGNATURE = new FqName("jet.runtime.typeinfo.KotlinSignature");
034    
035        public static final String ABI_VERSION_FIELD_NAME = "abiVersion";
036        public static final String DATA_FIELD_NAME = "data";
037        public static final Name DEFAULT_ANNOTATION_MEMBER_NAME = Name.identifier("value");
038    
039        public static final FqName JETBRAINS_NOT_NULL_ANNOTATION = new FqName("org.jetbrains.annotations.NotNull");
040        public static final FqName JETBRAINS_NULLABLE_ANNOTATION = new FqName("org.jetbrains.annotations.Nullable");
041        public static final FqName JETBRAINS_MUTABLE_ANNOTATION = new FqName("org.jetbrains.annotations.Mutable");
042        public static final FqName JETBRAINS_READONLY_ANNOTATION = new FqName("org.jetbrains.annotations.ReadOnly");
043    
044        public static class KotlinSyntheticClass {
045            public static final JvmClassName CLASS_NAME = JvmClassName.byInternalName("kotlin/jvm/internal/KotlinSyntheticClass");
046            public static final String KIND_INTERNAL_NAME = "kotlin/jvm/internal/KotlinSyntheticClass$Kind";
047    
048            public static final Name KIND_FIELD_NAME = Name.identifier("kind");
049    
050            /**
051             * This enum duplicates {@link kotlin.jvm.internal.KotlinSyntheticClass.Kind}, because this code can't depend on "runtime.jvm".
052             * Both places should be updated simultaneously
053             */
054            public enum Kind {
055                PACKAGE_PART,
056                TRAIT_IMPL,
057                SAM_WRAPPER,
058                SAM_LAMBDA,
059                CALLABLE_REFERENCE_WRAPPER,
060                LOCAL_FUNCTION,
061                ANONYMOUS_FUNCTION,
062                LOCAL_CLASS,
063                ANONYMOUS_OBJECT,
064                ;
065    
066                @Nullable
067                public static Kind valueOfOrNull(@NotNull String name) {
068                    try {
069                        return valueOf(name);
070                    }
071                    catch (IllegalArgumentException e) {
072                        return null;
073                    }
074                }
075            }
076    
077            private KotlinSyntheticClass() {
078            }
079        }
080    
081        @Deprecated
082        public static final FqName OLD_JET_CLASS_ANNOTATION = new FqName("jet.runtime.typeinfo.JetClass");
083        @Deprecated
084        public static final FqName OLD_JET_PACKAGE_CLASS_ANNOTATION = new FqName("jet.runtime.typeinfo.JetPackageClass");
085        @Deprecated
086        public static final FqName OLD_JET_VALUE_PARAMETER_ANNOTATION = new FqName("jet.runtime.typeinfo.JetValueParameter");
087        @Deprecated
088        public static final FqName OLD_KOTLIN_CLASS = new FqName("jet.KotlinClass");
089        @Deprecated
090        public static final FqName OLD_KOTLIN_PACKAGE = new FqName("jet.KotlinPackage");
091        @Deprecated
092        public static final FqName OLD_KOTLIN_PACKAGE_FRAGMENT = new FqName("jet.KotlinPackageFragment");
093        @Deprecated
094        public static final FqName OLD_KOTLIN_TRAIT_IMPL = new FqName("jet.KotlinTraitImpl");
095    
096        private static final Set<JvmClassName> SPECIAL_ANNOTATIONS = new HashSet<JvmClassName>();
097        static {
098            for (FqName fqName : Arrays.asList(KOTLIN_CLASS, KOTLIN_PACKAGE, KOTLIN_SIGNATURE, JETBRAINS_NOT_NULL_ANNOTATION,
099                                               JETBRAINS_NULLABLE_ANNOTATION)) {
100                SPECIAL_ANNOTATIONS.add(JvmClassName.byFqNameWithoutInnerClasses(fqName));
101            }
102            SPECIAL_ANNOTATIONS.add(KotlinSyntheticClass.CLASS_NAME);
103        }
104    
105        public static boolean isSpecialAnnotation(@NotNull FqName fqName) {
106            return isSpecialAnnotation(JvmClassName.byFqNameWithoutInnerClasses(fqName));
107        }
108    
109        public static boolean isSpecialAnnotation(@NotNull JvmClassName name) {
110            return SPECIAL_ANNOTATIONS.contains(name) || name.getInternalName().startsWith("jet/runtime/typeinfo/");
111        }
112    
113        private JvmAnnotationNames() {
114        }
115    }