java8: static method with body is allowed in interface (IDEA-100334)

This commit is contained in:
Anna Kozlova
2013-02-04 14:07:09 +04:00
parent e8c454f426
commit 68f7ee96f7
6 changed files with 57 additions and 6 deletions
@@ -853,21 +853,24 @@ public class HighlightMethodUtil {
@Nullable
static HighlightInfo checkMethodCanHaveBody(PsiMethod method) {
PsiClass aClass = method.getContainingClass();
boolean hasBody = method.getBody() == null;
boolean hasNoBody = method.getBody() == null;
boolean isInterface = aClass != null && aClass.isInterface();
boolean isExtension = method.hasModifierProperty(PsiModifier.DEFAULT);
boolean isStatic = method.hasModifierProperty(PsiModifier.STATIC);
String message = null;
if (hasBody) {
if (hasNoBody) {
if (isExtension) {
message = JavaErrorMessages.message("extension.method.should.have.a.body");
} else if (isInterface && isStatic) {
message = "Static methods in interfaces should have a body";
}
}
else if (isInterface) {
if (!isExtension) {
if (!isExtension && !isStatic) {
message = JavaErrorMessages.message("interface.methods.cannot.have.body");
}
else {
else if (isExtension) {
return HighlightUtil.checkExtensionMethodsFeature(method);
}
}
@@ -884,7 +887,7 @@ public class HighlightMethodUtil {
TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, textRange, message);
if (hasBody) {
if (hasNoBody) {
QuickFixAction.registerQuickFixAction(info, new DeleteMethodBodyFix(method));
}
if (method.hasModifierProperty(PsiModifier.ABSTRACT) && isInterface) {
@@ -161,7 +161,13 @@ public class HighlightUtil extends HighlightUtilBase {
Set<String> incompatibles = incompatibleModifiersHash.get(modifier);
if (incompatibles == null) return null;
final boolean level8OrHigher = PsiUtil.isLanguageLevel8OrHigher(modifierList);
for (@PsiModifier.ModifierConstant String incompatible : incompatibles) {
if (level8OrHigher) {
if (modifier.equals(PsiModifier.STATIC) && incompatible.equals(PsiModifier.ABSTRACT)){
continue;
}
}
if (modifierList.hasModifierProperty(incompatible)) {
return incompatible;
}
@@ -50,7 +50,7 @@ class D {
}
interface IllegalMods {
<error descr="Illegal combination of modifiers: 'static' and 'abstract'">static</error> void m1();
<error descr="Static methods in interfaces should have a body">static void m1()</error>;
<error descr="Illegal combination of modifiers: 'static' and 'default'">static</error> void m2() default { }
<error descr="Illegal combination of modifiers: 'static' and 'default'">static</error> <error descr="Illegal combination of modifiers: 'default' and 'static'">default</error> void m3() { }
@@ -0,0 +1,4 @@
interface Foo {
public static void bar1() {}
public <error descr="Illegal combination of modifiers: 'abstract' and 'static'">abstract</error> static void bar2() {}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.lambda;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import org.jetbrains.annotations.NonNls;
public class Interface8MethodsHighlightingTest extends LightDaemonAnalyzerTestCase {
@NonNls static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/lambda/interfaceMethods";
public void testStaticMethod() throws Exception {
doTest();
}
private void doTest() throws Exception {
doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false);
}
}
@@ -1,5 +1,7 @@
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.pom.java.LanguageLevel;
public class ModifierTest extends LightQuickFixTestCase {
public void test() throws Exception { doAllTests(); }
@@ -7,4 +9,9 @@ public class ModifierTest extends LightQuickFixTestCase {
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/modifier";
}
@Override
protected LanguageLevel getLanguageLevel() {
return LanguageLevel.JDK_1_7;
}
}