Implied readability + layers
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Fri Nov 6 10:59:17 UTC 2015
Hi,
On Fri, Nov 6, 2015 at 4:09 AM, Alex Buckley <alex.buckley at oracle.com>
wrote:
>
>
> com.foo in layer2 requires com.baz in layer1, right? Yes.
>
> com.baz in layer1 uses types from com.bar in layer1, and NOT from com.bar
> in layer2, right? Yes.
>
> Therefore, com.foo uses types from com.bar in layer1 (as required by
> com.baz in layer1), right? Yes.
>
> I don't know what it means to say "we use com.bar at 2 for layer2's
> modules". com.foo is in layer2, and you can make it read com.bar at 2 via
> reflection, but otherwise com.bar at 2 is not read by com.foo because
> com.baz doesn't know about it.
There is no need for reflection:
Please follow this sample and test it:
layer 1: com.baz and com.bar at 1
---------------------------
module com.bar {//version1
exports com.bar;
}
------------------
//Bar.java
package com.bar;
public class Bar {
public String bar(){ return "bar1";}
}
module com.baz {
requires com.bar;
exports com.baz;
}
-----------------
//Baz.java
package com.baz;
import com.bar.Bar;
public class Baz {
public String baz(){ return new Bar().bar();}
public Bar bar(){
return new Bar();
}
}
layer 2: com.foo and com.bar at 2
--------------------
module com.bar {//version2
exports com.bar;
}
-------------
//Bar.java
package com.bar;
public class Bar {
public String bar(){ return "bar2";}
}
-------------
module com.foo {
requires com.baz;
exports com.foo;
}
-------------
Foo.java
package com.foo;
import com.bar.Bar;
import com.baz.Baz;
public class Main {
public static void main(String[] args) {
System.out.println(new Baz().baz());
System.out.println(new Bar().bar());
}
}
-------------------------
test code
public class Test {
public static void main(String[] args) throws Exception {
ModuleFinder finder1 = ModuleFinder.of(Paths.get("mods1"));
Configuration cfg1 = Configuration.resolve(finder1,
Layer.boot(),ModuleFinder.empty(),"com.bar","com.baz");
ModuleClassLoader cl1 = new ModuleClassLoader(cfg1);
Layer layer1 = Layer.create(cfg1, m -> cl1);
ModuleFinder finder2 = ModuleFinder.of(Paths.get("mods2"));
Configuration cfg2 = Configuration.resolve(finder2,
layer1,ModuleFinder.empty(),"com.bar","com.foo");
ModuleClassLoader cl2 = new ModuleClassLoader(cl1,cfg2);
Layer layer2 = Layer.create(cfg2, m -> cl2);
Module foo = layer2.findModule("com.foo").get();
Module bar2 = layer2.findModule("com.bar").get();
Module bar1 = layer1.findModule("com.bar").get();
ClassLoader fooModuleLoader = layer2.findLoader("com.foo");
Class<?> mainClass = fooModuleLoader.loadClass("com.foo.Main");
Test.class.getModule().addReads(mainClass.getModule());
Method mainMethod = mainClass.getMethod("main", String[].class);
mainMethod.invoke(null, (Object)new String[0]);
}
}
Result:
bar1
bar2
As you can see com.foo reads com.bar at 2 without reflection.
I say this is puzzling since with almost the equivalent code I get
another result. If you want I can show for you in another post.
If you want I can send all test files to your mail?
--
Best Regards,
Ali Ebrahimi
More information about the jigsaw-dev
mailing list