Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip Webb committed Oct 9, 2013
1 parent b772f7c commit af0d08c
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@
*/
public class AopAutoConfigurationTests {

public interface TestInterface {

public abstract void foo();

}

private AnnotationConfigApplicationContext context;

@Test
Expand Down Expand Up @@ -118,4 +112,10 @@ public void before() {
}
}

public interface TestInterface {

public abstract void foo();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@ public interface ArtifactCoordinatesResolver {
/**
* Gets the group id of the artifact identified by the given {@code artifactId}.
* Returns {@code null} if the artifact is unknown to the resolver.
*
* @param artifactId The id of the artifact
*
* @return The group id of the artifact
*/
String getGroupId(String artifactId);

/**
* Gets the version of the artifact identified by the given {@code artifactId}.
* Returns {@code null} if the artifact is unknown to the resolver.
*
* @param artifactId The id of the artifact
*
* @return The version of the artifact
*/
String getVersion(String artifactId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.codehaus.groovy.ast.AnnotatedNode;
Expand All @@ -36,89 +35,70 @@
*/
public abstract class AstUtils {

/**
* Determine if a {@link ClassNode} has one or more of the specified annotations on
* the class or any of its methods. N.B. the type names are not normally fully
* qualified.
*/
public static boolean hasAtLeastOneAnnotation(ClassNode node, String... annotations) {
if (hasAtLeastOneAnnotation((AnnotatedNode) node, annotations)) {
return true;
}
for (MethodNode method : node.getMethods()) {
if (hasAtLeastOneAnnotation(method, annotations)) {
return true;
}
}
return false;
}

/**
* Determine if an {@link AnnotatedNode} has one or more of the specified annotations.
* N.B. the annotation type names are not normally fully qualified.
*/
public static boolean hasAtLeastOneAnnotation(AnnotatedNode node,
String... annotations) {

for (AnnotationNode annotationNode : node.getAnnotations()) {
for (String annotation : annotations) {
if (annotation.equals(annotationNode.getClassNode().getName())) {
return true;
}
}
}

return false;

}

/**
* Determine if a {@link ClassNode} has one or more of the specified annotations on the class
* or any of its methods.
* N.B. the type names are not normally fully qualified.
*/
public static boolean hasAtLeastOneAnnotation(ClassNode node, String... annotations) {
for (AnnotationNode annotationNode : node.getAnnotations()) {
for (String annotation : annotations) {
if (annotation.equals(annotationNode.getClassNode().getName())) {
return true;
}
}
}

List<MethodNode> methods = node.getMethods();
for (MethodNode method : methods) {
for (AnnotationNode annotationNode : method.getAnnotations()) {
for (String annotation : annotations) {
if (annotation.equals(annotationNode.getClassNode().getName())) {
return true;
}
}
}
}
return false;
}

/**
* Determine if a {@link ClassNode} has one or more fields of the specified types or
* method returning one or more of the specified types. N.B. the type names are not
* normally fully qualified.
*/
public static boolean hasAtLeastOneFieldOrMethod(ClassNode node, String... types) {

Set<String> set = new HashSet<String>(Arrays.asList(types));
List<FieldNode> fields = node.getFields();
for (FieldNode field : fields) {
if (set.contains(field.getType().getName())) {
Set<String> typesSet = new HashSet<String>(Arrays.asList(types));
for (FieldNode field : node.getFields()) {
if (typesSet.contains(field.getType().getName())) {
return true;
}
}
List<MethodNode> methods = node.getMethods();
for (MethodNode method : methods) {
if (set.contains(method.getReturnType().getName())) {
for (MethodNode method : node.getMethods()) {
if (typesSet.contains(method.getReturnType().getName())) {
return true;
}
}

return false;

}

/**
* Determine if a {@link ClassNode} subclasses any of the specified types
* N.B. the type names are not normally fully qualified.
*/
public static boolean subclasses(ClassNode node, String... types) {
for (String type : types) {
if (node.getSuperClass().getName().equals(type)) {
return true;
}
}

return false;
}
/**
* Determine if a {@link ClassNode} subclasses any of the specified types N.B. the
* type names are not normally fully qualified.
*/
public static boolean subclasses(ClassNode node, String... types) {
for (String type : types) {
if (node.getSuperClass().getName().equals(type)) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
*
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
*/
public class GroovyCompiler {

Expand All @@ -73,6 +74,8 @@ public class GroovyCompiler {

private ArtifactCoordinatesResolver artifactCoordinatesResolver;

private final ASTTransformation dependencyCoordinatesTransformation = new DefaultDependencyCoordinatesAstTransformation();

/**
* Create a new {@link GroovyCompiler} instance.
* @param configuration the compiler configuration
Expand Down Expand Up @@ -168,7 +171,6 @@ private void addAstTransformations(final CompilationUnit compilationUnit) {
try {
Field field = CompilationUnit.class.getDeclaredField("phaseOperations");
field.setAccessible(true);

LinkedList[] phaseOperations = (LinkedList[]) field.get(compilationUnit);
processConversionOperations(phaseOperations[Phases.CONVERSION]);
}
Expand All @@ -186,13 +188,10 @@ private void processConversionOperations(LinkedList conversionOperations) {
if (operation.getClass().getName()
.startsWith(ASTTransformationVisitor.class.getName())) {
conversionOperations.add(i, new CompilationUnit.SourceUnitOperation() {

private final ASTTransformation transformation = new DefaultDependencyCoordinatesAstTransformation();

@Override
public void call(SourceUnit source) throws CompilationFailedException {
this.transformation.visit(new ASTNode[] { source.getAST() },
source);
GroovyCompiler.this.dependencyCoordinatesTransformation.visit(
new ASTNode[] { source.getAST() }, source);
}
});
break;
Expand Down Expand Up @@ -312,6 +311,7 @@ private void transformGrabAnnotation(AnnotationNode grabAnnotation,
.getGroupId(module));
grabAnnotation.setMember("group", groupIdExpression);
}

if (grabAnnotation.getMember("version") == null) {
ConstantExpression versionExpression = new ConstantExpression(
GroovyCompiler.this.artifactCoordinatesResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import java.util.Collections;
import java.util.Properties;

/**
* {@link ArtifactCoordinatesResolver} backed by a properties file.
*
* @author Andy Wilkinson
*/
final class PropertiesArtifactCoordinatesResolver implements ArtifactCoordinatesResolver {

private final GroovyClassLoader loader;
Expand Down Expand Up @@ -60,15 +65,15 @@ private void loadProperties() {
try {
properties.load(inputStream);
}
catch (IOException ioe) {
catch (IOException ex) {
// Swallow and continue
}
finally {
inputStream.close();
}
}
}
catch (IOException e) {
catch (IOException ex) {
// Swallow and continue
}
this.properties = properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,26 @@

/**
* {@link CompilerAutoConfiguration} for JUnit
*
*
* @author Greg Turnquist
*/
public class JUnitCompilerAutoConfiguration extends CompilerAutoConfiguration {

@Override
public boolean matches(ClassNode classNode) {
return AstUtils.hasAtLeastOneAnnotation(classNode, "Test");
}

@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("junit").add("spring-test").add("hamcrest-library");
}
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.hasAtLeastOneAnnotation(classNode, "Test");
}

@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("org.junit")
.addStaticStars("org.junit.Assert").addImports()
.addStaticStars("org.hamcrest.MatcherAssert")
.addStaticStars("org.hamcrest.Matchers");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("junit").add("spring-test").add("hamcrest-library");
}

@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("org.junit").addStaticStars("org.junit.Assert")
.addStaticStars("org.hamcrest.MatcherAssert")
.addStaticStars("org.hamcrest.Matchers");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@

/**
* {@link CompilerAutoConfiguration} for Spock test framework
*
*
* @author Greg Turnquist
*/
public class SpockCompilerAutoConfiguration extends CompilerAutoConfiguration {

@Override
public boolean matches(ClassNode classNode) {
return AstUtils.subclasses(classNode, "Specification");
}
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.subclasses(classNode, "Specification");
}

@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("spock-core");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("spock-core");
}

@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("spock.lang");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("spock.lang");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SampleAmqpSimpleApplication {

@Autowired
private AmqpTemplate amqpTemplate;

@Autowired
private ConnectionFactory connectionFactory;

Expand All @@ -49,7 +49,8 @@ public Sender mySender() {

@Bean
public SimpleMessageListenerContainer container() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(
this.connectionFactory);
Object listener = new Object() {
@SuppressWarnings("unused")
public void handleMessage(String foo) {
Expand All @@ -62,7 +63,6 @@ public void handleMessage(String foo) {
return container;
}


public static void main(String[] args) throws Exception {
SpringApplication.run(SampleAmqpSimpleApplication.class, args);
}
Expand Down
Loading

0 comments on commit af0d08c

Please sign in to comment.