Search in sources :

Example 1 with Module

use of java.lang.Module in project openj9 by eclipse.

the class Test_Class method test_forName.

/**
 * @tests java.lang.Class#forName(java.lang.String)
 * @tests java.lang.Class#forName(java.lang.Module, java.lang.String)
 */
@Test
public void test_forName() {
    try {
        // package java.rmi exported by module java.rmi
        Class<?> jrAccessExceptionClz = Class.forName("java.rmi.AccessException");
        AssertJUnit.assertNotNull(jrAccessExceptionClz);
        if (!jrAccessExceptionClz.getProtectionDomain().implies(ALL_PERMISSION)) {
            AssertJUnit.fail("java.rmi.AccessException should have all permission!");
        }
        Module jrModule = jrAccessExceptionClz.getModule();
        Class<?> jrAlreadyBoundExceptionClz = Class.forName(jrModule, "java.rmi.AlreadyBoundException");
        if ((jrAlreadyBoundExceptionClz == null) || !jrAlreadyBoundExceptionClz.getProtectionDomain().implies(ALL_PERMISSION)) {
            AssertJUnit.fail("java.rmi.AlreadyBoundException should have all permission as well!");
        }
    } catch (ClassNotFoundException e) {
        AssertJUnit.fail("Unexpected ClassNotFoundException: " + e.getMessage());
    }
}
Also used : Module(java.lang.Module) ClassNotFoundException(java.lang.ClassNotFoundException) Test(org.testng.annotations.Test)

Example 2 with Module

use of java.lang.Module in project jboss-modules by jboss-modules.

the class JDKModuleFinder method findModule.

public ModuleSpec findModule(final String name, final ModuleLoader delegateLoader) {
    final Set<String> packages;
    final Module module;
    if ("org.jboss.modules".equals(name)) {
        module = getClass().getModule();
        if (module.isNamed()) {
            packages = module.getPackages();
        } else {
            packages = Set.of("org.jboss.modules", "org.jboss.modules.filter", "org.jboss.modules.log", "org.jboss.modules.management", "org.jboss.modules.maven", "org.jboss.modules.ref", "org.jboss.modules.security", "org.jboss.modules.xml");
        }
    } else {
        final Optional<Module> moduleOptional = layer.findModule(name);
        if (!moduleOptional.isPresent()) {
            return null;
        }
        module = moduleOptional.get();
        packages = module.getPackages();
    }
    final ModuleSpec.Builder builder = ModuleSpec.build(name);
    final ModuleDescriptor descriptor = module.getDescriptor();
    if (descriptor != null) {
        final Optional<String> version = descriptor.rawVersion();
        if (version.isPresent())
            builder.setVersion(Version.parse(version.get()));
        for (ModuleDescriptor.Requires require : descriptor.requires()) {
            final Set<ModuleDescriptor.Requires.Modifier> modifiers = require.modifiers();
            builder.addDependency(new ModuleDependencySpecBuilder().setName(require.name()).setExport(modifiers.contains(ModuleDescriptor.Requires.Modifier.TRANSITIVE)).setOptional(modifiers.contains(ModuleDescriptor.Requires.Modifier.STATIC)).build());
        }
    }
    final Set<String> paths = new HashSet<>(packages.size());
    for (String pkg : packages) {
        paths.add(pkg.replace('.', '/'));
    }
    final LocalDependencySpecBuilder depBuilder = new LocalDependencySpecBuilder();
    depBuilder.setLoaderPaths(paths);
    depBuilder.setExport(true);
    depBuilder.setLocalLoader(new JDKModuleLoader(module, packages));
    builder.addDependency(depBuilder.build());
    return builder.create();
}
Also used : ModuleDescriptor(java.lang.module.ModuleDescriptor) Module(java.lang.Module) HashSet(java.util.HashSet)

Example 3 with Module

use of java.lang.Module in project openj9 by eclipse.

the class MyLoader method dumpModules.

private void dumpModules() {
    HashSet<Option> opts = new HashSet<>(Arrays.asList(new Option[] { Option.RETAIN_CLASS_REFERENCE, Option.SHOW_HIDDEN_FRAMES, Option.SHOW_REFLECT_FRAMES }));
    List<StackFrame> frameList = (StackWalker.getInstance(opts)).walk(s -> s.collect(Collectors.toList()));
    for (StackFrame f : frameList) {
        Module frameModule = f.getDeclaringClass().getModule();
        if ((null != frameModule)) {
            ModuleDescriptor d = frameModule.getDescriptor();
            if (null != d) {
                StackTraceElement e = f.toStackTraceElement();
                String modName = d.name();
                if (null != modName) {
                    assertEquals(modName, e.getModuleName(), "Wrong module name");
                }
                Optional<Version> modVersion = d.version();
                if (modVersion.isPresent()) {
                    assertEquals(modVersion.get(), e.getModuleVersion(), "Wrong module version");
                }
                if (f.getDeclaringClass() == Thread.class) {
                    assertEquals("java.base", e.getModuleName(), "Wrong module name");
                }
            }
        }
    }
}
Also used : ModuleDescriptor(java.lang.module.ModuleDescriptor) Version(java.lang.module.ModuleDescriptor.Version) StackFrame(java.lang.StackWalker.StackFrame) Option(java.lang.StackWalker.Option) Module(java.lang.Module) HashSet(java.util.HashSet)

Example 4 with Module

use of java.lang.Module in project jboss-modules by jboss-modules.

the class JDKModuleFinder method iterateModules.

public Iterator<String> iterateModules(final String baseName, final boolean recursive) {
    final Set<Module> moduleSet = layer.modules();
    final ArrayList<String> nameList = new ArrayList<>(moduleSet.size() + 1);
    final Module ourModule = getClass().getModule();
    boolean foundUs = false;
    for (Module module : moduleSet) {
        if (module == ourModule) {
            foundUs = true;
        }
        nameList.add(module.getName());
    }
    if (!foundUs) {
        nameList.add("org.jboss.modules");
    }
    return nameList.iterator();
}
Also used : ArrayList(java.util.ArrayList) Module(java.lang.Module)

Aggregations

Module (java.lang.Module)4 ModuleDescriptor (java.lang.module.ModuleDescriptor)2 HashSet (java.util.HashSet)2 ClassNotFoundException (java.lang.ClassNotFoundException)1 Option (java.lang.StackWalker.Option)1 StackFrame (java.lang.StackWalker.StackFrame)1 Version (java.lang.module.ModuleDescriptor.Version)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1