Commit a6a19292 authored by Phillip Webb's avatar Phillip Webb

Extract inner-classes from GroovyCompiler

Extract and refactor inner-classes from the GroovyCompiler to improve
code readability.
parent 8d186945
/*
* Copyright 2012-2013 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 groovy.lang.GroovyClassLoader;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.transform.ASTTransformation;
/**
* {@link ASTTransformation} to apply
* {@link CompilerAutoConfiguration#applyDependencies(DependencyCustomizer) dependency
* auto-configuration}.
*
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
*/
class DependencyAutoConfigurationTransformation implements ASTTransformation {
private final GroovyClassLoader loader;
private final ArtifactCoordinatesResolver coordinatesResolver;
private final Iterable<CompilerAutoConfiguration> compilerAutoConfigurations;
DependencyAutoConfigurationTransformation(GroovyClassLoader loader,
ArtifactCoordinatesResolver coordinatesResolver,
Iterable<CompilerAutoConfiguration> compilerAutoConfigurations) {
this.loader = loader;
this.coordinatesResolver = coordinatesResolver;
this.compilerAutoConfigurations = compilerAutoConfigurations;
}
@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
for (ASTNode astNode : nodes) {
if (astNode instanceof ModuleNode) {
visitModule((ModuleNode) astNode);
}
}
}
private void visitModule(ModuleNode module) {
DependencyCustomizer dependencies = new DependencyCustomizer(this.loader, module,
this.coordinatesResolver);
for (ClassNode classNode : module.getClasses()) {
for (CompilerAutoConfiguration autoConfiguration : this.compilerAutoConfigurations) {
if (autoConfiguration.matches(classNode)) {
autoConfiguration.applyDependencies(dependencies);
}
}
}
}
}
......@@ -41,17 +41,17 @@ public class DependencyCustomizer {
private final ClassNode classNode;
private final ArtifactCoordinatesResolver artifactCoordinatesResolver;
private final ArtifactCoordinatesResolver coordinatesResolver;
/**
* Create a new {@link DependencyCustomizer} instance.
* @param loader
*/
public DependencyCustomizer(GroovyClassLoader loader, ModuleNode moduleNode,
ArtifactCoordinatesResolver artifactCoordinatesResolver) {
ArtifactCoordinatesResolver coordinatesResolver) {
this.loader = loader;
this.classNode = moduleNode.getClasses().get(0);
this.artifactCoordinatesResolver = artifactCoordinatesResolver;
this.coordinatesResolver = coordinatesResolver;
}
/**
......@@ -61,7 +61,7 @@ public class DependencyCustomizer {
protected DependencyCustomizer(DependencyCustomizer parent) {
this.loader = parent.loader;
this.classNode = parent.classNode;
this.artifactCoordinatesResolver = parent.artifactCoordinatesResolver;
this.coordinatesResolver = parent.coordinatesResolver;
}
public String getVersion(String artifactId) {
......@@ -70,7 +70,7 @@ public class DependencyCustomizer {
}
public String getVersion(String artifactId, String defaultVersion) {
String version = this.artifactCoordinatesResolver.getVersion(artifactId);
String version = this.coordinatesResolver.getVersion(artifactId);
if (version == null) {
version = defaultVersion;
}
......@@ -184,8 +184,8 @@ public class DependencyCustomizer {
* @return this {@link DependencyCustomizer} for continued use
*/
public DependencyCustomizer add(String module) {
return this.add(this.artifactCoordinatesResolver.getGroupId(module), module,
this.artifactCoordinatesResolver.getVersion(module), true);
return this.add(this.coordinatesResolver.getGroupId(module), module,
this.coordinatesResolver.getVersion(module), true);
}
/**
......@@ -198,8 +198,8 @@ public class DependencyCustomizer {
* @return this {@link DependencyCustomizer} for continued use
*/
public DependencyCustomizer add(String module, boolean transitive) {
return this.add(this.artifactCoordinatesResolver.getGroupId(module), module,
this.artifactCoordinatesResolver.getVersion(module), transitive);
return this.add(this.coordinatesResolver.getGroupId(module), module,
this.coordinatesResolver.getVersion(module), transitive);
}
private DependencyCustomizer add(String group, String module, String version,
......
/*
* Copyright 2012-2013 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 groovy.lang.Grab;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ImportNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.transform.ASTTransformation;
/**
* {@link ASTTransformation} to resolve {@link Grab} artifact coordinates.
* @author Andy Wilkinson
* @author Phillip Webb
*/
class ResolveDependencyCoordinatesTransformation implements ASTTransformation {
private static final Set<String> GRAB_ANNOTATION_NAMES = Collections
.unmodifiableSet(new HashSet<String>(Arrays.asList(Grab.class.getName(),
Grab.class.getSimpleName())));
private final ArtifactCoordinatesResolver coordinatesResolver;
ResolveDependencyCoordinatesTransformation(
ArtifactCoordinatesResolver coordinatesResolver) {
this.coordinatesResolver = coordinatesResolver;
}
@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
ClassVisitor classVisitor = new ClassVisitor(source);
for (ASTNode node : nodes) {
if (node instanceof ModuleNode) {
ModuleNode module = (ModuleNode) node;
for (ImportNode importNode : module.getImports()) {
visitAnnotatedNode(importNode);
}
for (ClassNode classNode : module.getClasses()) {
visitAnnotatedNode(classNode);
classNode.visitContents(classVisitor);
}
}
}
}
private void visitAnnotatedNode(AnnotatedNode annotatedNode) {
for (AnnotationNode annotationNode : annotatedNode.getAnnotations()) {
if (GRAB_ANNOTATION_NAMES.contains(annotationNode.getClassNode().getName())) {
transformGrabAnnotation(annotationNode);
}
}
}
private void transformGrabAnnotation(AnnotationNode grabAnnotation) {
grabAnnotation.setMember("initClass", new ConstantExpression(false));
String value = getValue(grabAnnotation);
if (value != null && !isConvenienceForm(value)) {
applyGroupAndVersion(grabAnnotation, value);
}
}
private String getValue(AnnotationNode annotation) {
Expression expression = annotation.getMember("value");
if (expression instanceof ConstantExpression) {
Object value = ((ConstantExpression) expression).getValue();
return (value instanceof String ? (String) value : null);
}
return null;
}
private boolean isConvenienceForm(String value) {
return value.contains(":") || value.contains("#");
}
private void applyGroupAndVersion(AnnotationNode annotation, String module) {
if (module != null) {
setMember(annotation, "module", module);
}
else {
Expression expression = annotation.getMembers().get("module");
module = (String) ((ConstantExpression) expression).getValue();
}
if (annotation.getMember("group") == null) {
setMember(annotation, "group", this.coordinatesResolver.getGroupId(module));
}
if (annotation.getMember("version") == null) {
setMember(annotation, "version", this.coordinatesResolver.getVersion(module));
}
}
private void setMember(AnnotationNode annotation, String name, String value) {
ConstantExpression expression = new ConstantExpression(value);
annotation.setMember(name, expression);
}
private class ClassVisitor extends ClassCodeVisitorSupport {
private final SourceUnit source;
public ClassVisitor(SourceUnit source) {
this.source = source;
}
@Override
protected SourceUnit getSourceUnit() {
return this.source;
}
@Override
public void visitAnnotations(AnnotatedNode node) {
visitAnnotatedNode(node);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment