Removed code duplication for getting annotation argument

This commit is contained in:
Valentin Kipyatkov
2015-05-29 15:51:33 +03:00
parent aefe0dd192
commit f1d8838bbd
4 changed files with 21 additions and 49 deletions
@@ -19,7 +19,9 @@ package org.jetbrains.kotlin.resolve.annotations
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.DescriptorUtils
public fun DeclarationDescriptor.hasInlineAnnotation(): Boolean {
@@ -49,3 +51,12 @@ private fun CallableDescriptor.isPlatformStaticIn(predicate: (DeclarationDescrip
}
else -> predicate(getContainingDeclaration()) && hasPlatformStaticAnnotation()
}
public fun AnnotationDescriptor.argumentValue(parameterName: String): Any? {
return getAllValueArguments().entrySet()
.singleOrNull { it.key.getName().asString() == parameterName }
?.value?.getValue()
}
public fun AnnotationDescriptor.deprecatedAnnotationMessage(): String?
= argumentValue("value") as? String
@@ -30,6 +30,8 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.annotations.argumentValue
import org.jetbrains.kotlin.resolve.annotations.deprecatedAnnotationMessage
public class DeprecatedSymbolValidator : SymbolUsageValidator {
private val JAVA_DEPRECATED = FqName(javaClass<Deprecated>().getName())
@@ -89,28 +91,13 @@ public class DeprecatedSymbolValidator : SymbolUsageValidator {
}
private fun createDeprecationDiagnostic(element: PsiElement, descriptor: DeclarationDescriptor, deprecated: AnnotationDescriptor): Diagnostic {
val message = getMessageFromAnnotationDescriptor(deprecated)
val message = deprecated.deprecatedAnnotationMessage()
return if (message == null)
Errors.DEPRECATED_SYMBOL.on(element, descriptor)
else
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE.on(element, descriptor, message)
}
private fun getMessageFromAnnotationDescriptor(descriptor: AnnotationDescriptor): String? {
val parameterName = Name.identifier("value")
for ((parameterDescriptor, argument) in descriptor.getAllValueArguments()) {
if (parameterDescriptor.getName() == parameterName) {
val parameterValue = argument.getValue()
if (parameterValue is String) {
return parameterValue
}
else
return null
}
}
return null
}
private val PROPERTY_SET_OPERATIONS = TokenSet.create(JetTokens.EQ, JetTokens.PLUSEQ, JetTokens.MINUSEQ, JetTokens.MULTEQ, JetTokens.DIVEQ, JetTokens.PERCEQ, JetTokens.PLUSPLUS, JetTokens.MINUSMINUS)
fun propertyGetterWorkaround(propertyDescriptor: PropertyDescriptor, trace: BindingTrace, expression: PsiElement) {
// property getters do not come as callable yet, so we analyse surroundings to check for deprecation annotation on getter