The @Mixin controller only works after recompiling a running application

In my last Grails 2.3.0 project, I use the @Mixin annotation to mix a helper class so that my controller more DRY.

The mixin just works if the changes made to the controller make the controller recompile. After the initial compilation ( grails run-app ), the helper does not mix - I get a MissingMethodException trying to access the method from the helper class.

Here is my witin src/groovy helper:

 class ProjectHelper { def withProject(id, Closure c) { def project = Project.get(id) if (project) { c.call project } else { flash.message = 'Project not found!' render view: 'myView' return } } } 

And (one of) the controller that uses ProjectHelper :

 @Mixin(ProjectHelper) class ProjectController { def index() { withProject params.projectId, {project -> // do something with the project } } } 

When I clean up the project using grails clean and run the application, I get the following error after accessing project/index :

 MissingMethodException occurred when processing request: [GET] /<myApp>/project/ No signature of method: <myPackage>.withProject() is applicable for argument types: (java.lang.String, <myPackage>.ProjectController$_index_closure1_closure10) values: [1, <myPackage> .ProjectController$_index_closure1_closure10@40d889b5 ] 

After some changes to the ReportController (for example, with the addition of a single space), grails compiles 2 source files and the withProject method can be used. Access to project/index works as expected.

What's going on here? Is this a mistake or am I missing something?

Update

It turns out I completely missed that using grails.util.Mixin gives me another exception ( MissingPropertyException ) due to the lack of access to the mixed properties of the class (in my case: flash ) (see JIRA this issue ) that works with groovy.lang.Mixin (after recompilation).

Is there a way to manually recompile / paste / mixin groovy.lang.Mixin at runtime or do I need to find another error handling for the else part until the problem is fixed?

Any suggestions?

+6
source share
2 answers

I regularly worked in the same MissingMethodException , following a similar code reuse pattern using mixins.

In my case, changing groovy.lang.Mixin to grails.util.Mixin (or, more specifically, adding an import for grails.util.Mixin to my controller) completely resolved the issue.

As for the fact that you do not have access to the controller variables, you can be sure that the GRAILS-9905 will be enabled. I should note that there are some suggested work options listed in the discussion of defects.

+2
source

The workaround that finally worked for me was to manually enter Mixin into the controller constructor:

 import com.example.MyMixin class SomethingController { def SomethingController() { SomethingController.metaClass.mixin(MyMixin) } } 
+1
source

Source: https://habr.com/ru/post/954536/


All Articles