Bug 246050 - [compiler] Warn if shadowing an outer method
Summary: [compiler] Warn if shadowing an outer method
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-03 04:15 EDT by Dani Megert CLA
Modified: 2008-09-03 11:24 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2008-09-03 04:15:06 EDT
I20080902-0800.

I was renaming a method and refactoring correctly warned me about a shading conflict. I then continued anyway and was surprised that we don't have a JDT Core warning for that.

Here's a test case:

public class Test {

	class Inner {
		void m() {
			m();
		}
	}

	public static void main(String[] args) {
		new Test().doit();		
	}

	private void doit() {
		new Inner().m();
	}

	void m() {
		System.out.println("I think this is called"); //$NON-NLS-1$
	}
}

Running that code results in a stack overflow which could have been detected if a warning would be reported on inner call to m();
Comment 1 Philipe Mulet CLA 2008-09-03 05:42:46 EDT
Indeed. Would you expect strict matches to be reported or compatible matches ?

e.g. 

class X {
  void foo(long l) {
  }
  class M {
     void foo(int i) {
     }
     void bar() {
        foo(0);
     }
  }
}
Comment 2 Dani Megert CLA 2008-09-03 06:28:29 EDT
I'm mainly interested in strict. Compatible could be nice too but I guess we would get some false positives here.
Comment 3 Markus Keller CLA 2008-09-03 07:10:31 EDT
I would expect a problem whenever removing the method declaration in the nested class (or one of its supertypes) could make a reference point to another method (I guess that means 'compatible').

> if a warning would be reported on inner call to m();

For fields and local variables, we already have a warning on the *declaration* if it hides another field or variable. Should we also have a warning on the declaration of a shadowing method? Unfortunately, we can also have shadowing situations where there's no method declaration to blame, e.g.

public class Y {
	public static void main(String[] args) {
		new Y().new Inner().bar();
	}

	void run() { System.out.println("Y"); }
	void run(int i) { System.out.println("Y" + i); }

	class Inner extends Sup {
		void bar() {
			run(); // Sup#run() shadows Y.this.run();
		}
	}
}

class Sup implements Runnable {
	public void run() { System.out.println("Sup"); }
}
Comment 4 Philipe Mulet CLA 2008-09-03 11:24:51 EDT
This feels like the refactoring prechecking about semantical shift... this would be slow to implement on normal compiler duty, I believe.

Flagging the shadowing declaration like we do for fields/types etc... is simpler and likely produces expected results.