-
Notifications
You must be signed in to change notification settings - Fork 683
Add RepositoryMetadataAccess
interface
#3145
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a925636
Prepare issue branch.
mp911de f309cce
Add `RepositoryMetadataAccess`.
mp911de 4da64de
Update documentation.
christophstrobl faa27b7
Additional repository bootstrap logging
christophstrobl 1309157
Where possible get rid of Optional in RepositoryFragment
christophstrobl 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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.ObjectUtils; | ||
|
@@ -41,6 +42,7 @@ | |
* Fragments are immutable. | ||
* | ||
* @author Mark Paluch | ||
* @author Christoph Strobl | ||
* @since 2.0 | ||
* @see RepositoryComposition | ||
*/ | ||
|
@@ -53,7 +55,7 @@ public interface RepositoryFragment<T> { | |
* @return | ||
*/ | ||
static <T> RepositoryFragment<T> implemented(T implementation) { | ||
return new ImplementedRepositoryFragment<T>(Optional.empty(), implementation); | ||
return new ImplementedRepositoryFragment<>((Class<T>) null, implementation); | ||
} | ||
|
||
/** | ||
|
@@ -64,7 +66,7 @@ static <T> RepositoryFragment<T> implemented(T implementation) { | |
* @return | ||
*/ | ||
static <T> RepositoryFragment<T> implemented(Class<T> interfaceClass, T implementation) { | ||
return new ImplementedRepositoryFragment<>(Optional.of(interfaceClass), implementation); | ||
return new ImplementedRepositoryFragment<>(interfaceClass, implementation); | ||
} | ||
|
||
/** | ||
|
@@ -134,7 +136,7 @@ public Class<?> getSignatureContributor() { | |
|
||
@Override | ||
public RepositoryFragment<T> withImplementation(T implementation) { | ||
return new ImplementedRepositoryFragment<>(Optional.of(interfaceOrImplementation), implementation); | ||
return new ImplementedRepositoryFragment<>(interfaceOrImplementation, implementation); | ||
} | ||
|
||
@Override | ||
|
@@ -164,47 +166,57 @@ public int hashCode() { | |
|
||
class ImplementedRepositoryFragment<T> implements RepositoryFragment<T> { | ||
|
||
private final Optional<Class<T>> interfaceClass; | ||
private final @Nullable Class<T> interfaceClass; | ||
private final T implementation; | ||
private final Optional<T> optionalImplementation; | ||
|
||
/** | ||
* Creates a new {@link ImplementedRepositoryFragment} for the given interface class and implementation. | ||
* | ||
* @param interfaceClass | ||
* @param implementation | ||
* @deprecated since 3.4 - use {@link ImplementedRepositoryFragment(Class, Object)} instead. | ||
*/ | ||
@Deprecated(since = "3.4") | ||
public ImplementedRepositoryFragment(Optional<Class<T>> interfaceClass, T implementation) { | ||
this(interfaceClass.orElse(null), implementation); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ImplementedRepositoryFragment} for the given interface class and implementation. | ||
* | ||
* @param interfaceClass must not be {@literal null}. | ||
* @param implementation must not be {@literal null}. | ||
*/ | ||
public ImplementedRepositoryFragment(Optional<Class<T>> interfaceClass, T implementation) { | ||
public ImplementedRepositoryFragment(@Nullable Class<T> interfaceClass, T implementation) { | ||
|
||
Assert.notNull(interfaceClass, "Interface class must not be null"); | ||
Assert.notNull(implementation, "Implementation object must not be null"); | ||
|
||
interfaceClass.ifPresent(it -> { | ||
if(interfaceClass != null) { | ||
|
||
Assert.isTrue(ClassUtils.isAssignableValue(it, implementation), | ||
() -> String.format("Fragment implementation %s does not implement %s", | ||
ClassUtils.getQualifiedName(implementation.getClass()), ClassUtils.getQualifiedName(it))); | ||
}); | ||
Assert.isTrue(ClassUtils.isAssignableValue(interfaceClass, implementation), | ||
() -> String.format("Fragment implementation %s does not implement %s", | ||
ClassUtils.getQualifiedName(implementation.getClass()), ClassUtils.getQualifiedName(interfaceClass))); | ||
}; | ||
|
||
this.interfaceClass = interfaceClass; | ||
this.implementation = implementation; | ||
this.optionalImplementation = Optional.of(implementation); | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public Class<?> getSignatureContributor() { | ||
return interfaceClass.orElseGet(() -> { | ||
|
||
if(implementation instanceof Class type) { | ||
return type; | ||
} | ||
return (Class<T>) implementation.getClass(); | ||
}); | ||
if(interfaceClass != null) { | ||
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. Nit: Formatting |
||
return interfaceClass; | ||
} | ||
|
||
if(implementation instanceof Class<?> type) { | ||
return type; | ||
} | ||
return implementation.getClass(); | ||
} | ||
|
||
@Override | ||
public Optional<T> getImplementation() { | ||
return optionalImplementation; | ||
return Optional.of(implementation); | ||
} | ||
|
||
@Override | ||
|
@@ -216,7 +228,7 @@ public RepositoryFragment<T> withImplementation(T implementation) { | |
public String toString() { | ||
|
||
return String.format("ImplementedRepositoryFragment %s%s", | ||
interfaceClass.map(ClassUtils::getShortName).map(it -> it + ":").orElse(""), | ||
interfaceClass != null ? (ClassUtils.getShortName(interfaceClass) + ";") : "", | ||
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. Nit: A slight change in |
||
ClassUtils.getShortName(implementation.getClass())); | ||
} | ||
|
||
|
@@ -235,18 +247,13 @@ public boolean equals(Object o) { | |
return false; | ||
} | ||
|
||
if (!ObjectUtils.nullSafeEquals(implementation, that.implementation)) { | ||
return false; | ||
} | ||
|
||
return ObjectUtils.nullSafeEquals(optionalImplementation, that.optionalImplementation); | ||
return ObjectUtils.nullSafeEquals(implementation, that.implementation); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = ObjectUtils.nullSafeHashCode(interfaceClass); | ||
result = 31 * result + ObjectUtils.nullSafeHashCode(implementation); | ||
result = 31 * result + ObjectUtils.nullSafeHashCode(optionalImplementation); | ||
return result; | ||
} | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/main/java/org/springframework/data/repository/core/support/RepositoryMetadataAccess.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,34 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.repository.core.support; | ||
|
||
/** | ||
* Marker for repository fragment implementation that intend to access repository method invocation metadata. | ||
* <p> | ||
* Note that this is a marker interface in the style of {@link java.io.Serializable}, semantically applying to a | ||
* fragment implementation class. In other words, this marker applies to a particular repository composition that | ||
* enables metadata access for the repository proxy when the composition contain fragments implementing this interface. | ||
* <p> | ||
* Ideally, in a repository composition only the fragment implementation uses this interface while the fragment | ||
* interface does not. | ||
* | ||
* @author Mark Paluch | ||
* @since 3.4 | ||
* @see RepositoryMethodContext | ||
*/ | ||
public interface RepositoryMetadataAccess { | ||
|
||
} |
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.
Let's add a
forRemoval=true
so that we clean up the API with Spring Data 4.0