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.ClassHelper;
024 import org.codehaus.groovy.control.CompilePhase;
025 import org.codehaus.groovy.control.SourceUnit;
026 import org.codehaus.groovy.transform.ASTTransformation;
027 import org.codehaus.groovy.transform.GroovyASTTransformation;
028 import org.crsh.cmdline.annotations.Argument;
029 import org.crsh.cmdline.annotations.Command;
030 import org.crsh.cmdline.annotations.Required;
031 import org.crsh.cmdline.annotations.Usage;
032 import org.crsh.cmdline.annotations.Man;
033 import org.crsh.cmdline.annotations.Option;
034 import org.crsh.command.CRaSHCommand;
035 import org.crsh.command.InvocationContext;
036 import org.crsh.command.ScriptException;
037 import org.slf4j.Logger;
038 import org.slf4j.LoggerFactory;
039
040 /**
041 * This class is a Groovy transformation that adds default import to CRaSH commands.
042 *
043 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
044 * @version $Revision$
045 */
046 @GroovyASTTransformation(phase= CompilePhase.CONVERSION)
047 public class DefaultImportTransformer implements ASTTransformation {
048
049 /** . */
050 private static final Logger log = LoggerFactory.getLogger(DefaultImportTransformer.class);
051
052 /** . */
053 private static final Class<?>[] defaultImports = {
054 Required.class,
055 Man.class,
056 Usage.class,
057 Argument.class,
058 Option.class,
059 Command.class,
060 ScriptException.class,
061 InvocationContext.class,
062 CRaSHCommand.class,
063 };
064
065 public void visit(ASTNode[] nodes, final SourceUnit source) {
066 log.debug("Transforming source to add default import package");
067 for (Class<?> defaultImport : defaultImports) {
068 log.debug("Adding default import for class " + defaultImport.getName());
069 if (source.getAST().getImport(defaultImport.getSimpleName()) == null) {
070 source.getAST().addImport(defaultImport.getSimpleName(), ClassHelper.make(defaultImport));
071 }
072 }
073 }
074 }