IDEA-96228 (restrict annotation owner at lower language levels)

This commit is contained in:
Roman Shevchenko
2012-11-30 20:18:28 +01:00
parent 1c95e8a529
commit c0d2012934
3 changed files with 25 additions and 4 deletions

View File

@@ -27,6 +27,7 @@ import com.intellij.psi.impl.source.JavaStubPsiElement;
import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.PairFunction;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -80,7 +81,8 @@ public class PsiAnnotationImpl extends JavaStubPsiElement<PsiAnnotationStub> imp
@Override
public <T extends PsiAnnotationMemberValue> T setDeclaredAttributeValue(@NonNls String attributeName, @Nullable T value) {
return (T)PsiImplUtil.setDeclaredAttributeValue(this, attributeName, value, ANNOTATION_CREATOR);
@SuppressWarnings("unchecked") T t = (T)PsiImplUtil.setDeclaredAttributeValue(this, attributeName, value, ANNOTATION_CREATOR);
return t;
}
public String toString() {
@@ -94,7 +96,8 @@ public class PsiAnnotationImpl extends JavaStubPsiElement<PsiAnnotationStub> imp
}
@Override
@Nullable public String getQualifiedName() {
@Nullable
public String getQualifiedName() {
final PsiJavaCodeReferenceElement nameRef = getNameReferenceElement();
if (nameRef == null) return null;
return nameRef.getCanonicalText();
@@ -119,6 +122,10 @@ public class PsiAnnotationImpl extends JavaStubPsiElement<PsiAnnotationStub> imp
@Override
public PsiAnnotationOwner getOwner() {
PsiElement parent = getParent();
if (!PsiUtil.isLanguageLevel8OrHigher(this)) {
return parent instanceof PsiModifierList ? (PsiAnnotationOwner)parent : null;
}
if (parent instanceof PsiTypeElement) {
return ((PsiTypeElement)parent).getOwner(this);
}

View File

@@ -1,3 +1,5 @@
import java.util.Collection;
@interface Anno {
Anno[] nested() default {};
}
@@ -13,4 +15,12 @@ abstract class C {
void notWrong() { }
}
class B extends <error descr="Annotations are not allowed here">@Deprecated</error> Object{}
class B extends <error descr="Annotations are not allowed here">@Deprecated</error> Object { }
enum E {
@Anno E1
}
interface I {
@<error descr="Duplicate annotation">Anno</error> public @<error descr="Duplicate annotation">Anno</error> Collection<<error descr="Annotations are not allowed here">@Anno</error> String> method(@<error descr="Duplicate annotation">Anno</error> @<error descr="Duplicate annotation">Anno</error> Object o);
}

View File

@@ -15,6 +15,7 @@
*/
package com.intellij.codeInsight.daemon;
import com.intellij.pom.java.LanguageLevel;
import org.jetbrains.annotations.NonNls;
/**
@@ -25,6 +26,7 @@ public class AnnotationsHighlightingTest extends LightDaemonAnalyzerTestCase {
private static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/annotations";
private void doTest(boolean checkWarnings) {
setLanguageLevel(LanguageLevel.JDK_1_7);
doTest(BASE_PATH + "/" + getTestName(true) + ".java", checkWarnings, false);
}
@@ -43,7 +45,9 @@ public class AnnotationsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testInapplicable() { doTest(false); }
public void testDuplicateAttribute() { doTest(false); }
public void testDuplicateTarget() { doTest(false); }
//public void testTypeAnnotations() { doTest(false); }
public void testInvalidPackageAnnotationTarget() { doTest(BASE_PATH + "/" + getTestName(true) + "/package-info.java", false, false); }
public void testPackageAnnotationNotInPackageInfo() { doTest(BASE_PATH + "/" + getTestName(true) + "/notPackageInfo.java", false, false); }
//public void testTypeAnnotations() { doTest(false); } // todo[r.sh] make separate test with correct language level
}