Merge remote-tracking branch 'origin/master'

This commit is contained in:
Yann Cebron
2011-06-24 19:51:57 +02:00
182 changed files with 2476 additions and 367 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 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.
@@ -687,7 +687,7 @@ public class HighlightControlFlowUtil {
else {
scope = new PsiElement[]{variable.getParent()};
}
if (scope.length < 1 || scope[0].getContainingFile() != context.getContainingFile()) return null;
if (scope.length < 1 || scope[0] == null || scope[0].getContainingFile() != context.getContainingFile()) return null;
PsiElement parent = context.getParent();
PsiElement prevParent = context;
@@ -2019,18 +2019,18 @@ public class HighlightUtil {
@Nullable
public static HighlightInfo checkSingleImportClassConflict(PsiImportStatement statement,
Map<String, Pair<PsiImportStaticReferenceElement, PsiClass>> singleImportedClasses) {
Map<String, Pair<PsiImportStaticReferenceElement, PsiClass>> importedClasses) {
if (statement.isOnDemand()) return null;
PsiElement element = statement.resolve();
if (element instanceof PsiClass) {
String name = ((PsiClass)element).getName();
Pair<PsiImportStaticReferenceElement, PsiClass> imported = singleImportedClasses.get(name);
Pair<PsiImportStaticReferenceElement, PsiClass> imported = importedClasses.get(name);
PsiClass importedClass = imported == null ? null : imported.getSecond();
if (importedClass != null && !element.getManager().areElementsEquivalent(importedClass, element)) {
String description = JavaErrorMessages.message("single.import.class.conflict", formatClass(importedClass));
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, statement, description);
}
singleImportedClasses.put(name, Pair.create((PsiImportStaticReferenceElement)statement.getReference(), (PsiClass)element));
importedClasses.put(name, Pair.create((PsiImportStaticReferenceElement)null, (PsiClass)element));
}
return null;
}
@@ -445,7 +445,9 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitImportStatement(final PsiImportStatement statement) {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkSingleImportClassConflict(statement, mySingleImportedClasses));
if (!myHolder.hasErrorResults()) {
myHolder.add(HighlightUtil.checkSingleImportClassConflict(statement, mySingleImportedClasses));
}
}
@Override
@@ -471,9 +473,11 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
final Pair<PsiImportStaticReferenceElement, PsiClass> imported = mySingleImportedClasses.get(refName);
final PsiClass aClass = imported == null ? null : imported.getSecond();
if (aClass != null && !manager.areElementsEquivalent(aClass, element)) {
description = imported.getFirst().equals(ref)
? JavaErrorMessages.message("class.is.ambiguous.in.single.static.import", refName)
: JavaErrorMessages.message("class.is.already.defined.in.single.static.import", refName);
description = imported.first == null
? JavaErrorMessages.message("single.import.class.conflict", refName)
: imported.first.equals(ref)
? JavaErrorMessages.message("class.is.ambiguous.in.single.static.import", refName)
: JavaErrorMessages.message("class.is.already.defined.in.single.static.import", refName);
}
mySingleImportedClasses.put(refName, Pair.create(ref, (PsiClass)element));
}
@@ -481,7 +485,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
final Pair<PsiImportStaticReferenceElement, PsiField> imported = mySingleImportedFields.get(refName);
final PsiField field = imported == null ? null : imported.getSecond();
if (field != null && !manager.areElementsEquivalent(field, element)) {
description = imported.getFirst().equals(ref)
description = imported.first.equals(ref)
? JavaErrorMessages.message("field.is.ambiguous.in.single.static.import", refName)
: JavaErrorMessages.message("field.is.already.defined.in.single.static.import", refName);
}
@@ -21,6 +21,7 @@ import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
import com.intellij.psi.*;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
@@ -62,5 +63,6 @@ public abstract class GenericsInspectionToolBase extends BaseLocalInspectionTool
return null;
}
@Nullable
public abstract ProblemDescriptor[] getDescriptions(PsiElement place, InspectionManager manager, boolean isOnTheFly);
}
@@ -29,8 +29,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
@@ -105,88 +103,12 @@ public class SuspiciousCollectionsMethodCallsInspection extends BaseLocalInspect
@Override public void visitMethodCallExpression(PsiMethodCallExpression methodCall) {
super.visitMethodCallExpression(methodCall);
final PsiReferenceExpression methodExpression = methodCall.getMethodExpression();
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (qualifier == null || qualifier instanceof PsiThisExpression || qualifier instanceof PsiSuperExpression) return;
final PsiExpression[] args = methodCall.getArgumentList().getExpressions();
if (args.length != 1) return;
PsiType argType = args[0].getType();
if (argType instanceof PsiPrimitiveType) {
argType = ((PsiPrimitiveType)argType).getBoxedType(methodCall);
}
if (!(argType instanceof PsiClassType)) return;
final JavaResolveResult resolveResult = methodExpression.advancedResolve(false);
PsiMethod calleeMethod = (PsiMethod)resolveResult.getElement();
if (calleeMethod == null) return;
PsiMethod contextMethod = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class);
synchronized (patternMethods) {
if (patternMethods.isEmpty()) {
setupPatternMethods(methodCall.getManager(), methodCall.getResolveScope(), patternMethods, indices);
}
}
for (int i = 0; i < patternMethods.size(); i++) {
PsiMethod patternMethod = patternMethods.get(i);
if (!patternMethod.getName().equals(methodExpression.getReferenceName())) continue;
int index = indices.get(i);
//we are in collections method implementation
if (contextMethod != null && isInheritorOrSelf(contextMethod, patternMethod)) return;
final PsiClass calleeClass = calleeMethod.getContainingClass();
PsiSubstitutor substitutor = resolveResult.getSubstitutor();
final PsiClass patternClass = patternMethod.getContainingClass();
substitutor = TypeConversionUtil.getClassSubstitutor(patternClass, calleeClass, substitutor);
if (substitutor == null) continue;
if (!patternMethod.getSignature(substitutor).equals(calleeMethod.getSignature(PsiSubstitutor.EMPTY))) continue;
PsiTypeParameter[] typeParameters = patternClass.getTypeParameters();
if (typeParameters.length <= index) return;
final PsiTypeParameter typeParameter = typeParameters[index];
PsiType typeParamMapping = substitutor.substitute(typeParameter);
if (typeParamMapping == null) return;
String message = null;
if (typeParamMapping instanceof PsiCapturedWildcardType) {
typeParamMapping = ((PsiCapturedWildcardType)typeParamMapping).getWildcard();
}
if (!typeParamMapping.isAssignableFrom(argType)) {
if (typeParamMapping.isConvertibleFrom(argType)) {
if (REPORT_CONVERTIBLE_METHOD_CALLS) {
message = InspectionsBundle.message("inspection.suspicious.collections.method.calls.problem.descriptor1",
PsiFormatUtil.formatMethod(calleeMethod, substitutor,
PsiFormatUtil.SHOW_NAME | PsiFormatUtil
.SHOW_CONTAINING_CLASS, PsiFormatUtil.SHOW_TYPE));
}
}
else {
PsiType qualifierType = qualifier.getType();
LOG.assertTrue(qualifierType != null);
message = InspectionsBundle.message("inspection.suspicious.collections.method.calls.problem.descriptor",
PsiFormatUtil.formatType(qualifierType, 0, PsiSubstitutor.EMPTY),
PsiFormatUtil.formatType(argType, 0, PsiSubstitutor.EMPTY));
}
}
if (message != null) {
holder.registerProblem(args[0], message);
}
return;
final String message = getSuspiciousMethodCallMessage(methodCall, REPORT_CONVERTIBLE_METHOD_CALLS, patternMethods, indices
);
if (message != null) {
holder.registerProblem(methodCall.getArgumentList().getExpressions()[0], message);
}
}
private boolean isInheritorOrSelf(PsiMethod inheritorCandidate, PsiMethod base) {
PsiClass aClass = inheritorCandidate.getContainingClass();
PsiClass bClass = base.getContainingClass();
if (aClass == null || bClass == null) return false;
PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(bClass, aClass, PsiSubstitutor.EMPTY);
return substitutor != null &&
MethodSignatureUtil.findMethodBySignature(bClass, inheritorCandidate.getSignature(substitutor), false) == base;
}
};
}
@@ -204,4 +126,96 @@ public class SuspiciousCollectionsMethodCallsInspection extends BaseLocalInspect
public String getShortName() {
return "SuspiciousMethodCalls";
}
@Nullable
private static String getSuspiciousMethodCallMessage(final PsiMethodCallExpression methodCall,
final boolean reportConvertibleMethodCalls, final List<PsiMethod> patternMethods,
final IntArrayList indices) {
final PsiExpression[] args = methodCall.getArgumentList().getExpressions();
if (args.length != 1) return null;
PsiType argType = args[0].getType();
return getSuspiciousMethodCallMessage(methodCall, argType, reportConvertibleMethodCalls, patternMethods, indices);
}
@Nullable
public static String getSuspiciousMethodCallMessage(PsiMethodCallExpression methodCall,
PsiType argType, boolean reportConvertibleMethodCalls, List<PsiMethod> patternMethods,
IntArrayList indices) {
final PsiReferenceExpression methodExpression = methodCall.getMethodExpression();
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (qualifier == null || qualifier instanceof PsiThisExpression || qualifier instanceof PsiSuperExpression) return null;
if (argType instanceof PsiPrimitiveType) {
argType = ((PsiPrimitiveType)argType).getBoxedType(methodCall);
}
if (!(argType instanceof PsiClassType)) return null;
final JavaResolveResult resolveResult = methodExpression.advancedResolve(false);
PsiMethod calleeMethod = (PsiMethod)resolveResult.getElement();
if (calleeMethod == null) return null;
PsiMethod contextMethod = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class);
synchronized (patternMethods) {
if (patternMethods.isEmpty()) {
setupPatternMethods(methodCall.getManager(), methodCall.getResolveScope(), patternMethods, indices);
}
}
for (int i = 0; i < patternMethods.size(); i++) {
PsiMethod patternMethod = patternMethods.get(i);
if (!patternMethod.getName().equals(methodExpression.getReferenceName())) continue;
int index = indices.get(i);
//we are in collections method implementation
if (contextMethod != null && isInheritorOrSelf(contextMethod, patternMethod)) return null;
final PsiClass calleeClass = calleeMethod.getContainingClass();
PsiSubstitutor substitutor = resolveResult.getSubstitutor();
final PsiClass patternClass = patternMethod.getContainingClass();
substitutor = TypeConversionUtil.getClassSubstitutor(patternClass, calleeClass, substitutor);
if (substitutor == null) continue;
if (!patternMethod.getSignature(substitutor).equals(calleeMethod.getSignature(PsiSubstitutor.EMPTY))) continue;
PsiTypeParameter[] typeParameters = patternClass.getTypeParameters();
if (typeParameters.length <= index) return null;
final PsiTypeParameter typeParameter = typeParameters[index];
PsiType typeParamMapping = substitutor.substitute(typeParameter);
if (typeParamMapping == null) return null;
String message = null;
if (typeParamMapping instanceof PsiCapturedWildcardType) {
typeParamMapping = ((PsiCapturedWildcardType)typeParamMapping).getWildcard();
}
if (!typeParamMapping.isAssignableFrom(argType)) {
if (typeParamMapping.isConvertibleFrom(argType)) {
if (reportConvertibleMethodCalls) {
message = InspectionsBundle.message("inspection.suspicious.collections.method.calls.problem.descriptor1",
PsiFormatUtil.formatMethod(calleeMethod, substitutor,
PsiFormatUtilBase.SHOW_NAME |
PsiFormatUtilBase.SHOW_CONTAINING_CLASS,
PsiFormatUtilBase.SHOW_TYPE));
}
}
else {
PsiType qualifierType = qualifier.getType();
LOG.assertTrue(qualifierType != null);
message = InspectionsBundle.message("inspection.suspicious.collections.method.calls.problem.descriptor",
PsiFormatUtil.formatType(qualifierType, 0, PsiSubstitutor.EMPTY),
PsiFormatUtil.formatType(argType, 0, PsiSubstitutor.EMPTY));
}
}
return message;
}
return null;
}
private static boolean isInheritorOrSelf(PsiMethod inheritorCandidate, PsiMethod base) {
PsiClass aClass = inheritorCandidate.getContainingClass();
PsiClass bClass = base.getContainingClass();
if (aClass == null || bClass == null) return false;
PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(bClass, aClass, PsiSubstitutor.EMPTY);
return substitutor != null &&
MethodSignatureUtil.findMethodBySignature(bClass, inheritorCandidate.getSignature(substitutor), false) == base;
}
}
@@ -15,18 +15,29 @@
*/
package com.intellij.codeInspection.redundantCast;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.miscGenerics.GenericsInspectionToolBase;
import com.intellij.codeInspection.miscGenerics.SuspiciousCollectionsMethodCallsInspection;
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.*;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.OverridingMethodsSearch;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.RedundantCastUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.IntArrayList;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
@@ -40,10 +51,15 @@ public class RedundantCastInspection extends GenericsInspectionToolBase {
private static final String DISPLAY_NAME = InspectionsBundle.message("inspection.redundant.cast.display.name");
@NonNls private static final String SHORT_NAME = "RedundantCast";
public boolean IGNORE_ANNOTATED_METHODS = false;
public boolean IGNORE_SUSPICIOUS_METHOD_CALLS = false;
public RedundantCastInspection() {
myQuickFixAction = new AcceptSuggested();
}
@Nullable
public ProblemDescriptor[] getDescriptions(PsiElement where, InspectionManager manager, boolean isOnTheFly) {
List<PsiTypeCastExpression> redundantCasts = RedundantCastUtil.getRedundantCastsInside(where);
if (redundantCasts.isEmpty()) return null;
@@ -58,10 +74,56 @@ public class RedundantCastInspection extends GenericsInspectionToolBase {
return descriptions.toArray(new ProblemDescriptor[descriptions.size()]);
}
@Override
public void writeSettings(Element node) throws WriteExternalException {
if (IGNORE_ANNOTATED_METHODS || IGNORE_SUSPICIOUS_METHOD_CALLS) {
super.writeSettings(node);
}
}
@Override
public JComponent createOptionsPanel() {
final MultipleCheckboxOptionsPanel optionsPanel = new MultipleCheckboxOptionsPanel(this);
optionsPanel.addCheckbox("Ignore casts appeared in suspicious collections method calls", "IGNORE_SUSPICIOUS_METHOD_CALLS");
optionsPanel.addCheckbox("Ignore casts to invoke @NotNull method which overrides @Nullable", "IGNORE_ANNOTATED_METHODS");
return optionsPanel;
}
@Nullable
private ProblemDescriptor createDescription(@NotNull PsiTypeCastExpression cast, @NotNull InspectionManager manager, boolean onTheFly) {
PsiExpression operand = cast.getOperand();
PsiTypeElement castType = cast.getCastType();
if (operand == null || castType == null) return null;
PsiElement parent = cast.getParent();
while (parent instanceof PsiParenthesizedExpression){
parent = parent.getParent();
}
if (parent instanceof PsiReferenceExpression) {
if (IGNORE_ANNOTATED_METHODS) {
final PsiElement gParent = parent.getParent();
if (gParent instanceof PsiMethodCallExpression) {
final PsiMethod psiMethod = ((PsiMethodCallExpression)gParent).resolveMethod();
if (psiMethod != null && AnnotationUtil.isNotNull(psiMethod)) {
final PsiClass superClass = PsiUtil.resolveClassInType(operand.getType());
for (PsiMethod method : psiMethod.findSuperMethods(superClass)) {
if (AnnotationUtil.isNullable(method)) {
return null;
}
}
}
}
}
} else if (parent instanceof PsiExpressionList) {
final PsiElement gParent = parent.getParent();
if (gParent instanceof PsiMethodCallExpression && IGNORE_SUSPICIOUS_METHOD_CALLS) {
final String message = SuspiciousCollectionsMethodCallsInspection
.getSuspiciousMethodCallMessage((PsiMethodCallExpression)gParent, operand.getType(), true, new ArrayList<PsiMethod>(), new IntArrayList());
if (message != null) {
return null;
}
}
}
String message = InspectionsBundle.message("inspection.redundant.cast.problem.descriptor",
"<code>" + operand.getText() + "</code>", "<code>#ref</code> #loc");
return manager.createProblemDescriptor(castType, message, myQuickFixAction, ProblemHighlightType.LIKE_UNUSED_SYMBOL, onTheFly);
@@ -1,4 +1,4 @@
/*
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -41,7 +41,7 @@ public class LanguageLevelProjectExtensionImpl extends LanguageLevelProjectExten
@Deprecated
@NonNls private static final String JDK_15_ATTR = "jdk-15";
private LanguageLevel myLanguageLevel = LanguageLevel.JDK_1_5;
private LanguageLevel myLanguageLevel = LanguageLevel.JDK_1_6;
private LanguageLevel myOriginalLanguageLevel = myLanguageLevel;
private final Project myProject;
@@ -317,7 +317,7 @@ public class MoveClassesOrPackagesHandlerBase extends MoveHandlerDelegate {
moveDirectoryDescription = "Move everything from " + myDirectories[0].getVirtualFile().getPresentableUrl() + " to another directory";
}
myRbMoveDirectory = new JRadioButton();
myRbMoveDirectory.setMnemonic('d');
myRbMoveDirectory.setMnemonic('e');
myRbMoveDirectory.setText(moveDirectoryDescription);
ButtonGroup gr = new ButtonGroup();
@@ -325,7 +325,11 @@ public class MoveClassesOrPackagesHandlerBase extends MoveHandlerDelegate {
gr.add(myRbRearrangePackage);
gr.add(myRbMoveDirectory);
new RadioUpDownListener(myRbMovePackage, myRbRearrangePackage, myRbMoveDirectory);
if (myRearrangePackagesEnabled) {
new RadioUpDownListener(myRbMovePackage, myRbRearrangePackage, myRbMoveDirectory);
} else {
new RadioUpDownListener(myRbMovePackage, myRbMoveDirectory);
}
Box box = Box.createVerticalBox();
box.add(Box.createVerticalStrut(5));
@@ -0,0 +1,23 @@
/*
* Copyright 2000-2011 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.
*/
import x.Base1.D;
import static <error descr="'D' is already defined in a single-type import">x.Base2.D</error>;
class UsageMixed {
void use() {
<error descr="Reference to 'D' is ambiguous, both 'x.Base1.D' and 'x.Base2.D' match">D</error>.class.getName();
}
}
@@ -19,6 +19,7 @@ public class Base1 {
public static final int F = 1;
public static void m(int i) { }
public static class F { }
public static class D { }
public interface I1 {
int IF = 1;
}
@@ -19,6 +19,7 @@ public class Base2 extends Base1 {
public static final float F = 2.0f;
public static void m(float f) { }
public static class F { }
public static class D { }
public interface II extends I1, I2 { }
public enum E { }
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>AmbigousParm1.java</file>
<line>11</line>
<description>Casting c to A is redundant</description>
</problem>
</problems>
@@ -0,0 +1,13 @@
interface A {}
interface B {}
class C implements A, B {}
public class AmbigousParameter {
public void ua(A a) {}
public void caller(C c) {
ua((A)c);
}
}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>AmbigousParm2.java</file>
<line>14</line>
<description>Casting c to A is redundant</description>
</problem>
<problem>
<file>AmbigousParm2.java</file>
<line>15</line>
<description>Casting a to A is redundant</description>
</problem>
</problems>
@@ -0,0 +1,17 @@
interface A {}
interface B {}
class C implements A, B {}
public class AmbigousParameter {
void m(A a) {}
void m(B b) {}
public void caller(C c) {
m((A)c);
m((A)null);
A a = (A)c;
m((A)a);
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>AmbigousParm3.java</file>
<line>13</line>
<description>Casting null to A is redundant</description>
</problem>
</problems>
@@ -0,0 +1,15 @@
interface A {}
interface B {}
public class AmbigousParameter {
public void f(A a) {}
public void f(B b) {}
public void f(Object o) {}
public void g(Object o) {}
public void caller() {
f((A)null);
g((A)null);
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,8 @@
import javax.swing.*;
import java.awt.*;
public class AmbigousParameter {
public void caller() {
new JDialog((Frame)null, "Title", true);
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,8 @@
import javax.swing.*;
import java.awt.*;
public class AmbigousParameter {
public void caller() {
new JDialog( ((Frame)null), "Title", true);
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,11 @@
package com;
public class Test {
static void f(Object s, Object o){}
static void f(String s, String o){}
void foo(){
Object o;
f((String)o, (String)o);
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Assignment1.java</file>
<line>4</line>
<description>Casting null to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
class Test{
static f(){
Object o;
o = (String)null;
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>4</line>
<description>Casting o to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
public class Test {
public boolean foo() {
Object o = null;
return (String) o == null;
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,12 @@
class Y {
int size = 4;
}
class Z extends Y {
int size = 5;
public static void main(String[] args) {
Z z = new Z();
System.out.println("z.size = " + ((Y)z).size);
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>DoubleCast1.java</file>
<line>7</line>
<description>Casting o to List is redundant</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
import java.util.List;
import java.util.ArrayList;
class Test{
static f(){
Object o;
ArrayList list = (ArrayList)(List)o;
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>DoubleCast2.java</file>
<line>7</line>
<description>Casting o to List is redundant</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
import java.util.List;
import java.util.ArrayList;
class Test{
static f(){
Object o;
ArrayList list = (ArrayList)((List)o);
}
}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>DoubleCast3.java</file>
<line>4</line>
<description>Casting '(String) o' to String is redundant</description>
</problem>
<problem>
<file>DoubleCast3.java</file>
<line>4</line>
<description>Casting 'o' to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
class Test{
static f(){
Object o;
String s = (String) (String) o;
}
}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>DoubleCast4.java</file>
<line>4</line>
<description>Casting '(String) o' to Object is redundant</description>
</problem>
<problem>
<file>DoubleCast4.java</file>
<line>4</line>
<description>Casting 'o' to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
class Test{
static f(){
Object o;
Object o2 = (Object) (String) o;
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,5 @@
class Test{
static f(double a){
double b = (double)(int)a / 100;
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,16 @@
class A {
void foo() throws Exception {}
}
class B extends A {
void foo() {
}
}
class C {
{
A a = new B();
((B) a).foo();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>A.java</file>
<line>3</line>
<description>Casting 'array' to String[] is redundant</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
public class Test {
public void foo(Object[] array) {
((String[]) array)[0] = " ";
}
public void bar(String[] array) {
((Object[]) array)[0] = new Object();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>A.java</file>
<line>13</line>
<description>Casting prices.get(null) to BidAsk is redundant</description>
</problem>
</problems>
@@ -0,0 +1,15 @@
class Getter {
BidAsk get(String s) {
return null;
}
}
class BidAsk {
public Object getOpenQuote;
}
public class A {
static Object f(Getter prices) {
return System.currentTimeMillis() == 2.0 ? ((BidAsk) prices.get(null)).getOpenQuote : null;
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>A.java</file>
<line>3</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;null&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
class A {
{
String s = true ? "" : (String) null; //cast is needed
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,12 @@
class Entry {
private final Object obj;
protected Entry(Directory parent) {
obj = ((Entry)parent).obj; //cast is needed because 'obj' is not visible with 'Directory' access class
}
}
class Directory extends Entry {
public Directory(Directory parent) {
super(parent);
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Initializer1.java</file>
<line>3</line>
<description>Casting null to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
class Test{
static f(){
Object o = (String)null;
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,5 @@
class A{
void f(){
double d = (double)1 / 5;
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>CastTest.java</file>
<line>7</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;ct1&lt;/code&gt; to &lt;code&gt;CastTest&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
class CastTest
{
public static void main (String[] args) throws CloneNotSupportedException
{
CastTest ct1 = new CastTest ();
// The cast of ct1 is obviously redundant (although the cast of the result is necessary)
CastTest ct2 = (CastTest) ((CastTest)ct1).clone();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>NestedThings.java</file>
<line>13</line>
<description>Casting "" to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,26 @@
class XXX {
String f() {
new Runnable() {
public void run() {
new Runnable() {
public void run() {
new Runnable() {
public void run() {
new Runnable() {
public void run() {
new Runnable() {
public void run() {
String s = (String)"";
}
};
}
};
}
};
}
};
}
};
return "";
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>New1.java</file>
<line>5</line>
<description>Casting null to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,7 @@
class Test{
Test(Object o){}
static f(){
new Test((String)null);
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>OneOfTwo.java</file>
<line>8</line>
<description>Casting o to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
package com;
public class Test {
static void f(String s, Object o){}
void foo(){
Object o;
f((String)o, (String)o);
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,9 @@
import java.awt.*;
public class Test {
void foo(){
Component c = null;
((Frame) c).show();
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,21 @@
class RedundantCastProblem {
public abstract static class Top {
public String f(Object o) {
return "Top.f(Object)";
}
}
public static class Sub extends Top {
public String f(String s) {
return "Middle.f(String)";
}
}
public static void main(String[] args) {
Sub sub = new Sub();
String aString = "";
System.out.println(((Top)sub).f(aString));
System.out.println(sub.f(aString));
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>3</line>
<description>Casting new Test() to Test is redundant</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
public class Test {
public void foo(Test t) {
foo(((Test) new Test()));
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,15 @@
public class Test {
class Super {
Object foo() { return new Object(); }
}
class Sub extends Super{
String foo() { return ""; }
}
public String get(final Super obj) {
if (obj instanceof Sub) {
return ((Sub)obj).foo();
} else {
return "The value is " + obj.foo();
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>3</line>
<description>Casting s to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
public class Test {
public void test(Object s) {
Object o = ((String) s);
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>7</line>
<description>Casting null to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
public class Test {
void foo(String msg){}
void foo(Object o){}
void method(){
foo((String)null);
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>4</line>
<description>Casting v to short is redundant</description>
</problem>
</problems>
@@ -0,0 +1,7 @@
class Test{
short foo(){
short v = 0;
short s = (short)v;
return s;
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,8 @@
class A{
String toHex(int i) {}
String toHex(short i) {}
void f(){
String result = toHex((short)'i');
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,5 @@
class A{
void f(){
double d = (int) 1.5;
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>VirtualMethod1.java</file>
<line>12</line>
<description>Casting a B is redundant</description>
</problem>
</problems>
@@ -0,0 +1,14 @@
class A{
void f(){}
}
class B extends A{
void f(){}
}
class Test{
static foo(){
A a;
((B)a).f();
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,14 @@
class A{
private void f(){}
}
class B extends A{
void f(){}
}
class Test{
static foo(){
A a;
((B)a).f();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>VirtualMethod3.java</file>
<line>4</line>
<description>Casting o to String is redundant</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
class Test{
static foo(){
Object o;
boolean res = ((String)o).equals(null);
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,7 @@
//This is a test for JDK_15 LanguageLevel
public class Test {
void foo () {
int x = 4;
((Integer) x).toString();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>3</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;&quot;&quot;&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -0,0 +1,7 @@
enum Test {
A((String) "");
Test(String s) {
}
}
@@ -0,0 +1,14 @@
<problems>
<problem>
<file>Test.java</file>
<line>11</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;this&lt;/code&gt; to &lt;code&gt;CastPreventsNPEDetection&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>Suspicious.java</file>
<line>9</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;str&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
import java.util.Map;
public class Suspicious {
Map<String, String> map;
void f(Object s){
String str = (String) s;
map.remove((String)s);
map.remove((String)str);
}
}
@@ -0,0 +1,21 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class CastPreventsNPEDetection {
@Nullable Object getParent() {
return null;
}
void f() {
((ChildCastImpl)this).getParent().toString();
((CastPreventsNPEDetection)this).getParent().toString();
}
}
class ChildCastImpl extends CastPreventsNPEDetection {
@NotNull
@Override
Object getParent() {
return super.getParent(); //To change body of overridden methods use File | Settings | File Templates.
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,20 @@
import java.util.Map;
class Test2 {
public String s;
public void maina(Object key, Map parameters) {
s = ((String[]) parameters.get(key))[0];
}
}
public class Test {
static class SomeClass {
public <T> T getX() {
return null;
}
}
public static void main(String[] args) {
//cast is needed for 'String' to be infered!
System.getProperty((String)new SomeClass().getX());
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,32 @@
import java.util.HashSet;
import java.util.Set;
final class Pair<A, B> {
public final A first;
public final B second;
public Pair(A first, B second) {
this.first = first;
this.second = second;
}
public final A getFirst() {
return first;
}
public final B getSecond() {
return second;
}
public static <A, B> Pair<A, B> create(A first, B second) {
return new Pair<A, B>(first, second);
}
}
class Test {
final Set<String> strings = new HashSet<String>();
final Pair<Set<String>, Set<String>> x = Boolean.TRUE.booleanValue()
? Pair.create(strings, strings)
: Pair.create(((Set<String>) null), (Set<String>) null); //these casts are not redundant
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>31</line>
<description>Casting 'null' to 'Set&lt;String&gt;' is redundant</description>
</problem>
<problem>
<file>Test.java</file>
<line>31</line>
<description>Casting 'null' to 'Set&lt;String&gt;' is redundant</description>
</problem>
</problems>
@@ -0,0 +1,32 @@
import java.util.HashSet;
import java.util.Set;
final class Pair<A, B> {
public final A first;
public final B second;
public Pair(A first, B second) {
this.first = first;
this.second = second;
}
public final A getFirst() {
return first;
}
public final B getSecond() {
return second;
}
public static <A> Pair<A, A> create(A first, A second) {
return new Pair<A, A>(first, second);
}
}
class Test {
final Set<String> strings = new HashSet<String>();
final Pair<Set<String>, Set<String>> x = Boolean.TRUE.booleanValue()
? Pair.create(strings, strings)
: Pair.create(((Set<String>) null), (Set<String>) null); //both casts are marked, but one is required for correct inference
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<description>Casting null to Class[] is redundant</description>
</problem>
<problem>
<file>Test.java</file>
<line>9</line>
<description>Casting null to Class[] is redundant</description>
</problem>
</problems>

Some files were not shown because too many files have changed in this diff Show More