Skip to content
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

WIP: Use methods of utility class as extensions #653

Closed

Conversation

EotT123
Copy link
Contributor

@EotT123 EotT123 commented Dec 31, 2024

Copied from the documentation:


In some cases, a utility class with methods for a specific object already exists. However, since these methods are not annotated, you can't directly use them as extensions.

Instead of 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 adding the utility class as a parameter to the @Extension annotation, you can easily incorporate its methods as extension methods.

For example, to use methods from java.nio.file.Files as extension methods for java.nio.file.Path:

package extensions.java.nio.file.Path;

import java.nio.file.Files;
import manifold.ext.rt.api.Extension;

@Extension(sources = { Files.class })
public class MyPathExt {
    // Additional extension methods can be added here
}

With this approach, all public, static methods from the Files class that take a Path as their first parameter
will be automatically available as extension methods for Path objects.

@EotT123 EotT123 changed the title Use methods of utility class as extensions WIP: Use methods of utility class as extensions Jan 17, 2025
@EotT123
Copy link
Contributor Author

EotT123 commented Jan 17, 2025

I don't think it's a good idea to add the source to the @Extension annotation. The @Extension can also be used on method level, where it doesn't make sense to add the source parameter.
Moreover, I want to add some extra configuration parameters. I'm working on (and already have a working implementation) using intercepting extension methods with this PR. Currently, this has the following syntax:

Most simple configuration:

package manifold.extensions.java.lang.String;

@Extension
@ExtensionSource( source = StringUtils.class )
public class MyStringExt { 
  // possibly some 'real' extension methods
}

This adds all methods from the StringUtils class which could be unannotated extension methods. However, when a duplicated method is found, it is not added.

To override duplicated methods:

@ExtensionSource( source = StringUtils.class, overrideExistingMethods = true )

If you don't want to add/override all methods from the StringUtils class, but only specific ones:

@ExtensionSource( 
    source = StringUtils.class, 
    overrideExistingMethods = true, 
    type = ExtensionMethodType.INCLUDE, // <-- Include specified methods
    methods = {
        @MethodSignature( name = "substring", paramTypes = { String.class, int.class } )
        // others
    } )

If you want to add/override all methods from the StringUtils class, except some specific ones:

@ExtensionSource( 
    source = StringUtils.class, 
    overrideExistingMethods = true, 
    type = ExtensionMethodType.EXCLUDE, // <-- Exclude specified methods
    methods = {
        @MethodSignature( name = "substring", paramTypes = { String.class, int.class } )
        // others
    } )

@rsmckinney Your thoughts on this?

@EotT123
Copy link
Contributor Author

EotT123 commented Jan 18, 2025

Reworked this in a new PR: #662

@EotT123 EotT123 closed this Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant