update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / daemon / impl / quickfix / CastMethodArgumentFix.java
blob6887a10a270de41f1505c3ae58fc62d707542fe3
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.
17 /**
18 * Created by IntelliJ IDEA.
19 * User: cdr
20 * Date: Nov 13, 2002
21 * Time: 3:26:50 PM
22 * To change this template use Options | File Templates.
24 package com.intellij.codeInsight.daemon.impl.quickfix;
26 import com.intellij.codeInsight.daemon.QuickFixBundle;
27 import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
28 import com.intellij.psi.*;
29 import com.intellij.util.IncorrectOperationException;
30 import org.jetbrains.annotations.NotNull;
32 public class CastMethodArgumentFix extends MethodArgumentFix {
33 private CastMethodArgumentFix(PsiExpressionList list, int i, PsiType toType, final ArgumentFixerActionFactory factory) {
34 super(list, i, toType, factory);
37 @NotNull
38 public String getText() {
39 if (myArgList.getExpressions().length == 1) {
40 return QuickFixBundle.message("cast.single.parameter.text", HighlightUtil.formatType(myToType));
43 return QuickFixBundle.message("cast.parameter.text", myIndex + 1, HighlightUtil.formatType(myToType));
46 private static class MyFixerActionFactory extends ArgumentFixerActionFactory {
47 public CastMethodArgumentFix createFix(final PsiExpressionList list, final int i, final PsiType toType) {
48 return new CastMethodArgumentFix(list, i, toType, this);
51 protected PsiExpression getModifiedArgument(final PsiExpression expression, PsiType toType) throws IncorrectOperationException {
52 final PsiType exprType = expression.getType();
53 if (exprType instanceof PsiClassType && toType instanceof PsiPrimitiveType) {
54 toType = ((PsiPrimitiveType)toType).getBoxedType(expression);
55 assert toType != null;
57 return AddTypeCastFix.createCastExpression(expression, expression.getProject(), toType);
60 public boolean areTypesConvertible(PsiType exprType, PsiType parameterType, final PsiElement context) {
61 if (exprType instanceof PsiClassType && parameterType instanceof PsiPrimitiveType) {
62 parameterType = ((PsiPrimitiveType)parameterType).getBoxedType(context); //unboxing from type of cast expression will take place at runtime
63 if (parameterType == null) return false;
65 return parameterType.isConvertibleFrom(exprType);
69 public static final ArgumentFixerActionFactory REGISTRAR = new MyFixerActionFactory();