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    package org.crsh.lang.impl.groovy.ast;
020    
021    import org.codehaus.groovy.ast.ASTNode;
022    import org.codehaus.groovy.ast.ClassHelper;
023    import org.codehaus.groovy.ast.ClassNode;
024    import org.codehaus.groovy.ast.FieldNode;
025    import org.codehaus.groovy.ast.MethodNode;
026    import org.codehaus.groovy.ast.Parameter;
027    import org.codehaus.groovy.ast.stmt.BlockStatement;
028    import org.codehaus.groovy.ast.stmt.ReturnStatement;
029    import org.codehaus.groovy.ast.stmt.Statement;
030    import org.codehaus.groovy.control.CompilePhase;
031    import org.codehaus.groovy.control.SourceUnit;
032    import org.codehaus.groovy.transform.ASTTransformation;
033    import org.codehaus.groovy.transform.GroovyASTTransformation;
034    import org.crsh.lang.impl.groovy.command.GroovyScriptCommand;
035    
036    import java.lang.reflect.Modifier;
037    import java.util.List;
038    
039    /**
040     * <p>A transformer that tags a CRaSH script class when it has an explicit return statement.
041     * Indeed Groovy script last statement are returned by default and this value is sometimes
042     * not desirable in CRaSH script commands unless they are explicitly returned by the script.</p>
043     *
044     * @author Julien Viet
045     */
046    @GroovyASTTransformation(phase= CompilePhase.SEMANTIC_ANALYSIS)
047    public class ScriptLastStatementTransformer implements ASTTransformation {
048    
049      /** . */
050      public static final String FIELD_NAME = "org_crsh_has_explicit_return";
051    
052      /** . */
053      private static final ClassNode GROOVY_SCRIPT_COMMAND = ClassHelper.make(GroovyScriptCommand.class);
054    
055      @Override
056      public void visit(ASTNode[] nodes, SourceUnit source) {
057        for (ClassNode classNode : source.getAST().getClasses()) {
058          if (classNode.isDerivedFrom(GROOVY_SCRIPT_COMMAND)) {
059            MethodNode run = classNode.getMethod("run", new Parameter[0]);
060            Statement code = run.getCode();
061            if (code instanceof BlockStatement) {
062              BlockStatement block = (BlockStatement)code;
063              List<Statement> statements = block.getStatements();
064              int size = statements.size();
065              if (size > 0) {
066                Statement last = statements.get(size - 1);
067                if (last instanceof ReturnStatement) {
068                  classNode.addField(new FieldNode(
069                      FIELD_NAME,
070                      Modifier.PUBLIC | Modifier.STATIC,
071                      ClassHelper.Boolean_TYPE,
072                      classNode,
073                      null));
074                }
075              }
076            }
077          }
078        }
079      }
080    }