001 /* 002 * Copyright (C) 2012 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.lang.impl.groovy.ast; 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.cli.Argument; 036 037 import java.util.List; 038 039 @GroovyASTTransformation(phase= CompilePhase.INSTRUCTION_SELECTION) 040 public class ArgumentNameTransformer implements ASTTransformation { 041 042 public void visit(ASTNode[] nodes, final SourceUnit source) { 043 for (ClassNode classNode : (List<ClassNode>)source.getAST().getClasses()) { 044 045 // 046 for (FieldNode field : classNode.getFields()) { 047 String name = field.getName(); 048 handle(name, field); 049 } 050 051 // 052 for (MethodNode method : classNode.getMethods()) { 053 for (Parameter parameter : method.getParameters()) { 054 String name = parameter.getName(); 055 handle(name, parameter); 056 } 057 } 058 } 059 } 060 061 private void handle(String name, AnnotatedNode annotated) { 062 for (AnnotationNode ann : (List<AnnotationNode>)annotated.getAnnotations()) { 063 if (ann.getClassNode().getName().endsWith(Argument.class.getName())) { 064 Expression expr = ann.getMember("name"); 065 if (expr == null) { 066 expr = new ConstantExpression(name); 067 ann.setMember("name", expr); 068 } 069 } 070 } 071 } 072 }