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

Failed to enable groovy TypeChecked and type-checking-extension in the config file #311

Open
jhqi opened this issue Jul 24, 2024 · 1 comment

Comments

@jhqi
Copy link

jhqi commented Jul 24, 2024

I need to enable the TypeChecked when compile my project. And I also want to customize the compile error output for certain types of compilation error, so I write a Type-Checking-Extension according to the doc. I build my project with maven and GMavenPlus-plugin. Both TypeCheck and type-checking-extension do not work. The compilation successed.

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.example:springboot_groovy >--------------------
[INFO] Building springboot_groovy 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- resources:3.2.0:resources (default-resources) @ springboot_groovy ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- compiler:3.8.1:compile (default-compile) @ springboot_groovy ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\jianh\Desktop\bendi_ht\springboot_groovy\target\classes
[INFO] 
[INFO] --- gplus:3.0.2:compile (default) @ springboot_groovy ---
[INFO] Using isolated classloader, without GMavenPlus classpath.
[INFO] Using Groovy 3.0.8 to perform compile.
[INFO] Parallel parsing disabled.
[INFO] Compiled 6 files.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.922 s
[INFO] Finished at: 2024-07-24T15:19:20+08:00
[INFO] ------------------------------------------------------------------------

Here's a demo of my project:
DemoController.groovy:

// a demo groovy source code with error!

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class DemoController {

    @GetMapping("/hello")
    String sayHello() {

        // use an undeclared variable
        println robot
    }
}

config.groovy

// I hope to enable TypeChecked and customized extension

import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer
import groovy.transform.TypeChecked

def config = new CompilerConfiguration()
config.addCompilationCustomizers(
    new ASTTransformationCustomizer(
        TypeChecked,
        extensions: ['CheckExtension.groovy'])
)

CheckExtension.groovy

// when meet an undeclared var which name is 'robot', then add customized error info

unresolvedVariable { var ->
    if ('robot'==var.name) {
        //customized compile error info
        addStaticTypeError("Customize Error Info: hahaha", var)
    }
}

The structure of my project is a little strange, however, I'm not allowed to change it:

project_basedir/
--groovy/
----DemoController.groovy
--groovyCompileCheck/
----config.groovy
----CheckExtension.groovy
--src/
----main/
------java/

I use GMavenPlus plugin in the maven:

<plugin>
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <sources>

            <!-- add project groovy source -->
            <source>
                <directory>${project.basedir}/groovy</directory>
                <includes>
                    <include>**/*.groovy</include>
                </includes>
            </source>

            <!-- add type-check-extension to classpath -->
            <source>
                <directory>${project.basedir}/groovyCompileCheck</directory>
                <includes>
                    <include>**/*.groovy</include>
                </includes>
            </source>
        </sources>

        <!-- set the config.groovy for compilation -->
        <configScript>${project.basedir}/groovyCompileCheck/config.groovy</configScript>
    </configuration>
</plugin>

I wonder what's wrong with my config? Thanks!

Moreover, my real need is to customize the compile error output for calling a method with miss-matching numbers of parameters, e.g., a method definition with 2 params, but only give 1 when call this method.
In this condition, what should the customized extension be like? I've tried to read the doc, but that's really difficult for me...

@keeganwitt
Copy link
Member

Can you share the error you are getting?

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