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

Extension Methods do not work #593

Closed
Benjamin-Bergman opened this issue May 17, 2024 · 3 comments
Closed

Extension Methods do not work #593

Benjamin-Bergman opened this issue May 17, 2024 · 3 comments

Comments

@Benjamin-Bergman
Copy link

Describe the bug
Extension methods fail too compile with a message like:

[ERROR] /mnt/c/Users/Work/Documents/YearUp/Java/Quarantine/Foo/src/main/java/com/example/Foo.java:[10,18] cannot find symbol
[ERROR]   symbol:   method qux()
[ERROR]   location: class com.example.Foo.Bar

Both usages of extension methods in the code below fail to compile.

To Reproduce
Steps to reproduce the behavior:
Here is my folder structure:

.
├── pom.xml
└── src
    └── main
        └── java
            └── com
                └── example
                    ├── Foo.java
                    └── extensions
                        └── com
                            └── example
                                └── Foo
                                    └── FooExt.java

Here is my pom.xml (which is exactly the example listed):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>my-ext-app</artifactId>
    <version>0.1-SNAPSHOT</version>

    <name>My Java Extension App</name>

    <properties>
        <!-- set latest manifold version here -->
        <manifold.version>2024.1.15</manifold.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>systems.manifold</groupId>
            <artifactId>manifold-ext-rt</artifactId>
            <version>${manifold.version}</version>
        </dependency>
    </dependencies>

    <!--Add the -Xplugin:Manifold argument for the javac compiler-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <encoding>UTF-8</encoding>
                    <compilerArgs>
                        <!-- Configure manifold plugin-->
                        <arg>-Xplugin:Manifold</arg>
                    </compilerArgs>
                    <!-- Add the processor path for the plugin -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>systems.manifold</groupId>
                            <artifactId>manifold-ext</artifactId>
                            <version>${manifold.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Here is Foo.java:

package com.example;

public class Foo {
    public static void main(String[] args) {
        new Foo().baz();
        new Bar().qux();
    }

    public static class Bar {

    }
}

Here is FooExt.java:

package com.example.extensions.com.example.Foo;

import com.example.*;
import manifold.ext.rt.api.*;

@Extension
public class FooExt {
    public static void baz(@This Foo foo) {
    }

    public static class Bar {
        public static void qux(@This Foo.Bar bar) {
        }
    }
}

Expected behavior
Running mvn compile should succeed without error.

Desktop (please complete the following information):

  • OS Type & Version: Windows 11 23H2 running Ubuntu 22.04.4 LTS via WSL
  • Java/JDK version: openjdk 17.0.10 2024-01-16
  • IDE version (IntelliJ IDEA or Android Studio): N/A
  • Manifold version: 2024.1.15
  • Manifold IntelliJ plugin version: N/A
  • javac --version: javac 17.0.10
  • mvn --version:
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 17.0.10, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.15.146.1-microsoft-standard-wsl2", arch: "amd64", family: "unix"

Stack trace
Here is the entire output of mvn compile --debug -Dstyle.color=never:
output.txt

@Benjamin-Bergman
Copy link
Author

This also fails with everything related to the nested class removed (The IntelliJ plugin marks only that extension method as not existing).

As a side note, it would be nice to have a full example project using manifold-ext (perhaps to add functionality to a json schema, which happens to be my use case). I'd be happy to submit a PR to add that once this is resolved.

@rsmckinney
Copy link
Member

Sorry, you cannot make class extensions on Java source files in the same module, only on compiled Java classes outside the module/project containing the extension class.

You can, however, extend types produced from type manifolds in the same module such as JSON and SQL types.

There is compiler warning about this on the offending extension class. It should probably be an error though shrug

@rsmckinney
Copy link
Member

As a side note, it would be nice to have a full example project using manifold-ext (perhaps to add functionality to a json schema, which happens to be my use case). I'd be happy to submit a PR to add that once this is resolved.

Thanks!

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

No branches or pull requests

2 participants