Use Utility Class Methods as Extensions #662
Open
+1,020
−24
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: