[groovy] do not allow modifier 'static' on toplevel classes (IDEA-162294)

This commit is contained in:
Daniil Ovchinnikov
2016-10-10 16:52:03 +03:00
parent 6ea448eaac
commit 2b3d3c87c4
5 changed files with 30 additions and 1 deletions
@@ -180,6 +180,7 @@ illegal.combination.of.modifiers=Illegal combination of modifiers
illegal.combination.of.modifiers.abstract.and.final=Illegal combination of modifiers 'abstract' and 'final'
modifier.volatile.not.allowed.here=Modifier 'volatile' not allowed here
modifier.transient.not.allowed.here=Modifier 'transient' not allowed here
modifier.0.not.allowed=Modifier ''{0}'' not allowed here
intarface.cannot.have.modifier.final=Interface cannot have modifier 'final'
script.method.cannot.have.modifier.abstract=Script method cannot have modifier 'abstract'
script.cannot.have.modifier.native=Script cannot have modifier 'native'
@@ -1906,6 +1906,12 @@ public class GroovyAnnotator extends GroovyElementVisitor {
if (typeDefinition.isInterface()) {
checkModifierIsNotAllowed(modifiersList, PsiModifier.FINAL, GroovyBundle.message("intarface.cannot.have.modifier.final"), holder);
}
if (GroovyConfigUtils.getInstance().isVersionAtLeast(typeDefinition, GroovyConfigUtils.GROOVY1_8)) {
if (typeDefinition.getContainingClass() == null && !(typeDefinition instanceof GrTraitTypeDefinition)) {
checkModifierIsNotAllowed(modifiersList, PsiModifier.STATIC, holder);
}
}
}
private static void checkDuplicateModifiers(AnnotationHolder holder, @NotNull GrModifierList list, PsiMember member) {
@@ -38,6 +38,12 @@ internal fun checkVariableModifiers(holder: AnnotationHolder, variableDeclaratio
}
}
internal fun checkModifierIsNotAllowed(modifierList: GrModifierList,
@GrModifierConstant modifier: String,
holder: AnnotationHolder) {
checkModifierIsNotAllowed(modifierList, modifier, GroovyBundle.message("modifier.0.not.allowed", modifier), holder)
}
internal fun checkModifierIsNotAllowed(modifierList: GrModifierList,
@GrModifierConstant modifier: String,
message: String?,
@@ -62,4 +62,13 @@ class Groovy16HighlightingTest extends LightCodeInsightFixtureTestCase {
void testSlashyStrings() { doTest() }
void testDiamonds() { doTest() }
void 'test static modifier on toplevel definition is allowed'() {
myFixture.with {
configureByText '_.groovy', '''\
static class A {}
static interface A {}
'''
}
}
}
@@ -17,7 +17,6 @@ package org.jetbrains.plugins.groovy.lang.highlighting
import com.intellij.codeInsight.generation.OverrideImplementExploreUtil
import com.intellij.codeInspection.InspectionProfileEntry
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.annotations.NotNull
@@ -270,5 +269,13 @@ class A {
}
}
void 'test static modifier on toplevel definition (not trait) is not allowed'() {
testHighlighting '''\
<error descr="Modifier 'static' not allowed here">static</error> class A {}
<error descr="Modifier 'static' not allowed here">static</error> interface I {}
static trait T {}
'''
}
final InspectionProfileEntry[] customInspections = [new GroovyAssignabilityCheckInspection(), new GrUnresolvedAccessInspection()]
}