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