Skip to content

Commit 378f6b7

Browse files
author
Dave Syer
committed
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
1 parent 3f14868 commit 378f6b7

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/DependencyAutoConfigurationTransformation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.codehaus.groovy.control.SourceUnit;
2525
import org.codehaus.groovy.transform.ASTTransformation;
2626
import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
27+
import org.springframework.core.annotation.Order;
2728

2829
/**
2930
* {@link ASTTransformation} to apply
@@ -34,8 +35,11 @@
3435
* @author Dave Syer
3536
* @author Andy Wilkinson
3637
*/
38+
@Order(DependencyAutoConfigurationTransformation.ORDER)
3739
public class DependencyAutoConfigurationTransformation implements ASTTransformation {
3840

41+
public static final int ORDER = GrabMetadataTransformation.ORDER + 100;
42+
3943
private final GroovyClassLoader loader;
4044

4145
private final DependencyResolutionContext dependencyResolutionContext;

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrabMetadataTransformation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,20 @@
4343
import org.springframework.boot.dependency.tools.ManagedDependencies;
4444
import org.springframework.boot.dependency.tools.PropertiesFileDependencies;
4545
import org.springframework.boot.groovy.GrabMetadata;
46+
import org.springframework.core.Ordered;
47+
import org.springframework.core.annotation.Order;
4648

4749
/**
4850
* {@link ASTTransformation} for processing {@link GrabMetadata @GrabMetadata}
4951
*
5052
* @author Andy Wilkinson
5153
* @since 1.1.0
5254
*/
55+
@Order(GrabMetadataTransformation.ORDER)
5356
public class GrabMetadataTransformation extends AnnotatedNodeASTTransformation {
5457

58+
public static final int ORDER = Ordered.HIGHEST_PRECEDENCE;
59+
5560
private static final Set<String> GRAB_METADATA_ANNOTATION_NAMES = Collections
5661
.unmodifiableSet(new HashSet<String>(Arrays.asList(
5762
GrabMetadata.class.getName(), GrabMetadata.class.getSimpleName())));

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyBeansTransformation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.codehaus.groovy.ast.stmt.BlockStatement;
3030
import org.codehaus.groovy.control.SourceUnit;
3131
import org.codehaus.groovy.transform.ASTTransformation;
32+
import org.springframework.core.annotation.Order;
3233

3334
/**
3435
* {@link ASTTransformation} to resolve beans declarations inside application source
@@ -38,8 +39,11 @@
3839
*
3940
* @author Dave Syer
4041
*/
42+
@Order(GroovyBeansTransformation.ORDER)
4143
public class GroovyBeansTransformation implements ASTTransformation {
4244

45+
public static final int ORDER = GrabMetadataTransformation.ORDER + 200;
46+
4347
@Override
4448
public void visit(ASTNode[] nodes, SourceUnit source) {
4549
for (ASTNode node : nodes) {

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
4848
import org.springframework.boot.cli.compiler.grape.GrapeEngineInstaller;
4949
import org.springframework.boot.cli.util.ResourceUtils;
50+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
5051

5152
/**
5253
* Compiler for Groovy sources. Primarily a simple Facade for
@@ -112,6 +113,11 @@ public GroovyCompiler(final GroovyCompilerConfiguration configuration) {
112113
this.transformations.add(new ResolveDependencyCoordinatesTransformation(
113114
resolutionContext));
114115
}
116+
for (ASTTransformation transformation : ServiceLoader
117+
.load(SpringBootAstTransformation.class)) {
118+
this.transformations.add(transformation);
119+
}
120+
Collections.sort(this.transformations, AnnotationAwareOrderComparator.INSTANCE);
115121
}
116122

117123
/**

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/ResolveDependencyCoordinatesTransformation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,20 @@
2929
import org.codehaus.groovy.ast.expr.Expression;
3030
import org.codehaus.groovy.transform.ASTTransformation;
3131
import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
32+
import org.springframework.core.annotation.Order;
3233

3334
/**
3435
* {@link ASTTransformation} to resolve {@link Grab} artifact coordinates.
3536
*
3637
* @author Andy Wilkinson
3738
* @author Phillip Webb
3839
*/
40+
@Order(ResolveDependencyCoordinatesTransformation.ORDER)
3941
public class ResolveDependencyCoordinatesTransformation extends
4042
AnnotatedNodeASTTransformation {
4143

44+
public static final int ORDER = GrabMetadataTransformation.ORDER + 300;
45+
4246
private static final Set<String> GRAB_ANNOTATION_NAMES = Collections
4347
.unmodifiableSet(new HashSet<String>(Arrays.asList(Grab.class.getName(),
4448
Grab.class.getSimpleName())));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.cli.compiler;
18+
19+
import org.codehaus.groovy.transform.ASTTransformation;
20+
21+
/**
22+
* Marker interface for AST transformations that should be installed automatically from
23+
* META-INF/services
24+
*
25+
* @author Dave Syer
26+
*/
27+
public interface SpringBootAstTransformation extends ASTTransformation {
28+
29+
}

0 commit comments

Comments
 (0)