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