36 * <pre>
37 * if (Util.JAVA_SPECIFICATION_VERSION >= 9) {
38 * // Use of ModuleAPI
39 * }
40 * </pre>
41 */
42 public final class ModuleAPI {
43
44 private ModuleAPI(Method method) {
45 this.method = method;
46 }
47
48 private final Method method;
49
50 /**
51 * {@code Class.getModule()}.
52 */
53 public static final ModuleAPI getModule;
54
55 /**
56 * {@code jdk.internal.module.Modules.addExports(Module, String, Module)}.
57 */
58 public static final ModuleAPI addExports;
59
60 /**
61 * {@code java.lang.Module.getResourceAsStream(String)}.
62 */
63 public static final ModuleAPI getResourceAsStream;
64
65 /**
66 * {@code java.lang.Module.canRead(Module)}.
67 */
68 public static final ModuleAPI canRead;
69
70 /**
71 * {@code java.lang.Module.isExported(String)}.
72 */
73 public static final ModuleAPI isExported;
74
75 /**
76 * {@code java.lang.Module.isExported(String, Module)}.
77 */
78 public static final ModuleAPI isExportedTo;
79
80 /**
99 checkAvailability();
100 assert !Modifier.isStatic(method.getModifiers());
101 try {
102 return (T) method.invoke(receiver, args);
103 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
104 throw new InternalError(e);
105 }
106 }
107
108 private void checkAvailability() throws InternalError {
109 if (method == null) {
110 throw new InternalError("Cannot use Module API on JDK " + JAVA_SPECIFICATION_VERSION);
111 }
112 }
113
114 static {
115 if (JAVA_SPECIFICATION_VERSION >= 9) {
116 try {
117 getModule = new ModuleAPI(Class.class.getMethod("getModule"));
118 Class<?> moduleClass = getModule.method.getReturnType();
119 Class<?> modulesClass = Class.forName("jdk.internal.module.Modules");
120 getResourceAsStream = new ModuleAPI(moduleClass.getMethod("getResourceAsStream", String.class));
121 canRead = new ModuleAPI(moduleClass.getMethod("canRead", moduleClass));
122 isExported = new ModuleAPI(moduleClass.getMethod("isExported", String.class));
123 isExportedTo = new ModuleAPI(moduleClass.getMethod("isExported", String.class, moduleClass));
124 addExports = new ModuleAPI(modulesClass.getDeclaredMethod("addExports", moduleClass, String.class, moduleClass));
125 } catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) {
126 throw new InternalError(e);
127 }
128 } else {
129 ModuleAPI unavailable = new ModuleAPI(null);
130 getModule = unavailable;
131 getResourceAsStream = unavailable;
132 canRead = unavailable;
133 isExported = unavailable;
134 isExportedTo = unavailable;
135 addExports = unavailable;
136 }
137
138 }
139 }
|
36 * <pre>
37 * if (Util.JAVA_SPECIFICATION_VERSION >= 9) {
38 * // Use of ModuleAPI
39 * }
40 * </pre>
41 */
42 public final class ModuleAPI {
43
44 private ModuleAPI(Method method) {
45 this.method = method;
46 }
47
48 private final Method method;
49
50 /**
51 * {@code Class.getModule()}.
52 */
53 public static final ModuleAPI getModule;
54
55 /**
56 * {@code java.lang.Module.getResourceAsStream(String)}.
57 */
58 public static final ModuleAPI getResourceAsStream;
59
60 /**
61 * {@code java.lang.Module.canRead(Module)}.
62 */
63 public static final ModuleAPI canRead;
64
65 /**
66 * {@code java.lang.Module.isExported(String)}.
67 */
68 public static final ModuleAPI isExported;
69
70 /**
71 * {@code java.lang.Module.isExported(String, Module)}.
72 */
73 public static final ModuleAPI isExportedTo;
74
75 /**
94 checkAvailability();
95 assert !Modifier.isStatic(method.getModifiers());
96 try {
97 return (T) method.invoke(receiver, args);
98 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
99 throw new InternalError(e);
100 }
101 }
102
103 private void checkAvailability() throws InternalError {
104 if (method == null) {
105 throw new InternalError("Cannot use Module API on JDK " + JAVA_SPECIFICATION_VERSION);
106 }
107 }
108
109 static {
110 if (JAVA_SPECIFICATION_VERSION >= 9) {
111 try {
112 getModule = new ModuleAPI(Class.class.getMethod("getModule"));
113 Class<?> moduleClass = getModule.method.getReturnType();
114 getResourceAsStream = new ModuleAPI(moduleClass.getMethod("getResourceAsStream", String.class));
115 canRead = new ModuleAPI(moduleClass.getMethod("canRead", moduleClass));
116 isExported = new ModuleAPI(moduleClass.getMethod("isExported", String.class));
117 isExportedTo = new ModuleAPI(moduleClass.getMethod("isExported", String.class, moduleClass));
118 } catch (NoSuchMethodException | SecurityException e) {
119 throw new InternalError(e);
120 }
121 } else {
122 ModuleAPI unavailable = new ModuleAPI(null);
123 getModule = unavailable;
124 getResourceAsStream = unavailable;
125 canRead = unavailable;
126 isExported = unavailable;
127 isExportedTo = unavailable;
128 }
129 }
130 }
|