A. Di Matteo created MCOMPILER-265:
--------------------------------------

             Summary: IllegalAccessError trying to access package-private 
method through public subclass (for same package) from another package 
                 Key: MCOMPILER-265
                 URL: https://issues.apache.org/jira/browse/MCOMPILER-265
             Project: Maven Compiler Plugin
          Issue Type: Bug
    Affects Versions: 3.5.1
         Environment: Eclipse Mars 4.5.1
Maven 3.3.3
JDK 8u31
            Reporter: A. Di Matteo


The Maven Compiler Plugin is producing an IllegalAccessError for the use case 
below when normal JDK compiler or Eclipse compiler would not. As such, this is 
considered a bug, unless further clarification would be provided as part of 
this ticket.

Given the following two classes in package com.sample.package1
{code}
package com.sample.package1;

abstract class Foo {

    public String getFoo() {
        return "foo";
    }

}
{code}

and 

{code}
package com.sample.package1;

public class Bar extends Foo {

        public String getBar() {
                return "bar";
        }

}
{code}

And the following test main in package com.sample.package2
{code}
package com.sample.package2;

import java.util.stream.Stream;
import com.sample.package1.Bar;

public class Main {

        public static void main(String[] args) {
                System.out.println(new Bar().getFoo());
                // "foo"

                Stream.of(new 
Bar()).map(Bar::getFoo).forEach(System.out::println);
                // IllegalAccessError
        }

}
{code}

The following scenarios occur:
- Compiling and running the main from Eclipse > No Error
- Compiling from console/Maven and running the main from Eclipse > Error, 
IllegalAccessError
- Compiling from console/Maven and running the main via exec:java from console 
> Error, IllegalAccessError
- Compiling from Eclipse and running the main via exec:java from console > No 
Error

Stack trace:
{code}
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalAccessError: tried to access class 
com.sample.package1.Foo from class com.sample.package2.Main
        at 
com.sample.package2.Main.lambda$MR$main$getFoo$e8593739$1(Main.java:14)
        at com.sample.package2.Main$$Lambda$1/1562888899.apply(Unknown Source)
        at 
java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at 
java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:419)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
        at 
java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
        at 
java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
        at 
java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at 
java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
        at com.sample.package2.Main.main(Main.java:14)
        ... 6 more
{code}

Given the following plugin configuration:
{code}
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>
{code}

And changing it to the following:
{code}
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <compilerId>eclipse</compilerId>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-eclipse</artifactId>
            <version>2.7</version>
        </dependency>
    </dependencies>
</plugin>
{code}

Would fix the issue, showing indeed an important difference between the Maven 
Compiler and the Eclipse Compiler.

Further details can be found on the related StackOverflow thread:
- 
http://stackoverflow.com/questions/36100552/demystifying-the-java-lang-illegalaccesserror




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to