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.cli.Argument; 029 import org.crsh.cli.Command; 030 import org.crsh.cli.Required; 031 import org.crsh.cli.Usage; 032 import org.crsh.cli.Man; 033 import org.crsh.cli.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.BorderStyle; 038 import org.crsh.text.Color; 039 import org.crsh.text.Decoration; 040 import org.crsh.text.Style; 041 042 import java.util.logging.Level; 043 import java.util.logging.Logger; 044 045 @GroovyASTTransformation(phase= CompilePhase.CONVERSION) 046 public 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 CRaSHCommand.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 }