Skip to content

Commit 13e3691

Browse files
committed
Make code more readable
1 parent f82cd61 commit 13e3691

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

grails-core/src/main/groovy/org/grails/compiler/injection/GrailsASTUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,16 @@ public static boolean hasAnyAnnotations(final ClassNode classNode, final Class<?
968968
return false;
969969
}
970970

971+
public static boolean removeAnnotation(final MethodNode methodNode, final Class<? extends Annotation> annotationClass) {
972+
List<AnnotationNode> annotations = methodNode.getAnnotations(new ClassNode(annotationClass));
973+
if (annotations.size() > 0) {
974+
methodNode.getAnnotations().removeAll(annotations);
975+
return true;
976+
} else {
977+
return false;
978+
}
979+
}
980+
971981
public static void addMethodIfNotPresent(ClassNode controllerClassNode, MethodNode methodNode) {
972982
MethodNode existing = controllerClassNode.getMethod(methodNode.getName(), methodNode.getParameters());
973983
if (existing == null) {
@@ -1488,4 +1498,8 @@ public static URL getSourceUrl(SourceUnit source) {
14881498
public static URL getSourceUrl(ClassNode classNode) {
14891499
return getSourceUrl(classNode.getModule().getContext());
14901500
}
1501+
1502+
public static boolean hasParameters(MethodNode methodNode) {
1503+
return methodNode.getParameters().length > 0;
1504+
}
14911505
}

grails-plugin-controllers/src/main/groovy/org/grails/compiler/web/ControllerActionTransformer.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import static org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression;
2121
import static org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression;
2222
import static org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression;
23+
import static org.grails.compiler.injection.GrailsASTUtils.hasAnnotation;
24+
import static org.grails.compiler.injection.GrailsASTUtils.hasParameters;
25+
import static org.grails.compiler.injection.GrailsASTUtils.removeAnnotation;
2326
import grails.artefact.Artefact;
2427
import grails.artefact.controller.support.AllowedMethodsHelper;
2528
import grails.compiler.DelegatingMethod;
@@ -37,6 +40,7 @@
3740
import groovy.lang.Closure;
3841

3942
import java.io.File;
43+
import java.lang.annotation.Annotation;
4044
import java.lang.reflect.Method;
4145
import java.lang.reflect.Modifier;
4246
import java.net.URL;
@@ -233,6 +237,10 @@ private boolean isExceptionHandlingMethod(MethodNode methodNode) {
233237
return isExceptionHandler;
234238
}
235239

240+
private boolean isTraitMethod(MethodNode methodNode) {
241+
return GrailsASTUtils.hasAnnotation(methodNode, Traits.TraitBridge.class);
242+
}
243+
236244
private void processMethods(ClassNode classNode, SourceUnit source,
237245
GeneratorContext context) {
238246

@@ -295,14 +303,10 @@ public Object call(Object object) {
295303
*/
296304
protected boolean methodShouldBeConfiguredAsControllerAction(final MethodNode method) {
297305
int minLineNumber = 0;
298-
// Methods inherited from traits marked with @Action that take a parameter:
299-
// Remove the @Action annotation so they will be processed to create the no-arg method
300-
if (method.getAnnotations(ClassHelper.make(Traits.TraitBridge.class)).size() > 0) {
301-
List<AnnotationNode> annotations = method.getAnnotations(ACTION_ANNOTATION_NODE.getClassNode());
302-
if (annotations.size() > 0 && method.getParameters().length > 0) {
303-
method.getAnnotations().removeAll(annotations);
304-
--minLineNumber;
305-
}
306+
if (isTraitMethod(method) && hasAnnotation(method, Action.class) && hasParameters(method)) {
307+
removeAnnotation(method, Action.class);
308+
//Trait methods have a line number of -1
309+
--minLineNumber;
306310
}
307311
return !method.isStatic() &&
308312
method.isPublic() &&

0 commit comments

Comments
 (0)