-
Notifications
You must be signed in to change notification settings - Fork 133
Use Utility Class Methods as Extensions #662
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
Merged
rsmckinney
merged 38 commits into
manifold-systems:master
from
EotT123:#597_utility_class_as_extension_new
Apr 16, 2025
Merged
Use Utility Class Methods as Extensions #662
rsmckinney
merged 38 commits into
manifold-systems:master
from
EotT123:#597_utility_class_as_extension_new
Apr 16, 2025
Conversation
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 was referenced Jan 18, 2025
Contributor
Author
|
This PR depends on #656 |
Contributor
Author
|
@rsmckinney Since #656 is merged, this PR is also ready. |
…new' into #597_utility_class_as_extension_new
Contributor
Author
|
@rsmckinney What are your thoughts on this PR? |
Member
It's a nice way to bridge utility classes. But I hesitate to add it because it's also a nice way to overcrowd a type with context-specific methods. I'm going to merge it though because manifold is for adults. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR depends on #656.
Using Utility Class Methods as Extensions
Methods in utility classes are often good candidates to be used as extension methods. However, since these methods are not annotated, they cannot be used directly as extension methods.
Rather than manually converting all those methods into extensions, which can be time-consuming and error-prone, you can leverage Manifold's built-in functionality to automate this process. By specifying the utility class as a parameter to the
@ExtensionSourceannotation, you can easily incorporate its methods as extension methods for the target type.Basic Usage
Suppose you want to use methods from the
org.apache.commons.lang3.StringUtilsclass as extension methods forjava.lang.String. You can do this with the following code:With this approach, all
public,staticmethods from theStringUtilsclass that take aStringas their first parameter will be automatically available as extension methods forStringobjects.Any methods that conflict with existing methods in the
Stringclass (i.e., methods with the same signature) will be excluded by default.Overriding Existing Methods
If you want to override existing methods (i.e., make the methods from the utility class replace the methods already present on the target object), you can set the
overrideExistingMethodsattribute totrue:Granular Control Over Included and Excluded Methods
You can further control which methods are included or excluded by specifying the method signatures you want to include or exclude. This is achieved using the
typeandmethodsattributes in the@ExtensionSourceannotation.For example, to include (or exclude) only specific methods from
StringUtils, you can specify theINCLUDEorEXCLUDEtypes and provide method signatures:The
namevalue can be specified as a regular expression.There are several ways to configure how parameter types are matched:
paramTypes: Intercepts all methods with the specified name, regardless of their parameter types.paramTypes: Intercepts only methods with the same name and no parameters.paramTypes: Intercepts methods with the specified name and matching parameter types. You can useany.classfor a parameter type that matches any class type.@MethodSignature(name = "foo", paramTypes = { String.class, any.class })matches methods likefoo(String, int)as well asfoo(String, String)(and other variations for the second parameter).