Search code examples
javaeclipsejava-11eclipse-2018-12

Allow Eclipse to recognize standard library JDK11 patched module


Just out of curiosity, I'm trying to setup Eclipse to allow it to compile and run an application with a standard library class being patched.

I have 2 Java projects patch and consumer: one containing a string patch class (with a new method size(), identical to length()) and the other which should use said method size(). The setup is the following one:

- patch
  - bin (contains class files)
  - src
    - java
      - lang
        - String.java

- consumer
  - src
    - consumer
      - Main.java
      - module-info.java

Main.java:

package consumer;
public class Main {
    public static void main(String[] args) {
        String s  = new String("hello");
        System.out.println(s.size());
    }
}

After compiling patch (thereby getting String.class inside patch/bin/java/lang/) I know I can easily use:

java --patch-module java.base=patchpjt/bin/  consumer/src/consumer/Main.java

To correctly call the newly added method size(), obtainining the result of 5.

The problem is that in Eclipse method size is still not recognized (the error is Method Size() is underfined for type String):

enter image description here

So my question is:

  • How to configure Eclipse in order for the project consumer to correctly compile, run and show no errors?
  • How to configure Eclipse in order to show on the Content Assist the method size()?

I know I need to use the Build Path -> Module Path -> Edit Is Modular -> Details Tab -> Patched module but I don't know how to configure both projects.

Some info you might find useful:

  • Eclipse 2018-12 (4.10.0) Build id: 20181214-0600

Thanks for any kind reply.


Solution

  • You are using --patch-module for which it is not intended (JEP 261: Module System):

    The --patch-module option is intended only for testing and debugging. Its use in production settings is strongly discouraged.

    In your scenario, you do not patch an existing method but add an additional method which breaks the API of the system library. Eclipse supports only patching without breaking the API of the system library. The fact that javac does not show any error (probably due to missing checks if the API will not be broken) is a bug in my opinion.

    If you create your own JRE, add it in Window > Preferences: Java > Installed JREs and make sure when creating a new Java project to not select an execution environment JRE, but your specific JRE.