001/* 002 * Copyright 2012-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.cli.compiler; 018 019import groovy.lang.GroovyClassLoader; 020import org.codehaus.groovy.ast.ClassNode; 021import org.codehaus.groovy.classgen.GeneratorContext; 022import org.codehaus.groovy.control.CompilationFailedException; 023import org.codehaus.groovy.control.CompilePhase; 024import org.codehaus.groovy.control.SourceUnit; 025import org.codehaus.groovy.control.customizers.ImportCustomizer; 026 027/** 028 * Strategy that can be used to apply some auto-configuration during the 029 * {@link CompilePhase#CONVERSION} Groovy compile phase. 030 * 031 * @author Phillip Webb 032 */ 033public abstract class CompilerAutoConfiguration { 034 035 /** 036 * Strategy method used to determine when compiler auto-configuration should be 037 * applied. Defaults to always. 038 * @param classNode the class node 039 * @return {@code true} if the compiler should be auto configured using this class. If 040 * this method returns {@code false} no other strategy methods will be called. 041 */ 042 public boolean matches(ClassNode classNode) { 043 return true; 044 } 045 046 /** 047 * Apply any dependency customizations. This method will only be called if 048 * {@link #matches} returns {@code true}. 049 * @param dependencies dependency customizer 050 * @throws CompilationFailedException if the dependencies cannot be applied 051 */ 052 public void applyDependencies(DependencyCustomizer dependencies) 053 throws CompilationFailedException { 054 } 055 056 /** 057 * Apply any import customizations. This method will only be called if 058 * {@link #matches} returns {@code true}. 059 * @param imports import customizer 060 * @throws CompilationFailedException if the imports cannot be applied 061 */ 062 public void applyImports(ImportCustomizer imports) throws CompilationFailedException { 063 } 064 065 /** 066 * Apply any customizations to the main class. This method will only be called if 067 * {@link #matches} returns {@code true}. This method is useful when a groovy file 068 * defines more than one class but customization only applies to the first class. 069 * @param loader the class loader being used during compilation 070 * @param configuration the compiler configuration 071 * @param generatorContext the current context 072 * @param source the source unit 073 * @param classNode the main class 074 * @throws CompilationFailedException if the customizations cannot be applied 075 */ 076 public void applyToMainClass(GroovyClassLoader loader, 077 GroovyCompilerConfiguration configuration, GeneratorContext generatorContext, 078 SourceUnit source, ClassNode classNode) throws CompilationFailedException { 079 } 080 081 /** 082 * Apply any additional configuration. 083 * @param loader the class loader being used during compilation 084 * @param configuration the compiler configuration 085 * @param generatorContext the current context 086 * @param source the source unit 087 * @param classNode the class 088 * @throws CompilationFailedException if the configuration cannot be applied 089 */ 090 public void apply(GroovyClassLoader loader, GroovyCompilerConfiguration configuration, 091 GeneratorContext generatorContext, SourceUnit source, ClassNode classNode) 092 throws CompilationFailedException { 093 } 094 095}