Add jpms-basics module: JPMS for working developers, exports vs opens, split packages, automatic modules, jdeps and jlink on JDK 25
Five modules built with plain javac/java/jdeps/jlink on Temurin 25.0.5.1. Sources, broken/ cases, run.sh and captured transcripts in output/. README row added. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4XrpfdpsacrvqnrBM
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
1fb1455734
commit
de2660e998
@@ -0,0 +1,97 @@
|
||||
package com.acme.app;
|
||||
|
||||
import com.acme.app.domain.Customer;
|
||||
import com.acme.greeter.api.Greeter;
|
||||
import com.acme.mapper.Mapper;
|
||||
import com.acme.model.Greeting;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** One program, one switch: pick a scenario with the first argument. */
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String mode = args.length > 0 ? args[0] : "hello";
|
||||
switch (mode) {
|
||||
case "hello" -> hello();
|
||||
case "inspect" -> inspect();
|
||||
case "reflect-private" -> reflectPrivate();
|
||||
case "reflect-public" -> reflectPublic();
|
||||
case "forname-internal" -> fornameInternal();
|
||||
case "mapper" -> mapper();
|
||||
case "services" -> services();
|
||||
case "jdk-internals" -> jdkInternals();
|
||||
default -> throw new IllegalArgumentException("unknown mode " + mode);
|
||||
}
|
||||
}
|
||||
|
||||
static void hello() {
|
||||
Greeting g = new Greeter().greet("Mumbai");
|
||||
System.out.println(g);
|
||||
}
|
||||
|
||||
static void inspect() {
|
||||
Module app = Main.class.getModule();
|
||||
Module greeter = Greeter.class.getModule();
|
||||
System.out.println("app.isNamed() = " + app.isNamed() + " name=" + app.getName());
|
||||
System.out.println("greeter.isNamed() = " + greeter.isNamed());
|
||||
System.out.println("app reads greeter = " + app.canRead(greeter));
|
||||
if (greeter.isNamed()) {
|
||||
System.out.println("greeter packages = " + greeter.getPackages().stream().sorted().collect(Collectors.toList()));
|
||||
}
|
||||
for (String p : new String[] {"com.acme.greeter.api", "com.acme.greeter.internal"}) {
|
||||
System.out.printf("%-27s exported to app: %-5s opened to app: %s%n",
|
||||
p, greeter.isExported(p, app), greeter.isOpen(p, app));
|
||||
}
|
||||
if (greeter.isNamed()) { // the unnamed module (class path) has no descriptor
|
||||
System.out.println("descriptor requires = " + greeter.getDescriptor().requires().stream()
|
||||
.map(Object::toString).sorted().collect(Collectors.toList()));
|
||||
System.out.println("descriptor uses = " + greeter.getDescriptor().uses());
|
||||
}
|
||||
System.out.println("String.class module = " + String.class.getModule());
|
||||
}
|
||||
|
||||
/** exports is not opens: the public API is usable, the private field is not. */
|
||||
static void reflectPrivate() throws Exception {
|
||||
Field f = Greeter.class.getDeclaredField("prefix");
|
||||
System.out.println("found the field: " + f);
|
||||
f.setAccessible(true);
|
||||
System.out.println("read it: " + f.get(new Greeter()));
|
||||
}
|
||||
|
||||
/** Reflection on public members of an exported package needs no `opens`. */
|
||||
static void reflectPublic() throws Exception {
|
||||
Method m = Greeter.class.getMethod("greet", String.class);
|
||||
Object r = m.invoke(new Greeter(), "reflection");
|
||||
System.out.println("invoked public method reflectively: " + r);
|
||||
}
|
||||
|
||||
/** A public class in a package that is not exported cannot be reached, even by name. */
|
||||
static void fornameInternal() throws Exception {
|
||||
Class<?> c = Class.forName("com.acme.greeter.internal.Secrets");
|
||||
System.out.println("class loaded: " + c.getName());
|
||||
Method m = c.getMethod("token");
|
||||
System.out.println("calling it...");
|
||||
System.out.println(m.invoke(null));
|
||||
}
|
||||
|
||||
static void mapper() throws Exception {
|
||||
System.out.println(Mapper.toMap(new Customer("Asha", 120)));
|
||||
}
|
||||
|
||||
static void services() {
|
||||
Greeter g = new Greeter();
|
||||
System.out.println("providers found: " + g.providerCount());
|
||||
System.out.println(g.greetFormally("Asha"));
|
||||
}
|
||||
|
||||
/** Deep reflection into java.base: the strongly-encapsulated classic. */
|
||||
static void jdkInternals() throws Exception {
|
||||
Field f = String.class.getDeclaredField("value");
|
||||
System.out.println("found: " + f);
|
||||
f.setAccessible(true);
|
||||
System.out.println("value length: " + ((byte[]) f.get("hello")).length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.acme.app.domain;
|
||||
|
||||
public class Customer {
|
||||
private final String name;
|
||||
private final int loyaltyPoints;
|
||||
|
||||
public Customer(String name, int loyaltyPoints) {
|
||||
this.name = name;
|
||||
this.loyaltyPoints = loyaltyPoints;
|
||||
}
|
||||
|
||||
public String name() { return name; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
module com.acme.app {
|
||||
requires com.acme.greeter; // model comes along because greeter says `requires transitive`
|
||||
requires com.acme.mapper;
|
||||
|
||||
// Deep reflection into the domain package is allowed for exactly one module.
|
||||
opens com.acme.app.domain to com.acme.mapper;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.acme.formal;
|
||||
|
||||
import com.acme.greeter.api.Formatter;
|
||||
|
||||
/** Not exported at all: the only way in is ServiceLoader. */
|
||||
public class FormalFormatter implements Formatter {
|
||||
public String name() { return "formal"; }
|
||||
public String format(String who) { return "Good day, " + who + "."; }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
module com.acme.formal {
|
||||
requires com.acme.greeter;
|
||||
provides com.acme.greeter.api.Formatter with com.acme.formal.FormalFormatter;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.acme.greeter.api;
|
||||
|
||||
/** Service interface. Providers live in other modules and are found with ServiceLoader. */
|
||||
public interface Formatter {
|
||||
String name();
|
||||
String format(String who);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.acme.greeter.api;
|
||||
|
||||
import com.acme.greeter.internal.Secrets;
|
||||
import com.acme.model.Greeting;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
/** The one public class of the module. Its private field is used by the reflection demos. */
|
||||
public final class Greeter {
|
||||
|
||||
private final String prefix = "Hello";
|
||||
|
||||
/** Plain greeting, no services involved. */
|
||||
public Greeting greet(String who) {
|
||||
return new Greeting(prefix + ", " + who + "! (" + Secrets.token() + ")", "built-in");
|
||||
}
|
||||
|
||||
/** Uses the first Formatter provider found; falls back to greet() when there is none. */
|
||||
public Greeting greetFormally(String who) {
|
||||
for (Formatter f : ServiceLoader.load(Formatter.class)) {
|
||||
return new Greeting(f.format(who), f.name());
|
||||
}
|
||||
return greet(who);
|
||||
}
|
||||
|
||||
public int providerCount() {
|
||||
int n = 0;
|
||||
for (Formatter ignored : ServiceLoader.load(Formatter.class)) n++;
|
||||
return n;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.acme.greeter.internal;
|
||||
|
||||
/** public class, public method -- but in a package the module does not export. */
|
||||
public final class Secrets {
|
||||
private Secrets() { }
|
||||
public static String token() { return "s3cr3t"; }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
module com.acme.greeter {
|
||||
// Greeting is part of Greeter's public signature, so every reader of greeter must also read model.
|
||||
requires transitive com.acme.model;
|
||||
|
||||
// The public surface. Anything not listed here is invisible outside the module,
|
||||
// even if the class is declared `public`.
|
||||
exports com.acme.greeter.api;
|
||||
|
||||
// com.acme.greeter.internal is deliberately NOT exported.
|
||||
|
||||
// We look up implementations of Formatter at run time instead of naming them.
|
||||
uses com.acme.greeter.api.Formatter;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.acme.mapper;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A twenty-line stand-in for Jackson, Hibernate or Spring: it reads every declared field of
|
||||
* any object it is given, private ones included. That is exactly the kind of code that
|
||||
* needs `opens` from the module that owns the object.
|
||||
*/
|
||||
public final class Mapper {
|
||||
private Mapper() { }
|
||||
|
||||
public static Map<String, Object> toMap(Object o) throws ReflectiveOperationException {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
for (Field f : o.getClass().getDeclaredFields()) {
|
||||
f.setAccessible(true); // <- the call that the module system polices
|
||||
m.put(f.getName(), f.get(o));
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module com.acme.mapper {
|
||||
exports com.acme.mapper;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.acme.model;
|
||||
|
||||
/** A value type that crosses a module boundary: greeter returns it, app reads it. */
|
||||
public record Greeting(String text, String formatter) { }
|
||||
@@ -0,0 +1,3 @@
|
||||
module com.acme.model {
|
||||
exports com.acme.model;
|
||||
}
|
||||
Reference in New Issue
Block a user