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
020package org.crsh.lang.impl.groovy.ast;
021
022import org.codehaus.groovy.ast.ASTNode;
023import org.codehaus.groovy.ast.ClassHelper;
024import org.codehaus.groovy.control.CompilePhase;
025import org.codehaus.groovy.control.SourceUnit;
026import org.codehaus.groovy.transform.ASTTransformation;
027import org.codehaus.groovy.transform.GroovyASTTransformation;
028import org.crsh.cli.Argument;
029import org.crsh.cli.Command;
030import org.crsh.cli.Required;
031import org.crsh.cli.Usage;
032import org.crsh.cli.Man;
033import org.crsh.cli.Option;
034import org.crsh.command.InvocationContext;
035import org.crsh.command.ScriptException;
036import org.crsh.groovy.GroovyCommand;
037import org.crsh.text.ui.BorderStyle;
038import org.crsh.text.Color;
039import org.crsh.text.Decoration;
040import org.crsh.text.Style;
041
042import java.util.logging.Level;
043import java.util.logging.Logger;
044
045@GroovyASTTransformation(phase= CompilePhase.CONVERSION)
046public class DefaultImportTransformer implements ASTTransformation {
047
048  /** . */
049  private static final Logger log = Logger.getLogger(DefaultImportTransformer.class.getName());
050
051  /** . */
052  private static final Class<?>[] defaultImports = {
053    Required.class,
054    Man.class,
055    Usage.class,
056    Argument.class,
057    Option.class,
058    Command.class,
059    ScriptException.class,
060    InvocationContext.class,
061    GroovyCommand.class,
062  };
063
064  /** . */
065  private static final Class<?>[] defaultStaticImports = {
066    Color.class,
067    Decoration.class,
068    Style.class,
069    BorderStyle.class
070  };
071
072  public void visit(ASTNode[] nodes, final SourceUnit source) {
073    log.log(Level.FINE, "Transforming source to add default import package");
074    for (Class<?> defaultImport : defaultImports) {
075      log.log(Level.FINE, "Adding default import for class " + defaultImport.getName());
076      if (source.getAST().getImport(defaultImport.getSimpleName()) == null) {
077        source.getAST().addImport(defaultImport.getSimpleName(), ClassHelper.make(defaultImport));
078      }
079    }
080    for (Class<?> defaultStaticImport : defaultStaticImports) {
081      log.log(Level.FINE, "Adding default static import for class " + defaultStaticImport.getName());
082      if (!source.getAST().getStaticStarImports().containsKey(defaultStaticImport.getSimpleName())) {
083        source.getAST().addStaticStarImport(defaultStaticImport.getSimpleName(), ClassHelper.make(defaultStaticImport));
084      }
085    }
086  }
087}