Add ServiceLoader for AST transformations

The loading is via a marker interface SpringBootAstTransformation
to avoid clashing with other services registered as org.groovy.*

Fixes gh-1392
This commit is contained in:
Dave Syer 2014-08-18 16:15:06 +01:00
parent 3f148683a1
commit 378f6b78ce
6 changed files with 52 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.transform.ASTTransformation;
import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
import org.springframework.core.annotation.Order;
/**
* {@link ASTTransformation} to apply
@ -34,8 +35,11 @@ import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
* @author Dave Syer
* @author Andy Wilkinson
*/
@Order(DependencyAutoConfigurationTransformation.ORDER)
public class DependencyAutoConfigurationTransformation implements ASTTransformation {
public static final int ORDER = GrabMetadataTransformation.ORDER + 100;
private final GroovyClassLoader loader;
private final DependencyResolutionContext dependencyResolutionContext;

View File

@ -43,6 +43,8 @@ import org.springframework.boot.dependency.tools.Dependencies;
import org.springframework.boot.dependency.tools.ManagedDependencies;
import org.springframework.boot.dependency.tools.PropertiesFileDependencies;
import org.springframework.boot.groovy.GrabMetadata;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
/**
* {@link ASTTransformation} for processing {@link GrabMetadata @GrabMetadata}
@ -50,8 +52,11 @@ import org.springframework.boot.groovy.GrabMetadata;
* @author Andy Wilkinson
* @since 1.1.0
*/
@Order(GrabMetadataTransformation.ORDER)
public class GrabMetadataTransformation extends AnnotatedNodeASTTransformation {
public static final int ORDER = Ordered.HIGHEST_PRECEDENCE;
private static final Set<String> GRAB_METADATA_ANNOTATION_NAMES = Collections
.unmodifiableSet(new HashSet<String>(Arrays.asList(
GrabMetadata.class.getName(), GrabMetadata.class.getSimpleName())));

View File

@ -29,6 +29,7 @@ import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.transform.ASTTransformation;
import org.springframework.core.annotation.Order;
/**
* {@link ASTTransformation} to resolve beans declarations inside application source
@ -38,8 +39,11 @@ import org.codehaus.groovy.transform.ASTTransformation;
*
* @author Dave Syer
*/
@Order(GroovyBeansTransformation.ORDER)
public class GroovyBeansTransformation implements ASTTransformation {
public static final int ORDER = GrabMetadataTransformation.ORDER + 200;
@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
for (ASTNode node : nodes) {

View File

@ -47,6 +47,7 @@ import org.springframework.boot.cli.compiler.grape.AetherGrapeEngineFactory;
import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
import org.springframework.boot.cli.compiler.grape.GrapeEngineInstaller;
import org.springframework.boot.cli.util.ResourceUtils;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
/**
* Compiler for Groovy sources. Primarily a simple Facade for
@ -112,6 +113,11 @@ public class GroovyCompiler {
this.transformations.add(new ResolveDependencyCoordinatesTransformation(
resolutionContext));
}
for (ASTTransformation transformation : ServiceLoader
.load(SpringBootAstTransformation.class)) {
this.transformations.add(transformation);
}
Collections.sort(this.transformations, AnnotationAwareOrderComparator.INSTANCE);
}
/**

View File

@ -29,6 +29,7 @@ import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.transform.ASTTransformation;
import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
import org.springframework.core.annotation.Order;
/**
* {@link ASTTransformation} to resolve {@link Grab} artifact coordinates.
@ -36,9 +37,12 @@ import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
* @author Andy Wilkinson
* @author Phillip Webb
*/
@Order(ResolveDependencyCoordinatesTransformation.ORDER)
public class ResolveDependencyCoordinatesTransformation extends
AnnotatedNodeASTTransformation {
public static final int ORDER = GrabMetadataTransformation.ORDER + 300;
private static final Set<String> GRAB_ANNOTATION_NAMES = Collections
.unmodifiableSet(new HashSet<String>(Arrays.asList(Grab.class.getName(),
Grab.class.getSimpleName())));

View File

@ -0,0 +1,29 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.cli.compiler;
import org.codehaus.groovy.transform.ASTTransformation;
/**
* Marker interface for AST transformations that should be installed automatically from
* META-INF/services
*
* @author Dave Syer
*/
public interface SpringBootAstTransformation extends ASTTransformation {
}