Bug 21804 - Need to surface compiler setting COMPILER_PB_STATIC_ACCESS_RECEIVER
Summary: Need to surface compiler setting COMPILER_PB_STATIC_ACCESS_RECEIVER
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Martin Aeschlimann CLA
QA Contact:
URL:
Whiteboard:
Keywords: investigate
Depends on:
Blocks:
 
Reported: 2002-07-23 09:12 EDT by Philipe Mulet CLA
Modified: 2002-08-22 03:41 EDT (History)
1 user (show)

See Also:


Attachments
new field for compiler option "static member accessed through non-static reference" (3.22 KB, patch)
2002-08-15 01:43 EDT, Renaud Waldura CLA
no flags Details | Diff
quickfix for non-static access to static member (9.00 KB, patch)
2002-08-19 23:42 EDT, Renaud Waldura CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Philipe Mulet CLA 2002-07-23 09:12:24 EDT
2.1 stream

New optional compilation problem got added to report non-static usage of static 
fields/methods. It should be surfaced in the preference page.

JavaCore.COMPILER_PB_STATIC_ACCESS_RECEIVER
Comment 1 Philipe Mulet CLA 2002-07-23 09:12:58 EDT
JDT/Core side was bug 21787
Comment 2 Dirk Baeumer CLA 2002-07-24 11:13:08 EDT
We should also add a quick fix for this.
Comment 3 Renaud Waldura CLA 2002-08-15 01:42:25 EDT
Martin:

Renaud again. I include below a (trivial) patch for this. Hope it's to your 
liking.

I'll try to think about a quickfix too.
Comment 4 Renaud Waldura CLA 2002-08-15 01:43:42 EDT
Created attachment 1849 [details]
new field for compiler option "static member accessed through non-static reference"
Comment 5 Martin Aeschlimann CLA 2002-08-15 04:13:35 EDT
Perfect! Released patch (reordered controls on the page).

> 20020813
Comment 6 Martin Aeschlimann CLA 2002-08-15 04:14:12 EDT
reopened for quick fix feature
Comment 7 Renaud Waldura CLA 2002-08-19 23:40:41 EDT
I'm not making any more progress on this. I've got a patch that solves the most 
common case, where a simple variable makes a non-static reference to a field or 
method. E.g.

File f = new File();
f.pathSeparator -> corrected to File.separator

This patch is included below (note that I also factored the code to create an 
import statement into a new method addImportToProposal).

But I'm having a hard time solving with more complicated cases (inner classes, 
references through method calls, references using this, etc.). Martin, maybe 
you want to take a crack at it?
Comment 8 Renaud Waldura CLA 2002-08-19 23:42:40 EDT
Created attachment 1861 [details]
quickfix for non-static access to static member
Comment 9 Martin Aeschlimann CLA 2002-08-20 05:37:20 EDT
released > 20020819

Can you give examples where the quick fix failes? I didn't see a problem.
Comment 10 Renaud Waldura CLA 2002-08-20 14:13:52 EDT
Here is a class I used for testing. MyInterface defines a constant N and 
MyInterfaceImpl defines staticField and staticMethod. The cases corrected OK 
are flagged with "works". The others don't -- I've been unable to find a simple 
solution that covers them all, partly because the correction expands to more 
than the problem node itself in the AST. This is the case with your foo().goo() 
example.


class Outer
{
    static class Inner
    {
        static class InnerInner
        {
	        static class InnerInnerInner
	        {
	            static int staticField = 3;
	            static void staticMethod() {};
	        }
	        static InnerInnerInner iii = new InnerInnerInner();
        }
        InnerInner ii = new InnerInner();
    }
    
    class MyInterfaceImplInner
    {
        MyInterfaceImpl my = new MyInterfaceImpl();
    }
    
    Inner _i = new Inner();
	MyInterfaceImpl _s = new MyInterfaceImpl();
	    
    void correctThis()
    {
    	_s.staticField += 1; // works
    	int n; n = _s.N + 1; // works
    	_s.staticMethod(); // works
    	
        bug21804.Outer.this._s.staticField = 99;        
        _i.ii.iii.staticField = 12;
        this._i.ii.iii.staticMethod();
    }
    
    void correctInner()
    {
        Inner i = new Inner();
        i.ii.iii.staticField = 123;
        i.ii.iii.staticMethod();
    }
    
	void correctSubInner()
	{        
        MyInterfaceImplInner miii = new MyInterfaceImplInner();
        miii.my.staticField = 99; // works
        miii.my.staticMethod(); // works too
    }
    
    void correctThroughReferences()
    {
		MyInterface mi = new MyInterfaceImpl();    	
		int n; n = mi.newInstance().N;
    }
}



Comment 11 Martin Aeschlimann CLA 2002-08-21 06:20:32 EDT
Thanks for the test code! Had to modify the test code to get it running, hope I 
did't corrected wrongly.
Added code to getQualifier; the given cases work now all fine > 20020820

What is sometimes distracting that the compiler generates more than one marker 
for some statements:

i.ii.iii.staticField = 123;
// static access of iii, static access of staticField 


My latest test code:
----
import junit.samples.Outer.Inner.InnerInner.InnerInnerInner;
public class MyInterfaceImpl {
	static int staticField;
	static int N;
	public static void staticMethod() {
	}
	public MyInterfaceImpl newInstance() {
		return null;
	}

}
class Outer {
	static class Inner {
		static class InnerInner {
			static class InnerInnerInner {
				static int staticField = 3;
				static void staticMethod() {
				};
			}
			static InnerInnerInner iii = new InnerInnerInner();
		}
		InnerInner ii = new InnerInner();
	}

	static InnerInnerInner _m() {
		return null;
	}	

	class MyInterfaceImplInner {
		MyInterfaceImpl my = new MyInterfaceImpl();

		Inner _i = new Inner();
		MyInterfaceImpl _s = new MyInterfaceImpl();
		
		void correctThis() {
			_s.staticField += 1;
			int n;
			n = _s.N + 1;
			_s.staticMethod();

			Outer.MyInterfaceImplInner.this._s.staticField = 99; 
			_i.ii.iii.staticField = 12;
			this._i.ii.iii.staticMethod();
			_m().staticField= 1;
			_m().staticMethod();
		}

		void correctInner() {
			Inner i = new Inner();
			i.ii.iii.staticField = 123;  
			i.ii.iii.staticMethod();
		}

		void correctSubInner() {
			MyInterfaceImplInner miii = new MyInterfaceImplInner();
			miii.my.staticField = 99;
			miii.my.staticMethod();
		}

		void correctThroughReferences() {
			MyInterfaceImpl mi = new MyInterfaceImpl();
			int n;
			n = mi.newInstance().N;
			mi.newInstance().staticMethod();
		}
	}
}
Comment 12 Renaud Waldura CLA 2002-08-22 02:30:43 EDT
Very cool, good job. So simple! I didn't figure out I could get the expression 
from the field access, and that would have solved it.

Well, I think we're done with this. Feel like closing the bug?
Comment 13 Martin Aeschlimann CLA 2002-08-22 03:41:00 EDT
Yes, closing the PR. Fixed.
Renaud, thanks for you efforts!