rewrite-migrate-java icon indicating copy to clipboard operation
rewrite-migrate-java copied to clipboard

Support ByteStreams.toByteArray -> inputStream.readAllBytes()

Open koppor opened this issue 1 year ago • 8 comments

Instead of Guava's ByteStreams#toByteArray(), InputStream.html#readAllBytes should be used. The latter is available since Java 9.

- String inputStr = new String(ByteStreams. toByteArray(inputStream), Charsets.UJTF_8);
+ String inputStr = new String(inputStream.readAllBytes()), Charsets.UJTF_8);

Should IMHO be doable with Refaster templates?

koppor avatar Jan 12 '24 10:01 koppor

Indeed looks like a good candidate! I've moved the issue to rewrite-migrate-java since that's where we already have our other Guava recipes and the rewrite-templating dependency. I'm doubting if the recipe would even need the new String(... ) surrounding it. (anyone) welcome to give this a go! :)

timtebeek avatar Jan 12 '24 10:01 timtebeek

I'm doubting if the recipe would even need the new String(... ) surrounding it.

Indeed, better to omit that and make the rule more generally applicable. :+1: I filed PicnicSupermarket/error-prone-support#963, but feel free to copy over to this repo. Sharing is caring ;)

Stephan202 avatar Jan 12 '24 15:01 Stephan202

Just be careful, this does not work under Android 9 & 10. I recently had the issue...

Siedlerchr avatar Jan 12 '24 18:01 Siedlerchr

I'm doubting if the recipe would even need the new String(... ) surrounding it.

Indeed, better to omit that and make the rule more generally applicable. 👍 I filed PicnicSupermarket/error-prone-support#963, but feel free to copy over to this repo. Sharing is caring ;)

Probably good to have our own copy here as well, to fit in with our no guava recipes. Thanks for the reference implementation @Stephan202 ! Would you want to add that here @koppor ? Then we can also run it at scale here.

Longer term we could look at adding Picnicsupermarket/error-prone-support as a dependency here and reference recipes from there in our composite yaml recipes, but that first needs a new release of EPS and some work on correctly attributing those recipes to the authors there. :)

timtebeek avatar Jan 14 '24 13:01 timtebeek

but that first needs a new release of EPS

@rickie and I have started to talk about doing a new release. Could be a few weeks (lots of other stuff going on), but hopefully less!

Stephan202 avatar Jan 14 '24 13:01 Stephan202

Turns out we have an interesting challenge when we want to integrate this into rewrite-migrate-java, which still uses Java 8. image

We might need to add a second source set of Refaster recipe classes does no make it into the the final jar, but do contribute the generated Java 8 runtime compatible recipes. 🤔 Fun corner case this. /cc @knutwannheden

timtebeek avatar Jan 21 '24 14:01 timtebeek

Yes, this issue has also crossed my mind. And indeed I think we should be able to add a separate source set. Another question would be if this should somehow automatically add a precondition for the required Java version. We should be able to check that somehow. But that would be more of a nice-to-have, I think.

knutwannheden avatar Jan 21 '24 14:01 knutwannheden

Neat; figured get start with that in https://github.com/openrewrite/rewrite-migrate-java/pull/399; Although come to think of it that might require us to complete https://github.com/openrewrite/rewrite-templating/pull/57 first

  • https://github.com/openrewrite/rewrite-templating/pull/57

timtebeek avatar Jan 21 '24 15:01 timtebeek