Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer Mono#fromSupplier over Mono#fromCallable where possible #232

Merged
merged 3 commits into from
Sep 15, 2022

Conversation

Stephan202
Copy link
Member

@Stephan202 Stephan202 commented Sep 10, 2022

Suggested commit message:

Prefer `Mono#fromSupplier` over `Mono#fromCallable` where possible (#232)

This rule is implemented using a Refaster template, relying on the new
`ThrowsCheckedException` matcher.

While there, introduce `AbstractTestChecker` to simplify the test setup for
Refaster `Matcher`s. This base class flags all `ExpressionTree`s matched by the
`Matcher` under test.

@Stephan202 Stephan202 added this to the 0.2.1 milestone Sep 10, 2022
@Stephan202 Stephan202 requested a review from rickie September 10, 2022 13:50
@Stephan202 Stephan202 marked this pull request as ready for review September 10, 2022 13:57
Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This significantly improves our setup, very cool!

Also nice Refaster template 🚀.

Have some questions and considerations :).

import com.sun.source.util.TreeScanner;
import javax.annotation.Nullable;

abstract class AbstractTestChecker extends BugChecker implements CompilationUnitTreeMatcher {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make clear the checker is solely focused on matching ExpressionTrees, should we either (1) change the name or (2) add some Javadoc explaining this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe AbstractMatcherTestChecker. Will add some documentation.

public Void visitImport(ImportTree node, @Nullable Void unused) {
/*
* We're not interested in matching import statements. While components of these
* can be `ExpressionTree`s.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do this to be consistent with the method below?

Suggested change
* can be `ExpressionTree`s.
* can be `ExpressionTree`s, they will never be matched by Refaster.

new TreeScanner<Void, Void>() {
@Nullable
@Override
public Void scan(Tree tree, @Nullable Void p) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Void scan(Tree tree, @Nullable Void p) {
public Void scan(Tree tree, @Nullable Void unused) {

The name p was most likely choosen over unused on purpose, but still wanted to ask, technically it's "unused" because p is Void... Does it make sense to let it be consistent with the visitImport one and also call it unused?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's named p after the method in the TreeScanner superclass. First it had another name, but then IDEA complained. But I see that IDEA handles unused specially, which makes sense. No strong opinion either way, so will change.

import com.sun.source.util.TreeScanner;
import javax.annotation.Nullable;

abstract class AbstractTestChecker extends BugChecker implements CompilationUnitTreeMatcher {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything against moving this to refaster-test-support?

People might want to use this AbstractTestChecker for their own Matchers as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd defer such a move for two reasons:

  1. This class isn't truly Refaster- specific.
  2. As-is the class isn't part of the public API, thus allowing us to change it more freely in the future.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
return new IsArray().matches(tree, state) ? describeMatch(tree) : Description.NO_MATCH;
// XXX: This is false positive reported by CheckStyle. See
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// XXX: This is false positive reported by CheckStyle. See
// XXX: This is a false positive reported by Checkstyle. See

Right?

It's funny, you usually write Github instead of GitHub and for Checkstyle it's the other way around 😉.

}
}

private static boolean containsCheckedException(Collection<Type> types, VisitorState state) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this method be above the other one because it comes before getThrownTypes? "Call-down" 😄 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In either case we would call down, but getThrownTypes is invoked before containsCheckedException, so I'd say the current order is "correct".

Stephan202 and others added 3 commits September 12, 2022 19:56
This rule is implemented using a Refaster template, relying on the new
`ThrowsCheckedException` matcher.

While there, introduce `AbstractTestChecker` to simplify the test setup
for Refaster `Matcher`s. This base class flags all `ExpressionTree`s
matched by the `Matcher` under test.
@Stephan202 Stephan202 force-pushed the sschroevers/prefer-mono-from-supplier branch from 0cd0e3a to 6f199a3 Compare September 12, 2022 19:21
Copy link
Member Author

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased and added a commit. Tnx for the review!

}
}

private static boolean containsCheckedException(Collection<Type> types, VisitorState state) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In either case we would call down, but getThrownTypes is invoked before containsCheckedException, so I'd say the current order is "correct".

import com.sun.source.util.TreeScanner;
import javax.annotation.Nullable;

abstract class AbstractTestChecker extends BugChecker implements CompilationUnitTreeMatcher {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd defer such a move for two reasons:

  1. This class isn't truly Refaster- specific.
  2. As-is the class isn't part of the public API, thus allowing us to change it more freely in the future.

import com.sun.source.util.TreeScanner;
import javax.annotation.Nullable;

abstract class AbstractTestChecker extends BugChecker implements CompilationUnitTreeMatcher {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe AbstractMatcherTestChecker. Will add some documentation.

new TreeScanner<Void, Void>() {
@Nullable
@Override
public Void scan(Tree tree, @Nullable Void p) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's named p after the method in the TreeScanner superclass. First it had another name, but then IDEA complained. But I see that IDEA handles unused specially, which makes sense. No strong opinion either way, so will change.

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes LGTM!

Suggested commit message is 👍🏻.

@rickie rickie requested a review from EnricSala September 14, 2022 06:26
Copy link

@EnricSala EnricSala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! From now on will be able to stop checking all Mono.fromCallable(... for checked exceptions 😄

Didn't review the ThrowsCheckedException matcher because it escapes my knowledge and I have no time to learn it right now, but looks cool :)

@rickie
Copy link
Member

rickie commented Sep 15, 2022

Thanks for the review @EnricSala 😄.

@rickie rickie merged commit 62fe10f into master Sep 15, 2022
@rickie rickie deleted the sschroevers/prefer-mono-from-supplier branch September 15, 2022 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants