Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller @Mixin just works after recompile of running app

Within my latest grails 2.3.0 project I'm using the @Mixin annotation to mixin a helper class to keep my controller more DRY.

The mixin is just working if a made some changes within the controller to force a recompile of the controller. After the initial compile (grails run-app) the helper isn't mixed in - I get a MissingMethodException trying to access a method from the helper class.

Here is my helper witin src/groovy:

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 which uses the ProjectHelper:

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

When I cleane the project using grails clean and starting the app, 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 in ReportController (e.g. adding a single space) grails compiles 2 source files and the method withProject could be used. Accessing project/index works as expected.

What's going wrong here? Is it a bug or do I miss something?

Update

It turns out that I totally missed, that using the grails.util.Mixin gives me another exception (MissingPropertyException) due to missing access to the mixed in class properties (in my case: flash) (see JIRA this issue) which is working with groovy.lang.Mixin (after recompile).

Is there a way to manually recompile/inject/mixin the groovy.lang.Mixin at runtime or do I have to find another error handling for the else part till the issue is fixed?

Any suggestions?

like image 652
aiolos Avatar asked Sep 24 '13 20:09

aiolos


2 Answers

I had been regularly running into the same MissingMethodException when 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) resolved the issue completely.

As for not having access to the controller variables, you may be stuck waiting for GRAILS-9905 to be resolved. I should note that there are some suggested work-arounds listed on the defect discussion, though.

like image 162
jstricker Avatar answered Oct 20 '22 16:10

jstricker


The workaround that finally worked for me was to manually injecting the Mixin in the Controller constructor:

import com.example.MyMixin

class SomethingController {
  def SomethingController() {
    SomethingController.metaClass.mixin(MyMixin)
  }
}
like image 1
grammar31 Avatar answered Oct 20 '22 16:10

grammar31