0
votes

In my grails project some of controllers' actions annotated with custom annotation, say CustomAnnotation. Also, there is a filter that checks controllers' actions whether they annotated by CustomAnnotation or not.

I've tried two ways to perform this check:

1) Look for annotations of bean's class methods

def artefact = grailsApplication.getArtefactByLogicalPropertyName("Controller", controllerName)
def controller = applicationContext.getBean(artefact.clazz.name)
def actionMethod = controller.class.declaredMethods.find { it.name == actionName }
def isAnnotated = actionMethod.isAnnotationPresent(CustomAnnotation) 

2) Look for annotations of artifact's clazz methods

def artefact = grailsApplication.getArtefactByLogicalPropertyName("Controller", controllerName)
def actionMethod = artefact.clazz.declaredMethods.find { it.name == actionName }
def isAnnotated = actionMethod.isAnnotationPresent(CustomAnnotation)

While the first way doesn't work for me, the second works well. Why these classes are different and what is the difference?

1
In the first one, what's the result of controller.getClass().name? - user800014
something like custom.package.CustomController$$EnhancerByCGLIB$$a131be82 - Mikhail Selivanov
Both of them worked for me in grails console (v2.2.0) with a minute change of applicationContext to grailsApplication.mainContext. Example. - dmahapatro
So the difference is that controller is a proxy in 1, but is the concrete class in 2. - user800014
@Sergio Michels: You're right, thank you for clarification. - Mikhail Selivanov

1 Answers

1
votes

Transforming the comment in answer, the difference can be clarified by printing the class of the controller:

In the first case, the bean class is a proxy:custom.package.CustomController$$EnhancerByCGLIB$$a131be82 you can notice that by the "EnhacerByCGLIB" part. In the second one the class is correct.