From 14788213a2b1061fe824b86af7596ce2491708d6 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Fri, 4 Jul 2014 12:32:48 +0400 Subject: [PATCH] Annotation parameter processing util --- .../impl/SimpleFunctionDescriptorImpl.java | 2 +- .../jet/lang/types/lang/AnnotationUtil.kt | 49 +++++++++++++++++ .../jet/lang/types/lang/InlineUtil.java | 53 ++++++------------- .../plugin/debugger/JetPositionManager.java | 2 +- 4 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 core/descriptors/src/org/jetbrains/jet/lang/types/lang/AnnotationUtil.kt diff --git a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SimpleFunctionDescriptorImpl.java b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SimpleFunctionDescriptorImpl.java index f16d62d9392..e9bc0a80211 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SimpleFunctionDescriptorImpl.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/SimpleFunctionDescriptorImpl.java @@ -65,7 +65,7 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme ) { super.initialize(receiverParameterType, expectedThisObject, typeParameters, unsubstitutedValueParameters, unsubstitutedReturnType, modality, visibility); - this.inlineStrategy = InlineUtil.getInlineType(getAnnotations()); + this.inlineStrategy = InlineUtil.getInlineType(this); return this; } diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/AnnotationUtil.kt b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/AnnotationUtil.kt new file mode 100644 index 00000000000..2320e316e28 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/AnnotationUtil.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.lang.types.lang + +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor +import org.jetbrains.jet.lang.descriptors.ClassDescriptor +import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor +import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant +import org.jetbrains.jet.lang.descriptors.annotations.Annotations + +public object AnnotationUtil { + + public fun getAnnotationSingleArgument(descriptor: DeclarationDescriptor, annotationClass: ClassDescriptor) : CompileTimeConstant? { + val annotation = getAnnotation(descriptor.getAnnotations(), annotationClass) + + if (annotation != null) { + val parameterDescriptor = annotationClass.getConstructors().elementAt(0).getValueParameters().first + if (parameterDescriptor != null) { + return annotation.getValueArgument(parameterDescriptor) + } + } + return null + } + + public fun getAnnotation(annotations: Annotations, annotationClass: ClassDescriptor): AnnotationDescriptor? { + for (annotation in annotations) { + if (annotationClass == annotation.getType().getConstructor().getDeclarationDescriptor()) { + return annotation + } + } + return null + } + + +} \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/InlineUtil.java b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/InlineUtil.java index 8badc9e74b1..9810dcadf71 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/lang/InlineUtil.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/lang/InlineUtil.java @@ -17,12 +17,11 @@ package org.jetbrains.jet.lang.types.lang; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; -import org.jetbrains.jet.lang.descriptors.annotations.Annotations; import org.jetbrains.jet.lang.resolve.constants.ArrayValue; import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant; import org.jetbrains.jet.lang.resolve.constants.EnumValue; @@ -37,13 +36,11 @@ public class InlineUtil { } @NotNull - public static InlineStrategy getInlineType(@Nullable Annotations annotations) { - KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance(); - ClassDescriptor annotationClass = builtIns.getInlineClassAnnotation(); - AnnotationDescriptor annotation = getAnnotation(annotations, annotationClass); + public static InlineStrategy getInlineType(@NotNull DeclarationDescriptor descriptor) { + ClassDescriptor annotationClass = KotlinBuiltIns.getInstance().getInlineClassAnnotation(); + AnnotationDescriptor annotation = AnnotationUtil.instance$.getAnnotation(descriptor.getAnnotations(), annotationClass); if (annotation != null) { - ValueParameterDescriptor parameterDescriptor = annotationClass.getConstructors().iterator().next().getValueParameters().get(0); - CompileTimeConstant argument = annotation.getValueArgument(parameterDescriptor); + CompileTimeConstant argument = AnnotationUtil.instance$.getAnnotationSingleArgument(descriptor, annotationClass); if (argument == null) { //default parameter return InlineStrategy.AS_FUNCTION; @@ -59,18 +56,6 @@ public class InlineUtil { } } - @Nullable - private static AnnotationDescriptor getAnnotation(@Nullable Annotations annotations, @NotNull ClassDescriptor annotationClass) { - if (annotations != null) { - for (AnnotationDescriptor annotation : annotations) { - if (annotationClass.equals(annotation.getType().getConstructor().getDeclarationDescriptor())) { - return annotation; - } - } - } - return null; - } - public static boolean hasOnlyLocalContinueAndBreak(@NotNull ValueParameterDescriptor descriptor) { return hasInlineOption(descriptor, InlineOption.LOCAL_CONTINUE_AND_BREAK); } @@ -80,30 +65,22 @@ public class InlineUtil { } private static boolean hasInlineOption(@NotNull ValueParameterDescriptor descriptor, @NotNull InlineOption option) { - KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance(); - ClassDescriptor annotationClass = builtIns.getInlineOptionsClassAnnotation(); - AnnotationDescriptor optionsAnnotation = getAnnotation(descriptor.getAnnotations(), annotationClass); + CompileTimeConstant argument = + AnnotationUtil.instance$.getAnnotationSingleArgument(descriptor, KotlinBuiltIns.getInstance() + .getInlineOptionsClassAnnotation()); - if (optionsAnnotation != null) { - ValueParameterDescriptor parameterDescriptor = annotationClass.getConstructors().iterator().next().getValueParameters().get(0); - CompileTimeConstant argument = optionsAnnotation.getValueArgument(parameterDescriptor); + if (argument instanceof ArrayValue) { + List> values = ((ArrayValue) argument).getValue(); - if (argument == null) { - return false; - } else { - if (argument instanceof ArrayValue) { - List> values = ((ArrayValue) argument).getValue(); - - for (CompileTimeConstant value : values) { - if (value instanceof EnumValue) { - if (((EnumValue) value).getValue().getName().asString().equals(option.name())) { - return true; - } - } + for (CompileTimeConstant value : values) { + if (value instanceof EnumValue) { + if (((EnumValue) value).getValue().getName().asString().equals(option.name())) { + return true; } } } } + return false; } } diff --git a/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java b/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java index 48a8e777b92..5a7099236f9 100644 --- a/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java +++ b/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java @@ -293,7 +293,7 @@ public class JetPositionManager implements PositionManager { if (call == null) return false; - InlineStrategy inlineType = InlineUtil.getInlineType(call.getResultingDescriptor().getAnnotations()); + InlineStrategy inlineType = InlineUtil.getInlineType(call.getResultingDescriptor()); if (!inlineType.isInline()) return false; for (Map.Entry entry : call.getValueArguments().entrySet()) {