diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/ControlFlowAnalyzer.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/ControlFlowAnalyzer.java index 524bc487cba8..86c3adf14d6b 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/ControlFlowAnalyzer.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/ControlFlowAnalyzer.java @@ -36,6 +36,7 @@ import com.intellij.codeInspection.dataFlow.types.DfTypes; import com.intellij.codeInspection.dataFlow.value.*; import com.intellij.codeInspection.dataFlow.value.DfaControlTransferValue.Trap; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.pom.java.JavaFeature; import com.intellij.psi.*; import com.intellij.psi.augment.PsiAugmentProvider; import com.intellij.psi.impl.source.tree.java.PsiEmptyExpressionImpl; @@ -1182,7 +1183,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { @NotNull DfaVariableValue patternDfaVar, @Nullable DfaAnchor instanceofAnchor) { if (sourcePattern == innerPattern || !JavaPsiPatternUtil.isUnconditionalForType(innerPattern, checkType)) { - addPatternTypeTest(innerPattern, instanceofAnchor, endPatternOffset, patternDfaVar); + addPatternTypeTest(innerPattern, instanceofAnchor, endPatternOffset, patternDfaVar, checkType); } else { addInstruction(new SimpleAssignmentInstruction(null, patternDfaVar)); @@ -1192,13 +1193,12 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { private void addPatternTypeTest(@NotNull PsiPattern pattern, @Nullable DfaAnchor instanceofAnchor, @NotNull DeferredOffset endPatternOffset, - @NotNull DfaVariableValue patternDfaVar) { + @NotNull DfaVariableValue patternDfaVar, + @NotNull PsiType checkType) { DeferredOffset condGotoOffset; addInstruction(new DupInstruction()); - addInstruction( - new PushValueInstruction(DfTypes.typedObject(JavaPsiPatternUtil.getPatternType(pattern), Nullability.NOT_NULL))); - - addInstruction(new InstanceofInstruction(instanceofAnchor, false)); + PsiType patternType = JavaPsiPatternUtil.getPatternType(pattern); + generateInstanceOfInstructions(pattern, instanceofAnchor, checkType, patternType); condGotoOffset = new DeferredOffset(); addInstruction(new ConditionalGotoInstruction(condGotoOffset, DfTypes.TRUE)); @@ -1211,6 +1211,163 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { condGotoOffset.setOffset(assignmentInstr.getIndex()); } + private void generateInstanceOfInstructions(@NotNull PsiElement context, + @Nullable DfaAnchor instanceofAnchor, + @NotNull PsiType checkType, + @Nullable PsiType patternType) { + if (patternType == null || + (!PsiUtil.isAvailable(JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS, context) && + (checkType instanceof PsiPrimitiveType || patternType instanceof PsiPrimitiveType)) || + ((checkType instanceof PsiPrimitiveType || patternType instanceof PsiPrimitiveType) && + !TypeConversionUtil.areTypesConvertible(checkType, patternType))) { + addInstruction(new PopInstruction()); + pushUnknown(); + return; + } + if ((checkType instanceof PsiPrimitiveType) && + patternType instanceof PsiPrimitiveType) { + if (checkType.equals(patternType) || + JavaPsiPatternUtil.isExactPrimitiveWideningConversion(checkType, patternType)) { + addInstruction(new PopInstruction()); + addInstruction(new PushValueInstruction(DfTypes.booleanValue(true))); + if (instanceofAnchor != null) { + addInstruction(new ResultOfInstruction(instanceofAnchor)); + } + } + else { + generateExactTestingConversion(context, checkType, patternType, instanceofAnchor); + } + } + else if (checkType instanceof PsiPrimitiveType) { + PsiClassType boxedType = ((PsiPrimitiveType)checkType).getBoxedType(context); + generateBoxingUnboxingInstructionFor(context, checkType, boxedType, false); + addInstruction(new PushValueInstruction(DfTypes.typedObject(patternType, Nullability.NOT_NULL))); + addInstruction(new InstanceofInstruction(instanceofAnchor, false)); + } + else if (patternType instanceof PsiPrimitiveType) { + PsiPrimitiveType unboxedCheckedType = PsiPrimitiveType.getUnboxedType(checkType); + if (unboxedCheckedType != null) { + boolean isWideningConversion = JavaPsiPatternUtil.isExactPrimitiveWideningConversion(unboxedCheckedType, patternType) || + unboxedCheckedType.equals(patternType); + if (!isWideningConversion) { + addInstruction(new DupInstruction()); + } + addInstruction(new PushValueInstruction(DfTypes.NULL)); + addInstruction(new BooleanBinaryInstruction(RelationType.NE, false, + isWideningConversion ? instanceofAnchor : null)); + if (!isWideningConversion) { + CFGBuilder builder = new CFGBuilder(this) //checkValue resultNotNull + .push(DfTypes.booleanValue(false)) //checkValue resultNotNull false + .ifCondition(RelationType.EQ) //checkValue + .pop() // + .push(DfTypes.booleanValue(false)) //false + .elseBranch(); //checkValue + generateBoxingUnboxingInstructionFor(context, checkType, unboxedCheckedType, false); + generateInstanceOfInstructions(context, null, unboxedCheckedType, patternType); + builder + .end(); + if (instanceofAnchor != null) { + addInstruction(new ResultOfInstruction(instanceofAnchor)); + } + } + } + else { + PsiClassType boxedType = ((PsiPrimitiveType)patternType).getBoxedType(context); + addInstruction(new PushValueInstruction(DfTypes.typedObject(boxedType, Nullability.NOT_NULL))); + addInstruction(new InstanceofInstruction(instanceofAnchor, false)); + } + } + else { + addInstruction(new PushValueInstruction(DfTypes.typedObject(patternType, Nullability.NOT_NULL))); + addInstruction(new InstanceofInstruction(instanceofAnchor, false)); + } + } + + private void generateExactTestingConversion(@NotNull PsiElement context, + @NotNull PsiType checkType, + @NotNull PsiType patternType, + @Nullable DfaAnchor instanceofAnchor) { + PsiType exactlyPromotedType = JavaPsiPatternUtil.getExactlyPromotedType(context, checkType, patternType); + boolean checkTypeIsDouble = checkType.equals(PsiTypes.doubleType()); + boolean checkTypeIsFloat = checkType.equals(PsiTypes.floatType()); + boolean checkTypeIsLong = checkType.equals(PsiTypes.longType()); + if (exactlyPromotedType.equalsToText("java.math.BigDecimal")) { + //there is no need to use BigDecimal explicitly, because + //there is some further simplification (see java.lang.runtime.ExactConversionsSupport) + if (checkTypeIsLong) { + exactlyPromotedType = PsiTypes.longType(); + } + else if (checkTypeIsFloat) { + exactlyPromotedType = PsiTypes.floatType(); + } + else { + exactlyPromotedType = PsiTypes.doubleType(); + } + } + CFGBuilder builder = new CFGBuilder(this); + boolean generateOr = false; + boolean generateAnd = false; + if (checkTypeIsFloat || checkTypeIsDouble) { + if (patternType.equals(PsiTypes.floatType()) || + patternType.equals(PsiTypes.doubleType())) { + generateOr = true; + builder //stack: checkValue + .dup() //checkValue checkValue + .dup() //checkValue checkValue checkValue + .compare(RelationType.EQ); //checkValue isNotNun + addInstruction(new NotInstruction(null)); //checkValue isNun + builder.swap(); //isNun checkValue + } + else { + generateAnd = true; + PsiClassType boxedType = checkTypeIsFloat + ? PsiTypes.floatType().getBoxedType(context) + : PsiTypes.doubleType().getBoxedType(context); + builder //stack: checkValue + .dup(); // checkValue checkValue + generateBoxingUnboxingInstructionFor(context, checkType, boxedType, false); //checkValue ReferenceOfCheckValue + builder.push(checkTypeIsFloat + ? DfTypes.floatValue(-0.0f) + : DfTypes.doubleValue(-0.0f)) ;// checkValue ReferenceOfCheckValue -0.0 + generateBoxingUnboxingInstructionFor(context, PsiTypes.doubleType(), boxedType, false); //checkValue ReferenceOfCheckValue ReferenceNegativeZero + addInstruction(new BooleanBinaryInstruction(RelationType.NE, true, null)); //checkValue IsNotMinusZero + builder.swap(); //IsNotMinusZero checkValue + if (patternType.equals(PsiTypes.longType())) { + builder.dup()//IsNotMinusZero checkValue checkValue + //see com.google.common.math.DoubleMath.MAX_LONG_AS_DOUBLE_PLUS_ONE for explanation + .push(checkTypeIsFloat + ? DfTypes.floatValue(0x1p63F) + : DfTypes.doubleValue(0x1p63)) //IsNotMinusZero checkValue checkValue maxLong + .compare(RelationType.NE) //IsNotMinusZero checkValue notMax + .splice(3, 1, 0, 2); //checkValue IsNotMinusZero notMax + addInstruction(new BooleanAndOrInstruction(false, null));//checkValue result + builder.swap(); //result checkValue + } + } + } else if(checkTypeIsLong && + (patternType.equals(PsiTypes.floatType()) || + patternType.equals(PsiTypes.doubleType()))){ + generateAnd = true; + builder //checkValue + .dup() //checkValue checkValue + .push(DfTypes.longValue(Long.MAX_VALUE)) //checkValue checkValue Max_long + .compare(RelationType.NE) //checkValue NotMaxLong + .swap(); //NotMaxLong checkValue + } + builder.dup(); //something checkValue checkValue + generateBoxingUnboxingInstructionFor(context, checkType, exactlyPromotedType, false); //something checkValue casted + builder.swap(); //something casted checkValue + generateBoxingUnboxingInstructionFor(context, checkType, patternType, false); + generateBoxingUnboxingInstructionFor(context, patternType, exactlyPromotedType, false); + addInstruction(new BooleanBinaryInstruction(RelationType.EQ, false, generateOr || generateAnd ? null : instanceofAnchor)); + if (generateOr) { + addInstruction(new BooleanAndOrInstruction(true, instanceofAnchor)); + } + if (generateAnd) { + addInstruction(new BooleanAndOrInstruction(false, instanceofAnchor)); + } + } + @Override public void visitMethodReferenceExpression(@NotNull PsiMethodReferenceExpression expression) { startElement(expression); @@ -1757,7 +1914,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { generateBoxingUnboxingInstructionFor(expression, expression.getType(), expectedType, false); } - void generateBoxingUnboxingInstructionFor(@NotNull PsiExpression context, PsiType actualType, PsiType expectedType, boolean explicit) { + void generateBoxingUnboxingInstructionFor(@NotNull PsiElement context, PsiType actualType, PsiType expectedType, boolean explicit) { if (PsiTypes.voidType().equals(expectedType)) return; if (TypeConversionUtil.isPrimitiveAndNotNull(expectedType) && @@ -1782,7 +1939,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { TypeConversionUtil.isPrimitiveAndNotNull(expectedType) && TypeConversionUtil.isNumericType(actualType) && TypeConversionUtil.isNumericType(expectedType)) { - DfaAnchor anchor = explicit ? new JavaExpressionAnchor(context) : null; + DfaAnchor anchor = (explicit && context instanceof PsiExpression psiExpression) ? new JavaExpressionAnchor(psiExpression) : null; addInstruction(new PrimitiveConversionInstruction((PsiPrimitiveType)expectedType, anchor)); } } @@ -1874,11 +2031,11 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { PsiTypeElement checkType = InstanceOfUtils.findCheckTypeElement(expression); CFGBuilder builder = new CFGBuilder(this); if (pattern == null) { - if (checkType == null) { + if (checkType == null || operandType == null) { pushUnknown(); } else { - buildSimpleInstanceof(builder, expression, operand, checkType); + buildSimpleInstanceof(builder, expression, operand, operandType, checkType); } } else if (operandType != null) { @@ -1895,15 +2052,15 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { finishElement(expression); } - private static void buildSimpleInstanceof(CFGBuilder builder, - PsiInstanceOfExpression expression, - PsiExpression operand, - PsiTypeElement checkType) { + private void buildSimpleInstanceof(@NotNull CFGBuilder builder, + @NotNull PsiInstanceOfExpression expression, + @NotNull PsiExpression operand, + @NotNull PsiType operandType, + @NotNull PsiTypeElement checkType) { PsiType type = checkType.getType(); builder - .pushExpression(operand) - .push(DfTypes.typedObject(type, Nullability.NOT_NULL)) - .isInstance(expression); + .pushExpression(operand); + generateInstanceOfInstructions(expression, new JavaExpressionAnchor(expression), operandType, type); } void addMethodThrows(@Nullable PsiMember methodOrClass) { diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/PatternVariableCanBeUsedInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/PatternVariableCanBeUsedInspection.java index 6c7ba7b20645..a1997030d648 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/PatternVariableCanBeUsedInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/PatternVariableCanBeUsedInspection.java @@ -177,7 +177,8 @@ public final class PatternVariableCanBeUsedInspection extends AbstractBaseJavaLo PsiExpression operand = cast.getOperand(); if (operand == null) return; PsiType castType = cast.getCastType().getType(); - if (castType instanceof PsiPrimitiveType) return; + if (castType instanceof PsiPrimitiveType && + !JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.isSufficient(PsiUtil.getLanguageLevel(operand))) return; if (!variable.getType().equals(castType)) return; PsiType operandType = operand.getType(); if (operandType == null || castType.isAssignableFrom(operandType)) return; @@ -431,7 +432,8 @@ public final class PatternVariableCanBeUsedInspection extends AbstractBaseJavaLo PsiExpression operand = expression.getOperand(); if (operand == null) return null; PsiType castType = castTypeElement.getType(); - if (castType instanceof PsiPrimitiveType) return null; + if (castType instanceof PsiPrimitiveType && + !JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.isSufficient(PsiUtil.getLanguageLevel(operand))) return null; PsiType operandType = operand.getType(); if (operandType == null || castType.isAssignableFrom(operandType)) return null; PsiInstanceOfExpression instanceOf = InstanceOfUtils.findPatternCandidate(expression, null); diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/dataFlow/ConstantValueInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/dataFlow/ConstantValueInspection.java index 6d76a93bc870..29bbcd23c5e3 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/dataFlow/ConstantValueInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/dataFlow/ConstantValueInspection.java @@ -321,13 +321,17 @@ public final class ConstantValueInspection extends AbstractBaseJavaLocalInspecti } if (expression instanceof PsiInstanceOfExpression instanceOf) { PsiType type = instanceOf.getOperand().getType(); - if (type == null || !TypeConstraints.instanceOf(type).isResolved()) return true; + LanguageLevel languageLevel = PsiUtil.getLanguageLevel(instanceOf); + if (type == null || + (!TypeConstraints.instanceOf(type).isResolved() && + (!JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.isSufficient(languageLevel) || + !(type instanceof PsiPrimitiveType)))) { + return true; + } PsiPattern pattern = instanceOf.getPattern(); if (pattern instanceof PsiTypeTestPattern typeTestPattern && typeTestPattern.getPatternVariable() != null) { PsiTypeElement checkType = typeTestPattern.getCheckType(); - LanguageLevel languageLevel = PsiUtil.getLanguageLevel(instanceOf); if (checkType != null && checkType.getType().isAssignableFrom(type) && - //see com.intellij.codeInsight.daemon.impl.analysis.HighlightVisitorImpl.visitInstanceOfExpression !JavaFeature.PATTERN_GUARDS_AND_RECORD_PATTERNS.isSufficient(languageLevel)) { // Reported as compilation error return true; diff --git a/java/java-psi-impl/src/com/intellij/psi/util/JavaPsiPatternUtil.java b/java/java-psi-impl/src/com/intellij/psi/util/JavaPsiPatternUtil.java index cbecb0bcfa0a..dab447e47d4e 100644 --- a/java/java-psi-impl/src/com/intellij/psi/util/JavaPsiPatternUtil.java +++ b/java/java-psi-impl/src/com/intellij/psi/util/JavaPsiPatternUtil.java @@ -4,6 +4,7 @@ package com.intellij.psi.util; import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; +import com.intellij.pom.java.JavaFeature; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.codeStyle.VariableKind; @@ -20,6 +21,9 @@ import org.jetbrains.annotations.Nullable; import java.util.*; public final class JavaPsiPatternUtil { + + private static final String JAVA_MATH_BIG_DECIMAL = "java.math.BigDecimal"; + /** * @param expression expression to search pattern variables in * @return list of pattern variables declared within an expression that could be visible outside of given expression. @@ -204,11 +208,109 @@ public final class JavaPsiPatternUtil { public static boolean isUnconditionalForType(@Nullable PsiCaseLabelElement pattern, @NotNull PsiType type, boolean forDomination) { PsiPrimaryPattern unconditionalPattern = findUnconditionalPattern(pattern); if (unconditionalPattern == null) return false; + PsiType patternType = getPatternType(unconditionalPattern); if (unconditionalPattern instanceof PsiDeconstructionPattern) { - return forDomination && dominates(getPatternType(unconditionalPattern), type); + return forDomination && dominates(patternType, type); } - else if (unconditionalPattern instanceof PsiTypeTestPattern || unconditionalPattern instanceof PsiUnnamedPattern) { - return dominates(getPatternType(unconditionalPattern), type); + else if ((unconditionalPattern instanceof PsiTypeTestPattern || + unconditionalPattern instanceof PsiUnnamedPattern) && + JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.isSufficient(PsiUtil.getLanguageLevel(pattern))) { + if (type instanceof PsiPrimitiveType || patternType instanceof PsiPrimitiveType) { + if (type.equals(patternType)) return true; + if (type instanceof PsiPrimitiveType && patternType instanceof PsiPrimitiveType) { + return isExactPrimitiveWideningConversion(type, patternType); + } + else if (!(type instanceof PsiPrimitiveType)) { + return false; + } + else { + PsiClassType boxedType = ((PsiPrimitiveType)type).getBoxedType(pattern); + return dominates(boxedType, type); + } + } + else { + return dominates(patternType, type); + } + } + return false; + } + + /** + * Returns the promoted type for a given context, type, and pattern type, according to 5.7.1 + * + * @param context the context element + * @param type the type to be promoted + * @param patternType the pattern type to compare with + * @return the promoted type, or the original type if no promotion is necessary + */ + @NotNull + public static PsiType getExactlyPromotedType(@NotNull PsiElement context, @NotNull PsiType type, @NotNull PsiType patternType) { + if (type.equals(patternType)) return type; + if ((type.equals(PsiTypes.byteType()) || + type.equals(PsiTypes.shortType())) && + patternType.equals(PsiTypes.charType())) { + return PsiTypes.intType(); + } + else if ((patternType.equals(PsiTypes.byteType()) || + patternType.equals(PsiTypes.shortType())) && + type.equals(PsiTypes.charType())) { + return PsiTypes.intType(); + } + else if ((type.equals(PsiTypes.intType()) && + patternType.equals(PsiTypes.floatType())) || + (type.equals(PsiTypes.floatType()) && + patternType.equals(PsiTypes.intType()))) { + return PsiTypes.doubleType(); + } + else if ((type.equals(PsiTypes.longType()) && + patternType.equals(PsiTypes.floatType())) || + (type.equals(PsiTypes.floatType()) && + patternType.equals(PsiTypes.longType())) || + + (type.equals(PsiTypes.longType()) && + patternType.equals(PsiTypes.doubleType())) || + (type.equals(PsiTypes.doubleType()) && + patternType.equals(PsiTypes.longType()))) { + return PsiType.getTypeByName(JAVA_MATH_BIG_DECIMAL, context.getProject(), context.getResolveScope()); + } + else { + return TypeConversionUtil.isAssignable(patternType, type) ? patternType : type; + } + } + + /** + * Checks if the given type is an exact primitive widening conversion of the pattern type according to 5.1.2 + * + * @param type the type to check + * @param patternType the pattern type to compare with + * @return true if the given type is an exact primitive widening conversion of the pattern type, false otherwise + */ + public static boolean isExactPrimitiveWideningConversion(@NotNull PsiType type, @NotNull PsiType patternType) { + if (type.equals(PsiTypes.byteType())) { + return patternType.equals(PsiTypes.shortType()) || + patternType.equals(PsiTypes.intType()) || + patternType.equals(PsiTypes.longType()) || + patternType.equals(PsiTypes.floatType()) || + patternType.equals(PsiTypes.doubleType()); + } + if (type.equals(PsiTypes.shortType())) { + return patternType.equals(PsiTypes.intType()) || + patternType.equals(PsiTypes.longType()) || + patternType.equals(PsiTypes.floatType()) || + patternType.equals(PsiTypes.doubleType()); + } + if (type.equals(PsiTypes.charType())) { + return patternType.equals(PsiTypes.intType()) || + patternType.equals(PsiTypes.longType()) || + patternType.equals(PsiTypes.floatType()) || + patternType.equals(PsiTypes.doubleType()); + } + if (type.equals(PsiTypes.intType())) { + return patternType.equals(PsiTypes.longType()) || + patternType.equals(PsiTypes.doubleType()); + } + if (type.equals(PsiTypes.floatType())) { + return patternType.equals(PsiTypes.doubleType()); } return false; } diff --git a/java/java-tests/testData/ig/com/siyeh/igtest/inheritance/redundant_method_override/ForEachPatterns.java b/java/java-tests/testData/ig/com/siyeh/igtest/inheritance/redundant_method_override/ForEachPatterns.java index bbe1bf7c928d..ca76cafc71c1 100644 --- a/java/java-tests/testData/ig/com/siyeh/igtest/inheritance/redundant_method_override/ForEachPatterns.java +++ b/java/java-tests/testData/ig/com/siyeh/igtest/inheritance/redundant_method_override/ForEachPatterns.java @@ -40,7 +40,7 @@ class Bar extends Foo { @Override void test2(Point[] points) { - for (Point(int x, int y) : points) {} + for (Point(int x, int y) : points) {} } @Override diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromBoxedObjectToPrimitive.java b/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromBoxedObjectToPrimitive.java new file mode 100644 index 000000000000..1ec270131af4 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromBoxedObjectToPrimitive.java @@ -0,0 +1,298 @@ +package dfa; + +public class InstanceofFromBoxedObjectToPrimitive { + public static void main(String[] args) { + System.out.println("testIntegerMin"); + testIntegerMin(); + System.out.println("testIntegerMax"); + testIntegerMax(); + System.out.println("testIntegerNull"); + testIntegerNull(); + System.out.println("testCharMin"); + testCharMin(); + System.out.println("testCharMax"); + testCharMax(); + System.out.println("testByteMin"); + testByteMin(); + System.out.println("testByteMax"); + testByteMax(); + System.out.println("testShortMin"); + testShortMin(); + System.out.println("testShortMax"); + testShortMax(); + System.out.println("testLongMin"); + testLongMin(); + System.out.println("testLongMax"); + testLongMax(); + System.out.println("testFloatMin"); + testFloatMin(); + System.out.println("testFloatMax"); + testFloatMax(); + System.out.println("testDoubleMin"); + testDoubleMin(); + System.out.println("testDoubleMax"); + testDoubleMax(); + System.out.println("testBooleanMin"); + testBooleanMin(); + System.out.println("testBooleanMax"); + testBooleanMax(); + } + + private static void testBooleanMin() { + Boolean o = false; + if (o instanceof boolean b) { //true + System.out.println("boolean"); + } + } + + private static void testBooleanMax() { + Boolean o = true; + if (o instanceof boolean b) { //true + System.out.println("boolean"); + } + } + + private static void testDoubleMin() { + Double o = 0.0; + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testDoubleMax() { + Double o = Double.MAX_VALUE; + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testFloatMin() { + Float o = 0.0F; + if (o instanceof float) { //true + System.out.println("float"); + } + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testFloatMax() { + Float o = Float.MAX_VALUE; + if (o instanceof float) { //true + System.out.println("float"); + } + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testLongMin() { + Long o = 0l; + if (o instanceof long) { //true + System.out.println("long"); + } + if (o instanceof float) { //true + System.out.println("float"); + } + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testLongMax() { + Long o = Long.MAX_VALUE; + if (o instanceof long) { //true + System.out.println("long"); + } + if (o instanceof float) { //false + System.out.println("float"); + } + if (o instanceof double) { //false + System.out.println("double"); + } + } + + private static void testShortMin() { + Short o = 0; + if (o instanceof int i) { //true + System.out.println("int"); + } + if (o instanceof short) { //true + System.out.println("short"); + } + if (o instanceof long) { //true + System.out.println("long"); + } + if (o instanceof float) { //true + System.out.println("float"); + } + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testShortMax() { + Short o = Short.MAX_VALUE; + if (o instanceof int i) { //true + System.out.println("int"); + } + if (o instanceof short) { //true + System.out.println("short"); + } + if (o instanceof long) { //true + System.out.println("long"); + } + if (o instanceof float) { //true + System.out.println("float"); + } + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testByteMin() { + Byte o = 0; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof byte) { //true + System.out.println("byte"); + } + if (o instanceof short) { //true + System.out.println("short"); + } + if (o instanceof long) { //true + System.out.println("long"); + } + if (o instanceof float) { //true + System.out.println("float"); + } + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testByteMax() { + Byte o = Byte.MAX_VALUE; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof byte) { //true + System.out.println("byte"); + } + if (o instanceof short) { //true + System.out.println("short"); + } + if (o instanceof long) { //true + System.out.println("long"); + } + if (o instanceof float) { //true + System.out.println("float"); + } + if (o instanceof double) { //true + System.out.println("double"); + } + } + + private static void testCharMin() { + Character o = 'c'; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testCharMax() { + Character o = Character.MAX_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testIntegerMin() { + Integer o = 0; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof long) {//true + System.out.println("long"); + } + if (o instanceof float) {//true + System.out.println("float"); + } + if (o instanceof double) {//true + System.out.println("double"); + } + } + + private static void testIntegerMax() { + Integer o = Integer.MAX_VALUE; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof long) {//true + System.out.println("long"); + } + if (o instanceof float) {//false + System.out.println("float"); + } + if (o instanceof double) {//true + System.out.println("double"); + } + } + + private static void testIntegerNull() { + Integer o = null; + if (o instanceof int) { //false + System.out.println("int"); + } + if (o instanceof long) {//false + System.out.println("long"); + } + if (o instanceof float) {//false + System.out.println("float"); + } + System.out.println("1"); + if (o instanceof double) {//false + System.out.println("double"); + } + } + + private static void testLongNull() { + Long o = null; + if (o instanceof long) {//false + System.out.println("long"); + } + if (o instanceof float) {//false + System.out.println("float"); + } + System.out.println("1"); + if (o instanceof double) {//false + System.out.println("double"); + } + System.out.println("2"); + } +} diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromObjectToPrimitive.java b/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromObjectToPrimitive.java new file mode 100644 index 000000000000..0d2a88e9a5bd --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromObjectToPrimitive.java @@ -0,0 +1,420 @@ +package dfa; + +public class InstanceofFromObjectToPrimitive { + public static void main(String[] args) { + testSimple(0); + testObjectNull(); + testObjectBoolean(); + testNumberInt(); + testObjectInt(); + testObjectDouble(); + testObjectBooleanPattern(); + testNumberIntPattern(); + testObjectIntPattern(); + testObjectNullPattern(); + testObjectDoublePattern(); + + testObjectRecord(); + testNumberRecord(); + testIntegerRecord(); + testIntegerRecordNotNull(); + testLongRecord(); + testLongRecordNotNull(); + } + + private static void testObjectRecord() { + ObjectRecord o = new ObjectRecord(1); + if (o instanceof ObjectRecord(int a)) { + System.out.println("int"); + } + if (o instanceof ObjectRecord(byte a)) { + System.out.println("byte"); + } + if (o instanceof ObjectRecord(short a)) { + System.out.println("short"); + } + if (o instanceof ObjectRecord(long a)) { + System.out.println("long"); + } + if (o instanceof ObjectRecord(float a)) { + System.out.println("float"); + } + if (o instanceof ObjectRecord(double a)) { + System.out.println("double"); + } + } + + private static void testNumberRecord() { + NumberRecord o = new NumberRecord(1); + if (o instanceof NumberRecord(int a)) { + System.out.println("int"); + } + if (o instanceof NumberRecord(byte a)) { + System.out.println("byte"); + } + if (o instanceof NumberRecord(short a)) { + System.out.println("short"); + } + if (o instanceof NumberRecord(long a)) { + System.out.println("long"); + } + if (o instanceof NumberRecord(float a)) { + System.out.println("float"); + } + if (o instanceof NumberRecord(double a)) { + System.out.println("double"); + } + } + + private static void testIntegerRecord() { + IntegerRecord o = new IntegerRecord(1); + if (o instanceof IntegerRecord(int a)) { + System.out.println("int"); + } + if (o instanceof IntegerRecord(long a)) { + System.out.println("long"); + } + if (o instanceof IntegerRecord(float a)) { + System.out.println("float"); + } + if (o instanceof IntegerRecord(double a)) { + System.out.println("double"); + } + } + + private static void testLongRecord() { + LongRecord o = new LongRecord(1L); + if (o instanceof LongRecord(long a)) { + System.out.println("long"); + } + if (o instanceof LongRecord(float a)) { + System.out.println("float"); + } + if (o instanceof LongRecord(double a)) { + System.out.println("double"); + } + } + + private static void testLongRecordNotNull() { + LongRecord o = new LongRecord(1L); + if (o.o() == null) { + return; + } + if (o instanceof LongRecord(long a)) { //true + System.out.println("long"); + } + if (o instanceof LongRecord(float a)) { + System.out.println("float"); + } + if (o instanceof LongRecord(double a)) { + System.out.println("double"); + } + } + + private static void testIntegerRecordNotNull() { + IntegerRecord o = new IntegerRecord(1); + if (o.o() == null) { + return; + } + if (o instanceof IntegerRecord(int a)) { //true + System.out.println("int"); + } + if (o instanceof IntegerRecord(long a)) { //true + System.out.println("long"); + } + if (o instanceof IntegerRecord(float a)) { + System.out.println("float"); + } + if (o instanceof IntegerRecord(double a)) {//true + System.out.println("double"); + } + } + + record ObjectRecord(Object o) { + } + + record NumberRecord(Number o) { + } + + record IntegerRecord(Integer o) { + } + + record LongRecord(Long o) { + } + + private static void testSimple(Object i) { + if (i instanceof int) { + System.out.println(i); + } + } + + public static void testObjectNull() { + Object o = null; + if (o instanceof int) { //false + System.out.println("int"); + } + if (o instanceof char) { //false + System.out.println("char"); + } + if (o instanceof byte) { //false + System.out.println("byte"); + } + if (o instanceof short) { //false + System.out.println("short"); + } + if (o instanceof long) { //false + System.out.println("long"); + } + if (o instanceof float) { //false + System.out.println("float"); + } + if (o instanceof double) { //false + System.out.println("double"); + } + if (o instanceof boolean) { //false + System.out.println("boolean"); + } + } + + + public static void testObjectInt() { + Object o = 1; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof char) { //false + System.out.println("char"); + } + if (o instanceof byte) { //false + System.out.println("byte"); + } + if (o instanceof short) { //false + System.out.println("short"); + } + if (o instanceof long) { //false + System.out.println("long"); + } + if (o instanceof float) { //false + System.out.println("float"); + } + if (o instanceof double) { //false + System.out.println("double"); + } + if (o instanceof boolean) { //false + System.out.println("boolean"); + } + } + + public static void testNumberInt() { + Number o = 1; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof byte) { //false + System.out.println("byte"); + } + if (o instanceof short) { //false + System.out.println("short"); + } + if (o instanceof long) { //false + System.out.println("long"); + } + if (o instanceof float) { //false + System.out.println("float"); + } + if (o instanceof double) { //false + System.out.println("double"); + } + } + + public static void testObjectBoolean() { + Object o = true; + if (o instanceof int) { //false + System.out.println("int"); + } + if (o instanceof char) { //false + System.out.println("char"); + } + if (o instanceof byte) { //false + System.out.println("byte"); + } + if (o instanceof short) { //false + System.out.println("short"); + } + if (o instanceof long) { //false + System.out.println("long"); + } + if (o instanceof float) { //false + System.out.println("float"); + } + if (o instanceof double) { //false + System.out.println("double"); + } + if (o instanceof boolean) { //true + System.out.println("boolean"); + } + } + + public static void testObjectDouble() { + Object o = 1.0; + if (o instanceof int) { //false + System.out.println("int"); + } + if (o instanceof char) { //false + System.out.println("char"); + } + if (o instanceof byte) { //false + System.out.println("byte"); + } + if (o instanceof short) { //false + System.out.println("short"); + } + if (o instanceof long) { //false + System.out.println("long"); + } + if (o instanceof float) { //false + System.out.println("float"); + } + if (o instanceof double) { //true + System.out.println("double"); + } + if (o instanceof boolean) { //false + System.out.println("boolean"); + } + } + + + public static void testObjectIntPattern() { + Object o = 1; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //false + System.out.println("double" + t); + } + if (o instanceof boolean t) { //false + System.out.println("boolean" + t); + } + } + + public static void testObjectNullPattern() { + Object o = null; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //false + System.out.println("double" + t); + } + if (o instanceof boolean t) { //false + System.out.println("boolean" + t); + } + } + + public static void testNumberIntPattern() { + Number o = 1; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //false + System.out.println("double" + t); + } + } + + public static void testObjectBooleanPattern() { + Object o = true; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //false + System.out.println("double" + t); + } + if (o instanceof boolean t) { //true + System.out.println("boolean" + t); + } + } + + public static void testObjectDoublePattern() { + Object o = 1.0; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + if (o instanceof boolean t) { //false + System.out.println("boolean" + t); + } + } +} diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromPrimitiveToObject.java b/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromPrimitiveToObject.java new file mode 100644 index 000000000000..0937719fc674 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromPrimitiveToObject.java @@ -0,0 +1,169 @@ +package dfa; + +public class InstanceofFromPrimitiveToObject { + public static void main(String[] args) { + testSimple(); + testInt(); + testChar(); + testByte(); + testShort(); + testLong(); + testFloat(); + testDouble(); + + testRecordInt(); + testRecordLong(); + testRecordFloat(); + } + + private static void testRecordInt() { + RecordInt a = new RecordInt(1); + if (a instanceof RecordInt(Object o)) { //true + System.out.println("Object"); + } + if (a instanceof RecordInt(Number o)) { //true + System.out.println("Number"); + } + if (a instanceof RecordInt(Integer o)) { //true + System.out.println("Integer"); + } + } + + private static void testRecordLong() { + RecordLong a = new RecordLong(1); + if (a instanceof RecordLong(Object o)) { //true + System.out.println("Object"); + } + if (a instanceof RecordLong(Number o)) { //true + System.out.println("Number"); + } + if (a instanceof RecordLong(Long o)) { //true + System.out.println("Long"); + } + } + + private static void testRecordFloat() { + RecordFloat a = new RecordFloat(1); + if (a instanceof RecordFloat(Object o)) { //true + System.out.println("Object"); + } + if (a instanceof RecordFloat(Number o)) { //true + System.out.println("Number"); + } + if (a instanceof RecordFloat(Float o)) { //true + System.out.println("Long"); + } + } + + record RecordInt(int a){} + record RecordLong(long a){} + record RecordFloat(float a){} + + + + private static void testDouble() { + double a = Double.MAX_VALUE; + if (a instanceof Object o) { //true + System.out.println("Object"); + } + if (a instanceof Number) { //true + System.out.println("Number"); + } + if (a instanceof Double) { //true + System.out.println("Double"); + } + } + + private static void testFloat() { + float a = 1; + if (a instanceof Object o) { //true + System.out.println("Object"); + } + if (a instanceof Number) { //true + System.out.println("Number"); + } + if (a instanceof Float) { //true + System.out.println("Float"); + } + } + + private static void testLong() { + long a = 1; + if (a instanceof Object o) { //true + System.out.println("Object"); + } + if (a instanceof Number) { //true + System.out.println("Number"); + } + if (a instanceof Long) { //true + System.out.println("Long"); + } + } + + private static void testShort() { + short a = 1; + if (a instanceof Object o) { //true + System.out.println("Object"); + } + if (a instanceof Number) { //true + System.out.println("Number"); + } + if (a instanceof Short) { //true + System.out.println("Short"); + } + } + + private static void testByte() { + byte a = 1; + if (a instanceof Object o) { //true + System.out.println("Object"); + } + if (a instanceof Number) { //true + System.out.println("Number"); + } + if (a instanceof Byte) { //true + System.out.println("Byte"); + } + } + + + private static void testChar() { + char a = Character.MAX_VALUE; + if (a instanceof Object o) { //true + System.out.println("Object"); + } + if (a instanceof Character) { //true + System.out.println("Character"); + } + } + + private static void testInt() { + int a = 1; + if (a instanceof Object o) { //true + System.out.println("Object"); + } + if (a instanceof Number) { //true + System.out.println("Number"); + } + if (a instanceof Integer) { //true + System.out.println("Integer"); + } + } + + private static void testSimple() { + int a = getInt(); + if (a instanceof Object o) { //true + System.out.println("Object"); + } + if (a instanceof Number) { //true + System.out.println("Number"); + } + if (a instanceof Integer) { //true + System.out.println("Integer"); + } + } + + private static int getInt() { + return 0; + } +} diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromPrimitiveToPrimitive.java b/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromPrimitiveToPrimitive.java new file mode 100644 index 000000000000..2ad2b191dd23 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/InstanceofFromPrimitiveToPrimitive.java @@ -0,0 +1,1141 @@ +package dfa; + +public class InstanceofFromPrimitiveToPrimitive { + public static void main(String[] args) { + System.out.println("testBoolean"); + testBoolean(); + System.out.println("testInt"); + testInt(); + System.out.println("testChar"); + testChar(); + System.out.println("testByte"); + testByte(); + System.out.println("testShort"); + testShort(); + System.out.println("testLong"); + testLong(); + System.out.println("testFloat"); + testFloat(); + System.out.println("testDouble"); + testDouble(); + } + + private static void testDouble() { + System.out.println(0); + double o = 0; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Float.MIN_VALUE"); + o = Float.MIN_VALUE; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Float.MAX_VALUE"); + o = Float.MAX_VALUE; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("Double.MIN_VALUE"); + o = Double.MIN_VALUE; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Double.MAX_VALUE"); + o = Double.MAX_VALUE; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("0x1p63"); + o = 0x1p63; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("0x1p5"); + o = 0x1p5; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("0x1p62"); + o = 0x1p62; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double) { //true + System.out.println("double"); + } + + + System.out.println("nan"); + o = Double.NaN; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("float positive inf"); + o = Float.POSITIVE_INFINITY; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("float negative inf"); + o = Float.NEGATIVE_INFINITY; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("double positive inf"); + o = Double.POSITIVE_INFINITY; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("double negative inf"); + o = Double.NEGATIVE_INFINITY; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("-0.0"); + o = -0.0f; + if (o instanceof int t) {//false + System.out.println("int" + t); + } + if (o instanceof char t) {//false + System.out.println("char" + t); + } + if (o instanceof byte t) {//false + System.out.println("byte" + t); + } + if (o instanceof short t) {//false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testFloat() { + System.out.println(0); + float o = 0; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Float.MIN_VALUE"); + o = Float.MIN_VALUE; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Float.MAX_VALUE"); + o = Float.MAX_VALUE; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("0x1p63f"); + o = 0x1p63f; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("0x1p5f"); + o = 0x1p5f; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("0x1p62f"); + o = 0x1p62f; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("nan"); + o = Float.NaN; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("positive inf"); + o = Float.POSITIVE_INFINITY; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("negative inf"); + o = Float.NEGATIVE_INFINITY; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("-0.0"); + o = -0.0f; + if (o instanceof int t) {//false + System.out.println("int" + t); + } + if (o instanceof char t) {//false + System.out.println("char" + t); + } + if (o instanceof byte t) {//false + System.out.println("byte" + t); + } + if (o instanceof short t) {//false + System.out.println("short" + t); + } + if (o instanceof long t) { //false + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("0x1p31f"); + o = 0x1p31f; + if (o instanceof int t) {//false + System.out.println("int" + t); + } + if (o instanceof char t) {//false + System.out.println("char" + t); + } + if (o instanceof byte t) {//false + System.out.println("byte" + t); + } + if (o instanceof short t) {//false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testLong() { + System.out.println(0); + long o = 0; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Long.MIN_VALUE"); + o = Long.MIN_VALUE; + if (o instanceof int t) { //false + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Long.MAX_VALUE"); + o = Long.MAX_VALUE; + if (o instanceof int) { //false + System.out.println("int"); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //false + System.out.println("double" + t); + } + + System.out.println("Long.MAX_VALUE - (long)1e19"); + o = Long.MAX_VALUE - (long) 9.22e18; + if (o instanceof int) { //false + System.out.println("int"); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("0x001fffffffffffffL"); + o = 0x001fffffffffffffL; + if (o instanceof int) { //false + System.out.println("int"); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testShort() { + System.out.println(0); + short o = 0; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Short.MIN_VALUE"); + o = Short.MIN_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Short.MAX_VALUE"); + o = Short.MAX_VALUE; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + + System.out.println("Byte.MIN_VALUE"); + o = Byte.MIN_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Byte.MAX_VALUE"); + o = Byte.MAX_VALUE; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testByte() { + System.out.println(0); + byte o = 0; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Byte.MIN_VALUE"); + o = Byte.MIN_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Byte.MAX_VALUE"); + o = Byte.MAX_VALUE; + if (o instanceof int) { //true + System.out.println("int"); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testChar() { + System.out.println(0); + char o = 0; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Character.MIN_VALUE"); + o = Character.MIN_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("Character.MAX_VALUE"); + o = Character.MAX_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testInt() { + System.out.println(0); + int o = 0; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //true + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + System.out.println("shortMax"); + o = Short.MAX_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //true + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //true + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + System.out.println("intMax"); + o = Integer.MAX_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //false + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + + System.out.println("intMax"); + o = Integer.MIN_VALUE; + if (o instanceof int t) { //true + System.out.println("int" + t); + } + if (o instanceof char t) { //false + System.out.println("char" + t); + } + if (o instanceof byte t) { //false + System.out.println("byte" + t); + } + if (o instanceof short t) { //false + System.out.println("short" + t); + } + if (o instanceof long t) { //true + System.out.println("long" + t); + } + if (o instanceof float t) { //true + System.out.println("float" + t); + } + if (o instanceof double t) { //true + System.out.println("double" + t); + } + } + + private static void testBoolean() { + System.out.println("true"); + boolean o = true; + if (o instanceof boolean b) { + System.out.println("boolean"); + System.out.println(b); + } + + System.out.println("false"); + o = false; + if (o instanceof boolean) { + System.out.println("boolean"); + } + } +} diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/SwitchWithPrimitive.java b/java/java-tests/testData/inspection/dataFlow/fixture/SwitchWithPrimitive.java new file mode 100644 index 000000000000..58bcc2d3c88e --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/SwitchWithPrimitive.java @@ -0,0 +1,38 @@ +package dfa; + +public class SwitchWithPrimitive { + public static void main(String[] args) { + testTestPattern(2L); + } + + private static void testTestPattern(Long l2) { + long l = 0; + switch (l) { + case int i -> System.out.println("1"); + case long i -> System.out.println("2"); + } + switch (l) { + case 0L -> System.out.println("2"); + case 1L -> System.out.println("2"); + case 2L -> System.out.println("2"); + case int i -> System.out.println("1"); + case long i -> System.out.println("1"); + } + switch (l) { + case 1L -> System.out.println("2"); + case 2L -> System.out.println("2"); + case long i -> System.out.println("1"); + } + switch (l2) { + case 0L -> System.out.println("2"); + case 1L -> System.out.println("2"); + case 2L -> System.out.println("2"); + case long i -> System.out.println("1"); + } + switch (l2) { + case 1L -> System.out.println("2"); + case 2L -> System.out.println("2"); + case long i -> System.out.println("1"); + } + } +} diff --git a/java/java-tests/testData/inspection/patternVariableCanBeUsedWithPrimitives/afterSimple.java b/java/java-tests/testData/inspection/patternVariableCanBeUsedWithPrimitives/afterSimple.java new file mode 100644 index 000000000000..9f7b486ab8c6 --- /dev/null +++ b/java/java-tests/testData/inspection/patternVariableCanBeUsedWithPrimitives/afterSimple.java @@ -0,0 +1,7 @@ +// "Replace 's' with pattern variable" "true" +class X { + void test(float obj) { + if (obj instanceof int s) { + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/patternVariableCanBeUsedWithPrimitives/beforeSimple.java b/java/java-tests/testData/inspection/patternVariableCanBeUsedWithPrimitives/beforeSimple.java new file mode 100644 index 000000000000..7173fcf0ed12 --- /dev/null +++ b/java/java-tests/testData/inspection/patternVariableCanBeUsedWithPrimitives/beforeSimple.java @@ -0,0 +1,8 @@ +// "Replace 's' with pattern variable" "true" +class X { + void test(float obj) { + if (obj instanceof int) { + int s = (int)obj; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightPrimitivePatternsHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightPrimitivePatternsHighlightingTest.java index 60c284ab693d..efc4199c23df 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightPrimitivePatternsHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightPrimitivePatternsHighlightingTest.java @@ -2,6 +2,7 @@ package com.intellij.java.codeInsight.daemon; import com.intellij.JavaTestUtil; +import com.intellij.pom.java.JavaFeature; import com.intellij.testFramework.LightProjectDescriptor; import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; import org.jetbrains.annotations.NotNull; @@ -15,8 +16,7 @@ public class LightPrimitivePatternsHighlightingTest extends LightJavaCodeInsight @NotNull @Override protected LightProjectDescriptor getProjectDescriptor() { - //todo change after new Java - return JAVA_X; + return new ProjectDescriptor(JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.getMinimumLevel()); } public void testRecordPrimitiveInstanceOfPattern() { diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionPrimitivesInPatternsTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionPrimitivesInPatternsTest.java new file mode 100644 index 000000000000..3e92190662c3 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionPrimitivesInPatternsTest.java @@ -0,0 +1,41 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.codeInspection; + +import com.intellij.JavaTestUtil; +import com.intellij.pom.java.JavaFeature; +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; + +public class DataFlowInspectionPrimitivesInPatternsTest extends DataFlowInspectionTestCase { + + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return new ProjectDescriptor(JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.getMinimumLevel()); + } + + public void testInstanceofFromBoxedObjectToPrimitive() { + doTest(); + } + + public void testInstanceofFromObjectToPrimitive() { + doTest(); + } + + public void testInstanceofFromPrimitiveToObject() { + doTest(); + } + + public void testInstanceofFromPrimitiveToPrimitive() { + doTest(); + } + + public void testSwitchWithPrimitive() { + doTest(); + } + + @Override + protected String getTestDataPath() { + return JavaTestUtil.getJavaTestDataPath() + "/inspection/dataFlow/fixture/"; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/PatternVariableCanBeUsedWithPrimitivesInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/PatternVariableCanBeUsedWithPrimitivesInspectionTest.java new file mode 100644 index 000000000000..859feb8776bb --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/PatternVariableCanBeUsedWithPrimitivesInspectionTest.java @@ -0,0 +1,35 @@ +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInspection; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.PatternVariableCanBeUsedInspection; +import com.intellij.pom.java.JavaFeature; +import com.intellij.pom.java.LanguageLevel; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NotNull; + +public class PatternVariableCanBeUsedWithPrimitivesInspectionTest extends LightQuickFixParameterizedTestCase { + @Override + protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() { + PatternVariableCanBeUsedInspection inspection = new PatternVariableCanBeUsedInspection(); + inspection.reportAlsoCastWithIntroducingNewVariable = true; + return new LocalInspectionTool[]{inspection}; + } + + @Override + protected LanguageLevel getLanguageLevel() { + return JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.getMinimumLevel(); + } + + @Override + protected String getBasePath() { + return "/inspection/patternVariableCanBeUsedWithPrimitives"; + } + + @Override + protected @NotNull LightProjectDescriptor getProjectDescriptor() { + return LightJavaCodeInsightFixtureTestCase.JAVA_X; + } +}