-
Notifications
You must be signed in to change notification settings - Fork 28
Integrate immutables:datatype with Plexus to remove the need for modifiable models #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ascopes
wants to merge
2
commits into
main
Choose a base branch
from
poc/immutables-plexus-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,8 @@ | |
| */ | ||
| package io.github.ascopes.protobufmavenplugin.dependencies; | ||
|
|
||
| import org.immutables.value.Value.Modifiable; | ||
| import org.immutables.datatype.Data; | ||
| import org.immutables.value.Value.Immutable; | ||
|
|
||
| /** | ||
| * Marker to exclude a specific transitive dependency. | ||
|
|
@@ -26,7 +27,8 @@ | |
| * @author Ashley Scopes | ||
| * @since 2.12.0 | ||
| */ | ||
| @Modifiable | ||
| @Data | ||
| @Immutable | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify any changes rebased into this PR do not contain |
||
| public interface MavenExclusion { | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...main/java/io/github/ascopes/protobufmavenplugin/plexus/ImmutablesDataPlexusConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| * Copyright (C) 2023 Ashley Scopes | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.github.ascopes.protobufmavenplugin.plexus; | ||
|
|
||
| import java.lang.reflect.ParameterizedType; | ||
| import java.lang.reflect.Type; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Optional; | ||
| import javax.inject.Named; | ||
| import javax.inject.Singleton; | ||
| import org.codehaus.plexus.component.configurator.ComponentConfigurationException; | ||
| import org.codehaus.plexus.component.configurator.ConfigurationListener; | ||
| import org.codehaus.plexus.component.configurator.converters.ParameterizedConfigurationConverter; | ||
| import org.codehaus.plexus.component.configurator.converters.basic.AbstractBasicConverter; | ||
| import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup; | ||
| import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; | ||
| import org.codehaus.plexus.configuration.PlexusConfiguration; | ||
| import org.immutables.datatype.Datatype; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Converter for Plexus components that can build a generated "immutables" value type, based | ||
| * on the interface it was derived from. | ||
| * | ||
| * <p>Note that all objects must be annotated with both {@link org.immutables.value.Value} | ||
| * <strong>and</strong> {@link org.immutables.datatype.Data}, otherwise deserialization will fail at | ||
| * runtime. | ||
| * | ||
| * <p>This will not store any references to the datatypes, to allow for sharing across classloaders | ||
| * safely (see GH-974 and GH-976). | ||
| * | ||
| * @author Ashley Scopes | ||
| * @since TBC | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO(ascopes): pin this to the next minor version. |
||
| */ | ||
| @Named | ||
| @Singleton | ||
| final class ImmutablesDataPlexusConverter extends AbstractBasicConverter { | ||
| @Override | ||
| public boolean canConvert(Class<?> cls) { | ||
| return datatypeFor(cls).isPresent(); | ||
| } | ||
|
|
||
| @Override | ||
| public Object fromConfiguration( | ||
| ConverterLookup lookup, | ||
| PlexusConfiguration configuration, | ||
| Class<?> type, | ||
| @Nullable Class<?> enclosingType, | ||
| @Nullable ClassLoader loader, | ||
| ExpressionEvaluator evaluator, | ||
| @Nullable ConfigurationListener listener | ||
| ) throws ComponentConfigurationException { | ||
| var datatype = datatypeFor(type) | ||
| .orElseThrow(() -> new NoSuchElementException("No datatype converter " + type.getName())); | ||
|
|
||
| var builder = datatype.builder(); | ||
| for (var child : configuration.getChildren()) { | ||
| consumeChild(builder, child, lookup, datatype, loader, evaluator, listener); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| private Optional<Datatype<Object>> datatypeFor(Class<?> cls) { | ||
| if (cls.isPrimitive() || cls.getClassLoader() == null) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| var loader = cls.getClassLoader(); | ||
| var outerClsName = cls.getPackageName() + ".Datatypes_" + cls.getSimpleName(); | ||
|
|
||
| try { | ||
| var outerCls = loader.loadClass(outerClsName); | ||
| var method = outerCls.getMethod("_" + cls.getSimpleName()); | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| var result = (Datatype<Object>) method.invoke(null); | ||
|
|
||
| return Optional.of(result); | ||
| } catch (ClassNotFoundException ex) { | ||
| return Optional.empty(); | ||
| } catch (ReflectiveOperationException ex) { | ||
| throw new IllegalStateException( | ||
| "Failed to find datatype for " + cls.getName() + ": " + ex, | ||
| ex | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private void consumeChild( | ||
| Datatype.Builder<Object> builder, | ||
| PlexusConfiguration child, | ||
| ConverterLookup lookup, | ||
| Datatype<?> datatype, | ||
| @Nullable ClassLoader loader, | ||
| ExpressionEvaluator evaluator, | ||
| @Nullable ConfigurationListener listener | ||
| ) throws ComponentConfigurationException { | ||
| try { | ||
| @SuppressWarnings("unchecked") | ||
| var feature = (Datatype.Feature<Object, Object>) datatype.feature(child.getName()); | ||
| var valueType = feature.type(); | ||
| var rawType = rawTypeOf(valueType); | ||
|
|
||
| var converter = lookup.lookupConverterForType(rawType); | ||
|
|
||
| Object value; | ||
|
|
||
| if (converter instanceof ParameterizedConfigurationConverter parameterizedConverter) { | ||
| var parameterizedType = (ParameterizedType) valueType; | ||
| value = parameterizedConverter.fromConfiguration( | ||
| lookup, | ||
| child, | ||
| rawType, | ||
| parameterizedType.getActualTypeArguments(), | ||
| rawType.getEnclosingClass(), | ||
| loader, | ||
| evaluator, | ||
| listener | ||
| ); | ||
| } else { | ||
| value = converter.fromConfiguration( | ||
| lookup, | ||
| child, | ||
| rawType, | ||
| rawType.getEnclosingClass(), | ||
| loader, | ||
| evaluator, | ||
| listener | ||
| ); | ||
| } | ||
|
|
||
| builder.set(feature, value); | ||
|
|
||
| } catch (NoSuchElementException ex) { | ||
| throw new ComponentConfigurationException( | ||
| "No attribute " + child.getName() + " exists for " + datatype.name(), | ||
| ex | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static Class<?> rawTypeOf(Type type) { | ||
| return type instanceof ParameterizedType parameterizedType | ||
| ? rawTypeOf(parameterizedType.getRawType()) | ||
| // Assumption: this cannot ever be a wildcard or union type. | ||
| : (Class<?>) type; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add checks to project structure for
@Modifiableannotations if possible so we can fail builds when they are used.