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;
}
@@ -0,0 +1,3 @@
data <!WRONG_ANNOTATION_TARGET!>annotation<!> enum class E {
D
}
@@ -0,0 +1,27 @@
package
kotlin.data() kotlin.annotation.annotation() internal final enum class E : kotlin.Enum<E> {
public enum entry D : E {
private constructor D()
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E): kotlin.Int
public final override /*1*/ /*fake_override*/ fun copy(): E
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
private constructor E()
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E): kotlin.Int
public final /*synthesized*/ fun copy(): E
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): E
public final /*synthesized*/ fun values(): kotlin.Array<E>
}
+3
View File
@@ -0,0 +1,3 @@
data annotation enum class E {
D
}
+35
View File
@@ -0,0 +1,35 @@
JetFile: EnumWithAnnotationKeyword.kt
PACKAGE_DIRECTIVE
<empty list>
IMPORT_LIST
<empty list>
CLASS
MODIFIER_LIST
ANNOTATION_ENTRY
CONSTRUCTOR_CALLEE
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('data')
PsiWhiteSpace(' ')
ANNOTATION_ENTRY
CONSTRUCTOR_CALLEE
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('annotation')
PsiWhiteSpace(' ')
PsiElement(enum)('enum')
PsiWhiteSpace(' ')
PsiElement(class)('class')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('E')
PsiWhiteSpace(' ')
CLASS_BODY
PsiElement(LBRACE)('{')
PsiWhiteSpace('\n ')
ENUM_ENTRY
OBJECT_DECLARATION_NAME
PsiElement(IDENTIFIER)('D')
PsiWhiteSpace('\n')
PsiElement(RBRACE)('}')
+7
View File
@@ -0,0 +1,7 @@
enum interface class E1 {
D
}
interface enum class E2 {
D
}
+57
View File
@@ -0,0 +1,57 @@
JetFile: InterfaceWithEnumKeyword.kt
PACKAGE_DIRECTIVE
<empty list>
IMPORT_LIST
<empty list>
CLASS
MODIFIER_LIST
PsiElement(enum)('enum')
PsiWhiteSpace(' ')
PsiElement(interface)('interface')
PsiErrorElement:Name expected
<empty list>
PsiWhiteSpace(' ')
CLASS
PsiElement(class)('class')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('E1')
PsiWhiteSpace(' ')
CLASS_BODY
PsiElement(LBRACE)('{')
PsiWhiteSpace('\n ')
MODIFIER_LIST
ANNOTATION_ENTRY
CONSTRUCTOR_CALLEE
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('D')
PsiErrorElement:Expecting member declaration
<empty list>
PsiWhiteSpace('\n')
PsiElement(RBRACE)('}')
PsiWhiteSpace('\n\n')
CLASS
PsiElement(interface)('interface')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('enum')
PsiWhiteSpace(' ')
CLASS
PsiElement(class)('class')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('E2')
PsiWhiteSpace(' ')
CLASS_BODY
PsiElement(LBRACE)('{')
PsiWhiteSpace('\n ')
MODIFIER_LIST
ANNOTATION_ENTRY
CONSTRUCTOR_CALLEE
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('D')
PsiErrorElement:Expecting member declaration
<empty list>
PsiWhiteSpace('\n')
PsiElement(RBRACE)('}')
@@ -5133,6 +5133,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("enumWithAnnotationKeyword.kt")
public void testEnumWithAnnotationKeyword() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/enumWithAnnotationKeyword.kt");
doTest(fileName);
}
@TestMetadata("enumWithEmptyName.kt")
public void testEnumWithEmptyName() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/enumWithEmptyName.kt");
@@ -259,6 +259,12 @@ public class JetParsingTestGenerated extends AbstractJetParsingTest {
doParsingTest(fileName);
}
@TestMetadata("EnumWithAnnotationKeyword.kt")
public void testEnumWithAnnotationKeyword() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/psi/EnumWithAnnotationKeyword.kt");
doParsingTest(fileName);
}
@TestMetadata("Enums.kt")
public void testEnums() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/psi/Enums.kt");
@@ -415,6 +421,12 @@ public class JetParsingTestGenerated extends AbstractJetParsingTest {
doParsingTest(fileName);
}
@TestMetadata("InterfaceWithEnumKeyword.kt")
public void testInterfaceWithEnumKeyword() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/psi/InterfaceWithEnumKeyword.kt");
doParsingTest(fileName);
}
@TestMetadata("Labels.kt")
public void testLabels() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/psi/Labels.kt");