107 return SystemModules.MODULE_NAMES.length > 0;
108 }
109
110 private static String[] moduleNames() {
111 if (isFastPathSupported())
112 // module names recorded at link time
113 return SystemModules.MODULE_NAMES;
114
115 // this happens when java.base is patched with java.base
116 // from an exploded image
117 return imageReader.getModuleNames();
118 }
119
120 // the set of modules in the run-time image
121 private final Set<ModuleReference> modules;
122
123 // maps module name to module reference
124 private final Map<String, ModuleReference> nameToModule;
125
126 // module name to hashes
127 private final Map<String, byte[]> hashes = new HashMap<>();
128
129 private SystemModuleFinder() {
130 String[] names = moduleNames();
131 int n = names.length;
132 moduleCount.add(n);
133
134 // fastpath is enabled by default.
135 // It can be disabled for troubleshooting purpose.
136 boolean disabled =
137 System.getProperty("jdk.system.module.finder.disabledFastPath") != null;
138
139 ModuleDescriptor[] descriptors;
140 ModuleHashes[] recordedHashes;
141 ModuleResolution[] moduleResolutions;
142
143 // fast loading of ModuleDescriptor of system modules
144 if (isFastPathSupported() && !disabled) {
145 descriptors = SystemModules.descriptors();
146 recordedHashes = SystemModules.hashes();
147 moduleResolutions = SystemModules.moduleResolutions();
148 } else {
149 // if fast loading of ModuleDescriptors is disabled
150 // fallback to read module-info.class
151 descriptors = new ModuleDescriptor[n];
152 recordedHashes = new ModuleHashes[n];
153 moduleResolutions = new ModuleResolution[n];
154 for (int i = 0; i < names.length; i++) {
155 String mn = names[i];
156 ImageLocation loc = imageReader.findLocation(mn, "module-info.class");
157 ModuleInfo.Attributes attrs =
158 ModuleInfo.read(imageReader.getResourceBuffer(loc), null);
159 descriptors[i] = attrs.descriptor();
160 recordedHashes[i] = attrs.recordedHashes();
161 moduleResolutions[i] = attrs.moduleResolution();
162 }
163 }
164
165 // record the hashes to build HashSupplier
166 for (ModuleHashes mh : recordedHashes) {
167 if (mh != null) {
168 hashes.putAll(mh.hashes());
169 }
170 }
171
172 ModuleReference[] mods = new ModuleReference[n];
173
174 @SuppressWarnings(value = {"rawtypes", "unchecked"})
175 Entry<String, ModuleReference>[] map
176 = (Entry<String, ModuleReference>[])new Entry[n];
177
178 for (int i = 0; i < n; i++) {
179 ModuleDescriptor md = descriptors[i];
180
181 // create the ModuleReference
182 ModuleReference mref = toModuleReference(md,
183 recordedHashes[i],
184 hashSupplier(names[i]),
185 moduleResolutions[i]);
186 mods[i] = mref;
187 map[i] = Map.entry(names[i], mref);
188
189 // counters
190 packageCount.add(md.packages().size());
|
107 return SystemModules.MODULE_NAMES.length > 0;
108 }
109
110 private static String[] moduleNames() {
111 if (isFastPathSupported())
112 // module names recorded at link time
113 return SystemModules.MODULE_NAMES;
114
115 // this happens when java.base is patched with java.base
116 // from an exploded image
117 return imageReader.getModuleNames();
118 }
119
120 // the set of modules in the run-time image
121 private final Set<ModuleReference> modules;
122
123 // maps module name to module reference
124 private final Map<String, ModuleReference> nameToModule;
125
126 // module name to hashes
127 private final Map<String, byte[]> hashes;
128
129 private SystemModuleFinder() {
130 String[] names = moduleNames();
131 int n = names.length;
132 moduleCount.add(n);
133
134 // fastpath is enabled by default.
135 // It can be disabled for troubleshooting purpose.
136 boolean disabled =
137 System.getProperty("jdk.system.module.finder.disabledFastPath") != null;
138
139 ModuleDescriptor[] descriptors;
140 ModuleHashes[] recordedHashes;
141 ModuleResolution[] moduleResolutions;
142
143 // fast loading of ModuleDescriptor of system modules
144 if (isFastPathSupported() && !disabled) {
145 descriptors = SystemModules.descriptors();
146 recordedHashes = SystemModules.hashes();
147 moduleResolutions = SystemModules.moduleResolutions();
148 } else {
149 // if fast loading of ModuleDescriptors is disabled
150 // fallback to read module-info.class
151 descriptors = new ModuleDescriptor[n];
152 recordedHashes = new ModuleHashes[n];
153 moduleResolutions = new ModuleResolution[n];
154 for (int i = 0; i < names.length; i++) {
155 String mn = names[i];
156 ImageLocation loc = imageReader.findLocation(mn, "module-info.class");
157 ModuleInfo.Attributes attrs =
158 ModuleInfo.read(imageReader.getResourceBuffer(loc), null);
159 descriptors[i] = attrs.descriptor();
160 recordedHashes[i] = attrs.recordedHashes();
161 moduleResolutions[i] = attrs.moduleResolution();
162 }
163 }
164
165 Map<String, byte[]> hashes = null;
166 boolean secondSeen = false;
167 // record the hashes to build HashSupplier
168 for (ModuleHashes mh : recordedHashes) {
169 if (mh != null) {
170 // if only one module contain ModuleHashes, use it
171 if (hashes == null) {
172 hashes = mh.hashes();
173 } else {
174 if (!secondSeen) {
175 hashes = new HashMap<>(hashes);
176 secondSeen = true;
177 }
178 hashes.putAll(mh.hashes());
179 }
180 }
181 }
182 this.hashes = (hashes == null) ? Map.of() : hashes;
183
184 ModuleReference[] mods = new ModuleReference[n];
185
186 @SuppressWarnings(value = {"rawtypes", "unchecked"})
187 Entry<String, ModuleReference>[] map
188 = (Entry<String, ModuleReference>[])new Entry[n];
189
190 for (int i = 0; i < n; i++) {
191 ModuleDescriptor md = descriptors[i];
192
193 // create the ModuleReference
194 ModuleReference mref = toModuleReference(md,
195 recordedHashes[i],
196 hashSupplier(names[i]),
197 moduleResolutions[i]);
198 mods[i] = mref;
199 map[i] = Map.entry(names[i], mref);
200
201 // counters
202 packageCount.add(md.packages().size());
|