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.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.crsh.text.ui.Border;
038 import org.crsh.text.Color;
039 import org.crsh.text.Decoration;
040 import org.crsh.text.Style;
041 import org.slf4j.Logger;
042 import org.slf4j.LoggerFactory;
043
044 @GroovyASTTransformation(phase= CompilePhase.CONVERSION)
045 public class DefaultImportTransformer implements ASTTransformation {
046
047 /** . */
048 private static final Logger log = LoggerFactory.getLogger(DefaultImportTransformer.class);
049
050 /** . */
051 private static final Class<?>[] defaultImports = {
052 Required.class,
053 Man.class,
054 Usage.class,
055 Argument.class,
056 Option.class,
057 Command.class,
058 ScriptException.class,
059 InvocationContext.class,
060 CRaSHCommand.class,
061 };
062
063 /** . */
064 private static final Class<?>[] defaultStaticImports = {
065 Color.class,
066 Decoration.class,
067 Style.class,
068 Border.class
069 };
070
071 public void visit(ASTNode[] nodes, final SourceUnit source) {
072 log.debug("Transforming source to add default import package");
073 for (Class<?> defaultImport : defaultImports) {
074 log.debug("Adding default import for class " + defaultImport.getName());
075 if (source.getAST().getImport(defaultImport.getSimpleName()) == null) {
076 source.getAST().addImport(defaultImport.getSimpleName(), ClassHelper.make(defaultImport));
077 }
078 }
079 for (Class<?> defaultStaticImport : defaultStaticImports) {
080 log.debug("Adding default static import for class " + defaultStaticImport.getName());
081 if (!source.getAST().getStaticStarImports().containsKey(defaultStaticImport.getSimpleName())) {
082 source.getAST().addStaticStarImport(defaultStaticImport.getSimpleName(), ClassHelper.make(defaultStaticImport));
083 }
084 }
085 }
086 }