update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / intention / impl / MakeTypeGenericAction.java
blob05f78aa4955da61c1b3d842dba1a616942c4c1b3
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.intention.impl;
18 import com.intellij.codeInsight.CodeInsightBundle;
19 import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
20 import com.intellij.openapi.editor.CaretModel;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.util.Pair;
24 import com.intellij.pom.java.LanguageLevel;
25 import com.intellij.psi.*;
26 import com.intellij.psi.util.PsiUtil;
27 import com.intellij.psi.util.TypeConversionUtil;
28 import com.intellij.util.IncorrectOperationException;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 /**
33 * @author dsl
35 public class MakeTypeGenericAction extends PsiElementBaseIntentionAction {
36 private String variableName;
37 private String newTypeName;
39 @NotNull
40 public String getFamilyName() {
41 return CodeInsightBundle.message("intention.make.type.generic.family");
44 @NotNull
45 public String getText() {
46 return CodeInsightBundle.message("intention.make.type.generic.text", variableName, newTypeName);
49 public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable PsiElement element) {
50 if (element == null) return false;
51 if (!PsiUtil.isLanguageLevel5OrHigher(element)) return false;
52 if (!element.isWritable()) return false;
53 return findVariable(element) != null;
56 private Pair<PsiVariable,PsiType> findVariable(final PsiElement element) {
57 PsiVariable variable = null;
58 if (element instanceof PsiIdentifier) {
59 if (element.getParent() instanceof PsiVariable) {
60 variable = (PsiVariable)element.getParent();
63 else if (element instanceof PsiJavaToken) {
64 final PsiJavaToken token = (PsiJavaToken)element;
65 if (token.getTokenType() != JavaTokenType.EQ) return null;
66 if (token.getParent() instanceof PsiVariable) {
67 variable = (PsiVariable)token.getParent();
70 if (variable == null) {
71 return null;
73 variableName = variable.getName();
74 final PsiExpression initializer = variable.getInitializer();
75 if (initializer == null) return null;
76 final PsiType variableType = variable.getType();
77 final PsiType initializerType = initializer.getType();
78 if (!(variableType instanceof PsiClassType)) return null;
79 final PsiClassType variableClassType = (PsiClassType) variableType;
80 if (!variableClassType.isRaw()) return null;
81 if (!(initializerType instanceof PsiClassType)) return null;
82 final PsiClassType initializerClassType = (PsiClassType) initializerType;
83 if (initializerClassType.isRaw()) return null;
84 final PsiClassType.ClassResolveResult variableResolveResult = variableClassType.resolveGenerics();
85 final PsiClassType.ClassResolveResult initializerResolveResult = initializerClassType.resolveGenerics();
86 if (initializerResolveResult.getElement() == null) return null;
87 final PsiSubstitutor targetSubstitutor = TypeConversionUtil.getClassSubstitutor(variableResolveResult.getElement(), initializerResolveResult.getElement(), initializerResolveResult.getSubstitutor());
88 if (targetSubstitutor == null) return null;
89 PsiType type =
90 JavaPsiFacade.getInstance(variable.getProject()).getElementFactory().createType(variableResolveResult.getElement(), targetSubstitutor);
91 newTypeName = type.getCanonicalText();
92 return Pair.create(variable, type);
95 public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
96 final CaretModel caretModel = editor.getCaretModel();
97 final int position = caretModel.getOffset();
98 final PsiElement element = file.findElementAt(position);
99 Pair<PsiVariable, PsiType> pair = findVariable(element);
100 if (pair == null) return;
101 PsiVariable variable = pair.getFirst();
102 PsiType type = pair.getSecond();
104 variable.getTypeElement().replace(JavaPsiFacade.getInstance(variable.getProject()).getElementFactory().createTypeElement(type));