convert primitive to boxed when appropriate (IDEA-60267)

This commit is contained in:
Anna Kozlova
2012-11-04 17:19:06 +01:00
parent af2ae07dd9
commit c661cba6ed
7 changed files with 159 additions and 2 deletions

View File

@@ -628,7 +628,7 @@ public class GenericsHighlightUtil {
return null;
}
public static HighlightInfo checkReferenceTypeUsedAsTypeArgument(PsiTypeElement typeElement) {
public static HighlightInfo checkReferenceTypeUsedAsTypeArgument(final PsiTypeElement typeElement) {
final PsiType type = typeElement.getType();
if (type != PsiType.NULL && type instanceof PsiPrimitiveType ||
type instanceof PsiWildcardType && ((PsiWildcardType)type).getBound() instanceof PsiPrimitiveType) {
@@ -639,7 +639,19 @@ public class GenericsHighlightUtil {
if (element == null) return null;
String description = JavaErrorMessages.message("generics.type.argument.cannot.be.of.primitive.type");
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, typeElement, description);
final HighlightInfo highlightInfo = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, typeElement, description);
PsiType toConvert = type;
if (type instanceof PsiWildcardType) {
toConvert = ((PsiWildcardType)type).getBound();
}
if (toConvert instanceof PsiPrimitiveType) {
final PsiClassType boxedType = ((PsiPrimitiveType)toConvert).getBoxedType(typeElement);
if (boxedType != null) {
QuickFixAction.registerQuickFixAction(highlightInfo,
new ReplacePrimitiveWithBoxedTypeAction(typeElement, toConvert.getPresentableText(), ((PsiPrimitiveType)toConvert).getBoxedTypeName()));
}
}
return highlightInfo;
}
return null;

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: anna
*/
class ReplacePrimitiveWithBoxedTypeAction extends LocalQuickFixAndIntentionActionOnPsiElement {
private final String myPrimitiveName;
private final String myBoxedTypeName;
private static final Logger LOG = Logger.getInstance("#" + ReplacePrimitiveWithBoxedTypeAction.class.getName());
protected ReplacePrimitiveWithBoxedTypeAction(@Nullable PsiTypeElement element, String typeName, String boxedTypeName) {
super(element);
myPrimitiveName = typeName;
myBoxedTypeName = boxedTypeName;
}
@NotNull
@Override
public String getText() {
return "Convert '" + myPrimitiveName + "' to '" + myBoxedTypeName + "'";
}
@NotNull
@Override
public String getFamilyName() {
return getText();
}
@Override
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
if (startElement instanceof PsiTypeElement) {
PsiType type = ((PsiTypeElement)startElement).getType();
if (type instanceof PsiWildcardType) {
type = ((PsiWildcardType)type).getBound();
}
if (type instanceof PsiPrimitiveType) {
return ((PsiPrimitiveType)type).getBoxedType(startElement) != null;
}
}
return false;
}
@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable("is null when called from inspection") Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
final PsiType type = ((PsiTypeElement)startElement).getType();
PsiType boxedType;
if (type instanceof PsiPrimitiveType) {
boxedType = ((PsiPrimitiveType)type).getBoxedType(startElement);
} else {
LOG.assertTrue(type instanceof PsiWildcardType);
final PsiWildcardType wildcardType = (PsiWildcardType)type;
final PsiClassType boxedBound = ((PsiPrimitiveType)wildcardType.getBound()).getBoxedType(startElement);
LOG.assertTrue(boxedBound != null);
boxedType = wildcardType.isExtends() ? PsiWildcardType.createExtends(startElement.getManager(), boxedBound)
: PsiWildcardType.createSuper(startElement.getManager(), boxedBound);
}
LOG.assertTrue(boxedType != null);
startElement.replace(JavaPsiFacade.getElementFactory(project).createTypeElement(boxedType));
}
@Override
public boolean startInWriteAction() {
return true;
}
}

View File

@@ -0,0 +1,6 @@
// "Convert 'int' to 'java.lang.Integer'" "true"
import java.util.*;
class Test {
Set<Integer> f;
}

View File

@@ -0,0 +1,6 @@
// "Convert 'int' to 'java.lang.Integer'" "true"
import java.util.*;
class Test {
Set<? extends Integer> f;
}

View File

@@ -0,0 +1,6 @@
// "Convert 'int' to 'java.lang.Integer'" "true"
import java.util.*;
class Test {
Set<in<caret>t> f;
}

View File

@@ -0,0 +1,6 @@
// "Convert 'int' to 'java.lang.Integer'" "true"
import java.util.*;
class Test {
Set<? extends in<caret>t> f;
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.quickFix;
public class ReplacePrimitiveWithBoxedTypeTest extends LightQuickFix15TestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/replacePrimitiveWithBoxed";
}
}