Skip to content

Commit

Permalink
better impl
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Jun 16, 2024
1 parent a5d8304 commit aa886ea
Showing 1 changed file with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GroovyASTUtils {

Expand Down Expand Up @@ -210,52 +209,58 @@ public static FieldNode getFieldFromExpression(PropertyExpression node, ASTConte
return null;
}

public static List<FieldNode> getFieldsForLeftSideOfPropertyExpression(ClassNode classNode, Expression node, ASTContext context) {
boolean statics = node instanceof ClassExpression;
return collectFields(classNode)
.filter(fieldNode -> statics == fieldNode.isStatic() && (fieldNode.getModifiers() & HIDDEN_MARKER) == 0)
.collect(Collectors.toList());
public static List<FieldNode> getFieldsForLeftSideOfPropertyExpression(ClassNode classNode, Expression expr, ASTContext context) {
boolean statics = expr instanceof ClassExpression;
return collectFields(classNode, new ArrayList<>(), node -> statics == node.isStatic() && (node.getModifiers() & HIDDEN_MARKER) == 0);
}

public static List<PropertyNode> getPropertiesForLeftSideOfPropertyExpression(ClassNode classNode, Expression node, ASTContext context) {
boolean statics = node instanceof ClassExpression;
return collectProperties(classNode)
.filter(propNode -> statics == propNode.isStatic() && (propNode.getModifiers() & HIDDEN_MARKER) == 0)
.collect(Collectors.toList());
public static List<PropertyNode> getPropertiesForLeftSideOfPropertyExpression(ClassNode classNode, Expression expr, ASTContext context) {
boolean statics = expr instanceof ClassExpression;
return collectProperties(classNode, new ArrayList<>(), node -> statics == node.isStatic() && (node.getModifiers() & HIDDEN_MARKER) == 0);
}

public static List<MethodNode> getMethodsForLeftSideOfPropertyExpression(ClassNode classNode, Expression node, ASTContext context) {
boolean statics = node instanceof ClassExpression;
return collectMethods(classNode)
.filter(methodNode -> statics == methodNode.isStatic() && (methodNode.getModifiers() & HIDDEN_MARKER) == 0)
.collect(Collectors.toList());
public static List<MethodNode> getMethodsForLeftSideOfPropertyExpression(ClassNode classNode, Expression expr, ASTContext context) {
boolean statics = expr instanceof ClassExpression;
return collectMethods(classNode, new ArrayList<>(), node -> statics == node.isStatic() && (node.getModifiers() & HIDDEN_MARKER) == 0);
}

public static Stream<MethodNode> collectMethods(ClassNode classNode) {
var stream = Stream.concat(classNode.getMethods().stream(),
Arrays.stream(classNode.getInterfaces()).flatMap(GroovyASTUtils::collectMethods));
if (classNode.getSuperClass() == null) {
return stream;
public static List<FieldNode> collectFields(ClassNode classNode, List<FieldNode> nodes, Predicate<FieldNode> test) {
for (FieldNode node : classNode.getFields()) {
if (test.test(node)) nodes.add(node);
}
return Stream.concat(stream, collectMethods(classNode.getSuperClass()));
for (ClassNode interfaze : classNode.getInterfaces()) {
collectFields(interfaze, nodes, test);
}
if (classNode.getSuperClass() != null) {
collectFields(classNode.getSuperClass(), nodes, test);
}
return nodes;
}

public static Stream<PropertyNode> collectProperties(ClassNode classNode) {
var stream = Stream.concat(classNode.getProperties().stream(),
Arrays.stream(classNode.getInterfaces()).flatMap(GroovyASTUtils::collectProperties));
if (classNode.getSuperClass() == null) {
return stream;
public static List<PropertyNode> collectProperties(ClassNode classNode, List<PropertyNode> nodes, Predicate<PropertyNode> test) {
for (PropertyNode node : classNode.getProperties()) {
if (test.test(node)) nodes.add(node);
}
for (ClassNode interfaze : classNode.getInterfaces()) {
collectProperties(interfaze, nodes, test);
}
return Stream.concat(stream, collectProperties(classNode.getSuperClass()));
if (classNode.getSuperClass() != null) {
collectProperties(classNode.getSuperClass(), nodes, test);
}
return nodes;
}

public static Stream<FieldNode> collectFields(ClassNode classNode) {
var stream = Stream.concat(classNode.getFields().stream(),
Arrays.stream(classNode.getInterfaces()).flatMap(GroovyASTUtils::collectFields));
if (classNode.getSuperClass() == null) {
return stream;
public static List<MethodNode> collectMethods(ClassNode classNode, List<MethodNode> nodes, Predicate<MethodNode> test) {
for (MethodNode node : classNode.getMethods()) {
if (test.test(node)) nodes.add(node);
}
for (ClassNode interfaze : classNode.getInterfaces()) {
collectMethods(interfaze, nodes, test);
}
if (classNode.getSuperClass() != null) {
collectMethods(classNode.getSuperClass(), nodes, test);
}
return Stream.concat(stream, collectFields(classNode.getSuperClass()));
return nodes;
}

public static ClassNode getTypeOfNode(ASTNode node, ASTContext context) {
Expand Down

0 comments on commit aa886ea

Please sign in to comment.