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