Search in sources :

Example 1 with ConfigurationMemberDeclaration

use of com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration in project graal by oracle.

the class TypeMethodsWithFlagsTest method populateConfig.

void populateConfig() {
    ConfigurationType oldType = new ConfigurationType(ConfigurationCondition.alwaysTrue(), getTypeName());
    setFlags(oldType);
    previousConfig.add(oldType);
    ConfigurationType newType = new ConfigurationType(ConfigurationCondition.alwaysTrue(), getTypeName());
    for (Map.Entry<ConfigurationMethod, ConfigurationMemberDeclaration> methodEntry : methodsThatMustExist.entrySet()) {
        newType.addMethod(methodEntry.getKey().getName(), methodEntry.getKey().getInternalSignature(), methodEntry.getValue());
    }
    for (Map.Entry<ConfigurationMethod, ConfigurationMemberDeclaration> methodEntry : methodsThatMustNotExist.entrySet()) {
        newType.addMethod(methodEntry.getKey().getName(), methodEntry.getKey().getInternalSignature(), methodEntry.getValue());
    }
    currentConfig.add(newType);
}
Also used : ConfigurationType(com.oracle.svm.configure.config.ConfigurationType) ConfigurationMethod(com.oracle.svm.configure.config.ConfigurationMethod) ConfigurationMemberDeclaration(com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ConfigurationMemberDeclaration

use of com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration in project graal by oracle.

the class TypeMethodsWithFlagsTest method generateTestMethods.

void generateTestMethods() {
    Map<ConfigurationMethod, ConfigurationMemberDeclaration> targetMap;
    targetMap = getMethodsMap(ConfigurationMemberDeclaration.DECLARED);
    targetMap.put(new ConfigurationMethod("<init>", INTERNAL_SIGNATURE_ONE), ConfigurationMemberDeclaration.DECLARED);
    targetMap.put(new ConfigurationMethod("testMethodDeclaredSpecificSignature", INTERNAL_SIGNATURE_ONE), ConfigurationMemberDeclaration.DECLARED);
    targetMap.put(new ConfigurationMethod("testMethodDeclaredMatchesAllSignature", null), ConfigurationMemberDeclaration.DECLARED);
    targetMap = getMethodsMap(ConfigurationMemberDeclaration.PUBLIC);
    targetMap.put(new ConfigurationMethod("<init>", INTERNAL_SIGNATURE_TWO), ConfigurationMemberDeclaration.PUBLIC);
    targetMap.put(new ConfigurationMethod("testMethodPublicSpecificSignature", INTERNAL_SIGNATURE_ONE), ConfigurationMemberDeclaration.PUBLIC);
    targetMap.put(new ConfigurationMethod("testMethodPublicMatchesAllSignature", null), ConfigurationMemberDeclaration.PUBLIC);
}
Also used : ConfigurationMethod(com.oracle.svm.configure.config.ConfigurationMethod) ConfigurationMemberDeclaration(com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration)

Example 3 with ConfigurationMemberDeclaration

use of com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration 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 ConfigurationMemberDeclaration

use of com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration 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 ConfigurationMemberDeclaration

use of com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration in project graal by oracle.

the class TypeMethodsWithFlagsTest method doTest.

void doTest() {
    TypeConfiguration currentConfigWithoutPrevious = TypeConfiguration.copyAndSubtract(currentConfig, previousConfig);
    String name = getTypeName();
    ConfigurationType configurationType = currentConfigWithoutPrevious.get(ConfigurationCondition.alwaysTrue(), name);
    if (methodsThatMustExist.size() == 0) {
        Assert.assertNull("Generated configuration type " + name + " exists. Expected it to be cleared as it is empty.", configurationType);
    } else {
        Assert.assertNotNull("Generated configuration type " + name + " does not exist. Has the test code changed?", configurationType);
        for (Map.Entry<ConfigurationMethod, ConfigurationMemberDeclaration> methodEntry : methodsThatMustExist.entrySet()) {
            ConfigurationMemberDeclaration kind = ConfigurationType.TestBackdoor.getMethodInfoIfPresent(configurationType, methodEntry.getKey()).getDeclaration();
            Assert.assertNotNull("Method " + methodEntry.getKey() + " unexpectedly NOT found in the new configuration.", kind);
            Assert.assertEquals("Method " + methodEntry.getKey() + " contains a different kind than expected in the new configuration.", kind, methodEntry.getValue());
        }
        for (Map.Entry<ConfigurationMethod, ConfigurationMemberDeclaration> methodEntry : methodsThatMustNotExist.entrySet()) {
            ConfigurationMemberInfo kind = ConfigurationType.TestBackdoor.getMethodInfoIfPresent(configurationType, methodEntry.getKey());
            Assert.assertNull("Method " + methodEntry.getKey() + " unexpectedly found in the new configuration.", kind);
        }
    }
}
Also used : ConfigurationType(com.oracle.svm.configure.config.ConfigurationType) ConfigurationMethod(com.oracle.svm.configure.config.ConfigurationMethod) ConfigurationMemberInfo(com.oracle.svm.configure.config.ConfigurationMemberInfo) ConfigurationMemberDeclaration(com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration) TypeConfiguration(com.oracle.svm.configure.config.TypeConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ConfigurationMemberDeclaration (com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberDeclaration)5 ConfigurationMethod (com.oracle.svm.configure.config.ConfigurationMethod)3 ConfigurationType (com.oracle.svm.configure.config.ConfigurationType)2 TypeConfiguration (com.oracle.svm.configure.config.TypeConfiguration)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ConfigurationCondition (org.graalvm.nativeimage.impl.ConfigurationCondition)2 ConfigurationMemberInfo (com.oracle.svm.configure.config.ConfigurationMemberInfo)1 ConfigurationMemberAccessibility (com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberAccessibility)1 ArrayList (java.util.ArrayList)1