Fix class kind detector: prioritize enum over annotations. Introduce new error about enum annotations classes.

This commit is contained in:
Nikolay Krasko
2015-07-16 15:29:19 +03:00
parent b2b8f1aabb
commit 71b406d792
11 changed files with 173 additions and 16 deletions
@@ -66,15 +66,17 @@ abstract public class JetClassOrObject : JetTypeParameterListOwnerStub<KotlinCla
public fun getSecondaryConstructors(): List<JetSecondaryConstructor> = getBody()?.getSecondaryConstructors().orEmpty()
deprecated(value = "It's no more possible to determine it exactly using AST. Use ClassDescriptor methods instead, e.g. getKind()")
public fun isAnnotation(): Boolean = hasAnnotation(KotlinBuiltIns.FQ_NAMES.annotation.shortName().asString())
public fun isAnnotation(): Boolean = getBuiltInAnnotationEntry() != null
private fun hasAnnotation(name: String): Boolean {
for (entry in getAnnotationEntries()) {
public fun getBuiltInAnnotationEntry(): JetAnnotationEntry? = getAnnotation(KotlinBuiltIns.FQ_NAMES.annotation.shortName().asString())
private fun getAnnotation(name: String): JetAnnotationEntry? {
return getAnnotationEntries().firstOrNull() { entry ->
val typeReference = entry.getTypeReference()
val userType = typeReference?.getStubOrPsiChild(JetStubElementTypes.USER_TYPE) ?: continue
if (name == userType.getReferencedName()) return true
val userType = typeReference?.getStubOrPsiChild(JetStubElementTypes.USER_TYPE)
name == userType?.getReferencedName()
}
return false
}
public override fun delete() {
@@ -254,15 +254,20 @@ public class DeclarationsChecker {
checkTraitModifiers(aClass);
checkConstructorInTrait(aClass);
}
else if (classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS) {
checkAnnotationClassWithBody(aClass);
checkValOnAnnotationParameter(aClass);
}
else if (aClass.isEnum()) {
checkEnumModifiers(aClass);
if (aClass.isLocal()) {
trace.report(LOCAL_ENUM_NOT_ALLOWED.on(aClass, classDescriptor));
}
if (classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS) {
JetAnnotationEntry entry = aClass.getBuiltInAnnotationEntry();
assert entry != null : "getBuiltinAnnotationEntry() should be synchronized with isAnnotation()";
trace.report(WRONG_ANNOTATION_TARGET.on(entry, "enum class"));
}
}
else if (classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS) {
checkAnnotationClassWithBody(aClass);
checkValOnAnnotationParameter(aClass);
}
else if (aClass.hasModifier(JetTokens.SEALED_KEYWORD)) {
checkSealedModifiers(aClass);
@@ -19,9 +19,9 @@ package org.jetbrains.kotlin.resolve.lazy.data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.ClassKind;
import org.jetbrains.kotlin.psi.*;
import java.util.List;
import org.jetbrains.kotlin.psi.JetClass;
import org.jetbrains.kotlin.psi.JetEnumEntry;
import org.jetbrains.kotlin.psi.JetTypeParameterList;
public class JetClassInfo extends JetClassOrObjectInfo<JetClass> {
private final ClassKind kind;
@@ -34,12 +34,12 @@ public class JetClassInfo extends JetClassOrObjectInfo<JetClass> {
else if (element.isInterface()) {
this.kind = ClassKind.INTERFACE;
}
else if (element.isAnnotation()) {
this.kind = ClassKind.ANNOTATION_CLASS;
}
else if (element.isEnum()) {
this.kind = ClassKind.ENUM_CLASS;
}
else if (element.isAnnotation()) {
this.kind = ClassKind.ANNOTATION_CLASS;
}
else {
this.kind = ClassKind.CLASS;
}