mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-13 15:52:01 +07:00
project coin: @SafeVarargs applicability
This commit is contained in:
@@ -38,6 +38,7 @@ import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.search.searches.SuperMethodsSearch;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
@@ -1069,6 +1070,72 @@ public class GenericsHighlightUtil {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static HighlightInfo checkSafeVarargsAnnotation(PsiMethod method) {
|
||||
PsiModifierList list = method.getModifierList();
|
||||
final PsiAnnotation safeVarargsAnnotation = list.findAnnotation("java.lang.SafeVarargs");
|
||||
if (safeVarargsAnnotation == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
if (!method.isVarArgs()) {
|
||||
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, safeVarargsAnnotation, "@SafeVarargs is not allowed on methods with fixed arity");
|
||||
}
|
||||
if (!method.hasModifierProperty(PsiModifier.STATIC) && !method.hasModifierProperty(PsiModifier.FINAL)) {
|
||||
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, safeVarargsAnnotation, "@SafeVarargs is not allowed on non-final instance methods");
|
||||
}
|
||||
|
||||
final PsiParameter varParameter = method.getParameterList().getParameters()[method.getParameterList().getParametersCount() - 1];
|
||||
|
||||
for (PsiReference reference : ReferencesSearch.search(varParameter)) {
|
||||
final PsiElement element = reference.getElement();
|
||||
if (element instanceof PsiExpression && !PsiUtil.isAccessedForReading((PsiExpression)element)) {
|
||||
return HighlightInfo.createHighlightInfo(HighlightInfoType.WARNING, element, "@SafeVarargs do not suppress potentially unsafe operations");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
LOG.assertTrue(varParameter.isVarArgs());
|
||||
final PsiEllipsisType ellipsisType = (PsiEllipsisType)varParameter.getType();
|
||||
final PsiType componentType = ellipsisType.getComponentType();
|
||||
if (isReifiableType(componentType)) {
|
||||
return HighlightInfo.createHighlightInfo(HighlightInfoType.WARNING, varParameter.getTypeElement(), "@SafeVarargs is not applicable for reifiable types");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch (IndexNotReadyException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static boolean isReifiableType(PsiType type) {
|
||||
if (type instanceof PsiArrayType) {
|
||||
return isReifiableType(((PsiArrayType)type).getComponentType());
|
||||
}
|
||||
|
||||
if (type instanceof PsiPrimitiveType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (PsiUtil.resolveClassInType(type) instanceof PsiTypeParameter) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type instanceof PsiClassType) {
|
||||
final PsiClassType classType = (PsiClassType)type;
|
||||
if (classType.isRaw()) {
|
||||
return true;
|
||||
}
|
||||
if (!classType.hasParameters()) {
|
||||
return true;
|
||||
}
|
||||
return !classType.hasNonTrivialParameters();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void checkEnumConstantForConstructorProblems(PsiEnumConstant enumConstant, final HighlightInfoHolder holder) {
|
||||
PsiClass containingClass = enumConstant.getContainingClass(); if (enumConstant.getInitializingClass() == null) {
|
||||
HighlightInfo highlightInfo = HighlightClassUtil.checkInstantiationOfAbstractClass(containingClass, enumConstant.getNameIdentifier());
|
||||
|
||||
@@ -556,6 +556,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
|
||||
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkConstructorHandleSuperClassExceptions(method));
|
||||
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkRecursiveConstructorInvocation(method));
|
||||
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkOverrideAnnotation(method));
|
||||
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkSafeVarargsAnnotation(method));
|
||||
if (!myHolder.hasErrorResults() && method.isConstructor()) {
|
||||
myHolder.add(HighlightClassUtil.checkThingNotAllowedInInterface(method, method.getContainingClass()));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import java.util.List;
|
||||
|
||||
@<error descr="'@SafeVarargs' not applicable to type">SafeVarargs</error>
|
||||
public class SafeVarargsTests {
|
||||
//fixed arity
|
||||
<error descr="@SafeVarargs is not allowed on methods with fixed arity">@SafeVarargs</error>
|
||||
public void testNonVarargs1(){}
|
||||
|
||||
<error descr="@SafeVarargs is not allowed on methods with fixed arity">@SafeVarargs</error>
|
||||
public void testNonVarargs2(int <warning descr="Parameter 'i' is never used">i</warning>){}
|
||||
|
||||
<error descr="@SafeVarargs is not allowed on methods with fixed arity">@SafeVarargs</error>
|
||||
public <T> void testNonVarargs3(T <warning descr="Parameter 't' is never used">t</warning>){}
|
||||
|
||||
//non static/non final
|
||||
<error descr="@SafeVarargs is not allowed on non-final instance methods">@SafeVarargs</error>
|
||||
public void testNonVarargs4(int... <warning descr="Parameter 'i' is never used">i</warning>){}
|
||||
|
||||
//reassigned
|
||||
@SafeVarargs
|
||||
public final <T> void testT(T[] tt, T... t) {
|
||||
<warning descr="@SafeVarargs do not suppress potentially unsafe operations">t</warning> = tt;
|
||||
System.out.println(t[0]);
|
||||
}
|
||||
|
||||
//incorrect types
|
||||
@SafeVarargs
|
||||
public final void testString(<warning descr="@SafeVarargs is not applicable for reifiable types">String...</warning> <warning descr="Parameter 'str' is never used">str</warning>){
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final void testStringArray(<warning descr="@SafeVarargs is not applicable for reifiable types">String[]...</warning> <warning descr="Parameter 'str' is never used">str</warning>){
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static void testUnbound(<warning descr="@SafeVarargs is not applicable for reifiable types">List<?>...</warning> <warning descr="Parameter 't' is never used">t</warning>){}
|
||||
|
||||
|
||||
//correct usages
|
||||
@SafeVarargs
|
||||
public static <T> void foo(T... <warning descr="Parameter 't' is never used">t</warning>){}
|
||||
@SafeVarargs
|
||||
public static <T> void foo1(List<T>... <warning descr="Parameter 't' is never used">t</warning>){}
|
||||
@SafeVarargs
|
||||
public static <T> void foo2(List<? extends T>... <warning descr="Parameter 't' is never used">t</warning>){}
|
||||
|
||||
}
|
||||
@@ -152,4 +152,8 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase {
|
||||
public void testMultiCatch() throws Exception {
|
||||
doTest(false, false);
|
||||
}
|
||||
|
||||
public void testSafeVarargsApplicability() throws Exception {
|
||||
doTest(true, false);
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user