|
42 | 42 | import java.util.Collections; |
43 | 43 | import java.util.Comparator; |
44 | 44 | import java.util.List; |
| 45 | +import java.util.function.Predicate; |
45 | 46 | import java.util.stream.Collectors; |
46 | 47 |
|
47 | 48 | public class GroovyASTUtils { |
@@ -208,25 +209,58 @@ public static FieldNode getFieldFromExpression(PropertyExpression node, ASTConte |
208 | 209 | return null; |
209 | 210 | } |
210 | 211 |
|
211 | | - public static List<FieldNode> getFieldsForLeftSideOfPropertyExpression(ClassNode classNode, Expression node, ASTContext context) { |
212 | | - boolean statics = node instanceof ClassExpression; |
213 | | - return classNode.getFields().stream() |
214 | | - .filter(fieldNode -> statics == fieldNode.isStatic() && (fieldNode.getModifiers() & HIDDEN_MARKER) == 0) |
215 | | - .collect(Collectors.toList()); |
| 212 | + public static List<FieldNode> getFieldsForLeftSideOfPropertyExpression(ClassNode classNode, Expression expr, ASTContext context) { |
| 213 | + boolean statics = expr instanceof ClassExpression; |
| 214 | + return collectFields(classNode, new ArrayList<>(), node -> statics == node.isStatic() && (node.getModifiers() & HIDDEN_MARKER) == 0); |
216 | 215 | } |
217 | 216 |
|
218 | | - public static List<PropertyNode> getPropertiesForLeftSideOfPropertyExpression(ClassNode classNode, Expression node, ASTContext context) { |
219 | | - boolean statics = node instanceof ClassExpression; |
220 | | - return classNode.getProperties().stream() |
221 | | - .filter(propNode -> statics == propNode.isStatic() && (propNode.getModifiers() & HIDDEN_MARKER) == 0) |
222 | | - .collect(Collectors.toList()); |
| 217 | + public static List<PropertyNode> getPropertiesForLeftSideOfPropertyExpression(ClassNode classNode, Expression expr, ASTContext context) { |
| 218 | + boolean statics = expr instanceof ClassExpression; |
| 219 | + return collectProperties(classNode, new ArrayList<>(), node -> statics == node.isStatic() && (node.getModifiers() & HIDDEN_MARKER) == 0); |
223 | 220 | } |
224 | 221 |
|
225 | | - public static List<MethodNode> getMethodsForLeftSideOfPropertyExpression(ClassNode classNode, Expression node, ASTContext context) { |
226 | | - boolean statics = node instanceof ClassExpression; |
227 | | - return classNode.getMethods().stream() |
228 | | - .filter(methodNode -> statics == methodNode.isStatic() && (methodNode.getModifiers() & HIDDEN_MARKER) == 0) |
229 | | - .collect(Collectors.toList()); |
| 222 | + public static List<MethodNode> getMethodsForLeftSideOfPropertyExpression(ClassNode classNode, Expression expr, ASTContext context) { |
| 223 | + boolean statics = expr instanceof ClassExpression; |
| 224 | + return collectMethods(classNode, new ArrayList<>(), node -> statics == node.isStatic() && (node.getModifiers() & HIDDEN_MARKER) == 0); |
| 225 | + } |
| 226 | + |
| 227 | + public static List<FieldNode> collectFields(ClassNode classNode, List<FieldNode> nodes, Predicate<FieldNode> test) { |
| 228 | + for (FieldNode node : classNode.getFields()) { |
| 229 | + if (test.test(node)) nodes.add(node); |
| 230 | + } |
| 231 | + for (ClassNode interfaze : classNode.getInterfaces()) { |
| 232 | + collectFields(interfaze, nodes, test); |
| 233 | + } |
| 234 | + if (classNode.getSuperClass() != null) { |
| 235 | + collectFields(classNode.getSuperClass(), nodes, test); |
| 236 | + } |
| 237 | + return nodes; |
| 238 | + } |
| 239 | + |
| 240 | + public static List<PropertyNode> collectProperties(ClassNode classNode, List<PropertyNode> nodes, Predicate<PropertyNode> test) { |
| 241 | + for (PropertyNode node : classNode.getProperties()) { |
| 242 | + if (test.test(node)) nodes.add(node); |
| 243 | + } |
| 244 | + for (ClassNode interfaze : classNode.getInterfaces()) { |
| 245 | + collectProperties(interfaze, nodes, test); |
| 246 | + } |
| 247 | + if (classNode.getSuperClass() != null) { |
| 248 | + collectProperties(classNode.getSuperClass(), nodes, test); |
| 249 | + } |
| 250 | + return nodes; |
| 251 | + } |
| 252 | + |
| 253 | + public static List<MethodNode> collectMethods(ClassNode classNode, List<MethodNode> nodes, Predicate<MethodNode> test) { |
| 254 | + for (MethodNode node : classNode.getMethods()) { |
| 255 | + if (test.test(node)) nodes.add(node); |
| 256 | + } |
| 257 | + for (ClassNode interfaze : classNode.getInterfaces()) { |
| 258 | + collectMethods(interfaze, nodes, test); |
| 259 | + } |
| 260 | + if (classNode.getSuperClass() != null) { |
| 261 | + collectMethods(classNode.getSuperClass(), nodes, test); |
| 262 | + } |
| 263 | + return nodes; |
230 | 264 | } |
231 | 265 |
|
232 | 266 | public static ClassNode getTypeOfNode(ASTNode node, ASTContext context) { |
|
0 commit comments