Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ClassLoader/getResource/automaticmodules/Main.java
41159 views
1
/*
2
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.lang.module.ModuleReader;
27
import java.lang.module.ModuleReference;
28
import java.lang.module.ResolvedModule;
29
import java.net.URL;
30
import java.util.Enumeration;
31
32
/**
33
* Usage: Main $MODULE
34
*
35
* Finds $MODULE in the boot layer and then tests that it can locate every
36
* resource in the module content (JAR file).
37
*/
38
39
public class Main {
40
41
static void testFind(String name) throws Exception {
42
// getResource
43
URL url = Main.class.getClassLoader().getResource(name);
44
if (url == null)
45
throw new RuntimeException("Unable to locate: " + name);
46
System.out.println(name + " => " + url);
47
48
// getResources
49
Enumeration<URL> urls = Main.class.getClassLoader().getResources(name);
50
if (!urls.hasMoreElements())
51
throw new RuntimeException("Unable to locate: " + name);
52
URL first = urls.nextElement();
53
if (!first.toURI().equals(url.toURI()))
54
throw new RuntimeException("found " + first + " ???");
55
56
// getResourceAsStream
57
if (!url.toString().endsWith("/")) {
58
InputStream in = Main.class.getClassLoader().getResourceAsStream(name);
59
if (in == null)
60
throw new RuntimeException("Unable to locate: " + name);
61
in.close();
62
}
63
}
64
65
static void testFindUnchecked(String name) {
66
try {
67
testFind(name);
68
} catch (Exception e) {
69
throw new RuntimeException(e);
70
}
71
}
72
73
public static void main(String[] args) throws Exception {
74
String mn = args[0];
75
76
ModuleReference mref = ModuleLayer.boot()
77
.configuration()
78
.findModule(mn)
79
.map(ResolvedModule::reference)
80
.orElseThrow(() -> new RuntimeException(mn + " not resolved!!"));
81
82
try (ModuleReader reader = mref.open()) {
83
reader.list().forEach(name -> {
84
testFindUnchecked(name);
85
86
// if the resource is a directory then find without trailing slash
87
if (name.endsWith("/")) {
88
testFindUnchecked(name.substring(0, name.length() - 1));
89
}
90
});
91
}
92
}
93
}
94
95