Assert that reference annotation argument is always an enum value
Rename JavaReferenceAnnotationArgument on that occasion to JavaEnumValueAnnotationArgument. Also add a test on a nested enum annotation argument
This commit is contained in:
+1
-1
@@ -39,7 +39,7 @@ public abstract class JavaAnnotationArgumentImpl<Psi extends PsiAnnotationMember
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (argument instanceof PsiReferenceExpression) {
|
if (argument instanceof PsiReferenceExpression) {
|
||||||
return new JavaReferenceAnnotationArgumentImpl((PsiReferenceExpression) argument, name);
|
return new JavaEnumValueAnnotationArgumentImpl((PsiReferenceExpression) argument, name);
|
||||||
}
|
}
|
||||||
else if (argument instanceof PsiArrayInitializerMemberValue) {
|
else if (argument instanceof PsiArrayInitializerMemberValue) {
|
||||||
return new JavaArrayAnnotationArgumentImpl((PsiArrayInitializerMemberValue) argument, name);
|
return new JavaArrayAnnotationArgumentImpl((PsiArrayInitializerMemberValue) argument, name);
|
||||||
|
|||||||
+11
-12
@@ -22,25 +22,24 @@ import com.intellij.psi.PsiField;
|
|||||||
import com.intellij.psi.PsiReferenceExpression;
|
import com.intellij.psi.PsiReferenceExpression;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.resolve.java.structure.JavaElement;
|
import org.jetbrains.jet.lang.resolve.java.structure.JavaEnumValueAnnotationArgument;
|
||||||
import org.jetbrains.jet.lang.resolve.java.structure.JavaReferenceAnnotationArgument;
|
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
|
|
||||||
public class JavaReferenceAnnotationArgumentImpl extends JavaAnnotationArgumentImpl<PsiReferenceExpression>
|
public class JavaEnumValueAnnotationArgumentImpl extends JavaAnnotationArgumentImpl<PsiReferenceExpression>
|
||||||
implements JavaReferenceAnnotationArgument {
|
implements JavaEnumValueAnnotationArgument {
|
||||||
protected JavaReferenceAnnotationArgumentImpl(@NotNull PsiReferenceExpression psiReferenceExpression, @Nullable Name name) {
|
protected JavaEnumValueAnnotationArgumentImpl(@NotNull PsiReferenceExpression psiReferenceExpression, @Nullable Name name) {
|
||||||
super(psiReferenceExpression, name);
|
super(psiReferenceExpression, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public JavaElement resolve() {
|
public JavaField resolve() {
|
||||||
PsiReferenceExpression expression = getPsi();
|
PsiElement element = getPsi().resolve();
|
||||||
PsiElement element = expression.resolve();
|
if (element == null) return null;
|
||||||
if (element instanceof PsiEnumConstant) {
|
if (!(element instanceof PsiEnumConstant)) {
|
||||||
return new JavaFieldImpl((PsiField) element);
|
throw new IllegalStateException("Reference argument should be an enum value, but was " + element + ": " + element.getText());
|
||||||
}
|
}
|
||||||
// TODO: other types of references
|
return new JavaFieldImpl((PsiField) element);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
public class NestedEnumArgument {
|
||||||
|
public enum E {
|
||||||
|
FIRST
|
||||||
|
}
|
||||||
|
|
||||||
|
@interface Anno {
|
||||||
|
E value();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Anno(E.FIRST)
|
||||||
|
void foo() {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
public open class NestedEnumArgument {
|
||||||
|
public constructor NestedEnumArgument()
|
||||||
|
test.NestedEnumArgument.Anno(value = E.FIRST: test.NestedEnumArgument.E) public/*package*/ open fun foo(): kotlin.Unit
|
||||||
|
|
||||||
|
public/*package*/ final annotation class Anno : kotlin.Annotation {
|
||||||
|
public/*package*/ constructor Anno(/*0*/ value: test.NestedEnumArgument.E)
|
||||||
|
public abstract fun value(): test.NestedEnumArgument.E
|
||||||
|
}
|
||||||
|
|
||||||
|
public final enum class E : kotlin.Enum<test.NestedEnumArgument.E> {
|
||||||
|
private constructor E()
|
||||||
|
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
|
||||||
|
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
|
||||||
|
|
||||||
|
public enum entry FIRST : test.NestedEnumArgument.E {
|
||||||
|
private constructor FIRST()
|
||||||
|
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
|
||||||
|
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
|
||||||
|
|
||||||
|
public class object <class-object-for-FIRST> : test.NestedEnumArgument.E.FIRST {
|
||||||
|
private constructor <class-object-for-FIRST>()
|
||||||
|
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
|
||||||
|
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Static members
|
||||||
|
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): test.NestedEnumArgument.E
|
||||||
|
public final /*synthesized*/ fun values(): kotlin.Array<test.NestedEnumArgument.E>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -374,6 +374,12 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
|
|||||||
doTestCompiledJava(fileName);
|
doTestCompiledJava(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("NestedEnumArgument.java")
|
||||||
|
public void testNestedEnumArgument() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/annotations/NestedEnumArgument.java");
|
||||||
|
doTestCompiledJava(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("PrimitiveValueInParam.java")
|
@TestMetadata("PrimitiveValueInParam.java")
|
||||||
public void testPrimitiveValueInParam() throws Exception {
|
public void testPrimitiveValueInParam() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/annotations/PrimitiveValueInParam.java");
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledJava/annotations/PrimitiveValueInParam.java");
|
||||||
|
|||||||
+3
-5
@@ -100,7 +100,7 @@ class LazyJavaAnnotationDescriptor(
|
|||||||
private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): CompileTimeConstant<*>? {
|
private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): CompileTimeConstant<*>? {
|
||||||
return when (argument) {
|
return when (argument) {
|
||||||
is JavaLiteralAnnotationArgument -> createCompileTimeConstant(argument.getValue(), true, false, false, null)
|
is JavaLiteralAnnotationArgument -> createCompileTimeConstant(argument.getValue(), true, false, false, null)
|
||||||
is JavaReferenceAnnotationArgument -> resolveFromReference(argument.resolve())
|
is JavaEnumValueAnnotationArgument -> resolveFromEnumValue(argument.resolve())
|
||||||
is JavaArrayAnnotationArgument -> resolveFromArray(argument.getName() ?: DEFAULT_ANNOTATION_MEMBER_NAME, argument.getElements())
|
is JavaArrayAnnotationArgument -> resolveFromArray(argument.getName() ?: DEFAULT_ANNOTATION_MEMBER_NAME, argument.getElements())
|
||||||
is JavaAnnotationAsAnnotationArgument -> resolveFromAnnotation(argument.getAnnotation())
|
is JavaAnnotationAsAnnotationArgument -> resolveFromAnnotation(argument.getAnnotation())
|
||||||
is JavaClassObjectAnnotationArgument -> resolveFromJavaClassObjectType(argument.getReferencedType())
|
is JavaClassObjectAnnotationArgument -> resolveFromJavaClassObjectType(argument.getReferencedType())
|
||||||
@@ -131,10 +131,8 @@ class LazyJavaAnnotationDescriptor(
|
|||||||
return ArrayValue(values, valueParameter.getType(), true, values.any { it.usesVariableAsConstant() })
|
return ArrayValue(values, valueParameter.getType(), true, values.any { it.usesVariableAsConstant() })
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resolveFromReference(element: JavaElement?): CompileTimeConstant<*>? {
|
private fun resolveFromEnumValue(element: JavaField?): CompileTimeConstant<*>? {
|
||||||
if (element !is JavaField) return null
|
if (element == null || !element.isEnumEntry()) return null
|
||||||
|
|
||||||
if (!element.isEnumEntry()) return null
|
|
||||||
|
|
||||||
val containingJavaClass = element.getContainingClass()
|
val containingJavaClass = element.getContainingClass()
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -18,7 +18,7 @@ package org.jetbrains.jet.lang.resolve.java.structure;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public interface JavaReferenceAnnotationArgument extends JavaAnnotationArgument {
|
public interface JavaEnumValueAnnotationArgument extends JavaAnnotationArgument {
|
||||||
@Nullable
|
@Nullable
|
||||||
JavaElement resolve();
|
JavaField resolve();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user