|
|
|
@ -18,7 +18,6 @@ package org.springframework.boot.cli.compiler; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
|
import org.codehaus.groovy.ast.AnnotatedNode; |
|
|
|
import org.codehaus.groovy.ast.AnnotatedNode; |
|
|
|
@ -36,13 +35,29 @@ import org.codehaus.groovy.ast.MethodNode; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public abstract class AstUtils { |
|
|
|
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. |
|
|
|
* Determine if an {@link AnnotatedNode} has one or more of the specified annotations. |
|
|
|
* N.B. the annotation type names are not normally fully qualified. |
|
|
|
* N.B. the annotation type names are not normally fully qualified. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean hasAtLeastOneAnnotation(AnnotatedNode node, |
|
|
|
public static boolean hasAtLeastOneAnnotation(AnnotatedNode node, |
|
|
|
String... annotations) { |
|
|
|
String... annotations) { |
|
|
|
|
|
|
|
|
|
|
|
for (AnnotationNode annotationNode : node.getAnnotations()) { |
|
|
|
for (AnnotationNode annotationNode : node.getAnnotations()) { |
|
|
|
for (String annotation : annotations) { |
|
|
|
for (String annotation : annotations) { |
|
|
|
if (annotation.equals(annotationNode.getClassNode().getName())) { |
|
|
|
if (annotation.equals(annotationNode.getClassNode().getName())) { |
|
|
|
@ -50,75 +65,40 @@ public abstract class AstUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
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 |
|
|
|
* 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 |
|
|
|
* method returning one or more of the specified types. N.B. the type names are not |
|
|
|
* normally fully qualified. |
|
|
|
* normally fully qualified. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean hasAtLeastOneFieldOrMethod(ClassNode node, String... types) { |
|
|
|
public static boolean hasAtLeastOneFieldOrMethod(ClassNode node, String... types) { |
|
|
|
|
|
|
|
Set<String> typesSet = new HashSet<String>(Arrays.asList(types)); |
|
|
|
Set<String> set = new HashSet<String>(Arrays.asList(types)); |
|
|
|
for (FieldNode field : node.getFields()) { |
|
|
|
List<FieldNode> fields = node.getFields(); |
|
|
|
if (typesSet.contains(field.getType().getName())) { |
|
|
|
for (FieldNode field : fields) { |
|
|
|
|
|
|
|
if (set.contains(field.getType().getName())) { |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
List<MethodNode> methods = node.getMethods(); |
|
|
|
for (MethodNode method : node.getMethods()) { |
|
|
|
for (MethodNode method : methods) { |
|
|
|
if (typesSet.contains(method.getReturnType().getName())) { |
|
|
|
if (set.contains(method.getReturnType().getName())) { |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Determine if a {@link ClassNode} subclasses any of the specified types |
|
|
|
* Determine if a {@link ClassNode} subclasses any of the specified types N.B. the |
|
|
|
* N.B. the type names are not normally fully qualified. |
|
|
|
* type names are not normally fully qualified. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean subclasses(ClassNode node, String... types) { |
|
|
|
public static boolean subclasses(ClassNode node, String... types) { |
|
|
|
for (String type : types) { |
|
|
|
for (String type : types) { |
|
|
|
if (node.getSuperClass().getName().equals(type)) { |
|
|
|
if (node.getSuperClass().getName().equals(type)) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|