annotation method: detect default value based on "default" keyword (IDEA-237774)

GitOrigin-RevId: 64be4b28fc7fe9e79aae58ddba140bd65d56c2ef
This commit is contained in:
Anna Kozlova
2020-04-16 19:41:33 +00:00
committed by intellij-monorepo-bot
parent 022c5be762
commit 947bb691fe
3 changed files with 35 additions and 4 deletions
@@ -20,7 +20,9 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiMethodStub;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.reference.SoftReference;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -63,9 +65,20 @@ public class PsiAnnotationMethodImpl extends PsiMethodImpl implements PsiAnnotat
myCachedDefaultValue = null;
final ASTNode node = getNode().findChildByRole(ChildRole.ANNOTATION_DEFAULT_VALUE);
if (node == null) return null;
return (PsiAnnotationMemberValue)node.getPsi();
boolean expectedDefault = false;
TreeElement childNode = getNode().getFirstChildNode();
while (childNode != null) {
final IElementType type = childNode.getElementType();
if (type == JavaTokenType.DEFAULT_KEYWORD) {
expectedDefault = true;
}
else if (expectedDefault && ElementType.ANNOTATION_MEMBER_VALUE_BIT_SET.contains(type)) {
return (PsiAnnotationMemberValue)childNode.getPsi();
}
childNode = childNode.getTreeNext();
}
return null;
}
@Override
@@ -0,0 +1,17 @@
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnoWithDefaults {
int foo() @Anno[] default {};
int @Anno [] foo1() default {};
String @Anno [] foo2() default {""};
String foo3() @Anno[] default {""};
@Anno String foo4() default "";
}
@Target({ElementType.TYPE, ElementType.TYPE_USE})
@interface Anno {}
@@ -47,6 +47,7 @@ public class AnnotationsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testInvalidPackageAnnotationTarget() { doTest(BASE_PATH + "/package-info.java", false, false); }
public void testPackageAnnotationNotInPackageInfo() { doTest(); }
public void testTypeAnnotations() { doTest(); }
public void testTypeAnnotationsWithCStyleArrays() { doTest(); }
public void testRepeatable() { doTest(); }
public void testEnumValues() { doTest(); }
public void testReceiverParameters() { doTest(); }