View | Details | Raw Unified | Return to bug 335845 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java (-1 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 60-65 Link Here
60
			flowInfo.unconditionalCopy(),
60
			flowInfo.unconditionalCopy(),
61
			currentScope);
61
			currentScope);
62
	}
62
	}
63
	if (this.binding.declaringClass.isMemberType() && !this.binding.declaringClass.isStatic()) {
64
		// allocating a non-static member type without an enclosing instance of parent type
65
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
66
		currentScope.resetEnclosingMethodStaticFlag();
67
	}
63
	manageEnclosingInstanceAccessIfNecessary(currentScope, flowInfo);
68
	manageEnclosingInstanceAccessIfNecessary(currentScope, flowInfo);
64
	manageSyntheticAccessIfNecessary(currentScope, flowInfo);
69
	manageSyntheticAccessIfNecessary(currentScope, flowInfo);
65
70
(-)src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java (-1 / +236 lines)
Lines 30-36 Link Here
30
// Static initializer to specify tests subset using TESTS_* static variables
30
// Static initializer to specify tests subset using TESTS_* static variables
31
// All specified tests which does not belong to the class are skipped...
31
// All specified tests which does not belong to the class are skipped...
32
static {
32
static {
33
//		TESTS_NAMES = new String[] { "test123" };
33
//		TESTS_NAMES = new String[] { "testBug335845g" };
34
//		TESTS_NUMBERS = new int[] { 113 };
34
//		TESTS_NUMBERS = new int[] { 113 };
35
//		TESTS_RANGE = new int[] { 108, -1 };
35
//		TESTS_RANGE = new int[] { 108, -1 };
36
}
36
}
Lines 6633-6636 Link Here
6633
		compilerOptions /* custom options */
6633
		compilerOptions /* custom options */
6634
	);
6634
	);
6635
}
6635
}
6636
6637
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
6638
// If method allocates an inner non-static type without an enclosing object, method can't be static
6639
public void testBug335845a() {
6640
	Map compilerOptions = getCompilerOptions();
6641
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6642
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6643
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6644
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE);
6645
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.IGNORE);
6646
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
6647
	compilerOptions.put(CompilerOptions.OPTION_ReportSyntheticAccessEmulation, CompilerOptions.IGNORE);
6648
	this.runNegativeTest(
6649
		new String[] {
6650
				"X.java", 
6651
				"public class X {\n" +
6652
				"	private class Bar {\n" +
6653
				"		int a = 1;\n" +
6654
				"	}\n" + 
6655
				"	private void foo() {\n" + 	// don't warn
6656
				"		new Bar();\n" +
6657
				"	}\n" + 
6658
				"}"
6659
		},
6660
		"",
6661
		null /* no extra class libraries */,
6662
		true /* flush output directory */,
6663
		compilerOptions /* custom options */
6664
	);
6665
}
6666
6667
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
6668
// If method allocates an inner non-static type without an enclosing object, method can't be static
6669
public void testBug335845b() {
6670
	Map compilerOptions = getCompilerOptions();
6671
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6672
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6673
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6674
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE);
6675
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.IGNORE);
6676
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
6677
	compilerOptions.put(CompilerOptions.OPTION_ReportSyntheticAccessEmulation, CompilerOptions.IGNORE);
6678
	this.runNegativeTest(
6679
		new String[] {
6680
				"X.java", 
6681
				"public class X {\n" +
6682
				"	private class Bar {\n" +
6683
				"		int a = 1;\n" +
6684
				"	}\n" + 
6685
				"	private void foo() {\n" + 	// don't warn
6686
				"		int x = new Bar().a;\n" +
6687
				"	}\n" + 
6688
				"}"
6689
		},
6690
		"",
6691
		null /* no extra class libraries */,
6692
		true /* flush output directory */,
6693
		compilerOptions /* custom options */
6694
	);
6695
}
6696
6697
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
6698
// If method allocates an inner static type without an enclosing object, method can be static
6699
public void testBug335845c() {
6700
	Map compilerOptions = getCompilerOptions();
6701
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6702
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6703
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6704
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE);
6705
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.IGNORE);
6706
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
6707
	compilerOptions.put(CompilerOptions.OPTION_ReportSyntheticAccessEmulation, CompilerOptions.IGNORE);
6708
	this.runNegativeTest(
6709
		new String[] {
6710
				"X.java", 
6711
				"public class X {\n" +
6712
				"	private static class Bar {\n" +
6713
				"		int a = 1;\n" +
6714
				"	}\n" + 
6715
				"	private void foo() {\n" + 	// warn since Bar is static
6716
				"		new Bar();\n" +
6717
				"		int x = new Bar().a;" +
6718
				"	}\n" + 
6719
				"}"
6720
		},
6721
		"----------\n" + 
6722
		"1. ERROR in X.java (at line 5)\n" + 
6723
		"	private void foo() {\n" + 
6724
		"	             ^^^^^\n" + 
6725
		"The method foo() from the type X can be declared as static\n" + 
6726
		"----------\n",
6727
		null /* no extra class libraries */,
6728
		true /* flush output directory */,
6729
		compilerOptions /* custom options */
6730
	);
6731
}
6732
6733
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
6734
// If method allocates an inner non-static type without an enclosing object, method can't be static
6735
public void testBug335845d() {
6736
	Map compilerOptions = getCompilerOptions();
6737
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6738
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6739
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6740
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE);
6741
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.IGNORE);
6742
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
6743
	compilerOptions.put(CompilerOptions.OPTION_ReportSyntheticAccessEmulation, CompilerOptions.IGNORE);
6744
	this.runNegativeTest(
6745
		new String[] {
6746
				"X.java", 
6747
				"public class X {\n" +
6748
				"	private class Bar {\n" +
6749
				"		class Bar2{}\n" +
6750
				"	}\n" + 
6751
				"	private void foo() {\n" + 	// don't warn
6752
				"		new Bar().new Bar2();\n" +
6753
				"	}\n" + 
6754
				"}"
6755
		},
6756
		"",
6757
		null /* no extra class libraries */,
6758
		true /* flush output directory */,
6759
		compilerOptions /* custom options */
6760
	);
6761
}
6762
6763
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
6764
// If method allocates an inner static type without an enclosing object, method can be static
6765
public void testBug335845e() {
6766
	Map compilerOptions = getCompilerOptions();
6767
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6768
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6769
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6770
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE);
6771
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.IGNORE);
6772
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
6773
	compilerOptions.put(CompilerOptions.OPTION_ReportSyntheticAccessEmulation, CompilerOptions.IGNORE);
6774
	this.runNegativeTest(
6775
		new String[] {
6776
				"X.java", 
6777
				"public class X {\n" +
6778
				"	private class Bar {\n" +
6779
				"		int a = 1;\n" +
6780
				"	}\n" + 
6781
				"	private void foo() {\n" + 	// warn since Bar is allocated via Test object
6782
				"		new X().new Bar();\n" +
6783
				"	}\n" + 
6784
				"}"
6785
		},
6786
		"----------\n" + 
6787
		"1. ERROR in X.java (at line 5)\n" + 
6788
		"	private void foo() {\n" + 
6789
		"	             ^^^^^\n" + 
6790
		"The method foo() from the type X can be declared as static\n" + 
6791
		"----------\n",
6792
		null /* no extra class libraries */,
6793
		true /* flush output directory */,
6794
		compilerOptions /* custom options */
6795
	);
6796
}
6797
6798
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
6799
// If method allocates an inner static type without an enclosing object, method can be static
6800
public void testBug335845f() {
6801
	Map compilerOptions = getCompilerOptions();
6802
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6803
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6804
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6805
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE);
6806
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.IGNORE);
6807
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
6808
	compilerOptions.put(CompilerOptions.OPTION_ReportSyntheticAccessEmulation, CompilerOptions.IGNORE);
6809
	this.runNegativeTest(
6810
		new String[] {
6811
				"X.java", 
6812
				"public class X {\n" +
6813
				"	private class Bar {\n" +
6814
				"		int a = 1;\n" +
6815
				"	}\n" + 
6816
				"	private void foo() {\n" + 	// warn since Bar is allocated via Test object
6817
				"		X x = new X();" +
6818
				"		x.new Bar().a = 2;\n" +
6819
				"	}\n" + 
6820
				"}"
6821
		},
6822
		"----------\n" + 
6823
		"1. ERROR in X.java (at line 5)\n" + 
6824
		"	private void foo() {\n" + 
6825
		"	             ^^^^^\n" + 
6826
		"The method foo() from the type X can be declared as static\n" + 
6827
		"----------\n",
6828
		null /* no extra class libraries */,
6829
		true /* flush output directory */,
6830
		compilerOptions /* custom options */
6831
	);
6832
}
6833
6834
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
6835
// If method allocates an inner static type without an enclosing object, method can be static
6836
public void testBug335845g() {
6837
	Map compilerOptions = getCompilerOptions();
6838
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6839
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6840
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6841
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE);
6842
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.IGNORE);
6843
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
6844
	compilerOptions.put(CompilerOptions.OPTION_ReportSyntheticAccessEmulation, CompilerOptions.IGNORE);
6845
	this.runConformTest(
6846
		new String[] {
6847
				"p/X.java", 
6848
				"package p;\n" +
6849
				"public class X {\n" +
6850
				"	class Bar {\n" +
6851
				"	}\n" +
6852
				"}"
6853
		}
6854
	);
6855
	this.runNegativeTest(
6856
		new String[] {
6857
				"p/Y.java",
6858
				"package p;\n" +
6859
				"public class Y extends X {\n" +
6860
				"	private void foo() {\n" + 	// warn since Bar is allocated via Test object
6861
				"		new Bar();\n" +
6862
				"	}\n" + 
6863
				"}"
6864
		},
6865
		"",
6866
		null /* no extra class libraries */,
6867
		false /* flush output directory */,
6868
		compilerOptions /* custom options */
6869
	);
6870
}
6636
}
6871
}

Return to bug 335845