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

Provide option for explicit toString calls in the generated interpolation code #14

Merged
merged 4 commits into from
Jul 14, 2020
Merged

Provide option for explicit toString calls in the generated interpolation code #14

merged 4 commits into from
Jul 14, 2020

Conversation

PawelLipski
Copy link
Contributor

@PawelLipski PawelLipski commented Jun 4, 2020

I found it pretty useful e.g. for static code analyzers to make sure that an explicit toString call is always present. E.g. in our project we'd like to forbid toString calls on certain classes (like Option(al)), since it's usually not intended (and in case of Option, it can result in a literal Some(...) being rendered in user-facing strings in the UI).

Plus also resolve a bug related to reported positions of compilation errors within interpolated strings & improve code style in a few places.

Depends on jupiter-tools/compile-test#1

@@ -71,9 +74,4 @@ private boolean isClassOrEnum(Element codeElement) {
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}

private void printBanner() {
String banner = "v0.3 String Interpolation Java Plugin, by Anatoliy Korovin";
Copy link
Contributor Author

@PawelLipski PawelLipski Jun 4, 2020

Choose a reason for hiding this comment

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

Resolving #13 btw ;)


public FakeJavaFileWrapper(String text) {
super(URI.create("myfake:/Test.java"), JavaFileObject.Kind.SOURCE);
this.text = "class Test { Object value = String.valueOf(" + text + "); }";
if (callToStringExplicitlyInInterpolations) {
this.text = "class Test { Object value = java.util.Objects.nonNull(" + text + ") ? java.util.Objects.requireNonNull(" + text + ").toString() : \"null\"; }";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's the best I've come up with so far, it performs boxing for primitive types, but unfortunately requires double evaluation of the expression :/

@antkorwin
Copy link
Owner

Hi @PawelLipski ,
thanks a lot for your work!
it looks pretty useful!

I will find time to cover this by unit tests and fix build errors,
unfortunately, now I'm very busy (a lot of work this week), maybe I can do it next week.

@PawelLipski
Copy link
Contributor Author

Ok! let me know if you agree with the general design, I can also provide docs for this as well

@codecov-commenter
Copy link

codecov-commenter commented Jun 8, 2020

Codecov Report

Merging #14 into master will increase coverage by 0.21%.
The diff coverage is 96.42%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master      #14      +/-   ##
============================================
+ Coverage     91.89%   92.10%   +0.21%     
+ Complexity       54       52       -2     
============================================
  Files             6        6              
  Lines           148      152       +4     
  Branches         16       16              
============================================
+ Hits            136      140       +4     
  Misses            7        7              
  Partials          5        5              
Impacted Files Coverage Δ Complexity Δ
.../antkorwin/betterstrings/ast/ExpressionParser.java 92.50% <90.00%> (+1.32%) 3.00 <1.00> (ø)
...ntkorwin/betterstrings/BetterStringsProcessor.java 100.00% <100.00%> (ø) 10.00 <0.00> (-1.00)
...etterstrings/ast/InnerStringVarsAstTranslator.java 86.79% <100.00%> (+0.79%) 26.00 <1.00> (ø)
...a/com/antkorwin/betterstrings/tokenizer/Token.java 88.88% <100.00%> (-1.12%) 4.00 <1.00> (-1.00)
...m/antkorwin/betterstrings/tokenizer/Tokenizer.java 96.29% <100.00%> (-0.14%) 8.00 <2.00> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d346269...8d41986. Read the comment docs.

@PawelLipski
Copy link
Contributor Author

Also, I fixed the build (see the latest commit), apparently com.sun.tools.javac.api.JavacTaskImpl#parse is declared throwing or not throwing IOException depending on the exact environment (Java version? I develop on OpenJDK/JBR 11.0.6)

@PawelLipski
Copy link
Contributor Author

PawelLipski commented Jun 15, 2020

@antkorwin added tests for the newly-added option; they depends on jupiter-tools/compile-test#1 getting released as v0.2 first, however (that's why Travis isn't passing as for now)

@PawelLipski
Copy link
Contributor Author

... and docs as well :) I'd appreciate your feedback since one of our projects would greatly benefit from this feature getting released

@mkondratek
Copy link

Hey @antkorwin 😄 how does the work go? when can we get it merged?

@@ -147,7 +147,7 @@
<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>compile-test</artifactId>
<version>0.1</version>
<version>0.2</version>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@antkorwin so now that compile-test v0.2 is on Maven Central (:tada:), could you re-run the most recent test? I apparently can't trigger a test re-run on Travis for this project myself. Or I can just amend & force-push the most recent commit.

@PawelLipski
Copy link
Contributor Author

@antkorwin long time no hear... how's going?

@antkorwin
Copy link
Owner

Hi, @PawelLipski
I'm so sorry about long delay

It looks great!
Thanks a lot for your patience!

@antkorwin antkorwin merged commit b701c91 into antkorwin:master Jul 14, 2020
@antkorwin
Copy link
Owner

I will check it today and deploy the next version to maven central...

@PawelLipski @mkondratek thanks a lot for your contribution!

@PawelLipski PawelLipski deleted the refactor/call-to-string branch July 14, 2020 11:42
@PawelLipski PawelLipski restored the refactor/call-to-string branch July 14, 2020 11:43
"Result: "
+ String.valueOf(obj)
+ ".method() = "
= String.valueOf(obj.method())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of = it should be + here... to be fixed with the next release

Copy link
Owner

Choose a reason for hiding this comment

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

wow, I read this text but didn't notice a typo =)
I have fixed it now.

@antkorwin
Copy link
Owner

the next release already published to the maven central
/~https://github.com/antkorwin/better-strings/releases/tag/v0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants