001 /*
002 * Copyright (C) 2010 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.groovy;
021
022 import org.codehaus.groovy.ast.ASTNode;
023 import org.codehaus.groovy.ast.AnnotatedNode;
024 import org.codehaus.groovy.ast.AnnotationNode;
025 import org.codehaus.groovy.ast.ClassNode;
026 import org.codehaus.groovy.ast.FieldNode;
027 import org.codehaus.groovy.ast.MethodNode;
028 import org.codehaus.groovy.ast.Parameter;
029 import org.codehaus.groovy.ast.expr.ConstantExpression;
030 import org.codehaus.groovy.ast.expr.Expression;
031 import org.codehaus.groovy.control.CompilePhase;
032 import org.codehaus.groovy.control.SourceUnit;
033 import org.codehaus.groovy.transform.ASTTransformation;
034 import org.codehaus.groovy.transform.GroovyASTTransformation;
035 import org.crsh.cmdline.annotations.Argument;
036
037 import java.util.List;
038
039 /**
040 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
041 * @version $Revision$
042 */
043 @GroovyASTTransformation(phase= CompilePhase.INSTRUCTION_SELECTION)
044 public class ArgumentNameTransformer implements ASTTransformation {
045
046 public void visit(ASTNode[] nodes, final SourceUnit source) {
047 for (ClassNode classNode : (List<ClassNode>)source.getAST().getClasses()) {
048
049 //
050 for (FieldNode field : classNode.getFields()) {
051 String name = field.getName();
052 handle(name, field);
053 }
054
055 //
056 for (MethodNode method : classNode.getMethods()) {
057 for (Parameter parameter : method.getParameters()) {
058 String name = parameter.getName();
059 handle(name, parameter);
060 }
061 }
062 }
063 }
064
065 private void handle(String name, AnnotatedNode annotated) {
066 for (AnnotationNode ann : (List<AnnotationNode>)annotated.getAnnotations()) {
067 if (ann.getClassNode().getName().endsWith(Argument.class.getName())) {
068 Expression expr = ann.getMember("name");
069 if (expr == null) {
070 expr = new ConstantExpression(name);
071 ann.setMember("name", expr);
072 }
073 }
074 }
075 }
076 }