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

Bug 528014

Summary: [compiler] Access to protected overloaded methods/constructors in foreign plug-in if in same package
Product: [Eclipse Project] JDT Reporter: Niko Stotz <eclipse>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: sasikanth.bharadwaj, stephan.herrmann
Version: 4.7.1a   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard: stalebug

Description Niko Stotz CLA 2017-12-01 08:36:13 EST
Using Eclipse 4.7.1a (Oxygen.1a) Build id: 20171005-1200

The compiler binds method / constructor calls to protected class members in the same package in a foreign plug-in, even though there would be a public binding.

This leads to IllegalAccessError on execution in an OSGi environment.

Example:

plug-in A (exports package a.b):

package a.b;
public class Target {
	public Target() {}
	
	// this should be bound in Caller.illegalAccessToProtectedConstructor*()
	public Target(Number n) {}
	
	// this is bound in Caller.illegalAccessToProtectedConstructor*()
	protected Target(Integer i) {}
	
	// this should be bound in Caller.illegalAccessToProtectedFunction*()
	public int f(Number n) {
		return 1;
	}
	
	// this is bound in Caller.illegalAccessToProtectedFunction*()
	protected int f(Integer i) {
		return 2;
	}

}

plug-in B (depends on A):

package a.b;

import a.b.Target;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class Caller {
	@Test
	public void illegalAccessToProtectedConstructorNull() throws Exception {
		new Target(null);
	}

	@Test
	public void illegalAccessToProtectedConstructor() throws Exception {
		new Target(new Integer(1));
	}
	
	@Test
	public void validAccessToPublicConstructor() throws Exception {
		new Target((Number) new Integer(1));
	}
	
	@Test
	public void validAccessToPublicFunction() throws Exception {
		Target target = new Target();
		assertEquals(1, target.f((Number) null));
	}
	
	@Test
	public void illegalAccessToProtectedFunctionNull() throws Exception {
		Target target = new Target();
		assertEquals(2, target.f((Integer) null));
	}
	
	@Test
	public void illegalAccessToProtectedFunction() throws Exception {
		Target target = new Target();
		assertEquals(2, target.f(new Integer(1)));
	}
}
Comment 1 Sasikanth Bharadwaj CLA 2017-12-12 05:58:38 EST
Access to protected members is allowed with in the same package as they are declared in, so I don't see anything wrong with the compiler behavior itself as such. The run time error *may be * a result of java9. Need to investigate how things worked before java9
Comment 2 Niko Stotz CLA 2017-12-12 06:08:41 EST
I ran this on Java8, so at least the runtime part should not be affected by Java9.
Comment 3 Eclipse Genie CLA 2020-02-19 20:01:29 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 4 Stephan Herrmann CLA 2020-02-20 04:50:50 EST
Compiler (and JLS) knows nothing about plug-ins, the boundaries are packages, not plug-ins. As Sasi pointed out the compiler is correct in this regard.