2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.psi
.impl
.source
.tree
.java
;
18 import com
.intellij
.lang
.ASTNode
;
19 import com
.intellij
.openapi
.diagnostic
.Logger
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.impl
.source
.tree
.ChildRole
;
22 import com
.intellij
.psi
.impl
.source
.tree
.CompositePsiElement
;
23 import com
.intellij
.psi
.impl
.source
.Constants
;
24 import com
.intellij
.psi
.scope
.PsiScopeProcessor
;
25 import com
.intellij
.psi
.scope
.util
.PsiScopesUtil
;
26 import com
.intellij
.psi
.tree
.IElementType
;
27 import com
.intellij
.psi
.tree
.ChildRoleBase
;
28 import org
.jetbrains
.annotations
.NotNull
;
29 import org
.jetbrains
.annotations
.Nullable
;
34 public class PsiCatchSectionImpl
extends CompositePsiElement
implements PsiCatchSection
, Constants
{
35 private static final Logger LOG
= Logger
.getInstance("#com.intellij.psi.impl.source.tree.java.PsiCatchSectionImpl");
37 public PsiCatchSectionImpl() {
41 public PsiParameter
getParameter() {
42 return (PsiParameter
)findChildByRoleAsPsiElement(ChildRole
.PARAMETER
);
45 public PsiCodeBlock
getCatchBlock() {
46 return (PsiCodeBlock
)findChildByRoleAsPsiElement(ChildRole
.CATCH_BLOCK
);
49 public PsiType
getCatchType() {
50 PsiParameter parameter
= getParameter();
51 if (parameter
== null) return null;
52 return parameter
.getType();
56 public PsiTryStatement
getTryStatement() {
57 return (PsiTryStatement
)getParent();
61 public PsiJavaToken
getLParenth() {
62 return (PsiJavaToken
)findChildByRole(ChildRole
.CATCH_BLOCK_PARAMETER_LPARENTH
);
66 public PsiJavaToken
getRParenth() {
67 return (PsiJavaToken
)findChildByRole(ChildRole
.CATCH_BLOCK_PARAMETER_RPARENTH
);
70 public void accept(@NotNull PsiElementVisitor visitor
) {
71 if (visitor
instanceof JavaElementVisitor
) {
72 ((JavaElementVisitor
)visitor
).visitCatchSection(this);
75 visitor
.visitElement(this);
79 public String
toString() {
80 return "PsiCatchSection";
83 public ASTNode
findChildByRole(int role
) {
88 case ChildRole
.PARAMETER
:
89 return findChildByType(PARAMETER
);
91 case ChildRole
.CATCH_KEYWORD
:
92 return findChildByType(CATCH_KEYWORD
);
94 case ChildRole
.CATCH_BLOCK_PARAMETER_LPARENTH
:
95 return findChildByType(LPARENTH
);
97 case ChildRole
.CATCH_BLOCK_PARAMETER_RPARENTH
:
98 return findChildByType(RPARENTH
);
100 case ChildRole
.CATCH_BLOCK
:
101 return findChildByType(CODE_BLOCK
);
105 public int getChildRole(ASTNode child
) {
106 LOG
.assertTrue(child
.getTreeParent() == this);
107 IElementType i
= child
.getElementType();
108 if (i
== PARAMETER
) {
109 return ChildRole
.PARAMETER
;
110 } else if (i
== CODE_BLOCK
) {
111 return ChildRole
.CATCH_BLOCK
;
112 } else if (i
== CATCH_KEYWORD
) {
113 return ChildRole
.CATCH_KEYWORD
;
114 } else if (i
== LPARENTH
) {
115 return ChildRole
.CATCH_BLOCK_PARAMETER_LPARENTH
;
116 } else if (i
== RPARENTH
) {
117 return ChildRole
.CATCH_BLOCK_PARAMETER_RPARENTH
;
120 return ChildRoleBase
.NONE
;
123 public boolean processDeclarations(@NotNull PsiScopeProcessor processor
,
124 @NotNull ResolveState state
,
125 PsiElement lastParent
,
126 @NotNull PsiElement place
) {
127 processor
.handleEvent(PsiScopeProcessor
.Event
.SET_DECLARATION_HOLDER
, this);
128 if (lastParent
== null || lastParent
.getParent() != this)
129 // Parent element should not see our vars
132 final PsiParameter catchParameter
= getParameter();
133 if (catchParameter
!= null) {
134 return processor
.execute(catchParameter
, state
);
137 return PsiScopesUtil
.walkChildrenScopes(this, processor
, state
, lastParent
, place
);