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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Extension
{
Class[] sources() default { };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2022 - Manifold Systems LLC
*
* 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
*
* http://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 manifold.extensions.java.nio.file.Path;

import java.nio.file.Files;

import manifold.ext.rt.api.Extension;


@Extension(sources = { Files.class })
public class MyPathExt
{
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.awt.Rectangle;
import java.io.Serializable;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -60,6 +61,11 @@ public void testMe()
new BasicIncrementalCompileDriver(true).hiBasic();
}

public void testExtensionUtilityClass(){
// isDirectory method should exist
assertFalse(Paths.get("X:\\invalid_path").isDirectory());
}

public void testSelfTypeOnExtension()
{
LinkedList<String> linkedList = new LinkedList<>();
Expand Down
27 changes: 27 additions & 0 deletions manifold-deps-parent/manifold-ext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,33 @@ to your project separately depending on its needs.
>}
>```

## Using Utility Class Methods as Extensions

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`:

```java
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.


## Generating Extension Classes

Sometimes the contents of an extension class reflect metadata from other resources. In this case rather
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package manifold.ext;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.tools.internal.jxc.gen.config.Classes;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
Expand Down Expand Up @@ -45,6 +46,7 @@
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -199,6 +201,7 @@ private String addExtensions( SrcClass extendedClass, DiagnosticListener<JavaFil
boolean methodExtensions = false;
boolean interfaceExtensions = false;
boolean annotationExtensions = false;
List<String> sourceClassFqns = new ArrayList<>();
Set<String> allExtensions = findAllExtensions();
_model.pushProcessing( _fqn );
try
Expand All @@ -223,13 +226,36 @@ private String addExtensions( SrcClass extendedClass, DiagnosticListener<JavaFil
{
addExtensionAnnotation( anno, extendedClass );
annotationExtensions = true;
if( anno.getAnnotationType().equals( Extension.class.getName() ) ) {
SrcArgument sourceClassesArg = anno.getArgument( "sources" );
if( sourceClassesArg != null ) {
String[] sourceClassFqnsSplit = sourceClassesArg.getValue().toString()
.replace("{", "").replace("}", "").split(",");
for ( String sourceClassFqn : sourceClassFqnsSplit ) {
sourceClassFqns.add( sourceClassFqn.substring( 0, sourceClassFqn.lastIndexOf(".class") ).trim() );
}
}
}
}
}
else
{
iterator.remove();
}
}
for(String sourceClassFqn : sourceClassFqns ) {
SrcClass srcClass = ClassSymbols.instance( getModule() ).makeSrcClassStub( sourceClassFqn );
for ( AbstractSrcMethod<?> method : srcClass.getMethods() ) {
if ( !method.getParameters().isEmpty() && method.getParameters().get( 0 ).getType().getFqName().equals( _fqn ) ) {
// Mark first param with @This annotation, so it is handled as an extension method
method.getParameters().get(0).addAnnotation(This.class);
AbstractSrcMethod duplicate = findMethod(method, extendedClass);
if (duplicate == null) {
addExtensionMethod(method, extendedClass, errorHandler);
}
}
}
}
if( !_existingSource.isEmpty() )
{
if( allExtensions.isEmpty() )
Expand Down Expand Up @@ -337,7 +363,7 @@ private Set<String> findAllExtensions()
// short-circuit e.g., extension producers
return Collections.emptySet();
}

Set<String> fqns = new LinkedHashSet<>();
findExtensionsOnDisk( fqns );
findExtensionsFromExtensionClassProviders( fqns );
Expand Down