Refactoring: AnnotationTarget / AnnotationRetention renamed to KotlinTarget / KotlinRetention
(for not clashing with the same built-in classes)
This commit is contained in:
@@ -168,26 +168,26 @@ public abstract class AnnotationCodegen {
|
||||
generateAnnotationIfNotPresent(annotationDescriptorsAlreadyPresent, annotationClass);
|
||||
}
|
||||
|
||||
private static final Map<AnnotationTarget, ElementType> annotationTargetMap =
|
||||
new EnumMap<AnnotationTarget, ElementType>(AnnotationTarget.class);
|
||||
private static final Map<KotlinTarget, ElementType> annotationTargetMap =
|
||||
new EnumMap<KotlinTarget, ElementType>(KotlinTarget.class);
|
||||
|
||||
static {
|
||||
annotationTargetMap.put(AnnotationTarget.PACKAGE, ElementType.PACKAGE);
|
||||
annotationTargetMap.put(AnnotationTarget.CLASSIFIER, ElementType.TYPE);
|
||||
annotationTargetMap.put(AnnotationTarget.ANNOTATION_CLASS, ElementType.ANNOTATION_TYPE);
|
||||
annotationTargetMap.put(AnnotationTarget.CONSTRUCTOR, ElementType.CONSTRUCTOR);
|
||||
annotationTargetMap.put(AnnotationTarget.LOCAL_VARIABLE, ElementType.LOCAL_VARIABLE);
|
||||
annotationTargetMap.put(AnnotationTarget.FUNCTION, ElementType.METHOD);
|
||||
annotationTargetMap.put(AnnotationTarget.PROPERTY_GETTER, ElementType.METHOD);
|
||||
annotationTargetMap.put(AnnotationTarget.PROPERTY_SETTER, ElementType.METHOD);
|
||||
annotationTargetMap.put(AnnotationTarget.FIELD, ElementType.FIELD);
|
||||
annotationTargetMap.put(AnnotationTarget.VALUE_PARAMETER, ElementType.PARAMETER);
|
||||
annotationTargetMap.put(KotlinTarget.PACKAGE, ElementType.PACKAGE);
|
||||
annotationTargetMap.put(KotlinTarget.CLASSIFIER, ElementType.TYPE);
|
||||
annotationTargetMap.put(KotlinTarget.ANNOTATION_CLASS, ElementType.ANNOTATION_TYPE);
|
||||
annotationTargetMap.put(KotlinTarget.CONSTRUCTOR, ElementType.CONSTRUCTOR);
|
||||
annotationTargetMap.put(KotlinTarget.LOCAL_VARIABLE, ElementType.LOCAL_VARIABLE);
|
||||
annotationTargetMap.put(KotlinTarget.FUNCTION, ElementType.METHOD);
|
||||
annotationTargetMap.put(KotlinTarget.PROPERTY_GETTER, ElementType.METHOD);
|
||||
annotationTargetMap.put(KotlinTarget.PROPERTY_SETTER, ElementType.METHOD);
|
||||
annotationTargetMap.put(KotlinTarget.FIELD, ElementType.FIELD);
|
||||
annotationTargetMap.put(KotlinTarget.VALUE_PARAMETER, ElementType.PARAMETER);
|
||||
}
|
||||
|
||||
private void generateTargetAnnotation(@NotNull ClassDescriptor classDescriptor, @NotNull Set<String> annotationDescriptorsAlreadyPresent) {
|
||||
String descriptor = Type.getType(Target.class).getDescriptor();
|
||||
if (!annotationDescriptorsAlreadyPresent.add(descriptor)) return;
|
||||
Set<AnnotationTarget> targets = AnnotationTargetChecker.INSTANCE$.possibleTargetSet(classDescriptor);
|
||||
Set<KotlinTarget> targets = AnnotationTargetChecker.INSTANCE$.possibleTargetSet(classDescriptor);
|
||||
Set<ElementType> javaTargets;
|
||||
if (targets == null) {
|
||||
javaTargets = getJavaTargetList(classDescriptor);
|
||||
@@ -195,7 +195,7 @@ public abstract class AnnotationCodegen {
|
||||
}
|
||||
else {
|
||||
javaTargets = EnumSet.noneOf(ElementType.class);
|
||||
for (AnnotationTarget target : targets) {
|
||||
for (KotlinTarget target : targets) {
|
||||
if (annotationTargetMap.get(target) == null) continue;
|
||||
javaTargets.add(annotationTargetMap.get(target));
|
||||
}
|
||||
@@ -368,13 +368,13 @@ public abstract class AnnotationCodegen {
|
||||
value.accept(argumentVisitor, null);
|
||||
}
|
||||
|
||||
private static final Map<AnnotationRetention, RetentionPolicy> annotationRetentionMap =
|
||||
new EnumMap<AnnotationRetention, RetentionPolicy>(AnnotationRetention.class);
|
||||
private static final Map<KotlinRetention, RetentionPolicy> annotationRetentionMap =
|
||||
new EnumMap<KotlinRetention, RetentionPolicy>(KotlinRetention.class);
|
||||
|
||||
static {
|
||||
annotationRetentionMap.put(AnnotationRetention.SOURCE, RetentionPolicy.SOURCE);
|
||||
annotationRetentionMap.put(AnnotationRetention.BINARY, RetentionPolicy.CLASS);
|
||||
annotationRetentionMap.put(AnnotationRetention.RUNTIME, RetentionPolicy.RUNTIME);
|
||||
annotationRetentionMap.put(KotlinRetention.SOURCE, RetentionPolicy.SOURCE);
|
||||
annotationRetentionMap.put(KotlinRetention.BINARY, RetentionPolicy.CLASS);
|
||||
annotationRetentionMap.put(KotlinRetention.RUNTIME, RetentionPolicy.RUNTIME);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -415,7 +415,7 @@ public abstract class AnnotationCodegen {
|
||||
JetType classObjectType = getClassObjectType(enumEntry);
|
||||
if (classObjectType != null) {
|
||||
if ("kotlin/annotation/AnnotationRetention".equals(typeMapper.mapType(classObjectType).getInternalName())) {
|
||||
AnnotationRetention retention = AnnotationRetention.valueOf(enumEntry.getName().asString());
|
||||
KotlinRetention retention = KotlinRetention.valueOf(enumEntry.getName().asString());
|
||||
return annotationRetentionMap.get(retention);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.types.JetType
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
import java.lang.annotation.ElementType
|
||||
import java.util.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationTarget
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||
import kotlin.annotation
|
||||
|
||||
public object AnnotationTargetChecker {
|
||||
@@ -62,7 +62,7 @@ public object AnnotationTargetChecker {
|
||||
|
||||
public fun checkExpression(expression: JetExpression, trace: BindingTrace) {
|
||||
for (entry in expression.getAnnotationEntries()) {
|
||||
checkAnnotationEntry(entry, listOf(AnnotationTarget.EXPRESSION), trace)
|
||||
checkAnnotationEntry(entry, listOf(KotlinTarget.EXPRESSION), trace)
|
||||
}
|
||||
if (expression is JetFunctionLiteralExpression) {
|
||||
for (parameter in expression.getValueParameters()) {
|
||||
@@ -71,55 +71,55 @@ public object AnnotationTargetChecker {
|
||||
}
|
||||
}
|
||||
|
||||
public fun possibleTargetSet(classDescriptor: ClassDescriptor): Set<AnnotationTarget>? {
|
||||
public fun possibleTargetSet(classDescriptor: ClassDescriptor): Set<KotlinTarget>? {
|
||||
val targetEntryDescriptor = classDescriptor.getAnnotations().findAnnotation(KotlinBuiltIns.FQ_NAMES.target)
|
||||
?: return null
|
||||
val valueArguments = targetEntryDescriptor.getAllValueArguments()
|
||||
val valueArgument = valueArguments.entrySet().firstOrNull()?.getValue() as? ArrayValue ?: return null
|
||||
return valueArgument.value.filterIsInstance<EnumValue>().map {
|
||||
AnnotationTarget.valueOrNull(it.value.getName().asString())
|
||||
KotlinTarget.valueOrNull(it.value.getName().asString())
|
||||
}.filterNotNull().toSet()
|
||||
}
|
||||
|
||||
private fun possibleTargetSet(entry: JetAnnotationEntry, trace: BindingTrace): Set<AnnotationTarget> {
|
||||
val descriptor = trace.get(BindingContext.ANNOTATION, entry) ?: return AnnotationTarget.DEFAULT_TARGET_SET
|
||||
private fun possibleTargetSet(entry: JetAnnotationEntry, trace: BindingTrace): Set<KotlinTarget> {
|
||||
val descriptor = trace.get(BindingContext.ANNOTATION, entry) ?: return KotlinTarget.DEFAULT_TARGET_SET
|
||||
// For descriptor with error type, all targets are considered as possible
|
||||
if (descriptor.getType().isError()) return AnnotationTarget.ALL_TARGET_SET
|
||||
val classDescriptor = TypeUtils.getClassDescriptor(descriptor.getType()) ?: return AnnotationTarget.DEFAULT_TARGET_SET
|
||||
return possibleTargetSet(classDescriptor) ?: AnnotationTarget.DEFAULT_TARGET_SET
|
||||
if (descriptor.getType().isError()) return KotlinTarget.ALL_TARGET_SET
|
||||
val classDescriptor = TypeUtils.getClassDescriptor(descriptor.getType()) ?: return KotlinTarget.DEFAULT_TARGET_SET
|
||||
return possibleTargetSet(classDescriptor) ?: KotlinTarget.DEFAULT_TARGET_SET
|
||||
}
|
||||
|
||||
private fun checkAnnotationEntry(entry: JetAnnotationEntry, actualTargets: List<AnnotationTarget>, trace: BindingTrace) {
|
||||
private fun checkAnnotationEntry(entry: JetAnnotationEntry, actualTargets: List<KotlinTarget>, trace: BindingTrace) {
|
||||
val possibleTargets = possibleTargetSet(entry, trace)
|
||||
if (actualTargets.any { it in possibleTargets }) return
|
||||
trace.report(Errors.WRONG_ANNOTATION_TARGET.on(entry, actualTargets.firstOrNull()?.description ?: "unidentified target"))
|
||||
}
|
||||
|
||||
private fun getActualTargetList(annotated: JetAnnotated, descriptor: ClassDescriptor?): List<AnnotationTarget> {
|
||||
private fun getActualTargetList(annotated: JetAnnotated, descriptor: ClassDescriptor?): List<KotlinTarget> {
|
||||
if (annotated is JetClassOrObject) {
|
||||
if (annotated is JetEnumEntry) return listOf(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
|
||||
if (annotated is JetEnumEntry) return listOf(KotlinTarget.PROPERTY, KotlinTarget.FIELD)
|
||||
return if (descriptor?.getKind() == ClassKind.ANNOTATION_CLASS) {
|
||||
listOf(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASSIFIER)
|
||||
listOf(KotlinTarget.ANNOTATION_CLASS, KotlinTarget.CLASSIFIER)
|
||||
}
|
||||
else {
|
||||
listOf(AnnotationTarget.CLASSIFIER)
|
||||
listOf(KotlinTarget.CLASSIFIER)
|
||||
}
|
||||
}
|
||||
if (annotated is JetProperty) {
|
||||
return if (annotated.isLocal()) listOf(AnnotationTarget.LOCAL_VARIABLE) else listOf(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
|
||||
return if (annotated.isLocal()) listOf(KotlinTarget.LOCAL_VARIABLE) else listOf(KotlinTarget.PROPERTY, KotlinTarget.FIELD)
|
||||
}
|
||||
if (annotated is JetParameter) {
|
||||
return if (annotated.hasValOrVar()) listOf(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD) else listOf(AnnotationTarget.VALUE_PARAMETER)
|
||||
return if (annotated.hasValOrVar()) listOf(KotlinTarget.PROPERTY, KotlinTarget.FIELD) else listOf(KotlinTarget.VALUE_PARAMETER)
|
||||
}
|
||||
if (annotated is JetConstructor<*>) return listOf(AnnotationTarget.CONSTRUCTOR)
|
||||
if (annotated is JetFunction) return listOf(AnnotationTarget.FUNCTION)
|
||||
if (annotated is JetConstructor<*>) return listOf(KotlinTarget.CONSTRUCTOR)
|
||||
if (annotated is JetFunction) return listOf(KotlinTarget.FUNCTION)
|
||||
if (annotated is JetPropertyAccessor) {
|
||||
return if (annotated.isGetter()) listOf(AnnotationTarget.PROPERTY_GETTER) else listOf(AnnotationTarget.PROPERTY_SETTER)
|
||||
return if (annotated.isGetter()) listOf(KotlinTarget.PROPERTY_GETTER) else listOf(KotlinTarget.PROPERTY_SETTER)
|
||||
}
|
||||
if (annotated is JetPackageDirective) return listOf(AnnotationTarget.PACKAGE)
|
||||
if (annotated is JetTypeReference) return listOf(AnnotationTarget.TYPE)
|
||||
if (annotated is JetFile) return listOf(AnnotationTarget.FILE)
|
||||
if (annotated is JetTypeParameter) return listOf(AnnotationTarget.TYPE_PARAMETER)
|
||||
if (annotated is JetPackageDirective) return listOf(KotlinTarget.PACKAGE)
|
||||
if (annotated is JetTypeReference) return listOf(KotlinTarget.TYPE)
|
||||
if (annotated is JetFile) return listOf(KotlinTarget.FILE)
|
||||
if (annotated is JetTypeParameter) return listOf(KotlinTarget.TYPE_PARAMETER)
|
||||
return listOf()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user