From 3720e054a0d45be57ed150f6ec7a811997daace7 Mon Sep 17 00:00:00 2001 From: Eugene Vigdorchik Date: Tue, 1 Feb 2005 15:37:52 +0300 Subject: [PATCH] PsiParameter.getDeclarationScope corrected for catch parameters --- .../impl/rules/UsageTypeGroupingRule.java | 2 +- .../src/com/intellij/psi/util/PsiUtil.java | 11 ++----- .../impl/analysis/GenericsHighlightUtil.java | 2 +- .../daemon/impl/analysis/HighlightUtil.java | 16 +++++----- .../daemon/impl/quickfix/DeleteCatchFix.java | 2 +- .../psi/impl/source/PsiParameterImpl.java | 2 +- .../source/tree/java/PsiCatchSectionImpl.java | 32 +++++++++++++++---- .../source/tree/java/PsiTryStatementImpl.java | 26 +-------------- .../TurnRefsToSuperProcessorBase.java | 2 +- 9 files changed, 41 insertions(+), 54 deletions(-) diff --git a/UsageView/src/com/intellij/usages/impl/rules/UsageTypeGroupingRule.java b/UsageView/src/com/intellij/usages/impl/rules/UsageTypeGroupingRule.java index b0b7d1264de3..8ae827f05e33 100644 --- a/UsageView/src/com/intellij/usages/impl/rules/UsageTypeGroupingRule.java +++ b/UsageView/src/com/intellij/usages/impl/rules/UsageTypeGroupingRule.java @@ -76,7 +76,7 @@ public class UsageTypeGroupingRule implements UsageGroupingRule { if (psiParameter != null) { final PsiElement scope = psiParameter.getDeclarationScope(); if (scope instanceof PsiMethod) return UsageType.CLASS_METHOD_PARAMETER_DECLARATION; - if (scope instanceof PsiTryStatement) return UsageType.CLASS_CATCH_CLAUSE_PARAMETER_DECLARATION; + if (scope instanceof PsiCatchSection) return UsageType.CLASS_CATCH_CLAUSE_PARAMETER_DECLARATION; if (scope instanceof PsiForeachStatement) return UsageType.CLASS_LOCAL_VAR_DECLARATION; return UsageType.UNCLASSIFIED; } diff --git a/openapi/src/com/intellij/psi/util/PsiUtil.java b/openapi/src/com/intellij/psi/util/PsiUtil.java index 3f822fce9f84..1e9c8792ffbc 100644 --- a/openapi/src/com/intellij/psi/util/PsiUtil.java +++ b/openapi/src/com/intellij/psi/util/PsiUtil.java @@ -336,15 +336,8 @@ public final class PsiUtil { PsiElement codeBlock = null; if (variable instanceof PsiParameter) { PsiElement declarationScope = ((PsiParameter)variable).getDeclarationScope(); - if (declarationScope instanceof PsiTryStatement) { - PsiElement element = variable; - while (element != null) { - if (element instanceof PsiCodeBlock) { - codeBlock = element; - break; - } - element = element.getNextSibling(); - } + if (declarationScope instanceof PsiCatchSection) { + codeBlock = ((PsiCatchSection)declarationScope).getCatchBlock(); } else if (declarationScope instanceof PsiForeachStatement) { codeBlock = (((PsiForeachStatement)declarationScope)).getBody(); diff --git a/source/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java b/source/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java index 1eba89441d6c..8c2b5b025663 100644 --- a/source/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java +++ b/source/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java @@ -690,7 +690,7 @@ public abstract class GenericsHighlightUtil { } public static HighlightInfo checkCatchParameterIsClass(PsiParameter parameter) { - if (parameter.getDeclarationScope() instanceof PsiTryStatement) { + if (parameter.getDeclarationScope() instanceof PsiCatchSection) { PsiType type = parameter.getType(); if (type instanceof PsiClassType) { PsiClass aClass = ((PsiClassType)type).resolve(); diff --git a/source/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/source/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java index d7a5f6705cc3..802260cdc4cd 100644 --- a/source/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java +++ b/source/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java @@ -577,8 +577,8 @@ public class HighlightUtil { PsiIdentifier identifier = variable.getNameIdentifier(); String name = identifier.getText(); if (variable instanceof PsiLocalVariable - || variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiTryStatement - || variable instanceof PsiParameter && variable.getParent() instanceof PsiForeachStatement) { + || variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiCatchSection + || variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiForeachStatement) { PsiElement scope = PsiTreeUtil.getParentOfType(variable, new Class[]{PsiFile.class, PsiMethod.class, PsiClassInitializer.class}, true); VariablesNotProcessor proc = new VariablesNotProcessor(variable, false); @@ -852,8 +852,8 @@ public class HighlightUtil { //@top static HighlightInfo checkExceptionThrownInTry(PsiParameter parameter) { PsiElement declarationScope = parameter.getDeclarationScope(); - if (!(declarationScope instanceof PsiTryStatement)) return null; - PsiTryStatement statement = (PsiTryStatement)declarationScope; + if (!(declarationScope instanceof PsiCatchSection)) return null; + PsiTryStatement statement = ((PsiCatchSection)declarationScope).getTryStatement(); PsiClassType[] classes = ExceptionUtil.collectUnhandledExceptions(statement.getTryBlock(), statement.getTryBlock()); if (classes == null) classes = PsiClassType.EMPTY_ARRAY; @@ -1098,7 +1098,7 @@ public class HighlightUtil { //@top public static HighlightInfo checkCatchParameterIsThrowable(PsiParameter parameter) { - if (parameter.getDeclarationScope() instanceof PsiTryStatement) { + if (parameter.getDeclarationScope() instanceof PsiCatchSection) { final PsiType type = parameter.getType(); return checkMustBeThrowable(type, parameter, true); } @@ -1604,11 +1604,11 @@ public class HighlightUtil { if (!(element.getParent() instanceof PsiTypeElement)) return null; PsiElement catchParameter = element.getParent().getParent(); if (!(catchParameter instanceof PsiParameter) - || !(((PsiParameter)catchParameter).getDeclarationScope() instanceof PsiTryStatement)) { + || !(((PsiParameter)catchParameter).getDeclarationScope() instanceof PsiCatchSection)) { return null; } - PsiCatchSection catchSection = (PsiCatchSection)catchParameter.getParent(); - PsiTryStatement statement = (PsiTryStatement)((PsiParameter)catchParameter).getDeclarationScope(); + PsiCatchSection catchSection = (PsiCatchSection)((PsiParameter)catchParameter).getDeclarationScope(); + PsiTryStatement statement = catchSection.getTryStatement(); PsiCatchSection[] catchSections = statement.getCatchSections(); int i = ArrayUtil.find(catchSections, catchSection); for (i--; i >= 0; i--) { diff --git a/source/com/intellij/codeInsight/daemon/impl/quickfix/DeleteCatchFix.java b/source/com/intellij/codeInsight/daemon/impl/quickfix/DeleteCatchFix.java index 18eafd8b2450..82b02c4a6149 100644 --- a/source/com/intellij/codeInsight/daemon/impl/quickfix/DeleteCatchFix.java +++ b/source/com/intellij/codeInsight/daemon/impl/quickfix/DeleteCatchFix.java @@ -43,7 +43,7 @@ public class DeleteCatchFix implements IntentionAction { public void invoke(Project project, Editor editor, PsiFile file) { if (!CodeInsightUtil.prepareFileForWrite(myCatchParameter.getContainingFile())) return; try { - PsiTryStatement tryStatement = (PsiTryStatement) myCatchParameter.getDeclarationScope(); + PsiTryStatement tryStatement = ((PsiCatchSection)myCatchParameter.getDeclarationScope()).getTryStatement(); final PsiElement tryParent = tryStatement.getParent(); if (tryStatement.getCatchBlocks().length == 1 && tryStatement.getFinallyBlock() == null) { PsiCodeBlock tryBlock = tryStatement.getTryBlock(); diff --git a/source/com/intellij/psi/impl/source/PsiParameterImpl.java b/source/com/intellij/psi/impl/source/PsiParameterImpl.java index ed52b9232fe0..b18b334ecffe 100644 --- a/source/com/intellij/psi/impl/source/PsiParameterImpl.java +++ b/source/com/intellij/psi/impl/source/PsiParameterImpl.java @@ -121,7 +121,7 @@ public class PsiParameterImpl extends IndexedRepositoryPsiElement implements Psi return parent; } else if (parent instanceof PsiCatchSection) { - return parent.getParent(); + return parent; } else{ PsiElement[] children = parent.getChildren(); diff --git a/source/com/intellij/psi/impl/source/tree/java/PsiCatchSectionImpl.java b/source/com/intellij/psi/impl/source/tree/java/PsiCatchSectionImpl.java index 63645a6df2f4..7ecf5a997d4a 100644 --- a/source/com/intellij/psi/impl/source/tree/java/PsiCatchSectionImpl.java +++ b/source/com/intellij/psi/impl/source/tree/java/PsiCatchSectionImpl.java @@ -1,13 +1,14 @@ package com.intellij.psi.impl.source.tree.java; -import com.intellij.psi.*; -import com.intellij.psi.tree.IElementType; -import com.intellij.psi.impl.source.tree.CompositePsiElement; -import com.intellij.psi.impl.source.tree.ChildRole; -import com.intellij.psi.impl.source.tree.TreeElement; -import com.intellij.psi.impl.source.tree.TreeUtil; -import com.intellij.openapi.diagnostic.Logger; import com.intellij.lang.ASTNode; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.psi.*; +import com.intellij.psi.impl.source.tree.ChildRole; +import com.intellij.psi.impl.source.tree.CompositePsiElement; +import com.intellij.psi.impl.source.tree.TreeUtil; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.psi.scope.util.PsiScopesUtil; +import com.intellij.psi.tree.IElementType; /** * @author ven @@ -84,4 +85,21 @@ public class PsiCatchSectionImpl extends CompositePsiElement implements PsiCatch return ChildRole.NONE; } + + public boolean processDeclarations(PsiScopeProcessor processor, + PsiSubstitutor substitutor, + PsiElement lastParent, + PsiElement place) { + processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, this); + if (lastParent == null || lastParent.getParent() != this) + // Parent element should not see our vars + return true; + + final PsiParameter catchParameter = getParameter(); + if (catchParameter != null) { + return processor.execute(catchParameter, substitutor); + } + + return PsiScopesUtil.walkChildrenScopes(this, processor, substitutor, lastParent, place); + } } diff --git a/source/com/intellij/psi/impl/source/tree/java/PsiTryStatementImpl.java b/source/com/intellij/psi/impl/source/tree/java/PsiTryStatementImpl.java index 65c84172251b..003c6243e62b 100644 --- a/source/com/intellij/psi/impl/source/tree/java/PsiTryStatementImpl.java +++ b/source/com/intellij/psi/impl/source/tree/java/PsiTryStatementImpl.java @@ -1,15 +1,13 @@ package com.intellij.psi.impl.source.tree.java; +import com.intellij.lang.ASTNode; import com.intellij.openapi.diagnostic.Logger; import com.intellij.psi.*; import com.intellij.psi.impl.source.SourceTreeToPsiMap; import com.intellij.psi.impl.source.tree.ChildRole; import com.intellij.psi.impl.source.tree.CompositePsiElement; import com.intellij.psi.impl.source.tree.TreeUtil; -import com.intellij.psi.scope.PsiScopeProcessor; -import com.intellij.psi.scope.util.PsiScopesUtil; import com.intellij.psi.tree.IElementType; -import com.intellij.lang.ASTNode; import java.util.ArrayList; @@ -129,26 +127,4 @@ public class PsiTryStatementImpl extends CompositePsiElement implements PsiTrySt public String toString() { return "PsiTryStatement"; } - - public boolean processDeclarations(PsiScopeProcessor processor, PsiSubstitutor substitutor, PsiElement lastParent, PsiElement place) { - processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, this); - if (lastParent == null){ - // Outside members should not know about inner elements - return true; - } - - if (!PsiScopesUtil.walkChildrenScopes(this, processor, substitutor, lastParent, place)) return false; - - PsiCatchSection[] sections = getCatchSections(); - for(int i = 0; i < sections.length; i++){ - if (lastParent.equals(sections[i])){ - PsiParameter parameter = sections[i].getParameter(); - if (parameter != null && !processor.execute(parameter, substitutor)){ - return false; - } - } - } - - return true; - } } diff --git a/source/com/intellij/refactoring/turnRefsToSuper/TurnRefsToSuperProcessorBase.java b/source/com/intellij/refactoring/turnRefsToSuper/TurnRefsToSuperProcessorBase.java index 6f3e311f476a..618fa374374c 100644 --- a/source/com/intellij/refactoring/turnRefsToSuper/TurnRefsToSuperProcessorBase.java +++ b/source/com/intellij/refactoring/turnRefsToSuper/TurnRefsToSuperProcessorBase.java @@ -250,7 +250,7 @@ public abstract class TurnRefsToSuperProcessorBase extends BaseRefactoringProces if (variable instanceof PsiParameter) { final PsiElement declScope = ((PsiParameter)variable).getDeclarationScope(); - if (declScope instanceof PsiTryStatement) { + if (declScope instanceof PsiCatchSection) { markNode(type); } else if (declScope instanceof PsiForeachStatement) {