Search in sources :

Example 6 with ConfigurationCondition

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

the class SerializationProcessor method processEntry.

@Override
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");
    if ("ObjectStreamClass.<init>".equals(function)) {
        expectSize(args, 2);
        if (advisor.shouldIgnore(LazyValueUtils.lazyValue((String) args.get(0)), LazyValueUtils.lazyValue(null))) {
            return;
        }
        serializationConfiguration.registerWithTargetConstructorClass(condition, (String) args.get(0), (String) args.get(1));
    }
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition) List(java.util.List)

Example 7 with ConfigurationCondition

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

the class Target_org_graalvm_compiler_truffle_compiler_hotspot_libgraal_TruffleToLibGraalEntryPoints method registerJNIConfiguration.

private static void registerJNIConfiguration(JNIRuntimeAccess.JNIRuntimeAccessibilitySupport registry, ImageClassLoader loader) {
    try (JNIConfigSource source = new JNIConfigSource(loader)) {
        Map<String, Class<?>> classes = new HashMap<>();
        ConfigurationCondition condition = ConfigurationCondition.alwaysTrue();
        for (String line : source.lines) {
            source.lineNo++;
            String[] tokens = line.split(" ");
            source.check(tokens.length >= 2, "Expected at least 2 tokens");
            String className = tokens[1].replace('/', '.');
            Class<?> clazz = classes.get(className);
            if (clazz == null) {
                clazz = source.findClass(className);
                registry.register(condition, clazz);
                registry.register(condition, Array.newInstance(clazz, 0).getClass());
                classes.put(className, clazz);
            }
            switch(tokens[0]) {
                case "field":
                    {
                        source.check(tokens.length == 4, "Expected 4 tokens for a field");
                        String fieldName = tokens[2];
                        try {
                            registry.register(condition, false, clazz.getDeclaredField(fieldName));
                        } catch (NoSuchFieldException e) {
                            throw source.error("Field %s.%s not found", clazz.getTypeName(), fieldName);
                        } catch (NoClassDefFoundError e) {
                            throw source.error("Could not register field %s.%s: %s", clazz.getTypeName(), fieldName, e);
                        }
                        break;
                    }
                case "method":
                    {
                        source.check(tokens.length == 4, "Expected 4 tokens for a method");
                        String methodName = tokens[2];
                        HotSpotSignature descriptor = new HotSpotSignature(runtime(), tokens[3]);
                        Class<?>[] parameters = // 
                        Arrays.asList(descriptor.toParameterTypes(null)).stream().map(JavaType::toClassName).map(// 
                        source::findClass).collect(// 
                        Collectors.toList()).toArray(new Class<?>[descriptor.getParameterCount(false)]);
                        try {
                            if ("<init>".equals(methodName)) {
                                Constructor<?> cons = clazz.getDeclaredConstructor(parameters);
                                registry.register(condition, false, cons);
                                if (Throwable.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {
                                    if (usedInTranslatedException(parameters)) {
                                        RuntimeReflection.register(clazz);
                                        RuntimeReflection.register(cons);
                                    }
                                }
                            } else {
                                registry.register(condition, false, clazz.getDeclaredMethod(methodName, parameters));
                            }
                        } catch (NoSuchMethodException e) {
                            throw source.error("Method %s.%s%s not found: %s", clazz.getTypeName(), methodName, descriptor, e);
                        } catch (NoClassDefFoundError e) {
                            throw source.error("Could not register method %s.%s%s: %s", clazz.getTypeName(), methodName, descriptor, e);
                        }
                        break;
                    }
                case "class":
                    {
                        source.check(tokens.length == 2, "Expected 2 tokens for a class");
                        break;
                    }
                default:
                    {
                        throw source.error("Unexpected token: " + tokens[0]);
                    }
            }
        }
    }
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) JavaType(jdk.vm.ci.meta.JavaType) HotSpotSignature(jdk.vm.ci.hotspot.HotSpotSignature) NodeClass(org.graalvm.compiler.graph.NodeClass) TargetClass(com.oracle.svm.core.annotate.TargetClass)

Example 8 with ConfigurationCondition

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

the class ReflectionConfigurationParser method parseClass.

private void parseClass(Map<String, Object> data) {
    checkAttributes(data, "reflection class descriptor object", Collections.singleton("name"), OPTIONAL_REFLECT_CONFIG_OBJECT_ATTRS);
    Object classObject = data.get("name");
    String className = asString(classObject, "name");
    TypeResult<ConfigurationCondition> conditionResult = delegate.resolveCondition(parseCondition(data).getTypeName());
    if (!conditionResult.isPresent()) {
        handleError("Could not resolve condition " + parseCondition(data).getTypeName() + " for reflection.", conditionResult.getException());
        return;
    }
    ConfigurationCondition condition = conditionResult.get();
    TypeResult<T> result = delegate.resolveType(condition, className);
    if (!result.isPresent()) {
        handleError("Could not resolve " + className + " for reflection configuration.", result.getException());
        return;
    }
    T clazz = result.get();
    delegate.registerType(clazz);
    for (Map.Entry<String, Object> entry : data.entrySet()) {
        String name = entry.getKey();
        Object value = entry.getValue();
        try {
            switch(name) {
                case "allDeclaredConstructors":
                    if (asBoolean(value, "allDeclaredConstructors")) {
                        delegate.registerDeclaredConstructors(false, clazz);
                    }
                    break;
                case "allPublicConstructors":
                    if (asBoolean(value, "allPublicConstructors")) {
                        delegate.registerPublicConstructors(false, clazz);
                    }
                    break;
                case "allDeclaredMethods":
                    if (asBoolean(value, "allDeclaredMethods")) {
                        delegate.registerDeclaredMethods(false, clazz);
                    }
                    break;
                case "allPublicMethods":
                    if (asBoolean(value, "allPublicMethods")) {
                        delegate.registerPublicMethods(false, clazz);
                    }
                    break;
                case "allDeclaredFields":
                    if (asBoolean(value, "allDeclaredFields")) {
                        delegate.registerDeclaredFields(clazz);
                    }
                    break;
                case "allPublicFields":
                    if (asBoolean(value, "allPublicFields")) {
                        delegate.registerPublicFields(clazz);
                    }
                    break;
                case "allDeclaredClasses":
                    if (asBoolean(value, "allDeclaredClasses")) {
                        delegate.registerDeclaredClasses(clazz);
                    }
                    break;
                case "allPermittedSubclasses":
                    if (asBoolean(value, "allPermittedSubclasses")) {
                        delegate.registerPermittedSubclasses(clazz);
                    }
                    break;
                case "allPublicClasses":
                    if (asBoolean(value, "allPublicClasses")) {
                        delegate.registerPublicClasses(clazz);
                    }
                    break;
                case "queryAllDeclaredConstructors":
                    if (asBoolean(value, "queryAllDeclaredConstructors")) {
                        delegate.registerDeclaredConstructors(true, clazz);
                    }
                    break;
                case "queryAllPublicConstructors":
                    if (asBoolean(value, "queryAllPublicConstructors")) {
                        delegate.registerPublicConstructors(true, clazz);
                    }
                    break;
                case "queryAllDeclaredMethods":
                    if (asBoolean(value, "queryAllDeclaredMethods")) {
                        delegate.registerDeclaredMethods(true, clazz);
                    }
                    break;
                case "queryAllPublicMethods":
                    if (asBoolean(value, "queryAllPublicMethods")) {
                        delegate.registerPublicMethods(true, clazz);
                    }
                    break;
                case "methods":
                    parseMethods(false, asList(value, "Attribute 'methods' must be an array of method descriptors"), clazz);
                    break;
                case "queriedMethods":
                    parseMethods(true, asList(value, "Attribute 'queriedMethods' must be an array of method descriptors"), clazz);
                    break;
                case "fields":
                    parseFields(asList(value, "Attribute 'fields' must be an array of field descriptors"), clazz);
                    break;
            }
        } catch (LinkageError e) {
            handleError("Could not register " + delegate.getTypeName(clazz) + ": " + name + " for reflection.", e);
        }
    }
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition) Map(java.util.Map)

Example 9 with ConfigurationCondition

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

the class ResourceConfigurationParser method parseStringEntry.

private void parseStringEntry(Object data, String valueKey, BiConsumer<ConfigurationCondition, String> resourceRegistry, String expectedType, String parentType) {
    Map<String, Object> resource = asMap(data, "Elements of " + parentType + " must be a " + expectedType);
    checkAttributes(resource, "resource and resource bundle descriptor object", Collections.singletonList(valueKey), Collections.singletonList(CONDITIONAL_KEY));
    ConfigurationCondition condition = parseCondition(resource);
    Object valueObject = resource.get(valueKey);
    String value = asString(valueObject, valueKey);
    resourceRegistry.accept(condition, value);
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition)

Example 10 with ConfigurationCondition

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

the class SerializationConfigurationParser method parseSerializationDescriptorObject.

private void parseSerializationDescriptorObject(Map<String, Object> data) {
    checkAttributes(data, "serialization descriptor object", Collections.singleton(NAME_KEY), Arrays.asList(CUSTOM_TARGET_CONSTRUCTOR_CLASS_KEY, CONDITIONAL_KEY));
    ConfigurationCondition unresolvedCondition = parseCondition(data);
    String targetSerializationClass = asString(data.get(NAME_KEY));
    Object optionalCustomCtorValue = data.get(CUSTOM_TARGET_CONSTRUCTOR_CLASS_KEY);
    String customTargetConstructorClass = optionalCustomCtorValue != null ? asString(optionalCustomCtorValue) : null;
    serializationSupport.registerWithTargetConstructorClass(unresolvedCondition, targetSerializationClass, customTargetConstructorClass);
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition)

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