Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 428261

Summary: [1.8][compiler] Incorrect error: No enclosing instance of the type X is accessible in scope
Product: [Eclipse Project] JDT Reporter: Srikanth Sankaran <srikanth_sankaran>
Component: CoreAssignee: Srikanth Sankaran <srikanth_sankaran>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: noopur_gupta, stephan.herrmann
Version: 4.4   
Target Milestone: BETA J8   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Bug Depends on:    
Bug Blocks: 427787    

Description Srikanth Sankaran CLA 2014-02-15 13:14:17 EST
The following program should compile, but is presently rejected:

// --
interface I {
	X foo(int a);
}

public class X {
	public static void main(String[] args) {
		String s = "Blah";
		class Local extends X {
			Local(int a) {
				System.out.println(a);
				System.out.println(s);
			}
		}
		I i = Local::new; // Incorrect error here.
                i.foo(10);
	}
}

Point is Local being in static context, does not have an enclosing instance.
Comment 1 Srikanth Sankaran CLA 2014-02-15 13:16:37 EST
See that javac 8b129 rejects this program incorrectly:

interface I {
	X foo(int a);
}

public class X {
	public void main(String[] args) {
		class Local extends X {
			Local(int a) {
				System.out.println(a);
			}
		}
		I i = Local::new;
        i.foo(10);
	}
}

as there being no suitable enclosing instance.
Comment 2 Srikanth Sankaran CLA 2014-02-17 16:20:44 EST
This required special treatment - when the local type captures outer
locals, the situation gets tricky: the captured outer locals come *after*
the functional interface parameters and there is no way the meta factory
can handle that case.

So I arranged to generate code for the method reference by treating it as
an implicit lambda expression.

Fix and tests here: 

http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=BETA_JAVA8&id=d7bf97b0906e6821b36f900e536d18f1ccdddff2
Comment 3 Noopur Gupta CLA 2014-02-21 03:31:58 EST
Verified as working for Eclipse + Java 8 RC1 using Kepler SR2(RC4) +   
Eclipse Java Development Tools Patch for Java 8 Support (BETA)	
1.0.0.v20140220-2054