update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / highlighting / HighlightExceptionsHandlerFactory.java
blob343488c8b81a79a7a63ee716e0855fd5f769fc01
1 /*
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.codeInsight.highlighting;
18 import com.intellij.codeInsight.ExceptionUtil;
19 import com.intellij.codeInsight.TargetElementUtilBase;
20 import com.intellij.featureStatistics.FeatureUsageTracker;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.util.Condition;
23 import com.intellij.psi.*;
24 import org.jetbrains.annotations.Nullable;
26 import java.util.ArrayList;
27 import java.util.Collection;
29 /**
30 * @author yole
32 public class HighlightExceptionsHandlerFactory implements HighlightUsagesHandlerFactory {
33 public HighlightUsagesHandlerBase createHighlightUsagesHandler(final Editor editor, final PsiFile file) {
34 int offset = TargetElementUtilBase.adjustOffset(editor.getDocument(), editor.getCaretModel().getOffset());
35 PsiElement target = file.findElementAt(offset);
36 if (target instanceof PsiKeyword) {
37 PsiElement parent = target.getParent();
38 if (PsiKeyword.TRY.equals(target.getText()) && parent instanceof PsiTryStatement) {
39 return createHighlightTryHandler(editor, file, target, parent);
41 if (PsiKeyword.CATCH.equals(target.getText()) && parent instanceof PsiCatchSection) {
42 return createHighlightCatchHandler(editor, file, target, parent);
44 if (PsiKeyword.THROWS.equals(target.getText())) {
45 return createThrowsHandler(editor, file, target);
48 return null;
51 @Nullable
52 private static HighlightUsagesHandlerBase createHighlightTryHandler(final Editor editor,
53 final PsiFile file,
54 final PsiElement target,
55 final PsiElement parent) {
56 final PsiTryStatement tryStatement = (PsiTryStatement)parent;
57 FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.highlight.throws");
58 final PsiCodeBlock tryBlock = tryStatement.getTryBlock();
59 if (tryBlock == null) return null;
60 final Collection<PsiClassType> psiClassTypes = ExceptionUtil.collectUnhandledExceptions(tryBlock, tryBlock);
61 return new HighlightExceptionsHandler(editor, file, target, psiClassTypes.toArray(new PsiClassType[psiClassTypes.size()]), tryBlock, Condition.TRUE);
64 @Nullable
65 private static HighlightUsagesHandlerBase createHighlightCatchHandler(final Editor editor,
66 final PsiFile file,
67 final PsiElement target,
68 final PsiElement parent) {
69 final PsiCatchSection catchSection = (PsiCatchSection)parent;
70 FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.highlight.throws");
72 PsiTryStatement tryStatement = catchSection.getTryStatement();
74 final PsiParameter param = catchSection.getParameter();
75 if (param == null) return null;
77 final PsiParameter[] catchBlockParameters = tryStatement.getCatchBlockParameters();
79 final Collection<PsiClassType> allThrownExceptions = ExceptionUtil.collectUnhandledExceptions(tryStatement.getTryBlock(),
80 tryStatement.getTryBlock());
81 Condition<PsiType> filter = new Condition<PsiType>() {
82 public boolean value(PsiType type) {
83 for (PsiParameter parameter : catchBlockParameters) {
84 boolean isAssignable = parameter.getType().isAssignableFrom(type);
85 if (parameter != param) {
86 if (isAssignable) return false;
88 else {
89 return isAssignable;
92 return false;
96 ArrayList<PsiClassType> filtered = new ArrayList<PsiClassType>();
97 for (PsiClassType type : allThrownExceptions) {
98 if (filter.value(type)) filtered.add(type);
101 return new HighlightExceptionsHandler(editor, file, target, filtered.toArray(new PsiClassType[filtered.size()]),
102 tryStatement.getTryBlock(), filter);
105 @Nullable
106 private static HighlightUsagesHandlerBase createThrowsHandler(final Editor editor, final PsiFile file, final PsiElement target) {
107 FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.highlight.throws");
108 PsiElement grand = target.getParent().getParent();
109 if (!(grand instanceof PsiMethod)) return null;
110 PsiMethod method = (PsiMethod)grand;
111 if (method.getBody() == null) return null;
113 final Collection<PsiClassType> psiClassTypes = ExceptionUtil.collectUnhandledExceptions(method.getBody(), method.getBody());
115 return new HighlightExceptionsHandler(editor, file, target, psiClassTypes.toArray(new PsiClassType[psiClassTypes.size()]), method.getBody(), Condition.TRUE);