Search in sources :

Example 6 with GrCaseLabel

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel in project intellij-community by JetBrains.

the class GroovyDuplicateSwitchBranchInspection method collectCaseLabels.

private static GrCaseLabel[] collectCaseLabels(final GrSwitchStatement containingStatelent) {
    final Set<GrCaseLabel> labels = new HashSet<>();
    final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitCaseLabel(@NotNull GrCaseLabel grCaseLabel) {
            super.visitCaseLabel(grCaseLabel);
            labels.add(grCaseLabel);
        }

        @Override
        public void visitSwitchStatement(@NotNull GrSwitchStatement grSwitchStatement) {
            if (containingStatelent.equals(grSwitchStatement)) {
                super.visitSwitchStatement(grSwitchStatement);
            }
        }
    };
    containingStatelent.accept(visitor);
    return labels.toArray(new GrCaseLabel[labels.size()]);
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement) NotNull(org.jetbrains.annotations.NotNull) HashSet(java.util.HashSet)

Example 7 with GrCaseLabel

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel in project intellij-community by JetBrains.

the class GrCreateMissingSwitchBranchesIntention method findUnusedConstants.

private static List<PsiEnumConstant> findUnusedConstants(GrSwitchStatement switchStatement) {
    final GrExpression condition = switchStatement.getCondition();
    if (condition == null)
        return Collections.emptyList();
    final PsiType type = condition.getType();
    if (!(type instanceof PsiClassType))
        return Collections.emptyList();
    final PsiClass resolved = ((PsiClassType) type).resolve();
    if (resolved == null || !resolved.isEnum())
        return Collections.emptyList();
    final PsiField[] fields = resolved.getFields();
    final List<PsiEnumConstant> constants = ContainerUtil.findAll(fields, PsiEnumConstant.class);
    final GrCaseSection[] sections = switchStatement.getCaseSections();
    for (GrCaseSection section : sections) {
        for (GrCaseLabel label : section.getCaseLabels()) {
            final GrExpression value = label.getValue();
            if (value instanceof GrReferenceExpression) {
                final PsiElement r = ((GrReferenceExpression) value).resolve();
                constants.remove(r);
            }
        }
    }
    return constants;
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)

Aggregations

GrCaseLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)7 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 NotNull (org.jetbrains.annotations.NotNull)2 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 PsiRecursiveElementVisitor (com.intellij.psi.PsiRecursiveElementVisitor)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)1 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrAnnotationArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrSwitchStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1