Skip to content

Commit e6eb015

Browse files
committed
svm: add ResolvedJavaModule, ResolvedJavaModuleLayer and ResolvedJavaPackage
1 parent bf9d313 commit e6eb015

File tree

8 files changed

+441
-0
lines changed

8 files changed

+441
-0
lines changed

substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/JVMCIReflectionUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ public static String getPackageName(ResolvedJavaType type) {
238238
return (dot != -1) ? cn.substring(0, dot).intern() : "";
239239
}
240240

241+
public static ResolvedJavaPackage getPackage(ResolvedJavaType type) {
242+
return JVMCIReflectionUtilFallback.getPackage(type);
243+
}
244+
241245
/**
242246
* Gets the return type for a {@link ResolvedJavaMethod}. This is the same as calling
243247
* {@link Method#getReturnType()} on the underlying method.
@@ -274,4 +278,8 @@ public static String getTypeName(ResolvedJavaType type) {
274278
}
275279
return type.toClassName();
276280
}
281+
282+
public static ResolvedJavaModule getModule(ResolvedJavaType declaringClass) {
283+
return JVMCIReflectionUtilFallback.getModule(declaringClass);
284+
}
277285
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2025, 2025, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.util;
26+
27+
import jdk.vm.ci.meta.ResolvedJavaType;
28+
29+
/**
30+
* Fallback implementations for {@link JVMCIReflectionUtil} that is still implemented in terms of
31+
* core reflection due to missing features in JVMCI.
32+
*/
33+
final class JVMCIReflectionUtilFallback {
34+
35+
private static Class<?> getJavaClass(ResolvedJavaType type) {
36+
return OriginalClassProvider.getJavaClass(type);
37+
}
38+
39+
static ResolvedJavaPackage getPackage(ResolvedJavaType type) {
40+
return new ResolvedJavaPackageImpl(getJavaClass(type).getPackage());
41+
}
42+
43+
static ResolvedJavaModule getModule(ResolvedJavaType declaringClass) {
44+
return new ResolvedJavaModuleImpl(getJavaClass(declaringClass).getModule());
45+
}
46+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2025, 2025, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.util;
26+
27+
import java.lang.module.ModuleDescriptor;
28+
import java.util.Set;
29+
30+
/**
31+
* JVMCI equivalent to {@link Module}. Do not compare with {@code ==}, use {@code #equals(Object)}
32+
* instead.
33+
*/
34+
public interface ResolvedJavaModule {
35+
/**
36+
* Returns the module name or {@code null} if this module is an unnamed module. See
37+
* {@link Module#getName()}.
38+
*/
39+
String getName();
40+
41+
/**
42+
* Returns {@code true} if this module has <em>opened</em> a package unconditionally. See
43+
* {@link Module#isOpen(String)}.
44+
*/
45+
boolean isOpen(String pn);
46+
47+
/**
48+
* Returns {@code true} if this module has <em>opened</em> a package to at least the given
49+
* module. See {@link Module#isOpen(String, java.lang.Module)}.
50+
*/
51+
boolean isOpen(String packageName, ResolvedJavaModule accessingModule);
52+
53+
/**
54+
* Returns {@code true} if this module exports the given package unconditionally. See
55+
* {@link Module#isExported(String)}.
56+
*/
57+
boolean isExported(String packageName);
58+
59+
/**
60+
* Returns {@code true} if this module exports the given package to at least the given module.
61+
* See {@link Module#isExported(String, java.lang.Module)}.
62+
*/
63+
boolean isExported(String packageName, ResolvedJavaModule accessingModule);
64+
65+
/**
66+
* Returns the set of package names for the packages in this module. See
67+
* {@link java.lang.Module#getPackages()}.
68+
*/
69+
Set<String> getPackages();
70+
71+
/**
72+
* Returns {@code true} if this module is a named module. See {@link Module#isNamed()}.
73+
*/
74+
boolean isNamed();
75+
76+
/**
77+
* Returns the module descriptor for this module or {@code null} if this module is an unnamed
78+
* module. See {@link Module#getDescriptor()}.
79+
* <p>
80+
* Note that although {@link ModuleDescriptor} is a JDK class, it is OK in JVMCI because it is
81+
* purely symbolic.
82+
*/
83+
ModuleDescriptor getDescriptor();
84+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2025, 2025, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.util;
26+
27+
import java.lang.module.ModuleDescriptor;
28+
import java.util.Objects;
29+
import java.util.Set;
30+
31+
/**
32+
* Fallback implementation of {@link ResolvedJavaModule} based on {@link Module}.
33+
*/
34+
final class ResolvedJavaModuleImpl implements ResolvedJavaModule {
35+
36+
private final Module module;
37+
38+
ResolvedJavaModuleImpl(Module module) {
39+
this.module = Objects.requireNonNull(module);
40+
}
41+
42+
@Override
43+
public String getName() {
44+
return module.getName();
45+
}
46+
47+
@Override
48+
public boolean equals(Object o) {
49+
if (o == null || getClass() != o.getClass()) {
50+
return false;
51+
}
52+
ResolvedJavaModuleImpl that = (ResolvedJavaModuleImpl) o;
53+
return module.equals(that.module);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return module.hashCode();
59+
}
60+
61+
@Override
62+
public boolean isOpen(String pn) {
63+
return module.isOpen(pn);
64+
}
65+
66+
@Override
67+
public boolean isOpen(String packageName, ResolvedJavaModule accessingModule) {
68+
return module.isOpen(packageName, toImpl(accessingModule).module);
69+
}
70+
71+
@Override
72+
public boolean isExported(String packageName) {
73+
return module.isExported(packageName);
74+
}
75+
76+
@Override
77+
public boolean isExported(String packageName, ResolvedJavaModule accessingModule) {
78+
return module.isExported(packageName, toImpl(accessingModule).module);
79+
}
80+
81+
@Override
82+
public Set<String> getPackages() {
83+
return module.getPackages();
84+
}
85+
86+
@Override
87+
public boolean isNamed() {
88+
return module.isNamed();
89+
}
90+
91+
@Override
92+
public ModuleDescriptor getDescriptor() {
93+
return module.getDescriptor();
94+
}
95+
96+
private static ResolvedJavaModuleImpl toImpl(ResolvedJavaModule module) {
97+
if (module instanceof ResolvedJavaModuleImpl moduleImpl) {
98+
return moduleImpl;
99+
}
100+
throw new IllegalArgumentException("Unsupported ResolvedJavaModule implementation: " + module.getClass().getName());
101+
}
102+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2025, 2025, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.util;
26+
27+
import java.util.Optional;
28+
29+
/**
30+
* JVMCI equivalent for {@link ModuleLayer}. Do not compare with {@code ==}, use
31+
* {@code #equals(Object)} instead.
32+
*/
33+
public interface ResolvedJavaModuleLayer {
34+
/**
35+
* Returns the boot layer. See {@link java.lang.ModuleLayer#boot()}.
36+
*/
37+
static ResolvedJavaModuleLayer boot() {
38+
return new ResolvedJavaModuleLayerImpl(ModuleLayer.boot());
39+
}
40+
41+
/**
42+
* Returns the module with the given name in this layer, or if not in this layer, the parent
43+
* layers. See {@link java.lang.ModuleLayer#findModule(String)}.
44+
*/
45+
Optional<ResolvedJavaModule> findModule(String moduleName);
46+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2025, 2025, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.util;
26+
27+
import java.util.Objects;
28+
import java.util.Optional;
29+
30+
/**
31+
* Fallback implementation of {@link ResolvedJavaModuleLayer} based on {@link ModuleLayer}.
32+
*/
33+
final class ResolvedJavaModuleLayerImpl implements ResolvedJavaModuleLayer {
34+
35+
private final ModuleLayer moduleLayer;
36+
37+
ResolvedJavaModuleLayerImpl(ModuleLayer moduleLayer) {
38+
this.moduleLayer = Objects.requireNonNull(moduleLayer);
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (o == null || getClass() != o.getClass()) {
44+
return false;
45+
}
46+
ResolvedJavaModuleLayerImpl that = (ResolvedJavaModuleLayerImpl) o;
47+
return moduleLayer.equals(that.moduleLayer);
48+
}
49+
50+
@Override
51+
public int hashCode() {
52+
return moduleLayer.hashCode();
53+
}
54+
55+
@Override
56+
public Optional<ResolvedJavaModule> findModule(String moduleName) {
57+
return moduleLayer.findModule(moduleName).map(ResolvedJavaModuleImpl::new);
58+
}
59+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2025, 2025, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.util;
26+
27+
/**
28+
* JVMCI equivalent to {@link Package}. Do not compare with {@code ==}, use {@code #equals(Object)}
29+
* instead.
30+
*/
31+
public interface ResolvedJavaPackage {
32+
/**
33+
* Return the version of this implementation. See
34+
* {@link java.lang.Package#getImplementationVersion()}.
35+
*/
36+
String getImplementationVersion();
37+
}

0 commit comments

Comments
 (0)