IDEA-91665 (highlight extension methods outside interfaces)

This commit is contained in:
Roman Shevchenko
2012-09-17 17:12:37 +04:00
parent 27079e6adf
commit f51b828729
3 changed files with 13 additions and 3 deletions
@@ -850,16 +850,21 @@ public class HighlightMethodUtil {
static HighlightInfo checkMethodCanHaveBody(PsiMethod method) {
if (method.getBody() == null) return null;
PsiClass aClass = method.getContainingClass();
boolean isInterface = aClass != null && aClass.isInterface();
boolean isExtension = PsiUtil.isExtensionMethod(method);
String message = null;
if (aClass != null && aClass.isInterface()) {
if (!PsiUtil.isExtensionMethod(method)) {
if (isInterface) {
if (!isExtension) {
message = JavaErrorMessages.message("interface.methods.cannot.have.body");
}
else {
return HighlightUtil.checkExtensionMethodsFeature(method);
}
}
else if (isExtension) {
message = JavaErrorMessages.message("extension.method.in.class");
}
else if (method.hasModifierProperty(PsiModifier.ABSTRACT)) {
message = JavaErrorMessages.message("abstract.methods.cannot.have.a.body");
}
@@ -871,7 +876,7 @@ public class HighlightMethodUtil {
TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, textRange, message);
QuickFixAction.registerQuickFixAction(info, new DeleteMethodBodyFix(method));
if (method.hasModifierProperty(PsiModifier.ABSTRACT) && aClass != null && !aClass.isInterface()) {
if (method.hasModifierProperty(PsiModifier.ABSTRACT) && isInterface) {
IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.ABSTRACT, false, false);
QuickFixAction.registerQuickFixAction(info, fix);
}
@@ -173,6 +173,7 @@ incompatible.types.html.tooltip=\
interface.methods.cannot.have.body=Interface methods cannot have body
abstract.methods.cannot.have.a.body=Abstract methods cannot have a body
native.methods.cannot.have.a.body=Native methods cannot have a body
extension.method.in.class=Extension methods can only be used within an interface
instance.method.cannot.override.static.method=Instance method ''{0}'' in ''{1}'' cannot override static method ''{2}'' in ''{3}''
static.method.cannot.override.instance.method=Static method ''{0}'' in ''{1}'' cannot override instance method ''{2}'' in ''{3}''
@@ -43,4 +43,8 @@ class C {
}
}.m();
}
}
class D {
<error descr="Extension methods can only be used within an interface">void m()</error> default { }
}