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.resolve;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
022    import org.jetbrains.kotlin.descriptors.ClassDescriptor;
023    import org.jetbrains.kotlin.descriptors.VariableDescriptor;
024    import org.jetbrains.kotlin.descriptors.annotations.Annotated;
025    import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
026    import org.jetbrains.kotlin.name.FqName;
027    import org.jetbrains.kotlin.psi.JetExpression;
028    import org.jetbrains.kotlin.psi.JetParameter;
029    import org.jetbrains.kotlin.psi.JetTypeReference;
030    import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
031    import org.jetbrains.kotlin.resolve.constants.BooleanValue;
032    import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
033    import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
034    import org.jetbrains.kotlin.types.JetType;
035    import org.jetbrains.kotlin.types.TypeProjection;
036    import org.jetbrains.kotlin.types.TypeUtils;
037    
038    import java.util.Collection;
039    import java.util.List;
040    
041    import static org.jetbrains.kotlin.diagnostics.Errors.INVALID_TYPE_OF_ANNOTATION_MEMBER;
042    import static org.jetbrains.kotlin.diagnostics.Errors.NULLABLE_TYPE_OF_ANNOTATION_MEMBER;
043    import static org.jetbrains.kotlin.resolve.BindingContext.VALUE_PARAMETER;
044    import static org.jetbrains.kotlin.resolve.DescriptorUtils.isAnnotationClass;
045    import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumClass;
046    
047    public class CompileTimeConstantUtils {
048    
049        public static void checkConstructorParametersType(@NotNull List<JetParameter> parameters, @NotNull BindingTrace trace) {
050            for (JetParameter parameter : parameters) {
051                VariableDescriptor parameterDescriptor = trace.getBindingContext().get(VALUE_PARAMETER, parameter);
052                if (parameterDescriptor == null) continue;
053                JetType parameterType = parameterDescriptor.getType();
054                JetTypeReference typeReference = parameter.getTypeReference();
055                if (typeReference != null) {
056                    if (parameterType.isMarkedNullable()) {
057                        trace.report(NULLABLE_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
058                    }
059                    else if (!isAcceptableTypeForAnnotationParameter(parameterType)) {
060                        trace.report(INVALID_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
061                    }
062                }
063            }
064        }
065    
066        private static boolean isAcceptableTypeForAnnotationParameter(@NotNull JetType parameterType) {
067            ClassDescriptor typeDescriptor = TypeUtils.getClassDescriptor(parameterType);
068            if (typeDescriptor == null) {
069                return false;
070            }
071    
072            KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
073            if (isEnumClass(typeDescriptor) ||
074                isAnnotationClass(typeDescriptor) ||
075                isJavaLangClass(typeDescriptor) ||
076                KotlinBuiltIns.isPrimitiveArray(parameterType) ||
077                KotlinBuiltIns.isPrimitiveType(parameterType) ||
078                builtIns.getStringType().equals(parameterType)) {
079                    return true;
080            }
081    
082            if (KotlinBuiltIns.isArray(parameterType)) {
083                List<TypeProjection> arguments = parameterType.getArguments();
084                if (arguments.size() == 1) {
085                    JetType arrayType = arguments.get(0).getType();
086                    if (arrayType.isMarkedNullable()) {
087                        return false;
088                    }
089                    ClassDescriptor arrayTypeDescriptor = TypeUtils.getClassDescriptor(arrayType);
090                    if (arrayTypeDescriptor != null) {
091                        return isEnumClass(arrayTypeDescriptor) ||
092                               isAnnotationClass(arrayTypeDescriptor) ||
093                               isJavaLangClass(arrayTypeDescriptor) ||
094                               builtIns.getStringType().equals(arrayType);
095                    }
096                }
097            }
098            return false;
099        }
100    
101        @Nullable
102        public static String getIntrinsicAnnotationArgument(@NotNull Annotated annotatedDescriptor) {
103            AnnotationDescriptor intrinsicAnnotation =
104                    annotatedDescriptor.getAnnotations().findAnnotation(new FqName("kotlin.jvm.internal.Intrinsic"));
105            if (intrinsicAnnotation == null) return null;
106    
107            Collection<CompileTimeConstant<?>> values = intrinsicAnnotation.getAllValueArguments().values();
108            if (values.isEmpty()) return null;
109    
110            Object value = values.iterator().next().getValue();
111            return value instanceof String ? (String) value : null;
112        }
113    
114        public static boolean isArrayMethodCall(@NotNull ResolvedCall<?> resolvedCall) {
115            return "kotlin.arrays.array".equals(getIntrinsicAnnotationArgument(resolvedCall.getResultingDescriptor().getOriginal()));
116        }
117    
118        public static boolean isJavaClassMethodCall(@NotNull ResolvedCall<?> resolvedCall) {
119            return "kotlin.javaClass.function".equals(getIntrinsicAnnotationArgument(resolvedCall.getResultingDescriptor().getOriginal()));
120        }
121    
122        public static boolean isJavaLangClass(ClassDescriptor descriptor) {
123            return "java.lang.Class".equals(DescriptorUtils.getFqName(descriptor).asString());
124        }
125    
126        public static boolean canBeReducedToBooleanConstant(
127                @Nullable JetExpression expression,
128                @NotNull BindingTrace trace,
129                @Nullable Boolean expectedValue
130        ) {
131            if (expression == null) return false;
132            CompileTimeConstant<?> compileTimeConstant =
133                    ConstantExpressionEvaluator.OBJECT$.evaluate(expression, trace, KotlinBuiltIns.getInstance().getBooleanType());
134            if (!(compileTimeConstant instanceof BooleanValue) || compileTimeConstant.usesVariableAsConstant()) return false;
135    
136            Boolean value = ((BooleanValue) compileTimeConstant).getValue();
137            return expectedValue == null || expectedValue.equals(value);
138        }
139    
140        private CompileTimeConstantUtils() {
141        }
142    }