Search in sources :

Example 1 with ConfigurationCondition

use of org.graalvm.nativeimage.impl.ConfigurationCondition in project graal by oracle.

the class ResourceConfigurationParser method parseBundle.

private void parseBundle(Object bundle) {
    Map<String, Object> resource = asMap(bundle, "Elements of 'bundles' list must be a bundle descriptor object");
    checkAttributes(resource, "bundle descriptor object", Collections.singletonList("name"), Arrays.asList("locales", "classNames", "condition"));
    String basename = asString(resource.get("name"));
    ConfigurationCondition condition = parseCondition(resource);
    Object locales = resource.get("locales");
    if (locales != null) {
        List<Locale> asList = asList(locales, "Attribute 'locales' must be a list of locales").stream().map(ResourceConfigurationParser::parseLocale).collect(Collectors.toList());
        if (!asList.isEmpty()) {
            registry.addResourceBundles(condition, basename, asList);
        }
    }
    Object classNames = resource.get("classNames");
    if (classNames != null) {
        List<Object> asList = asList(classNames, "Attribute 'classNames' must be a list of classes");
        for (Object o : asList) {
            String className = asString(o);
            registry.addClassBasedResourceBundle(condition, basename, className);
        }
    }
    if (locales == null && classNames == null) {
        /* If nothing more precise is specified, register in every included locale */
        registry.addResourceBundles(condition, basename);
    }
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition) Locale(java.util.Locale)

Example 2 with ConfigurationCondition

use of org.graalvm.nativeimage.impl.ConfigurationCondition in project graal by oracle.

the class TypeMethodsWithFlagsTest method doTestResourceConfig.

private static void doTestResourceConfig(ResourceConfiguration resourceConfig) {
    Assert.assertFalse(resourceConfig.anyResourceMatches("seenResource.txt"));
    Assert.assertTrue(resourceConfig.anyResourceMatches("unseenResource.txt"));
    ConfigurationCondition condition = ConfigurationCondition.alwaysTrue();
    Assert.assertFalse(resourceConfig.anyBundleMatches(condition, "seenBundle"));
    Assert.assertTrue(resourceConfig.anyBundleMatches(condition, "unseenBundle"));
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition)

Example 3 with ConfigurationCondition

use of org.graalvm.nativeimage.impl.ConfigurationCondition in project graal by oracle.

the class ReflectionProcessor method processEntry.

@Override
@SuppressWarnings("fallthrough")
public void processEntry(Map<String, ?> entry) {
    boolean invalidResult = Boolean.FALSE.equals(entry.get("result"));
    ConfigurationCondition condition = ConfigurationCondition.alwaysTrue();
    if (invalidResult) {
        return;
    }
    String function = (String) entry.get("function");
    List<?> args = (List<?>) entry.get("args");
    switch(function) {
        // always filter based on the caller class.
        case "getResource":
        case "getResourceAsStream":
        case "getSystemResource":
        case "getSystemResourceAsStream":
        case "getResources":
        case "getSystemResources":
            String literal = singleElement(args);
            String regex = Pattern.quote(literal);
            resourceConfiguration.addResourcePattern(condition, regex);
            return;
    }
    String callerClass = (String) entry.get("caller_class");
    boolean isLoadClass = function.equals("loadClass");
    if (isLoadClass || function.equals("forName") || function.equals("findClass")) {
        String name = singleElement(args);
        if (isLoadClass) {
            // different array syntax
            name = MetaUtil.internalNameToJava(MetaUtil.toInternalName(name), true, true);
        }
        if (!advisor.shouldIgnore(lazyValue(name), lazyValue(callerClass)) && !(isLoadClass && advisor.shouldIgnoreLoadClass(lazyValue(name), lazyValue(callerClass)))) {
            configuration.getOrCreateType(condition, name);
        }
        return;
    } else if (function.equals("methodTypeDescriptor")) {
        List<String> typeNames = singleElement(args);
        for (String type : typeNames) {
            if (!advisor.shouldIgnore(lazyValue(type), lazyValue(callerClass))) {
                configuration.getOrCreateType(condition, type);
            }
        }
    }
    String clazz = (String) entry.get("class");
    if (advisor.shouldIgnore(lazyValue(clazz), lazyValue(callerClass))) {
        return;
    }
    ConfigurationMemberDeclaration declaration = ConfigurationMemberDeclaration.PUBLIC;
    ConfigurationMemberAccessibility accessibility = ConfigurationMemberAccessibility.QUERIED;
    String clazzOrDeclaringClass = entry.containsKey("declaring_class") ? (String) entry.get("declaring_class") : clazz;
    switch(function) {
        case "getDeclaredFields":
            {
                configuration.getOrCreateType(condition, clazz).setAllDeclaredFields();
                break;
            }
        case "getFields":
            {
                configuration.getOrCreateType(condition, clazz).setAllPublicFields();
                break;
            }
        case "getDeclaredMethods":
            {
                configuration.getOrCreateType(condition, clazz).setAllDeclaredMethods(accessibility);
                break;
            }
        case "asInterfaceInstance":
            accessibility = ConfigurationMemberAccessibility.ACCESSED;
        // fallthrough
        case "getMethods":
            {
                configuration.getOrCreateType(condition, clazz).setAllPublicMethods(accessibility);
                break;
            }
        case "getDeclaredConstructors":
            {
                configuration.getOrCreateType(condition, clazz).setAllDeclaredConstructors(accessibility);
                break;
            }
        case "getConstructors":
            {
                configuration.getOrCreateType(condition, clazz).setAllPublicConstructors(accessibility);
                break;
            }
        case "getDeclaredClasses":
            {
                configuration.getOrCreateType(condition, clazz).setAllDeclaredClasses();
                break;
            }
        case "getPermittedSubclasses":
            {
                configuration.getOrCreateType(condition, clazz).setAllPermittedSubclasses();
                break;
            }
        case "getClasses":
            {
                configuration.getOrCreateType(condition, clazz).setAllPublicClasses();
                break;
            }
        case "objectFieldOffset":
        case "findFieldHandle":
        case "unreflectField":
        case "getDeclaredField":
            declaration = "findFieldHandle".equals(function) ? ConfigurationMemberDeclaration.PRESENT : ConfigurationMemberDeclaration.DECLARED;
        // fall through
        case "getField":
            {
                configuration.getOrCreateType(condition, clazzOrDeclaringClass).addField(singleElement(args), declaration, false);
                if (!clazzOrDeclaringClass.equals(clazz)) {
                    configuration.getOrCreateType(condition, clazz);
                }
                break;
            }
        case "getDeclaredMethod":
        case "findMethodHandle":
        case "invokeMethod":
            declaration = "getDeclaredMethod".equals(function) ? ConfigurationMemberDeclaration.DECLARED : ConfigurationMemberDeclaration.PRESENT;
        // fall through
        case "getMethod":
            {
                accessibility = (function.equals("invokeMethod") || function.equals("findMethodHandle")) ? ConfigurationMemberAccessibility.ACCESSED : ConfigurationMemberAccessibility.QUERIED;
                expectSize(args, 2);
                String name = (String) args.get(0);
                List<?> parameterTypes = (List<?>) args.get(1);
                if (parameterTypes == null) {
                    // tolerated and equivalent to no parameter types
                    parameterTypes = Collections.emptyList();
                }
                configuration.getOrCreateType(condition, clazzOrDeclaringClass).addMethod(name, SignatureUtil.toInternalSignature(parameterTypes), declaration, accessibility);
                if (!clazzOrDeclaringClass.equals(clazz)) {
                    configuration.getOrCreateType(condition, clazz);
                }
                break;
            }
        case "getDeclaredConstructor":
        case "findConstructorHandle":
        case "invokeConstructor":
            declaration = "getDeclaredConstructor".equals(function) ? ConfigurationMemberDeclaration.DECLARED : ConfigurationMemberDeclaration.PRESENT;
        // fall through
        case "getConstructor":
            {
                accessibility = (function.equals("invokeConstructor") || function.equals("findConstructorHandle")) ? ConfigurationMemberAccessibility.ACCESSED : ConfigurationMemberAccessibility.QUERIED;
                List<String> parameterTypes = singleElement(args);
                if (parameterTypes == null) {
                    // tolerated and equivalent to no parameter types
                    parameterTypes = Collections.emptyList();
                }
                String signature = SignatureUtil.toInternalSignature(parameterTypes);
                assert clazz.equals(clazzOrDeclaringClass) : "Constructor can only be accessed via declaring class";
                configuration.getOrCreateType(condition, clazzOrDeclaringClass).addMethod(ConfigurationMethod.CONSTRUCTOR_NAME, signature, declaration, accessibility);
                break;
            }
        case "getProxyClass":
            {
                expectSize(args, 2);
                addDynamicProxy((List<?>) args.get(1), lazyValue(callerClass));
                break;
            }
        case "newProxyInstance":
            {
                expectSize(args, 3);
                addDynamicProxy((List<?>) args.get(1), lazyValue(callerClass));
                break;
            }
        case "newMethodHandleProxyInstance":
            {
                expectSize(args, 1);
                addDynamicProxyUnchecked((List<?>) args.get(0), Collections.singletonList("sun.invoke.WrapperInstance"), lazyValue(callerClass));
                break;
            }
        case "getEnclosingConstructor":
        case "getEnclosingMethod":
            {
                String result = (String) entry.get("result");
                addFullyQualifiedDeclaredMethod(result);
                break;
            }
        case "newInstance":
            {
                if (clazz.equals("java.lang.reflect.Array")) {
                    // reflective array instantiation
                    configuration.getOrCreateType(condition, (String) args.get(0));
                } else {
                    configuration.getOrCreateType(condition, clazz).addMethod(ConfigurationMethod.CONSTRUCTOR_NAME, "()V", ConfigurationMemberDeclaration.DECLARED, ConfigurationMemberAccessibility.ACCESSED);
                }
                break;
            }
        case "getBundleImplJDK8OrEarlier":
            {
                expectSize(args, 6);
                String baseName = (String) args.get(0);
                @SuppressWarnings("unchecked") List<String> classNames = (List<String>) args.get(4);
                @SuppressWarnings("unchecked") List<String> locales = (List<String>) args.get(5);
                resourceConfiguration.addBundle(condition, classNames, locales, baseName);
                break;
            }
        case "getBundleImplJDK11OrLater":
            {
                expectSize(args, 7);
                String baseName = (String) args.get(2);
                @SuppressWarnings("unchecked") List<String> classNames = (List<String>) args.get(5);
                @SuppressWarnings("unchecked") List<String> locales = (List<String>) args.get(6);
                resourceConfiguration.addBundle(condition, classNames, locales, baseName);
                break;
            }
        default:
            System.err.println("Unsupported reflection method: " + function);
    }
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition) ConfigurationMemberDeclaration(com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) ConfigurationMemberAccessibility(com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberAccessibility)

Example 4 with ConfigurationCondition

use of org.graalvm.nativeimage.impl.ConfigurationCondition in project graal by oracle.

the class JniProcessor method processEntry.

@Override
@SuppressWarnings("fallthrough")
void processEntry(Map<String, ?> entry) {
    ConfigurationCondition condition = ConfigurationCondition.alwaysTrue();
    boolean invalidResult = Boolean.FALSE.equals(entry.get("result"));
    if (invalidResult) {
        return;
    }
    String function = (String) entry.get("function");
    String callerClass = (String) entry.get("caller_class");
    List<?> args = (List<?>) entry.get("args");
    LazyValue<String> callerClassLazyValue = lazyValue(callerClass);
    // Special: FindClass and DefineClass take the class in question as a string argument
    if (function.equals("FindClass") || function.equals("DefineClass")) {
        String lookupName = singleElement(args);
        String internalName = (lookupName.charAt(0) != '[') ? ('L' + lookupName + ';') : lookupName;
        String forNameString = MetaUtil.internalNameToJava(internalName, true, true);
        if (!advisor.shouldIgnore(lazyValue(forNameString), callerClassLazyValue)) {
            if (function.equals("FindClass")) {
                configuration.getOrCreateType(condition, forNameString);
            } else if (!AccessAdvisor.PROXY_CLASS_NAME_PATTERN.matcher(lookupName).matches()) {
                // DefineClass
                logWarning("Unsupported JNI function DefineClass used to load class " + forNameString);
            }
        }
        return;
    }
    String clazz = (String) entry.get("class");
    if (advisor.shouldIgnore(lazyValue(clazz), callerClassLazyValue)) {
        return;
    }
    String declaringClass = (String) entry.get("declaring_class");
    String declaringClassOrClazz = (declaringClass != null) ? declaringClass : clazz;
    ConfigurationMemberDeclaration declaration = (declaringClass != null) ? ConfigurationMemberDeclaration.DECLARED : ConfigurationMemberDeclaration.PRESENT;
    TypeConfiguration config = configuration;
    switch(function) {
        case "GetStaticMethodID":
        case "GetMethodID":
            {
                expectSize(args, 2);
                String name = (String) args.get(0);
                String signature = (String) args.get(1);
                if (!advisor.shouldIgnoreJniMethodLookup(lazyValue(clazz), lazyValue(name), lazyValue(signature), callerClassLazyValue)) {
                    config.getOrCreateType(condition, declaringClassOrClazz).addMethod(name, signature, declaration);
                    if (!declaringClassOrClazz.equals(clazz)) {
                        config.getOrCreateType(condition, clazz);
                    }
                }
                break;
            }
        case "GetFieldID":
        case "GetStaticFieldID":
            {
                expectSize(args, 2);
                String name = (String) args.get(0);
                config.getOrCreateType(condition, declaringClassOrClazz).addField(name, declaration, false);
                if (!declaringClassOrClazz.equals(clazz)) {
                    config.getOrCreateType(condition, clazz);
                }
                break;
            }
        case "ThrowNew":
            {
                // exception message, ignore
                expectSize(args, 1);
                String name = ConfigurationMethod.CONSTRUCTOR_NAME;
                String signature = "(Ljava/lang/String;)V";
                if (!advisor.shouldIgnoreJniMethodLookup(lazyValue(clazz), lazyValue(name), lazyValue(signature), callerClassLazyValue)) {
                    config.getOrCreateType(condition, declaringClassOrClazz).addMethod(name, signature, declaration);
                    assert declaringClassOrClazz.equals(clazz) : "Constructor can only be accessed via declaring class";
                }
                break;
            }
        case "ToReflectedField":
            // fall through
            config = reflectionConfiguration;
        case "FromReflectedField":
            {
                expectSize(args, 1);
                String name = (String) args.get(0);
                config.getOrCreateType(condition, declaringClassOrClazz).addField(name, declaration, false);
                break;
            }
        case "ToReflectedMethod":
            // fall through
            config = reflectionConfiguration;
        case "FromReflectedMethod":
            {
                expectSize(args, 2);
                String name = (String) args.get(0);
                String signature = (String) args.get(1);
                config.getOrCreateType(condition, declaringClassOrClazz).addMethod(name, signature, declaration);
                break;
            }
        case "NewObjectArray":
            {
                expectSize(args, 0);
                String arrayQualifiedJavaName = MetaUtil.internalNameToJava(clazz, true, true);
                config.getOrCreateType(condition, arrayQualifiedJavaName);
                break;
            }
    }
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition) ConfigurationMemberDeclaration(com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration) List(java.util.List) TypeConfiguration(com.oracle.svm.configure.config.TypeConfiguration)

Example 5 with ConfigurationCondition

use of org.graalvm.nativeimage.impl.ConfigurationCondition in project graal by oracle.

the class ResourceConfigurationTest method printJson.

@Test
public void printJson() {
    ResourceConfiguration rc = new ResourceConfiguration();
    ConfigurationCondition defaultCond = ConfigurationCondition.alwaysTrue();
    rc.addResourcePattern(defaultCond, ".*/Resource.*txt$");
    rc.ignoreResourcePattern(defaultCond, ".*/Resource2.txt$");
    PipedWriter pw = new PipedWriter();
    JsonWriter jw = new JsonWriter(pw);
    try (PipedReader pr = new PipedReader()) {
        pr.connect(pw);
        Thread writerThread = new Thread(() -> {
            try (JsonWriter w = jw) {
                rc.printJson(w);
            } catch (IOException e) {
                Assert.fail(e.getMessage());
            }
        });
        List<String> addedResources = new LinkedList<>();
        List<String> ignoredResources = new LinkedList<>();
        ResourcesRegistry registry = new ResourcesRegistry() {

            @Override
            public void addResources(ConfigurationCondition condition, String pattern) {
                addedResources.add(pattern);
            }

            @Override
            public void ignoreResources(ConfigurationCondition condition, String pattern) {
                ignoredResources.add(pattern);
            }

            @Override
            public void addResourceBundles(ConfigurationCondition condition, String name) {
            }

            @Override
            public void addResourceBundles(ConfigurationCondition condition, String basename, Collection<Locale> locales) {
            }

            @Override
            public void addClassBasedResourceBundle(ConfigurationCondition condition, String basename, String className) {
            }
        };
        ResourceConfigurationParser rcp = new ResourceConfigurationParser(registry, true);
        writerThread.start();
        rcp.parseAndRegister(pr);
        writerThread.join();
        Assert.assertTrue(addedResources.contains(".*/Resource.*txt$"));
        Assert.assertTrue(ignoredResources.contains(".*/Resource2.txt$"));
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition) IOException(java.io.IOException) JsonWriter(com.oracle.svm.configure.json.JsonWriter) PipedReader(java.io.PipedReader) LinkedList(java.util.LinkedList) ResourceConfiguration(com.oracle.svm.configure.config.ResourceConfiguration) ResourcesRegistry(com.oracle.svm.core.configure.ResourcesRegistry) PipedWriter(java.io.PipedWriter) Collection(java.util.Collection) ResourceConfigurationParser(com.oracle.svm.core.configure.ResourceConfigurationParser) Test(org.junit.Test)

Aggregations

ConfigurationCondition (org.graalvm.nativeimage.impl.ConfigurationCondition)14 List (java.util.List)3 ConfigurationMemberDeclaration (com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration)2 ResourceConfiguration (com.oracle.svm.configure.config.ResourceConfiguration)2 Test (org.junit.Test)2 ConfigurationMemberAccessibility (com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberAccessibility)1 TypeConfiguration (com.oracle.svm.configure.config.TypeConfiguration)1 JsonWriter (com.oracle.svm.configure.json.JsonWriter)1 TargetClass (com.oracle.svm.core.annotate.TargetClass)1 ResourceConfigurationParser (com.oracle.svm.core.configure.ResourceConfigurationParser)1 ResourcesRegistry (com.oracle.svm.core.configure.ResourcesRegistry)1 IOException (java.io.IOException)1 PipedReader (java.io.PipedReader)1 PipedWriter (java.io.PipedWriter)1 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Locale (java.util.Locale)1