Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 376550 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java (-1 / +762 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
2
 * Copyright (c) 2000, 2012 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 7188-7191 Link Here
7188
		// javac options
7188
		// javac options
7189
		JavacTestOptions.SKIP_UNTIL_FRAMEWORK_FIX /* javac test options */);
7189
		JavacTestOptions.SKIP_UNTIL_FRAMEWORK_FIX /* javac test options */);
7190
}
7190
}
7191
7192
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7193
// SingleNameReference, assignment of instance field inside a local class method
7194
public void test376550_1a() {
7195
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7196
		return;
7197
	Map compilerOptions = getCompilerOptions();
7198
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7199
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7200
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7201
	this.runNegativeTest(
7202
		new String[] {
7203
				"X.java", 
7204
				"public class X {\n" +
7205
				"	int i = 1;\n" +
7206
				"   public void upper1(){}\n" +
7207
				"   public void foo(){\n" + // can't be static
7208
				"   	class Local{\n" +
7209
				"			int i2 = 1;\n" +
7210
				"			void method1() {\n" + // can't be static
7211
				"				i = 1;\n" +
7212
				"			}\n" +
7213
				"		}\n" +
7214
				"	}\n" +
7215
				"}"
7216
		},
7217
		"",
7218
		null /* no extra class libraries */,
7219
		true /* flush output directory */,
7220
		compilerOptions /* custom options */
7221
	);
7222
}
7223
7224
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7225
// SingleNameReference, assignment of instance field of local class inside a local class method
7226
public void test376550_1b() {
7227
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7228
		return;
7229
	Map compilerOptions = getCompilerOptions();
7230
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7231
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7232
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7233
	this.runNegativeTest(
7234
		new String[] {
7235
				"X.java", 
7236
				"public class X {\n" +
7237
				"	int i = 1;\n" +
7238
				"   public void upper1(){}\n" +
7239
				"   public void foo(){\n" + // can be static
7240
				"   	class Local{\n" +
7241
				"			int i2 = 1;\n" +
7242
				"			void method2() {\n" +  // can't be static
7243
				"				i2 = 1;\n" +
7244
				"			}\n" +
7245
				"		}\n" +
7246
				"	}\n" +
7247
				"}"
7248
		},
7249
		"----------\n" + 
7250
		"1. ERROR in X.java (at line 4)\n" + 
7251
		"	public void foo(){\n" + 
7252
		"	            ^^^^^\n" + 
7253
		"The method foo() from the type X can potentially be declared as static\n" + 
7254
		"----------\n",
7255
		null /* no extra class libraries */,
7256
		true /* flush output directory */,
7257
		compilerOptions /* custom options */
7258
	);
7259
}
7260
7261
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7262
// LocalDeclaration with type as a type variable binding
7263
public void test376550_2a() {
7264
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7265
		return;
7266
	Map compilerOptions = getCompilerOptions();
7267
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7268
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7269
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7270
	this.runNegativeTest(
7271
		new String[] {
7272
				"X.java", 
7273
				"public class X<T> {\n" +
7274
				"   public void upper1(){}\n" +
7275
				"   public void foo(){\n" + // can be static
7276
				"   	class Local<K>{\n" +
7277
				"			void method2() {\n" +  // can't be static
7278
				"				K k;\n" +
7279
				"			}\n" +
7280
				"		}\n" +
7281
				"	}\n" +
7282
				"}"
7283
		},
7284
		"----------\n" + 
7285
		"1. ERROR in X.java (at line 3)\n" + 
7286
		"	public void foo(){\n" + 
7287
		"	            ^^^^^\n" + 
7288
		"The method foo() from the type X<T> can potentially be declared as static\n" + 
7289
		"----------\n",
7290
		null /* no extra class libraries */,
7291
		true /* flush output directory */,
7292
		compilerOptions /* custom options */
7293
	);
7294
}
7295
7296
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7297
// LocalDeclaration with type as a type variable binding
7298
public void test376550_2b() {
7299
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7300
		return;
7301
	Map compilerOptions = getCompilerOptions();
7302
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7303
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7304
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7305
	this.runNegativeTest(
7306
		new String[] {
7307
				"X.java", 
7308
				"public class X<T> {\n" +
7309
				"   public void upper1(){}\n" +
7310
				"   public void foo(){\n" + // can't be static
7311
				"   	class Local<K>{\n" +
7312
				"			void method2() {\n" +  // can't be static
7313
				"				T t;\n" +
7314
				"			}\n" +
7315
				"		}\n" +
7316
				"	}\n" +
7317
				"}"
7318
		},
7319
		"",
7320
		null /* no extra class libraries */,
7321
		true /* flush output directory */,
7322
		compilerOptions /* custom options */
7323
	);
7324
}
7325
7326
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7327
// MessageSend, calling outer class method inside a local class method
7328
public void test376550_3a() {
7329
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7330
		return;
7331
	Map compilerOptions = getCompilerOptions();
7332
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7333
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7334
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7335
	this.runNegativeTest(
7336
		new String[] {
7337
				"X.java", 
7338
				"public class X<T> {\n" +
7339
				"   public void upper1(){}\n" +
7340
				"   public void foo(){\n" + // can't be static
7341
				"   	class Local<K>{\n" +
7342
				"			void lower() {}\n" +
7343
				"			void method2() {\n" +  // can't be static
7344
				"				upper1();\n" +
7345
				"			}\n" +
7346
				"		}\n" +
7347
				"	}\n" +
7348
				"}"
7349
		},
7350
		"",
7351
		null /* no extra class libraries */,
7352
		true /* flush output directory */,
7353
		compilerOptions /* custom options */
7354
	);
7355
}
7356
7357
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7358
// MessageSend, calling local class method inside a local class method
7359
public void test376550_3b() {
7360
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7361
		return;
7362
	Map compilerOptions = getCompilerOptions();
7363
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7364
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7365
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7366
	this.runNegativeTest(
7367
		new String[] {
7368
				"X.java", 
7369
				"public class X<T> {\n" +
7370
				"   public void upper1(){}\n" +
7371
				"   public void foo(){\n" + // can be static
7372
				"   	class Local<K>{\n" +
7373
				"			void lower() {}\n" +
7374
				"			void method2() {\n" +  // can't be static
7375
				"				lower();\n" +
7376
				"			}\n" +
7377
				"		}\n" +
7378
				"	}\n" +
7379
				"}"
7380
		},
7381
		"----------\n" + 
7382
		"1. ERROR in X.java (at line 3)\n" + 
7383
		"	public void foo(){\n" + 
7384
		"	            ^^^^^\n" + 
7385
		"The method foo() from the type X<T> can potentially be declared as static\n" + 
7386
		"----------\n",
7387
		null /* no extra class libraries */,
7388
		true /* flush output directory */,
7389
		compilerOptions /* custom options */
7390
	);
7391
}
7392
7393
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7394
// Local class instance field is an argument in messageSend in local class method
7395
public void test376550_4a() {
7396
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7397
		return;
7398
	Map compilerOptions = getCompilerOptions();
7399
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7400
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7401
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7402
	this.runNegativeTest(
7403
		new String[] {
7404
				"X.java", 
7405
				"public class X<T> {\n" +
7406
				"   int i1 = 1;\n" +
7407
				"   public void foo(){\n" + // can be static
7408
				"   	class Local<K>{\n" +
7409
				"			int i2 = 1;\n" +
7410
				"			void lower(int i) {}\n" +
7411
				"			void method2() {\n" +  // can't be static
7412
				"				lower(i2);\n" +
7413
				"			}\n" +
7414
				"		}\n" +
7415
				"	}\n" +
7416
				"}"
7417
		},
7418
		"----------\n" + 
7419
		"1. ERROR in X.java (at line 3)\n" + 
7420
		"	public void foo(){\n" + 
7421
		"	            ^^^^^\n" + 
7422
		"The method foo() from the type X<T> can potentially be declared as static\n" + 
7423
		"----------\n",
7424
		null /* no extra class libraries */,
7425
		true /* flush output directory */,
7426
		compilerOptions /* custom options */
7427
	);
7428
}
7429
7430
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7431
// Outerclass instance field is an argument in messageSend in local class method
7432
public void test376550_4b() {
7433
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7434
		return;
7435
	Map compilerOptions = getCompilerOptions();
7436
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7437
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7438
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7439
	this.runNegativeTest(
7440
		new String[] {
7441
				"X.java", 
7442
				"public class X<T> {\n" +
7443
				"   int i1 = 1;\n" +
7444
				"   public void foo(){\n" + // can't be static
7445
				"   	class Local<K>{\n" +
7446
				"			int i2 = 1;\n" +
7447
				"			void lower(int i) {}\n" +
7448
				"			void method2() {\n" +  // can't be static
7449
				"				lower(i1);\n" +
7450
				"			}\n" +
7451
				"		}\n" +
7452
				"	}\n" +
7453
				"}"
7454
		},
7455
		"",
7456
		null /* no extra class libraries */,
7457
		true /* flush output directory */,
7458
		compilerOptions /* custom options */
7459
	);
7460
}
7461
7462
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7463
// QualifiedNameReference, accessing local class instance field
7464
public void test376550_5a() {
7465
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7466
		return;
7467
	Map compilerOptions = getCompilerOptions();
7468
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7469
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7470
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7471
	this.runNegativeTest(
7472
		new String[] {
7473
				"X.java", 
7474
				"public class X {\n" +
7475
				"   int i1 = 1;\n" +
7476
				"   public void foo(){\n" + // can be static
7477
				"   	class Local{\n" +
7478
				"			int i2 = 1;\n" +
7479
				"			void method2() {\n" +  // can't be static
7480
				"				Local.this.i2 = 1;\n" +
7481
				"			}\n" +
7482
				"		}\n" +
7483
				"	}\n" +
7484
				"}"
7485
		},
7486
		"----------\n" + 
7487
		"1. ERROR in X.java (at line 3)\n" + 
7488
		"	public void foo(){\n" + 
7489
		"	            ^^^^^\n" + 
7490
		"The method foo() from the type X can potentially be declared as static\n" + 
7491
		"----------\n",
7492
		null /* no extra class libraries */,
7493
		true /* flush output directory */,
7494
		compilerOptions /* custom options */
7495
	);
7496
}
7497
7498
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7499
//QualifiedNameReference, accessing outer class instance field
7500
public void test376550_5b() {
7501
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7502
		return;
7503
	Map compilerOptions = getCompilerOptions();
7504
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7505
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7506
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7507
	this.runNegativeTest(
7508
		new String[] {
7509
				"X.java", 
7510
				"public class X {\n" +
7511
				"   int i1 = 1;\n" +
7512
				"   public void foo(){\n" + // can't be static
7513
				"   	class Local{\n" +
7514
				"			int i2 = 1;\n" +
7515
				"			void method2() {\n" +  // can't be static
7516
				"				X.this.i1 = 1;\n" +
7517
				"			}\n" +
7518
				"		}\n" +
7519
				"	}\n" +
7520
				"}"
7521
		},
7522
		"",
7523
		null /* no extra class libraries */,
7524
		true /* flush output directory */,
7525
		compilerOptions /* custom options */
7526
	);
7527
}
7528
7529
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7530
// QualifiedNameRef.analyseCode()
7531
public void test376550_6a() {
7532
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7533
		return;
7534
	Map compilerOptions = getCompilerOptions();
7535
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7536
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7537
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7538
	this.runNegativeTest(
7539
		new String[] {
7540
				"X.java", 
7541
				"public class X {\n" +
7542
				"   int i1 = 1;\n" +
7543
				"   public void foo(){\n" + // can be static
7544
				"   	class Local{\n" +
7545
				"			int i2 = 1;\n" +
7546
				"			boolean method2() {\n" +  // can't be static
7547
				"				return Local.this.i2 == 1;\n" +
7548
				"			}\n" +
7549
				"		}\n" +
7550
				"	}\n" +
7551
				"}"
7552
		},
7553
		"----------\n" + 
7554
		"1. ERROR in X.java (at line 3)\n" + 
7555
		"	public void foo(){\n" + 
7556
		"	            ^^^^^\n" + 
7557
		"The method foo() from the type X can potentially be declared as static\n" + 
7558
		"----------\n",
7559
		null /* no extra class libraries */,
7560
		true /* flush output directory */,
7561
		compilerOptions /* custom options */
7562
	);
7563
}
7564
7565
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7566
// QualifiedNameRef.analyseCode()
7567
public void test376550_6b() {
7568
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7569
		return;
7570
	Map compilerOptions = getCompilerOptions();
7571
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7572
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7573
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7574
	this.runNegativeTest(
7575
		new String[] {
7576
				"X.java", 
7577
				"public class X {\n" +
7578
				"   int i1 = 1;\n" +
7579
				"   public void foo(){\n" + // can't be static
7580
				"   	class Local{\n" +
7581
				"			int i2 = 1;\n" +
7582
				"			boolean method2() {\n" +  // can't be static
7583
				"				return X.this.i1 == 1;\n" +
7584
				"			}\n" +
7585
				"		}\n" +
7586
				"	}\n" +
7587
				"}"
7588
		},
7589
		"",
7590
		null /* no extra class libraries */,
7591
		true /* flush output directory */,
7592
		compilerOptions /* custom options */
7593
	);
7594
}
7595
7596
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7597
// QualifiedAllocationExpression, allocating an anonymous type without an enclosing instance of parent type
7598
// anon. type is declared in local class
7599
public void test376550_7a() {
7600
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7601
		return;
7602
	Map compilerOptions = getCompilerOptions();
7603
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7604
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7605
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7606
	this.runNegativeTest(
7607
		new String[] {
7608
				"X.java", 
7609
				"public class X {\n" +
7610
				"   abstract class AbsUp{}\n" +
7611
				"   public void foo(){\n" + // can be static
7612
				"   	class Local{\n" +
7613
				"			abstract class AbsLow{}\n" +
7614
				"			void method2() {\n" +  // can't be static
7615
				"				new AbsLow(){};\n" +
7616
				"			}\n" +
7617
				"		}\n" +
7618
				"	}\n" +
7619
				"}"
7620
		},
7621
		"----------\n" + 
7622
		"1. ERROR in X.java (at line 3)\n" + 
7623
		"	public void foo(){\n" + 
7624
		"	            ^^^^^\n" + 
7625
		"The method foo() from the type X can potentially be declared as static\n" + 
7626
		"----------\n",
7627
		null /* no extra class libraries */,
7628
		true /* flush output directory */,
7629
		compilerOptions /* custom options */
7630
	);
7631
}
7632
7633
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7634
// QualifiedAllocationExpression, allocating an anonymous type without an enclosing instance of parent type
7635
// anon. type is declared in outer class
7636
public void test376550_7b() {
7637
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7638
		return;
7639
	Map compilerOptions = getCompilerOptions();
7640
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7641
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7642
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7643
	this.runNegativeTest(
7644
		new String[] {
7645
				"X.java", 
7646
				"public class X {\n" +
7647
				"   abstract class AbsUp{}\n" +
7648
				"   public void foo(){\n" + // can't be static
7649
				"   	class Local{\n" +
7650
				"			abstract  class AbsLow{}\n" +
7651
				"			void method2() {\n" +  // can't be static
7652
				"				new AbsUp(){};\n" +
7653
				"			}\n" +
7654
				"		}\n" +
7655
				"	}\n" +
7656
				"}"
7657
		},
7658
		"",
7659
		null /* no extra class libraries */,
7660
		true /* flush output directory */,
7661
		compilerOptions /* custom options */
7662
	);
7663
}
7664
7665
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7666
// FieldRef, from object of a class in outer class
7667
public void test376550_8a() {
7668
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7669
		return;
7670
	Map compilerOptions = getCompilerOptions();
7671
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7672
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7673
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7674
	this.runNegativeTest(
7675
		new String[] {
7676
				"X.java", 
7677
				"public class X {\n" +
7678
				"   class AbsUp{ int a;}\n" +
7679
				"   public void foo(){\n" + // can be static
7680
				"   	class Local{\n" +
7681
				"			class AbsLow{  int a;}\n" +
7682
				"			void method2() {\n" +  // can't be static
7683
				"				int abc = new AbsLow().a;\n" +
7684
				"			}\n" +
7685
				"		}\n" +
7686
				"	}\n" +
7687
				"}"
7688
		},
7689
		"----------\n" + 
7690
		"1. ERROR in X.java (at line 3)\n" + 
7691
		"	public void foo(){\n" + 
7692
		"	            ^^^^^\n" + 
7693
		"The method foo() from the type X can potentially be declared as static\n" + 
7694
		"----------\n",
7695
		null /* no extra class libraries */,
7696
		true /* flush output directory */,
7697
		compilerOptions /* custom options */
7698
	);
7699
}
7700
7701
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7702
//FieldRef, from object of a class in local class
7703
public void test376550_8b() {
7704
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7705
		return;
7706
	Map compilerOptions = getCompilerOptions();
7707
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7708
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7709
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7710
	this.runNegativeTest(
7711
		new String[] {
7712
				"X.java", 
7713
				"public class X {\n" +
7714
				"   class AbsUp{ int a;}\n" +
7715
				"   public void foo(){\n" + // can't be static
7716
				"   	class Local{\n" +
7717
				"			class AbsLow{  int a;}\n" +
7718
				"			void method2() {\n" +  // can't be static
7719
				"				int abc = new AbsUp().a;\n" +
7720
				"			}\n" +
7721
				"		}\n" +
7722
				"	}\n" +
7723
				"}"
7724
		},
7725
		"",
7726
		null /* no extra class libraries */,
7727
		true /* flush output directory */,
7728
		compilerOptions /* custom options */
7729
	);
7730
}
7731
7732
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7733
// QualifiedNameRef, accessing a field from local class field
7734
public void test376550_9a() {
7735
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7736
		return;
7737
	Map compilerOptions = getCompilerOptions();
7738
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7739
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7740
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7741
	this.runNegativeTest(
7742
		new String[] {
7743
				"X.java", 
7744
				"public class X {\n" +
7745
				"   X xup;\n" +
7746
				"	int i = 1;\n" +
7747
				"   public void foo(){\n" + // can be static
7748
				"   	class Local{\n" +
7749
				"			X xdown;\n" +
7750
				"			class AbsLow{  int a;}\n" +
7751
				"			void method2() {\n" +  // can't be static
7752
				"				int abc = xdown.i;\n" +
7753
				"			}\n" +
7754
				"		}\n" +
7755
				"	}\n" +
7756
				"}"
7757
		},
7758
		"----------\n" + 
7759
		"1. ERROR in X.java (at line 4)\n" + 
7760
		"	public void foo(){\n" + 
7761
		"	            ^^^^^\n" + 
7762
		"The method foo() from the type X can potentially be declared as static\n" + 
7763
		"----------\n",
7764
		null /* no extra class libraries */,
7765
		true /* flush output directory */,
7766
		compilerOptions /* custom options */
7767
	);
7768
}
7769
7770
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7771
// QualifiedNameRef, accessing a field from local class field
7772
public void test376550_9b() {
7773
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7774
		return;
7775
	Map compilerOptions = getCompilerOptions();
7776
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7777
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7778
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7779
	this.runNegativeTest(
7780
		new String[] {
7781
				"X.java", 
7782
				"public class X {\n" +
7783
				"   X xup;\n" +
7784
				"	int i = 1;\n" +
7785
				"   public void foo(){\n" + // can't be static
7786
				"   	class Local{\n" +
7787
				"			X xdown;\n" +
7788
				"			class AbsLow{  int a;}\n" +
7789
				"			void method2() {\n" +  // can't be static
7790
				"				int abc = xup.i;\n" +
7791
				"			}\n" +
7792
				"		}\n" +
7793
				"	}\n" +
7794
				"}"
7795
		},
7796
		"",
7797
		null /* no extra class libraries */,
7798
		true /* flush output directory */,
7799
		compilerOptions /* custom options */
7800
	);
7801
}
7802
7803
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7804
// QualifiedNameRef, accessing a field from local class field
7805
public void test376550_10a() {
7806
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7807
		return;
7808
	Map compilerOptions = getCompilerOptions();
7809
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7810
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7811
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7812
	this.runNegativeTest(
7813
		new String[] {
7814
				"X.java", 
7815
				"public class X {\n" +
7816
				"   X xup;\n" +
7817
				"	int i = 1;\n" +
7818
				"   public void foo(){\n" + // can be static
7819
				"   	class Local{\n" +
7820
				"			X xdown;\n" +
7821
				"			void calc(int i1){}\n" +
7822
				"			void method2() {\n" +  // can't be static
7823
				"				calc(xdown.i);\n" +
7824
				"			}\n" +
7825
				"		}\n" +
7826
				"	}\n" +
7827
				"}"
7828
		},
7829
		"----------\n" + 
7830
		"1. ERROR in X.java (at line 4)\n" + 
7831
		"	public void foo(){\n" + 
7832
		"	            ^^^^^\n" + 
7833
		"The method foo() from the type X can potentially be declared as static\n" + 
7834
		"----------\n",
7835
		null /* no extra class libraries */,
7836
		true /* flush output directory */,
7837
		compilerOptions /* custom options */
7838
	);
7839
}
7840
7841
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7842
// QualifiedNameRef, accessing a field from local class field
7843
public void test376550_10b() {
7844
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7845
		return;
7846
	Map compilerOptions = getCompilerOptions();
7847
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7848
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7849
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7850
	this.runNegativeTest(
7851
		new String[] {
7852
				"X.java", 
7853
				"public class X {\n" +
7854
				"   X xup;\n" +
7855
				"	int i = 1;\n" +
7856
				"   public void foo(){\n" + // can't be static
7857
				"   	class Local{\n" +
7858
				"			X xdown;\n" +
7859
				"			void calc(int i1){}\n" +
7860
				"			void method2() {\n" +  // can't be static
7861
				"				calc(xup.i);\n" +
7862
				"			}\n" +
7863
				"		}\n" +
7864
				"	}\n" +
7865
				"}"
7866
		},
7867
		"",
7868
		null /* no extra class libraries */,
7869
		true /* flush output directory */,
7870
		compilerOptions /* custom options */
7871
	);
7872
}
7873
7874
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7875
// bug test case
7876
public void test376550_11() {
7877
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7878
		return;
7879
	Map compilerOptions = getCompilerOptions();
7880
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7881
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7882
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7883
	this.runNegativeTest(
7884
		new String[] {
7885
				"X.java", 
7886
				"import java.util.ArrayList;\n" +
7887
				"import java.util.Collection;\n" +
7888
				"public class X {\n" +
7889
				"   private Object o = new Object();\n" +
7890
				"   public final Collection<Object> go() {\n" + // can't be static
7891
				"   	return new ArrayList<Object>() {\n" +
7892
				"			{ add(o);}\n" +
7893
				"		};\n" +
7894
				"	}\n" +
7895
				"}"
7896
		},
7897
		"----------\n" + 
7898
		"1. WARNING in X.java (at line 6)\n" + 
7899
		"	return new ArrayList<Object>() {\n" + 
7900
		"	           ^^^^^^^^^^^^^^^^^^^\n" + 
7901
		"The serializable class  does not declare a static final serialVersionUID field of type long\n" + 
7902
		"----------\n" + 
7903
		"2. WARNING in X.java (at line 7)\n" + 
7904
		"	{ add(o);}\n" + 
7905
		"	      ^\n" + 
7906
		"Read access to enclosing field X.o is emulated by a synthetic accessor method\n" + 
7907
		"----------\n",
7908
		null /* no extra class libraries */,
7909
		true /* flush output directory */,
7910
		compilerOptions /* custom options */
7911
	);
7912
}
7913
7914
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
7915
public void test376550_12() {
7916
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
7917
		return;
7918
	Map compilerOptions = getCompilerOptions();
7919
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
7920
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
7921
	compilerOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
7922
	this.runNegativeTest(
7923
		new String[] {
7924
				"X.java", 
7925
				"import java.util.ArrayList;\n" +
7926
				"import java.util.Collection;\n" +
7927
				"public class X<E> {\n" +
7928
				"   private Object o = new Object();\n" +
7929
				"   public final <E1> Collection<E1> go() {\n" + // CAN be static
7930
				"   	return new ArrayList<E1>() {\n" +
7931
				"			{ E1 e;}\n" +
7932
				"		};\n" +
7933
				"	}\n" +
7934
				"}"
7935
		},
7936
		"----------\n" + 
7937
		"1. ERROR in X.java (at line 5)\n" + 
7938
		"	public final <E1> Collection<E1> go() {\n" + 
7939
		"	                                 ^^^^\n" + 
7940
		"The method go() from the type X<E> can be declared as static\n" + 
7941
		"----------\n" + 
7942
		"2. WARNING in X.java (at line 6)\n" + 
7943
		"	return new ArrayList<E1>() {\n" + 
7944
		"	           ^^^^^^^^^^^^^^^\n" + 
7945
		"The serializable class  does not declare a static final serialVersionUID field of type long\n" + 
7946
		"----------\n",
7947
		null /* no extra class libraries */,
7948
		true /* flush output directory */,
7949
		compilerOptions /* custom options */
7950
	);
7951
}
7191
}
7952
}
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java (-1 / +1 lines)
Lines 90-96 Link Here
90
	if (this.binding.declaringClass.isMemberType() && !this.binding.declaringClass.isStatic()) {
90
	if (this.binding.declaringClass.isMemberType() && !this.binding.declaringClass.isStatic()) {
91
		// allocating a non-static member type without an enclosing instance of parent type
91
		// allocating a non-static member type without an enclosing instance of parent type
92
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
92
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
93
		currentScope.resetEnclosingMethodStaticFlag();
93
		currentScope.resetDeclaringClassMethodStaticFlag(this.binding.declaringClass.enclosingType());
94
	}
94
	}
95
	manageEnclosingInstanceAccessIfNecessary(currentScope, flowInfo);
95
	manageEnclosingInstanceAccessIfNecessary(currentScope, flowInfo);
96
	manageSyntheticAccessIfNecessary(currentScope, flowInfo);
96
	manageSyntheticAccessIfNecessary(currentScope, flowInfo);
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FieldReference.java (-4 / +4 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
2
 * Copyright (c) 2000, 2012 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 111-122 Link Here
111
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
111
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
112
	if (!this.binding.isStatic()) {
112
	if (!this.binding.isStatic()) {
113
		if (this.receiver.isThis()) {
113
		if (this.receiver.isThis()) {
114
			currentScope.resetEnclosingMethodStaticFlag();
114
			currentScope.resetDeclaringClassMethodStaticFlag(this.binding.declaringClass);
115
		}
115
		}
116
	} else if (this.receiver.isThis()) {
116
	} else if (this.receiver.isThis()) {
117
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
117
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
118
			// explicit this, not allowed in static context
118
			// explicit this, not allowed in static context
119
			currentScope.resetEnclosingMethodStaticFlag();
119
			currentScope.resetDeclaringClassMethodStaticFlag(this.binding.declaringClass);
120
		}
120
		}
121
	}
121
	}
122
	return flowInfo;
122
	return flowInfo;
Lines 133-139 Link Here
133
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
133
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
134
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
134
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
135
		if (this.receiver.isThis()) {
135
		if (this.receiver.isThis()) {
136
			currentScope.resetEnclosingMethodStaticFlag();
136
			currentScope.resetDeclaringClassMethodStaticFlag(this.binding.declaringClass);
137
		}
137
		}
138
	} else if (this.receiver.isThis()) {
138
	} else if (this.receiver.isThis()) {
139
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
139
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java (-3 / +14 lines)
Lines 47-59 Link Here
47
		this.bits |= ASTNode.IsLocalDeclarationReachable; // only set if actually reached
47
		this.bits |= ASTNode.IsLocalDeclarationReachable; // only set if actually reached
48
	}
48
	}
49
	if (this.binding != null && this.type.resolvedType instanceof TypeVariableBinding) {
49
	if (this.binding != null && this.type.resolvedType instanceof TypeVariableBinding) {
50
		TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.type.resolvedType;
50
		MethodScope methodScope= this.binding.declaringScope.methodScope();
51
		MethodScope methodScope= this.binding.declaringScope.methodScope();
52
		if (methodScope != null && methodScope.referenceContext instanceof TypeDeclaration) {
53
			// initialization scope
54
			methodScope = methodScope.enclosingMethodScope();
55
		}
51
		AbstractMethodDeclaration methodDeclaration = methodScope.referenceMethod();
56
		AbstractMethodDeclaration methodDeclaration = methodScope.referenceMethod();
52
		if (methodDeclaration != null && ((methodDeclaration.bits & ASTNode.CanBeStatic) != 0) && methodDeclaration.binding != null) {
57
		if (methodDeclaration != null && methodDeclaration.binding != null) {
53
			TypeVariableBinding[] typeVariables = methodDeclaration.binding.typeVariables();
58
			TypeVariableBinding[] typeVariables = methodDeclaration.binding.typeVariables();
54
			if (typeVariables == Binding.NO_TYPE_VARIABLES) {
59
			if (typeVariables == Binding.NO_TYPE_VARIABLES) {
55
				// Method declares no type variables.
60
				// Method declares no type variables.
56
				currentScope.resetEnclosingMethodStaticFlag();
61
				if (typeVariableBinding.declaringElement instanceof TypeBinding)
62
					currentScope.resetDeclaringClassMethodStaticFlag((TypeBinding) typeVariableBinding.declaringElement);
63
				else
64
					currentScope.resetEnclosingMethodStaticFlag();
57
			} else {
65
			} else {
58
				// to check whether the resolved type for this is declared by enclosing method as a type variable
66
				// to check whether the resolved type for this is declared by enclosing method as a type variable
59
				boolean usesEnclosingTypeVar = false; 
67
				boolean usesEnclosingTypeVar = false; 
Lines 65-71 Link Here
65
				}
73
				}
66
				if (!usesEnclosingTypeVar) {
74
				if (!usesEnclosingTypeVar) {
67
					// uses a type variable not declared by enclosing method
75
					// uses a type variable not declared by enclosing method
68
					currentScope.resetEnclosingMethodStaticFlag();
76
					if (typeVariableBinding.declaringElement instanceof TypeBinding)
77
						currentScope.resetDeclaringClassMethodStaticFlag((TypeBinding) typeVariableBinding.declaringElement);
78
					else
79
						currentScope.resetEnclosingMethodStaticFlag();
69
				}
80
				}
70
			}
81
			}
71
		}
82
		}
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java (-1 / +1 lines)
Lines 86-92 Link Here
86
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
86
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
87
		if (this.receiver.isThis()) {
87
		if (this.receiver.isThis()) {
88
			// accessing non-static method without an object
88
			// accessing non-static method without an object
89
			currentScope.resetEnclosingMethodStaticFlag();
89
			currentScope.resetDeclaringClassMethodStaticFlag(this.binding.declaringClass);
90
		}
90
		}
91
	} else if (this.receiver.isThis()) {
91
	} else if (this.receiver.isThis()) {
92
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
92
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedAllocationExpression.java (-1 / +1 lines)
Lines 67-73 Link Here
67
		} else {
67
		} else {
68
			if (this.binding.declaringClass.superclass().isMemberType() && !this.binding.declaringClass.superclass().isStatic()) {
68
			if (this.binding.declaringClass.superclass().isMemberType() && !this.binding.declaringClass.superclass().isStatic()) {
69
				// creating an anonymous type of a non-static member type without an enclosing instance of parent type
69
				// creating an anonymous type of a non-static member type without an enclosing instance of parent type
70
				currentScope.resetEnclosingMethodStaticFlag();
70
				currentScope.resetDeclaringClassMethodStaticFlag(this.binding.declaringClass.superclass().enclosingType());
71
			}
71
			}
72
		}
72
		}
73
73
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java (-2 / +2 lines)
Lines 86-92 Link Here
86
				}
86
				}
87
			}
87
			}
88
			if (!lastFieldBinding.isStatic()) {
88
			if (!lastFieldBinding.isStatic()) {
89
				currentScope.resetEnclosingMethodStaticFlag();
89
				currentScope.resetDeclaringClassMethodStaticFlag(lastFieldBinding.declaringClass);
90
			}
90
			}
91
			break;
91
			break;
92
		case Binding.LOCAL :
92
		case Binding.LOCAL :
Lines 195-201 Link Here
195
				}
195
				}
196
			}
196
			}
197
			if (!fieldBinding.isStatic()) {
197
			if (!fieldBinding.isStatic()) {
198
				currentScope.resetEnclosingMethodStaticFlag();
198
				currentScope.resetDeclaringClassMethodStaticFlag(fieldBinding.declaringClass);
199
			}
199
			}
200
			break;
200
			break;
201
		case Binding.LOCAL : // reading a local variable
201
		case Binding.LOCAL : // reading a local variable
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SingleNameReference.java (-4 / +4 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
2
 * Copyright (c) 2000, 2012 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 73-79 Link Here
73
				}
73
				}
74
				if (!fieldBinding.isStatic()) {
74
				if (!fieldBinding.isStatic()) {
75
					// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
75
					// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
76
					currentScope.resetEnclosingMethodStaticFlag();
76
					currentScope.resetDeclaringClassMethodStaticFlag(fieldBinding.declaringClass);
77
				}
77
				}
78
				manageSyntheticAccessIfNecessary(currentScope, flowInfo, true /*read-access*/);
78
				manageSyntheticAccessIfNecessary(currentScope, flowInfo, true /*read-access*/);
79
				break;
79
				break;
Lines 121-127 Link Here
121
			}
121
			}
122
			if (!fieldBinding.isStatic()) {
122
			if (!fieldBinding.isStatic()) {
123
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
123
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
124
				currentScope.resetEnclosingMethodStaticFlag();
124
				currentScope.resetDeclaringClassMethodStaticFlag(fieldBinding.declaringClass);
125
			}
125
			}
126
			break;
126
			break;
127
		case Binding.LOCAL : // assigning to a local variable
127
		case Binding.LOCAL : // assigning to a local variable
Lines 174-180 Link Here
174
			}
174
			}
175
			if (!fieldBinding.isStatic()) {
175
			if (!fieldBinding.isStatic()) {
176
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
176
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
177
				currentScope.resetEnclosingMethodStaticFlag();
177
				currentScope.resetDeclaringClassMethodStaticFlag(fieldBinding.declaringClass);
178
			}
178
			}
179
			break;
179
			break;
180
		case Binding.LOCAL : // reading a local variable
180
		case Binding.LOCAL : // reading a local variable
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java (-4 / +37 lines)
Lines 957-972 Link Here
957
	return s;
957
	return s;
958
}
958
}
959
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
959
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
960
/**
961
 * This method is used to reset the CanBeStatic the enclosing method of the current block
962
 */
960
public void resetEnclosingMethodStaticFlag() {
963
public void resetEnclosingMethodStaticFlag() {
961
	MethodScope methodScope = methodScope();
964
	MethodScope methodScope = methodScope();
965
	if (methodScope != null) {
966
		if (methodScope.referenceContext instanceof MethodDeclaration) {
967
			MethodDeclaration methodDeclaration = (MethodDeclaration) methodScope.referenceContext;
968
			methodDeclaration.bits &= ~ASTNode.CanBeStatic;
969
		} else if (methodScope.referenceContext instanceof TypeDeclaration) {
970
			// anonymous type, find enclosing method
971
			methodScope = methodScope.enclosingMethodScope();
972
			if (methodScope != null && methodScope.referenceContext instanceof MethodDeclaration) {
973
				MethodDeclaration methodDeclaration = (MethodDeclaration) methodScope.referenceContext;
974
				methodDeclaration.bits &= ~ASTNode.CanBeStatic;
975
			}
976
		}
977
	}
978
}
979
980
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=376550
981
/**
982
 * This method is used to reset the CanBeStatic on all enclosing methods until the method 
983
 * belonging to the declaringClass
984
 * @param declaringClass
985
 */
986
public void resetDeclaringClassMethodStaticFlag(TypeBinding declaringClass) {
987
	MethodScope methodScope = methodScope();
988
	if (methodScope != null && methodScope.referenceContext instanceof TypeDeclaration) {
989
		// anonymous type, find enclosing method
990
		methodScope = methodScope.enclosingMethodScope();
991
	}
962
	while (methodScope != null && methodScope.referenceContext instanceof MethodDeclaration) {
992
	while (methodScope != null && methodScope.referenceContext instanceof MethodDeclaration) {
963
		MethodDeclaration methodDeclaration= (MethodDeclaration) methodScope.referenceContext;
993
		MethodDeclaration methodDeclaration = (MethodDeclaration) methodScope.referenceContext;
964
		methodDeclaration.bits &= ~ASTNode.CanBeStatic;
994
		methodDeclaration.bits &= ~ASTNode.CanBeStatic;
965
		ClassScope enclosingClassScope = methodScope.enclosingClassScope();
995
		ClassScope enclosingClassScope = methodScope.enclosingClassScope();
966
		if (enclosingClassScope != null) {
996
		if (enclosingClassScope != null) {
967
			methodScope = enclosingClassScope.methodScope();
997
			TypeDeclaration type = enclosingClassScope.referenceContext;
968
		} else {
998
			if (type.binding != null && declaringClass != null && type.binding != declaringClass.original()) {
969
			break;
999
				methodScope = enclosingClassScope.enclosingMethodScope();
1000
			} else {
1001
				break;
1002
			}
970
		}
1003
		}
971
	}
1004
	}
972
}
1005
}

Return to bug 376550