Vertispan / j2clmavenplugin

Maven plugin to launch new J2CL compilation
https://vertispan.github.io/j2clmavenplugin/
Apache License 2.0
53 stars 26 forks source link

Is stripped bytecode using the right JRE copy? #140

Open xamde opened 2 years ago

xamde commented 2 years ago

I have a class A which implements Externalizable. It needs to look like this:

public class A implements Externalizable {

    @Override
    @GwtIncompatible("ObjectOutput not in GWT")
    public void writeExternal(final ObjectOutput out) throws IOException {
        // write internal state to out
    }

    @GwtIncompatible("ObjectOutput not in GWT")
    @Override
    public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
        // create internal state from in.readObject()
    }

}

Then comes the source code stripper:

public class A implements Externalizable {
}

Next runs the stripped byte code task:

A.java:26 
org.example.A
is not abstract and does not override abstract method readExternal(java.io.ObjectInput) in java.io.Externalizable

So how is this supposed to work? The GWT/J2cl JRE emulation of Externalizable states:

package java.io;

/**
 * A version of java.io.Externalizable to be able to compile shared Java code that uses it.
 * <p>
 * Externalization is not supported in GWT. Shared classes that implement the interface can
 * mark the corresponding methods as {@code @GwtIncompatible} but the interface still needs to
 * exist for the code to compile.
 * <p>
 * The implementation has to mark all the overriding methods as {@code @GwtIncompatible}.
 */
public interface Externalizable extends Serializable {
  // public void readExternal(ObjectInput input);
  // public void writeExternal(ObjectOutput output);
}

So either I have some project setup issue or maybe j2cl maven plugin is using the JRE-JRE instead of the GWT/J2CL-JRE?

xamde commented 2 years ago

When using mvnDebug and stepping around in the running plugin, I could see that the correct Externalizable class (with methods stripped away) was present in bot the bootstrap-classpath jar and tje jre-0.10 jar. So double good. I still have no idea why/where javac finds the original JRE class.

xamde commented 2 years ago

I found a case where the "stripped_bytecode" task ran, marked the directory as "success" but the output.log shows compiler ERRORs. The compiler errors are complaining about the Externalizable not being properly implemented.

So it seems in addition to using the wrong JRE classes we also misinterpret the javac results.

xamde commented 2 years ago

Found a solution, see https://github.com/Vertispan/j2clmavenplugin/pull/142