Home > front end >  Can't use my Manifold extension method in Java gradle project
Can't use my Manifold extension method in Java gradle project

Time:10-11

I am working on an Java 11 project,

This is my build.gradle:

plugins {
    id 'java'
}
//

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

repositories {
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
    implementation 'org.projectlombok:lombok:1.18.18'
    implementation "io.vavr:vavr:0.10.3"
    compileOnly 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
    implementation 'systems.manifold:manifold-science:2021.1.25-SNAPSHOT'

    testCompileOnly 'org.projectlombok:lombok:1.18.20'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
    testAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess'
    testImplementation 'org.junit.jupiter:junit-jupiter-engine'
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

My Manifold's extension method:

import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;

@Extension
public class MyStringEx {

    public static void echo(@This String thiz) {
        System.out.println(thiz);
    }
}

It successfully compiled. But now, I want to add a method to the java.lang.String class through the Manifold extension annotation, I have tried this:

public class Main {
    public static void main(String[] args) {
        "hello".echo(); // Add your own methods to String!
    }
}

But I get error:

java: cannot find symbol
  symbol:   method echo()
  location: class java.lang.String

CodePudding user response:

See the Gradle Setup docs for manifold-science. Your dependencies should be:

implementation 'systems.manifold:manifold-science:2021.1.25'
annotationProcessor 'systems.manifold:manifold-ext:2021.1.25'

also you need to add the compiler argument for Manifold:

if (JavaVersion.current() != JavaVersion.VERSION_1_8 &&
    sourceSets.main.allJava.files.any {it.name == "module-info.java"}) {
    tasks.withType(JavaCompile) {
        // if you DO define a module-info.java file:
        options.compilerArgs  = ['-Xplugin:Manifold', '--module-path', it.classpath.asPath]
    }
} else {
    tasks.withType(JavaCompile) {
        // If you DO NOT define a module-info.java file:
        options.compilerArgs  = ['-Xplugin:Manifold']
    }
}

Also, the package of your extension class must follow the convention documented here. Basically the qualified name of the class tack on to the 'extensions' package. If you're using Java modules, the package should be qualified with the name of the module. For example,

package mymodule.extensions.java.lang.String;

CodePudding user response:

Firstly, you can try the answer mentioned above. But if you're using IntelliJ IDEA, then an easier solution might be:

Ctrl Alt S to open IDE Settings -> Plugins -> Search for mainfold -> Install it -> Create a new Extension class -> Type the class you wanna extend -> Create and do your work in it.

  • Related