Lines 7-12
Link Here
|
7 |
* |
7 |
* |
8 |
* Contributors: |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
|
|
10 |
* Stephan Herrmann - [null] no warning when unboxing SingleNameReference causes NPE, see https://bugs.eclipse.org/319201 |
10 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core.tests.compiler.regression; |
12 |
package org.eclipse.jdt.core.tests.compiler.regression; |
12 |
|
13 |
|
Lines 11662-11667
Link Here
|
11662 |
"----------\n", |
11663 |
"----------\n", |
11663 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
11664 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
11664 |
} |
11665 |
} |
|
|
11666 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201 |
11667 |
// unboxing raises an NPE |
11668 |
// LocalDeclaration |
11669 |
public void testBug319201() { |
11670 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
11671 |
return; |
11672 |
runNegativeTest( |
11673 |
new String[] { |
11674 |
"X.java", |
11675 |
"public class X {\n" + |
11676 |
" public void foo() {\n" + |
11677 |
" Integer i = null;\n" + |
11678 |
" int j = i;\n" + // should warn |
11679 |
" }\n" + |
11680 |
"}"}, |
11681 |
"----------\n" + |
11682 |
"1. ERROR in X.java (at line 4)\n" + |
11683 |
" int j = i;\n" + |
11684 |
" ^\n" + |
11685 |
"Null pointer access: The variable i can only be null at this location\n" + |
11686 |
"----------\n", |
11687 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
11688 |
} |
11689 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201 |
11690 |
// unboxing could raise an NPE |
11691 |
// Assignment |
11692 |
public void testBug319201a() { |
11693 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
11694 |
return; |
11695 |
runNegativeTest( |
11696 |
new String[] { |
11697 |
"X.java", |
11698 |
"public class X {\n" + |
11699 |
" public void foo(Integer i) {\n" + |
11700 |
" if (i == null) {};\n" + |
11701 |
" int j;\n" + |
11702 |
" j = i;\n" + // should warn |
11703 |
" }\n" + |
11704 |
"}"}, |
11705 |
"----------\n" + |
11706 |
"1. ERROR in X.java (at line 5)\n" + |
11707 |
" j = i;\n" + |
11708 |
" ^\n" + |
11709 |
"Potential null pointer access: The variable i may be null at this location\n" + |
11710 |
"----------\n", |
11711 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
11712 |
} |
11713 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201 |
11714 |
// unboxing raises an NPE |
11715 |
// MessageSend |
11716 |
public void testBug319201b() { |
11717 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
11718 |
return; |
11719 |
runNegativeTest( |
11720 |
new String[] { |
11721 |
"X.java", |
11722 |
"public class X {\n" + |
11723 |
" public void foo() {\n" + |
11724 |
" Boolean bo = null;;\n" + |
11725 |
" bar(bo);\n" + // should warn |
11726 |
" }\n" + |
11727 |
" void bar(boolean b) {}\n" + |
11728 |
"}"}, |
11729 |
"----------\n" + |
11730 |
"1. ERROR in X.java (at line 4)\n" + |
11731 |
" bar(bo);\n" + |
11732 |
" ^^\n" + |
11733 |
"Null pointer access: The variable bo can only be null at this location\n" + |
11734 |
"----------\n", |
11735 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
11736 |
} |
11737 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201 |
11738 |
// unboxing raises an NPE |
11739 |
// Node types covered (in this order): |
11740 |
// ExplicitConstructorCall |
11741 |
// AllocationExpression |
11742 |
// AND_AND_Expression |
11743 |
// OR_OR_Expression |
11744 |
// ArrayAllocationExpression |
11745 |
// ForStatement |
11746 |
// DoStatement |
11747 |
// IfStatement |
11748 |
// QualifiedAllocationExpression |
11749 |
// SwitchStatement |
11750 |
// WhileStatement |
11751 |
// CastExpression |
11752 |
// AssertStatement |
11753 |
// ReturnStatement |
11754 |
public void testBug319201c() { |
11755 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
11756 |
return; |
11757 |
runNegativeTest( |
11758 |
new String[] { |
11759 |
"X.java", |
11760 |
"class Y { public Y(boolean b1, boolean b2) {} }\n" + |
11761 |
"public class X extends Y {\n" + |
11762 |
" public X(boolean b, Boolean b2) {\n" + |
11763 |
" super(b2 == null, b2);\n" + |
11764 |
" }\n" + |
11765 |
" class Z {\n" + |
11766 |
" public Z(boolean b) {}\n" + |
11767 |
" }\n" + |
11768 |
" boolean fB = (Boolean)null;\n" + |
11769 |
" public boolean foo(boolean inB) {\n" + |
11770 |
" Boolean b1 = null;\n" + |
11771 |
" X x = new X(b1, null);\n" + |
11772 |
" Boolean b2 = null;\n" + |
11773 |
" boolean dontcare = b2 && inB;\n" + |
11774 |
" Boolean b3 = null;\n" + |
11775 |
" dontcare = inB || b3;\n" + |
11776 |
" Integer dims = null;\n" + |
11777 |
" char[] cs = new char[dims];\n" + |
11778 |
" Boolean b5 = null;\n" + |
11779 |
" do {\n" + |
11780 |
" Boolean b4 = null;\n" + |
11781 |
" for (int i=0;b4; i++);\n" + |
11782 |
" } while (b5);\n" + |
11783 |
" Boolean b6 = null;\n" + |
11784 |
" if (b6) { }\n" + |
11785 |
" Boolean b7 = null;\n" + |
11786 |
" Z z = this.new Z(b7);\n" + |
11787 |
" Integer sel = null;\n" + |
11788 |
" switch(sel) {\n" + |
11789 |
" case 1: break;\n" + |
11790 |
" default: break;\n" + |
11791 |
" }\n" + |
11792 |
" Boolean b8 = null;\n" + |
11793 |
" while (b8) {}\n" + |
11794 |
" Boolean b9 = null;\n" + |
11795 |
" dontcare = (boolean)b9;\n" + |
11796 |
" Boolean b10 = null;\n" + |
11797 |
" assert b10 : \"shouldn't happen, but will\";\n" + |
11798 |
" Boolean b11 = null;\n" + |
11799 |
" return b11;\n" + |
11800 |
" }\n" + |
11801 |
"}"}, |
11802 |
"----------\n" + |
11803 |
"1. ERROR in X.java (at line 4)\n" + |
11804 |
" super(b2 == null, b2);\n" + |
11805 |
" ^^\n" + |
11806 |
"Potential null pointer access: The variable b2 may be null at this location\n" + |
11807 |
"----------\n" + |
11808 |
"2. ERROR in X.java (at line 12)\n" + |
11809 |
" X x = new X(b1, null);\n" + |
11810 |
" ^^\n" + |
11811 |
"Null pointer access: The variable b1 can only be null at this location\n" + |
11812 |
"----------\n" + |
11813 |
"3. ERROR in X.java (at line 14)\n" + |
11814 |
" boolean dontcare = b2 && inB;\n" + |
11815 |
" ^^\n" + |
11816 |
"Null pointer access: The variable b2 can only be null at this location\n" + |
11817 |
"----------\n" + |
11818 |
"4. ERROR in X.java (at line 16)\n" + |
11819 |
" dontcare = inB || b3;\n" + |
11820 |
" ^^\n" + |
11821 |
"Null pointer access: The variable b3 can only be null at this location\n" + |
11822 |
"----------\n" + |
11823 |
"5. ERROR in X.java (at line 18)\n" + |
11824 |
" char[] cs = new char[dims];\n" + |
11825 |
" ^^^^\n" + |
11826 |
"Null pointer access: The variable dims can only be null at this location\n" + |
11827 |
"----------\n" + |
11828 |
"6. ERROR in X.java (at line 22)\n" + |
11829 |
" for (int i=0;b4; i++);\n" + |
11830 |
" ^^\n" + |
11831 |
"Null pointer access: The variable b4 can only be null at this location\n" + |
11832 |
"----------\n" + |
11833 |
"7. ERROR in X.java (at line 23)\n" + |
11834 |
" } while (b5);\n" + |
11835 |
" ^^\n" + |
11836 |
"Null pointer access: The variable b5 can only be null at this location\n" + |
11837 |
"----------\n" + |
11838 |
"8. ERROR in X.java (at line 25)\n" + |
11839 |
" if (b6) { }\n" + |
11840 |
" ^^\n" + |
11841 |
"Null pointer access: The variable b6 can only be null at this location\n" + |
11842 |
"----------\n" + |
11843 |
"9. ERROR in X.java (at line 27)\n" + |
11844 |
" Z z = this.new Z(b7);\n" + |
11845 |
" ^^\n" + |
11846 |
"Null pointer access: The variable b7 can only be null at this location\n" + |
11847 |
"----------\n" + |
11848 |
"10. ERROR in X.java (at line 29)\n" + |
11849 |
" switch(sel) {\n" + |
11850 |
" ^^^\n" + |
11851 |
"Null pointer access: The variable sel can only be null at this location\n" + |
11852 |
"----------\n" + |
11853 |
"11. ERROR in X.java (at line 34)\n" + |
11854 |
" while (b8) {}\n" + |
11855 |
" ^^\n" + |
11856 |
"Null pointer access: The variable b8 can only be null at this location\n" + |
11857 |
"----------\n" + |
11858 |
"12. ERROR in X.java (at line 36)\n" + |
11859 |
" dontcare = (boolean)b9;\n" + |
11860 |
" ^^\n" + |
11861 |
"Null pointer access: The variable b9 can only be null at this location\n" + |
11862 |
"----------\n" + |
11863 |
"13. ERROR in X.java (at line 38)\n" + |
11864 |
" assert b10 : \"shouldn\'t happen, but will\";\n" + |
11865 |
" ^^^\n" + |
11866 |
"Null pointer access: The variable b10 can only be null at this location\n" + |
11867 |
"----------\n" + |
11868 |
"14. ERROR in X.java (at line 40)\n" + |
11869 |
" return b11;\n" + |
11870 |
" ^^^\n" + |
11871 |
"Null pointer access: The variable b11 can only be null at this location\n" + |
11872 |
"----------\n", |
11873 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
11874 |
} |
11875 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201 |
11876 |
// unboxing raises an NPE |
11877 |
// DoStatement, variants with assignement and/or continue in the body & empty body |
11878 |
public void testBug319201d() { |
11879 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
11880 |
return; |
11881 |
Map customOptions = getCompilerOptions(); |
11882 |
customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryElse, CompilerOptions.IGNORE); |
11883 |
runNegativeTest( |
11884 |
new String[] { |
11885 |
"X.java", |
11886 |
"public class X {\n" + |
11887 |
" public void foo(boolean cond, boolean cond2) {\n" + |
11888 |
" Boolean b = null;\n" + |
11889 |
" do {\n" + |
11890 |
" b = false;\n" + |
11891 |
" if (cond) continue;\n" + // shouldn't make a difference |
11892 |
" } while (b);\n" + // don't complain, loop body has already assigned b |
11893 |
" Boolean b2 = null;\n" + |
11894 |
" do {\n" + |
11895 |
" if (cond) continue;\n" + |
11896 |
" b2 = false;\n" + |
11897 |
" } while (b2);\n" + // complain here: potentially null |
11898 |
" Boolean b3 = null;\n" + |
11899 |
" do {\n" + |
11900 |
" } while (b3);\n" + // complain here: definitely null |
11901 |
" Boolean b4 = null;\n" + |
11902 |
" do {\n" + |
11903 |
" if (cond) {\n" + |
11904 |
" b4 = true;\n" + |
11905 |
" if (cond2) continue;\n" + |
11906 |
" }\n" + |
11907 |
" b4 = false;\n" + |
11908 |
" } while (b4);\n" + // don't complain here: definitely non-null |
11909 |
" Boolean b5 = null;\n" + |
11910 |
" do {\n" + |
11911 |
" b5 = true;\n" + |
11912 |
" } while (b5);\n" + // don't complain |
11913 |
" Boolean b6 = null;\n" + |
11914 |
" do {\n" + |
11915 |
" b6 = true;\n" + |
11916 |
" continue;\n" + |
11917 |
" } while (b6); \n" + // don't complain |
11918 |
" Boolean b7 = null;\n" + |
11919 |
" Boolean b8 = null;\n" + |
11920 |
" do {\n" + |
11921 |
" if (cond) {\n" + |
11922 |
" b7 = true;\n" + |
11923 |
" continue;\n" + |
11924 |
" } else {\n" + |
11925 |
" b8 = true;\n" + |
11926 |
" }\n" + |
11927 |
" } while (b7);\n" + // complain here: after else branch b7 can still be null |
11928 |
" }\n" + |
11929 |
"}"}, |
11930 |
"----------\n" + |
11931 |
"1. ERROR in X.java (at line 12)\n" + |
11932 |
" } while (b2);\n" + |
11933 |
" ^^\n" + |
11934 |
"Potential null pointer access: The variable b2 may be null at this location\n" + |
11935 |
"----------\n" + |
11936 |
"2. ERROR in X.java (at line 15)\n" + |
11937 |
" } while (b3);\n" + |
11938 |
" ^^\n" + |
11939 |
"Null pointer access: The variable b3 can only be null at this location\n" + |
11940 |
"----------\n" + |
11941 |
"3. ERROR in X.java (at line 42)\n" + |
11942 |
" } while (b7);\n" + |
11943 |
" ^^\n" + |
11944 |
"Potential null pointer access: The variable b7 may be null at this location\n" + |
11945 |
"----------\n", |
11946 |
null/*classLibraries*/, |
11947 |
true/*shouldFlushOutputDirectory*/, |
11948 |
customOptions); |
11949 |
} |
11665 |
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=320414 |
11950 |
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=320414 |
11666 |
public void testBug320414() throws Exception { |
11951 |
public void testBug320414() throws Exception { |
11667 |
Map options = getCompilerOptions(); |
11952 |
Map options = getCompilerOptions(); |