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
.SourceTreeToPsiMap
;
22 import com
.intellij
.psi
.impl
.source
.Constants
;
23 import com
.intellij
.psi
.impl
.source
.tree
.ChildRole
;
24 import com
.intellij
.psi
.impl
.source
.tree
.CompositePsiElement
;
25 import com
.intellij
.psi
.tree
.IElementType
;
26 import com
.intellij
.psi
.tree
.ChildRoleBase
;
27 import org
.jetbrains
.annotations
.NotNull
;
29 import java
.util
.ArrayList
;
31 public class PsiTryStatementImpl
extends CompositePsiElement
implements PsiTryStatement
, Constants
{
32 private static final Logger LOG
= Logger
.getInstance("#com.intellij.psi.impl.source.tree.java.PsiTryStatementImpl");
33 private volatile PsiParameter
[] myCachedCatchParameters
= null;
35 public void clearCaches() {
37 myCachedCatchParameters
= null;
40 public PsiTryStatementImpl() {
44 public PsiCodeBlock
getTryBlock() {
45 return (PsiCodeBlock
)findChildByRoleAsPsiElement(ChildRole
.TRY_BLOCK
);
49 public PsiCodeBlock
[] getCatchBlocks() {
50 ASTNode tryBlock
= SourceTreeToPsiMap
.psiElementToTree(getTryBlock());
51 if (tryBlock
!= null) {
52 PsiCatchSection
[] catchSections
= getCatchSections();
53 if (catchSections
.length
== 0) return PsiCodeBlock
.EMPTY_ARRAY
;
54 boolean lastIncomplete
= catchSections
[catchSections
.length
- 1].getCatchBlock() == null;
55 PsiCodeBlock
[] blocks
= new PsiCodeBlock
[lastIncomplete ? catchSections
.length
- 1 : catchSections
.length
];
56 for (int i
= 0; i
< blocks
.length
; i
++) {
57 blocks
[i
] = catchSections
[i
].getCatchBlock();
61 return PsiCodeBlock
.EMPTY_ARRAY
;
65 public PsiParameter
[] getCatchBlockParameters() {
66 PsiParameter
[] catchParameters
= myCachedCatchParameters
;
67 if (catchParameters
== null) {
68 PsiCatchSection
[] catchSections
= getCatchSections();
69 if (catchSections
.length
== 0) return PsiParameter
.EMPTY_ARRAY
;
70 boolean lastIncomplete
= catchSections
[catchSections
.length
- 1].getCatchBlock() == null;
71 int limit
= lastIncomplete ? catchSections
.length
- 1 : catchSections
.length
;
72 ArrayList
<PsiParameter
> parameters
= new ArrayList
<PsiParameter
>();
73 for (int i
= 0; i
< limit
; i
++) {
74 PsiParameter parameter
= catchSections
[i
].getParameter();
75 if (parameter
!= null) parameters
.add(parameter
);
77 myCachedCatchParameters
= catchParameters
= parameters
.toArray(new PsiParameter
[parameters
.size()]);
79 return catchParameters
;
83 public PsiCatchSection
[] getCatchSections() {
84 return getChildrenAsPsiElements(CATCH_SECTION_BIT_SET
, PSI_CATCH_SECTION_ARRAYS_CONSTRUCTOR
);
87 public PsiCodeBlock
getFinallyBlock() {
88 return (PsiCodeBlock
)findChildByRoleAsPsiElement(ChildRole
.FINALLY_BLOCK
);
91 public ASTNode
findChildByRole(int role
) {
92 LOG
.assertTrue(ChildRole
.isUnique(role
));
97 case ChildRole
.TRY_KEYWORD
:
98 return findChildByType(TRY_KEYWORD
);
100 case ChildRole
.TRY_BLOCK
:
101 return findChildByType(CODE_BLOCK
);
103 case ChildRole
.FINALLY_KEYWORD
:
104 return findChildByType(FINALLY_KEYWORD
);
106 case ChildRole
.FINALLY_BLOCK
:
108 ASTNode finallyKeyword
= findChildByRole(ChildRole
.FINALLY_KEYWORD
);
109 if (finallyKeyword
== null) return null;
110 for(ASTNode child
= finallyKeyword
.getTreeNext(); child
!= null; child
= child
.getTreeNext()){
111 if (child
.getElementType() == CODE_BLOCK
){
120 public int getChildRole(ASTNode child
) {
121 LOG
.assertTrue(child
.getTreeParent() == this);
122 IElementType i
= child
.getElementType();
123 if (i
== TRY_KEYWORD
) {
124 return ChildRole
.TRY_KEYWORD
;
126 else if (i
== FINALLY_KEYWORD
) {
127 return ChildRole
.FINALLY_KEYWORD
;
129 else if (i
== CATCH_SECTION
) {
130 return ChildRole
.CATCH_SECTION
;
133 if (child
.getElementType() == CODE_BLOCK
) {
134 int role
= getChildRole(child
, ChildRole
.TRY_BLOCK
);
135 if (role
!= ChildRoleBase
.NONE
) return role
;
136 return getChildRole(child
, ChildRole
.FINALLY_BLOCK
);
139 return ChildRoleBase
.NONE
;
144 public void accept(@NotNull PsiElementVisitor visitor
) {
145 if (visitor
instanceof JavaElementVisitor
) {
146 ((JavaElementVisitor
)visitor
).visitTryStatement(this);
149 visitor
.visitElement(this);
153 public String
toString() {
154 return "PsiTryStatement";