Skip to content

Conversation

@AlekSimpson
Copy link
Contributor

@AlekSimpson AlekSimpson commented Jun 19, 2023

What's changed?

toString should not be called on array instances. RSPEC-2116

What's your motivation?

#44

Anything in particular you'd like reviewers to focus on?

I think the recipe is pretty much finished, only two things is there is an edge case with String.format() calls I still haven't quite worked out. It gives an error when I try and run the tests on them. Second thing is that I'm not sure how to get the preconditions to check for if the code is using the array type before it runs.

Anyone you would like to review specifically?

@timtebeek

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've added the license header to any new files through ./gradlew licenseFormat
  • I've used the IntelliJ auto-formatter on affected files
  • I've updated the documentation (if applicable)

AlekSimpson and others added 14 commits June 16, 2023 13:58
…e not just raw string variables but also things like method calls
…for two tests which are testing an edge cases related to String.format()
@AlekSimpson AlekSimpson requested a review from timtebeek June 19, 2023 22:22
@AlekSimpson AlekSimpson self-assigned this Jun 19, 2023
@AlekSimpson AlekSimpson marked this pull request as draft June 19, 2023 22:22
@timtebeek
Copy link
Member

There's some conflicts here; could you resolve those? Did you perhaps start your branch from your previous branch?

@timtebeek timtebeek requested a review from joanvr June 20, 2023 04:34
@AlekSimpson
Copy link
Contributor Author

I figured out the edge case and also the precondition check. I think this recipe is functionally complete, but maybe there could be last minute polishes I don't see.

@AlekSimpson AlekSimpson marked this pull request as ready for review June 20, 2023 23:31
Copy link
Member

@sambsnyd sambsnyd left a comment

Choose a reason for hiding this comment

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

Good work! This will be a useful recipe. 👍
After you address my feedback this looks good to check in, no need to seek another round of review from me.

…so reworked recipe so that it uses cursor messaging
@AlekSimpson AlekSimpson force-pushed the alek/RemoveToStringCallsFromArrayInstances branch from 8077b60 to bd586b7 Compare June 22, 2023 18:53
@AlekSimpson
Copy link
Contributor Author

AlekSimpson commented Jun 22, 2023

Ok, so I've tried to incorporate all the feedback from this thread. I still need to add more tests for a bunch of the other methods mentioned above. However, I think I should wait to do that till I have the cursor messaging working. I've updated the branch with my implementation of the cursor messaging but it doesn't work. The message never seems to be found in the visitExpression() visitor and I'm not sure why. @knutwannheden

@AlekSimpson AlekSimpson mentioned this pull request Jun 22, 2023
4 tasks
@knutwannheden
Copy link
Contributor

Ok, so I've tried to incorporate all the feedback from this thread. I still need to add more tests for a bunch of the other methods mentioned above. However, I think I should wait to do that till I have the cursor messaging working. I've updated the branch with my implementation of the cursor messaging but it doesn't work. The message never seems to be found in the visitExpression() visitor and I'm not sure why. @knutwannheden

@AlekSimpson You got it almost all right! The important thing here is that the super.visitMethodInvocation() call comes after the message is added to the cursor. Otherwise visitExpression() will end up getting called before the cursor message is added. I made some changes and will let you take it from here.

@rpau rpau linked an issue Jun 23, 2023 that may be closed by this pull request
…ipe also covers arrays concatenated with strings now
@AlekSimpson
Copy link
Contributor Author

All changes have been made and tests have been updated. All tests pass.

Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

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

Almost there! I added a few more comments which should be addressed.

public Expression visitExpression(Expression exp, ExecutionContext ctx) {
Expression e = (Expression) super.visitExpression(exp, ctx);
if (e.getType() instanceof JavaType.Array) {
if (e instanceof TypedTree && e.getType() instanceof JavaType.Array) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this is more a question for Knut of Kun, but I had expected a call out to TypeUtils here rather than a direct use of instanceof. When would we use each?

Copy link
Contributor

Choose a reason for hiding this comment

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

We could replace e.getType() instanceof JavaType.Array with TypeUtils.asArray(e.getType()) != null but I don't think it is really much better. IMHO the instanceof here is fine.

@AlekSimpson AlekSimpson force-pushed the alek/RemoveToStringCallsFromArrayInstances branch from 5963274 to ffadf48 Compare June 26, 2023 16:32
Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

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

Apart from one last minor comment I think this looks good.

public Expression visitExpression(Expression exp, ExecutionContext ctx) {
Expression e = (Expression) super.visitExpression(exp, ctx);
if (e.getType() instanceof JavaType.Array) {
if (e instanceof TypedTree && e.getType() instanceof JavaType.Array) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could replace e.getType() instanceof JavaType.Array with TypeUtils.asArray(e.getType()) != null but I don't think it is really much better. IMHO the instanceof here is fine.

…lsFromArrayInstances.java

Co-authored-by: Knut Wannheden <[email protected]>
@timtebeek timtebeek merged commit 2127a09 into main Jun 27, 2023
@timtebeek timtebeek deleted the alek/RemoveToStringCallsFromArrayInstances branch June 27, 2023 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

hashCode and toString should not be called on array instances. RSPEC-2116

5 participants