Search in sources :

Example 1 with JavaLangAccess

use of jdk.internal.misc.JavaLangAccess in project openj9 by eclipse.

the class IbmTypeAnnotationsApiTest method testGetRawClassTypeAnnotations.

@Test
public void testGetRawClassTypeAnnotations() {
    JavaLangAccess jlAccess = SharedSecrets.getJavaLangAccess();
    byte[] attr = jlAccess.getRawClassTypeAnnotations(typeAnnotatedClass);
    TypeAnnotationUtils.dumpTypeAnnotation("class", typeAnnotatedClass.getName(), typeAnnotatedClass, attr);
}
Also used : JavaLangAccess(jdk.internal.misc.JavaLangAccess) Test(org.testng.annotations.Test)

Example 2 with JavaLangAccess

use of jdk.internal.misc.JavaLangAccess in project openj9 by eclipse.

the class IbmTypeAnnotationsApiTest method testGetRawClassAnnotations.

@Test
public void testGetRawClassAnnotations() {
    JavaLangAccess jlAccess = SharedSecrets.getJavaLangAccess();
    byte[] attr = jlAccess.getRawClassAnnotations(typeAnnotatedClass);
    ByteArrayInputStream attrReader = new ByteArrayInputStream(attr);
    TypeAnnotationUtils.dumpAnnotations(attrReader);
}
Also used : JavaLangAccess(jdk.internal.misc.JavaLangAccess) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test)

Example 3 with JavaLangAccess

use of jdk.internal.misc.JavaLangAccess in project Bytecoder by mirkosertic.

the class TypeAnnotationParser method parseAllTypeAnnotations.

/*
     * Parse all type annotations on the declaration supplied. This is needed
     * when you go from for example an annotated return type on a method that
     * is a type variable declared on the class. In this case you need to
     * 'jump' to the decl of the class and parse all type annotations there to
     * find the ones that are applicable to the type variable.
     */
static TypeAnnotation[] parseAllTypeAnnotations(AnnotatedElement decl) {
    Class<?> container;
    byte[] rawBytes;
    JavaLangAccess javaLangAccess = SharedSecrets.getJavaLangAccess();
    if (decl instanceof Class) {
        container = (Class<?>) decl;
        rawBytes = javaLangAccess.getRawClassTypeAnnotations(container);
    } else if (decl instanceof Executable) {
        container = ((Executable) decl).getDeclaringClass();
        rawBytes = javaLangAccess.getRawExecutableTypeAnnotations((Executable) decl);
    } else {
        // Should not reach here. Assert?
        return EMPTY_TYPE_ANNOTATION_ARRAY;
    }
    return parseTypeAnnotations(rawBytes, javaLangAccess.getConstantPool(container), decl, container);
}
Also used : JavaLangAccess(jdk.internal.misc.JavaLangAccess)

Example 4 with JavaLangAccess

use of jdk.internal.misc.JavaLangAccess in project Bytecoder by mirkosertic.

the class ModuleBootstrap method addIllegalAccess.

/**
 * Process the --illegal-access option (and its default) to open packages
 * of system modules in the boot layer to code in unnamed modules.
 */
private static void addIllegalAccess(ModuleFinder upgradeModulePath, SystemModules systemModules, ModuleLayer bootLayer, boolean extraExportsOrOpens) {
    String value = getAndRemoveProperty("jdk.module.illegalAccess");
    IllegalAccessLogger.Mode mode = IllegalAccessLogger.Mode.ONESHOT;
    if (value != null) {
        switch(value) {
            case "deny":
                return;
            case "permit":
                break;
            case "warn":
                mode = IllegalAccessLogger.Mode.WARN;
                break;
            case "debug":
                mode = IllegalAccessLogger.Mode.DEBUG;
                break;
            default:
                fail("Value specified to --illegal-access not recognized:" + " '" + value + "'");
                return;
        }
    }
    IllegalAccessLogger.Builder builder = new IllegalAccessLogger.Builder(mode, System.err);
    Map<String, Set<String>> map1 = systemModules.concealedPackagesToOpen();
    Map<String, Set<String>> map2 = systemModules.exportedPackagesToOpen();
    if (map1.isEmpty() && map2.isEmpty()) {
        // need to generate (exploded build)
        IllegalAccessMaps maps = IllegalAccessMaps.generate(limitedFinder());
        map1 = maps.concealedPackagesToOpen();
        map2 = maps.exportedPackagesToOpen();
    }
    // open specific packages in the system modules
    for (Module m : bootLayer.modules()) {
        ModuleDescriptor descriptor = m.getDescriptor();
        String name = m.getName();
        // skip open modules
        if (descriptor.isOpen()) {
            continue;
        }
        // skip modules loaded from the upgrade module path
        if (upgradeModulePath != null && upgradeModulePath.find(name).isPresent()) {
            continue;
        }
        Set<String> concealedPackages = map1.getOrDefault(name, Set.of());
        Set<String> exportedPackages = map2.getOrDefault(name, Set.of());
        // refresh the set of concealed and exported packages if needed
        if (extraExportsOrOpens) {
            concealedPackages = new HashSet<>(concealedPackages);
            exportedPackages = new HashSet<>(exportedPackages);
            Iterator<String> iterator = concealedPackages.iterator();
            while (iterator.hasNext()) {
                String pn = iterator.next();
                if (m.isExported(pn, BootLoader.getUnnamedModule())) {
                    // concealed package is exported to ALL-UNNAMED
                    iterator.remove();
                    exportedPackages.add(pn);
                }
            }
            iterator = exportedPackages.iterator();
            while (iterator.hasNext()) {
                String pn = iterator.next();
                if (m.isOpen(pn, BootLoader.getUnnamedModule())) {
                    // exported package is opened to ALL-UNNAMED
                    iterator.remove();
                }
            }
        }
        // log reflective access to all types in concealed packages
        builder.logAccessToConcealedPackages(m, concealedPackages);
        // log reflective access to non-public members/types in exported packages
        builder.logAccessToExportedPackages(m, exportedPackages);
        // open the packages to unnamed modules
        JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
        jla.addOpensToAllUnnamed(m, concat(concealedPackages.iterator(), exportedPackages.iterator()));
    }
    builder.complete();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) JavaLangAccess(jdk.internal.misc.JavaLangAccess) ModuleDescriptor(java.lang.module.ModuleDescriptor) ResolvedModule(java.lang.module.ResolvedModule)

Example 5 with JavaLangAccess

use of jdk.internal.misc.JavaLangAccess in project Bytecoder by mirkosertic.

the class AnnotationType method getInstance.

/**
 * Returns an AnnotationType instance for the specified annotation type.
 *
 * @throws IllegalArgumentException if the specified class object
 *         does not represent a valid annotation type
 */
public static AnnotationType getInstance(Class<? extends Annotation> annotationClass) {
    JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
    // volatile read
    AnnotationType result = jla.getAnnotationType(annotationClass);
    if (result == null) {
        result = new AnnotationType(annotationClass);
        // try to CAS the AnnotationType: null -> result
        if (!jla.casAnnotationType(annotationClass, null, result)) {
            // somebody was quicker -> read it's result
            result = jla.getAnnotationType(annotationClass);
            assert result != null;
        }
    }
    return result;
}
Also used : JavaLangAccess(jdk.internal.misc.JavaLangAccess)

Aggregations

JavaLangAccess (jdk.internal.misc.JavaLangAccess)5 Test (org.testng.annotations.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ModuleDescriptor (java.lang.module.ModuleDescriptor)1 ResolvedModule (java.lang.module.ResolvedModule)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1