Skip to content

Commit

Permalink
Introduce the unresolved configuration condition
Browse files Browse the repository at this point in the history
The UnresolvedConfigurationCondition is used only during parsing and uses strings to distinguish types. The ConditionalElement is also a parsing only concept now.

As a consequence all parsers are now generic in the condition that they use and they accept the ConditionResolver.
  • Loading branch information
vjovanov committed Jan 26, 2024
1 parent ab798e8 commit 58cc679
Show file tree
Hide file tree
Showing 53 changed files with 808 additions and 673 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -40,16 +40,16 @@
*/
package org.graalvm.nativeimage.hosted;

import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.nativeimage.impl.RuntimeResourceSupport;

import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;

/**
* This class can be used to register Java resources and ResourceBundles that should be accessible
* at run time.
Expand Down Expand Up @@ -96,7 +96,7 @@ public static void addResource(Module module, String resourcePath, byte[] resour
*/
public static void addResourceBundle(Module module, String baseBundleName, Locale[] locales) {
Objects.requireNonNull(locales);
ImageSingletons.lookup(RuntimeResourceSupport.class).addResourceBundles(ConfigurationCondition.alwaysTrue(),
RuntimeResourceSupport.singleton().addResourceBundles(ConfigurationCondition.alwaysTrue(),
withModuleName(module, baseBundleName), Arrays.asList(locales));
}

Expand All @@ -108,7 +108,7 @@ public static void addResourceBundle(Module module, String baseBundleName, Local
* @since 22.3
*/
public static void addResourceBundle(Module module, String bundleName) {
ImageSingletons.lookup(RuntimeResourceSupport.class).addResourceBundles(ConfigurationCondition.alwaysTrue(),
RuntimeResourceSupport.singleton().addResourceBundles(ConfigurationCondition.alwaysTrue(),
withModuleName(module, bundleName));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -40,7 +40,6 @@
*/
package org.graalvm.nativeimage.hosted;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
Expand Down Expand Up @@ -73,7 +72,7 @@ public final class RuntimeSerialization {
* @since 21.3
*/
public static void registerIncludingAssociatedClasses(Class<?> clazz) {
ImageSingletons.lookup(RuntimeSerializationSupport.class).registerIncludingAssociatedClasses(ConfigurationCondition.alwaysTrue(), clazz);
RuntimeSerializationSupport.singleton().registerIncludingAssociatedClasses(ConfigurationCondition.alwaysTrue(), clazz);
}

/**
Expand All @@ -82,7 +81,7 @@ public static void registerIncludingAssociatedClasses(Class<?> clazz) {
* @since 21.3
*/
public static void register(Class<?>... classes) {
ImageSingletons.lookup(RuntimeSerializationSupport.class).register(ConfigurationCondition.alwaysTrue(), classes);
RuntimeSerializationSupport.singleton().register(ConfigurationCondition.alwaysTrue(), classes);
}

/**
Expand All @@ -97,7 +96,7 @@ public static void register(Class<?>... classes) {
* @since 21.3
*/
public static void registerWithTargetConstructorClass(Class<?> clazz, Class<?> customTargetConstructorClazz) {
ImageSingletons.lookup(RuntimeSerializationSupport.class).registerWithTargetConstructorClass(ConfigurationCondition.alwaysTrue(), clazz, customTargetConstructorClazz);
RuntimeSerializationSupport.singleton().registerWithTargetConstructorClass(ConfigurationCondition.alwaysTrue(), clazz, customTargetConstructorClazz);
}

/**
Expand All @@ -108,7 +107,7 @@ public static void registerWithTargetConstructorClass(Class<?> clazz, Class<?> c
* @since 22.3
*/
public static void registerLambdaCapturingClass(Class<?> lambdaCapturingClass) {
ImageSingletons.lookup(RuntimeSerializationSupport.class).registerLambdaCapturingClass(ConfigurationCondition.alwaysTrue(), lambdaCapturingClass);
RuntimeSerializationSupport.singleton().registerLambdaCapturingClass(ConfigurationCondition.alwaysTrue(), lambdaCapturingClass);
}

/**
Expand All @@ -119,7 +118,7 @@ public static void registerLambdaCapturingClass(Class<?> lambdaCapturingClass) {
* @since 22.3
*/
public static void registerProxyClass(Class<?>... implementedInterfaces) {
ImageSingletons.lookup(RuntimeSerializationSupport.class).registerProxyClass(ConfigurationCondition.alwaysTrue(), implementedInterfaces);
RuntimeSerializationSupport.singleton().registerProxyClass(ConfigurationCondition.alwaysTrue(), implementedInterfaces);
}

private RuntimeSerialization() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -42,32 +42,42 @@

import java.util.Objects;

public final class ConfigurationCondition implements Comparable<ConfigurationCondition> {
private final String typeName;
private static final ConfigurationCondition OBJECT_REACHABLE = new ConfigurationCondition(Object.class.getTypeName());
/**
* A condition that describes if a reflectively accessed element in Native Image is visible by the
* user.
* <p>
* Currently, there is only one type of condition (<code>typeReached</code>) so this is a single
* class instead of the class hierarchy. The {@link ConfigurationCondition#type} represents the
* {@link Class<>} that needs to be reached by analysis in order for an element to be visible.
*/
public final class ConfigurationCondition {

/* Cached to save space: it is used as a marker for all non-conditional elements */
private static final ConfigurationCondition JAVA_LANG_OBJECT_REACHED = new ConfigurationCondition(Object.class);

public static ConfigurationCondition alwaysTrue() {
return OBJECT_REACHABLE;
return JAVA_LANG_OBJECT_REACHED;
}

public static boolean isAlwaysTrue(ConfigurationCondition condition) {
return ConfigurationCondition.alwaysTrue().equals(condition);
}
private final Class<?> type;

public static ConfigurationCondition create(String typeReachability) {
Objects.requireNonNull(typeReachability);
if (OBJECT_REACHABLE.typeName.equals(typeReachability)) {
return OBJECT_REACHABLE;
public static ConfigurationCondition create(Class<?> type) {
if (JAVA_LANG_OBJECT_REACHED.getType().equals(type)) {
return JAVA_LANG_OBJECT_REACHED;
}
return new ConfigurationCondition(typeReachability);
return new ConfigurationCondition(type);
}

public boolean isAlwaysTrue() {
return ConfigurationCondition.alwaysTrue().equals(this);
}

private ConfigurationCondition(String typeName) {
this.typeName = typeName;
private ConfigurationCondition(Class<?> type) {
this.type = type;
}

public String getTypeName() {
return typeName;
public Class<?> getType() {
return type;
}

@Override
Expand All @@ -79,21 +89,12 @@ public boolean equals(Object o) {
return false;
}
ConfigurationCondition condition = (ConfigurationCondition) o;
return Objects.equals(typeName, condition.typeName);
return Objects.equals(type, condition.type);
}

@Override
public int hashCode() {
return Objects.hash(typeName);
return Objects.hash(type);
}

@Override
public int compareTo(ConfigurationCondition o) {
return this.typeName.compareTo(o.typeName);
}

@Override
public String toString() {
return "[typeReachable: \"" + typeName + "\"" + "]";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -43,16 +43,24 @@
import java.util.Collection;
import java.util.Locale;

public interface RuntimeResourceSupport {
void addResources(ConfigurationCondition condition, String pattern);
import org.graalvm.nativeimage.ImageSingletons;

public interface RuntimeResourceSupport<C> {

@SuppressWarnings("unchecked")
static RuntimeResourceSupport<ConfigurationCondition> singleton() {
return ImageSingletons.lookup(RuntimeResourceSupport.class);
}

void addResources(C condition, String pattern);

void addResource(Module module, String resourcePath);

void injectResource(Module module, String resourcePath, byte[] resourceContent);

void ignoreResources(ConfigurationCondition condition, String pattern);
void ignoreResources(C condition, String pattern);

void addResourceBundles(ConfigurationCondition condition, String name);
void addResourceBundles(C condition, String name);

void addResourceBundles(ConfigurationCondition condition, String basename, Collection<Locale> locales);
void addResourceBundles(C condition, String basename, Collection<Locale> locales);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -44,25 +44,32 @@
import java.util.List;
import java.util.stream.Collectors;

public interface RuntimeSerializationSupport {
import org.graalvm.nativeimage.ImageSingletons;

void registerIncludingAssociatedClasses(ConfigurationCondition condition, Class<?> clazz);
public interface RuntimeSerializationSupport<C> {

void register(ConfigurationCondition condition, Class<?>... classes);
@SuppressWarnings("unchecked")
static RuntimeSerializationSupport<ConfigurationCondition> singleton() {
return ImageSingletons.lookup(RuntimeSerializationSupport.class);
}

void registerIncludingAssociatedClasses(C condition, Class<?> clazz);

void register(C condition, Class<?>... classes);

void registerWithTargetConstructorClass(ConfigurationCondition condition, Class<?> clazz, Class<?> customTargetConstructorClazz);
void registerWithTargetConstructorClass(C condition, Class<?> clazz, Class<?> customTargetConstructorClazz);

void registerWithTargetConstructorClass(ConfigurationCondition condition, String className, String customTargetConstructorClassName);
void registerWithTargetConstructorClass(C condition, String className, String customTargetConstructorClassName);

void registerLambdaCapturingClass(ConfigurationCondition condition, String lambdaCapturingClassName);
void registerLambdaCapturingClass(C condition, String lambdaCapturingClassName);

default void registerLambdaCapturingClass(ConfigurationCondition condition, Class<?> lambdaCapturingClass) {
default void registerLambdaCapturingClass(C condition, Class<?> lambdaCapturingClass) {
registerLambdaCapturingClass(condition, lambdaCapturingClass.getName());
}

void registerProxyClass(ConfigurationCondition condition, List<String> implementedInterfaces);
void registerProxyClass(C condition, List<String> implementedInterfaces);

default void registerProxyClass(ConfigurationCondition condition, Class<?>... implementedInterfaces) {
default void registerProxyClass(C condition, Class<?>... implementedInterfaces) {
registerProxyClass(condition, Arrays.stream(implementedInterfaces).map(Class::getName).collect(Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.nativeimage.impl;

import java.util.Objects;

/**
* This is an unresolved version of the {@link ConfigurationCondition} used only during parsing.
*/
public class UnresolvedConfigurationCondition implements Comparable<UnresolvedConfigurationCondition> {
private final String typeName;
private static final UnresolvedConfigurationCondition JAVA_LANG_OBJECT_REACHED = new UnresolvedConfigurationCondition(Object.class.getTypeName());

public static UnresolvedConfigurationCondition create(String typeName) {
Objects.requireNonNull(typeName);
if (JAVA_LANG_OBJECT_REACHED.getTypeName().equals(typeName)) {
return JAVA_LANG_OBJECT_REACHED;
}
return new UnresolvedConfigurationCondition(typeName);
}

protected UnresolvedConfigurationCondition(String typeName) {
this.typeName = typeName;
}

public static UnresolvedConfigurationCondition alwaysTrue() {
return JAVA_LANG_OBJECT_REACHED;
}

public String getTypeName() {
return typeName;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
UnresolvedConfigurationCondition condition = (UnresolvedConfigurationCondition) o;
return Objects.equals(typeName, condition.typeName);
}

@Override
public int hashCode() {
return Objects.hash(typeName);
}

@Override
public String toString() {
return "[\"typeReachable\": \"" + typeName + "\"" + "]";
}

@Override
public int compareTo(UnresolvedConfigurationCondition c) {
return this.typeName.compareTo(c.typeName);
}

public boolean isAlwaysTrue() {
return typeName.equals(JAVA_LANG_OBJECT_REACHED.getTypeName());
}
}
2 changes: 1 addition & 1 deletion substratevm/mx.substratevm/mx_substratevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def dummy_harness(test_deps, vm_launcher, vm_args):
if not preserve_image:
mx.rmtree(junit_test_dir)

_mask_str = '#'
_mask_str = '$mask$'


def _mask(arg, arg_list):
Expand Down
Loading

0 comments on commit 58cc679

Please sign in to comment.