-
Notifications
You must be signed in to change notification settings - Fork 125
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
Use Utility Class Methods as Extensions #662
Open
EotT123
wants to merge
28
commits into
manifold-systems:master
Choose a base branch
from
EotT123:#597_utility_class_as_extension_new
base: master
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.
Open
Use Utility Class Methods as Extensions #662
EotT123
wants to merge
28
commits into
manifold-systems:master
from
EotT123:#597_utility_class_as_extension_new
Conversation
This file contains 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
This PR depends on #656 |
@rsmckinney Since #656 is merged, this PR is also ready. |
…new' into #597_utility_class_as_extension_new
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
@ExtensionSource
annotation, 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.StringUtils
class as extension methods forjava.lang.String
. You can do this with the following code:With this approach, all
public
,static
methods from theStringUtils
class that take aString
as their first parameter will be automatically available as extension methods forString
objects.Any methods that conflict with existing methods in the
String
class (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
overrideExistingMethods
attribute 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
type
andmethods
attributes in the@ExtensionSource
annotation.For example, to include (or exclude) only specific methods from
StringUtils
, you can specify theINCLUDE
orEXCLUDE
types and provide method signatures:The
name
value 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.class
for 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).