GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [core] NodeStream API by oowekyala · Pull Request #1622 · pmd/pmd

Conversation

@oowekyala

@oowekyala oowekyala commented Jan 27, 2019

Copy link
Copy Markdown
Member

Context

With the removal of Jaxen for 7.0.0, the method Node.findChildrenWithXPath will be broken (it throws a checked JaxenException). It's usually only used when the path at which the node looked for is too deep to be roamed in a type-safe, null-safe manner without making the code size explode, e.g. in ForLoopCanBeForeach:

pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java

Lines 171 to 178 in 8a208b7

return "./StatementExpressionList[count(*)=1]"
+ "/StatementExpression"
+ "/*[self::PostfixExpression and @Image='++' or self::PreIncrementExpression]"
+ "/PrimaryExpression"
+ "/PrimaryPrefix"
+ "/Name"
+ (itName == null ? "" : "[@Image='" + itName + "']");
}

Doing the same check in Java without XPath would probably involve around 30 lines of clumsy null-check and type checks/casts. Changing the grammar to flatten the AST will simplify this partially, but it doesn't remove the problem.

An alternative that Java 8 enables is node stream processing. Basically we can design an API where we can stream the descendants of a node, allowing to filter, flatmap to children of a certain type, flatmap to descendants, etc, in a completely type safe and null safe manner.

Saxon itself has an API to write XPath-like queries in Java, leveraging the standard Stream API. From the Saxon HE doc:

From Saxon 9.9, additional methods exploiting the Java 8 Streams API become available to navigate directly from XdmNode instances. In many cases these will be faster and more convenient than navigation using XPath. For example, instead of executing the XPath expression .//chapter[@title='Introduction'] to find a specific element within a document, it is possible to use the Java expression node.select(descendant("chapter").where(eq(attribute("title"),"Introduction)")).asNode().

That API is nice, but it's not typesafe, uses strings everywhere, and using it directly would force us to wrap the nodes in a Saxon adapter.

This PR

This PR implements a feature-complete, self-contained API to stream nodes. NodeStreams

  • are lazy
  • can be iterated multiple times
  • are type-safe and null-safe
  • can be easily converted to a regular Stream
  • can be easily obtained from a node or a collection of nodes
  • are as expressive as XPath expressions, faster, probably more memory efficient (albeit more verbose)

See the javadoc on the NodeStream interface for more details.

The PR also refactors ForLoopCanBeForeachRule to use that new API. You can see how it blends well with Optional and regular Streams. What's more interesting is that I observed that the rule runs at least 30% faster!

I would argue that with that new API in 7.0.0, we could deprecate the methods getFirstDescendantOfType, getFirstChildOfType, findFirstParentOfType, etc. until 8.0.0. Their functionality is superseded by the new API, and they're not lazy.

What's missing yet:

  • a benchmark between the two possible implementations of descendantStream(). I would think the current implementation is the fastest, but it could also be implemented with a lazy tree visit.
  • support for stopping descendantStream() on find boundaries. This can be added later, because it needs the Stream#takeWhile function, which was introduced in JDK 9, so we'll have to implement it ourselves.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
oowekyala added 23 commits January 7, 2019 05:06
@oowekyala
Add NodeStream
23ce8fc
@oowekyala
Add SingleChildNode interface
e4502b1
@oowekyala
Fix compilation
7cfe69d
@oowekyala
Document API
06f7a00
@oowekyala
Add forkJoin
31167f2
@oowekyala
Make streams iterable multiple times
86dde89
@oowekyala
Move NodeStream into ast package
f0d3b34
@oowekyala
Test laziness
a8d499a
@oowekyala
Union streams fetch values from their substreams
1c73f6f
@oowekyala
Avoid caching by default, provide a cached() method instead
af7ccce
@oowekyala
Simplify implementation
5389e71
@oowekyala
Make functional interface
43fa708
@oowekyala
Test laziness
9a947cc
@oowekyala
Add some tests
77ddf5d
@oowekyala
Add methods for more axes
7b5361b
@oowekyala
Refactor more of ForLoopCanBeForeach
7af212f
@oowekyala
Fix javadoc warnings
ad7c1c3
@oowekyala
Remove dependency on DocumentNavigator
82abeeb
@oowekyala
Add some tests
6893e23
@oowekyala
Merge branch '7.0.x' into node-stream-api
6e2aeea
@oowekyala
Brushup
40f619f
@oowekyala
Add usage doc
3a28733
@oowekyala
Add ancestors(Class)
caf7773
@oowekyala oowekyala added an:enhancement An improvement on existing features / rules in:ast About the AST structure or API, the parsing step labels Jan 27, 2019
@oowekyala oowekyala added this to the 7.0.0 milestone Jan 27, 2019
oowekyala added 2 commits January 27, 2019 16:32
@oowekyala
Fix tests
d20bbd7
@oowekyala
Fix doc link
e51e162
@ghost

ghost commented Jan 27, 2019 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
1 Message
📖 No java rules are changed!

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala
Override Iterable methods
fe82a95
oowekyala added 22 commits September 10, 2019 02:55
@oowekyala
Hide impl
6843571
@oowekyala
Opt sibling methods
0ef2409
@oowekyala
Add #last operation
2c61271
@oowekyala
Implement ancestors in terms of ancestorOrSelf
5724ebe
@oowekyala
Use filtermap to share implementation
cf5e37b
@oowekyala
Add more singleton methods
a4bfd7d
@oowekyala
Implement flatmap and filter in terms of Iterator
4a82612
@oowekyala
Remove many default implementations,
a3770a4
use iterator as main implementation
@oowekyala
Cleanup, add annotations
bc25e58
@oowekyala
Test iterator util
e5a67a5
@oowekyala
Hide filtermap
405df25
@oowekyala
Property testing of implementations
d0da7fd
@oowekyala
Cleanup defaults and doc
550de74
@oowekyala
Improve children slice
83a8852
@oowekyala
Add tests
ccd9c19
@oowekyala
Cleanup slice checks
e86902f
@oowekyala
Implement regular children stream with slice
61a9b0a
@oowekyala
Add singleton iterator
29e481d
@oowekyala
Simplify
5b32c4c
@oowekyala
Merge branch '7.0.x' into node-stream-api
049f659
@oowekyala
Merge branch '7.0.x' into node-stream-api
d058c17
@oowekyala
Checkstyle
8e89b80
@adangel adangel self-assigned this Dec 14, 2019
@adangel adangel changed the title [core] Node stream api [core] Node Stream API Dec 14, 2019
@adangel adangel changed the title [core] Node Stream API [core] NodeStream API Dec 14, 2019
@adangel
adangel merged commit 8e89b80 into pmd:pmd/7.0.x Dec 14, 2019
@oowekyala
oowekyala deleted the node-stream-api branch December 14, 2019 18:00
@adangel adangel mentioned this pull request May 23, 2020
[core] XPath Improvements for PMD 7 #2523
Closed
14 tasks done
@oowekyala oowekyala mentioned this pull request Oct 30, 2020
[core] Deprecate old traversal methods on the Node interface #2886
Merged
3 of 5 tasks
@adangel adangel mentioned this pull request Jan 23, 2023
PMD 7 Tracking Issue #3898
Closed
55 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@adangel adangel

Labels

an:enhancement An improvement on existing features / rules in:ast About the AST structure or API, the parsing step

Projects

None yet

Milestone

7.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@oowekyala @jsotuyod @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.