PsiParameter.getDeclarationScope corrected for catch parameters

This commit is contained in:
Eugene Vigdorchik
2005-02-01 15:37:52 +03:00
parent 952d56832a
commit 3720e054a0
9 changed files with 41 additions and 54 deletions
@@ -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;
}
@@ -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();
@@ -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();
@@ -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--) {
@@ -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();
@@ -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();
@@ -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);
}
}
@@ -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;
}
}
@@ -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) {