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

Extra Parenthesis after Spoon getAssignment() #4807

Closed
bhatnagar444 opened this issue Jul 19, 2022 · 7 comments · Fixed by #4809
Closed

Extra Parenthesis after Spoon getAssignment() #4807

bhatnagar444 opened this issue Jul 19, 2022 · 7 comments · Fixed by #4809

Comments

@bhatnagar444
Copy link

bhatnagar444 commented Jul 19, 2022

Hi Team,

I have an issue where I am getting extra parenthesis after getAssignment().

Below is the replication of the problem -

My code during build model

import spoon.Launcher;
import spoon.compiler.Environment;
import spoon.reflect.CtModel;
import spoon.support.sniper.SniperJavaPrettyPrinter;

Launcher launcher = new Launcher();
launcher.addInputResource(dirPath);
final Environment e = launcher.getEnvironment();
e.setPrettyPrinterCreator(() -> new SniperJavaPrettyPrinter(e));
launcher.buildModel(); 

Input which I am giving

ResultSet rs1 = stmt.executeQuery(QUERY1 + getTableName() + QUERY2 + "id=" + javaFileName.getEmployeeId() + QUERY3);

And trying to extract what is inside executeQuery so the
Expected output - QUERY1 + getTableName() + QUERY2 + "id=" + javaFileName.getEmployeeId() + QUERY3
Output I am getting - ((((QUERY1 + getTableName()) + QUERY2) + "id=") + javaFileName.getEmployeeId()) + QUERY3

Please suggest how can I get rid of the extra parenthesis (Excluding parenthesis of methods like getTableName())...!!!

@algomaster99
Copy link
Contributor

This should have been resolved by #3823. @slarse do you have any idea? Nevertheless, I can take a look tomorrow.

@bhatnagar444
Copy link
Author

Hi @slarse ,

Please let me know which spoon dependency do I need to take or update original one (v10.1.1) after this change ?
Also is the review done or any ETA for the same ?

@bhatnagar444
Copy link
Author

Hi @algomaster99 ,

As you suggested will try changing my spoon version from 10.1.1 to 10.2.0-beta-12 and will test parenthesis issue also will wait for your green flag if the spoon changes got synced with maven central.

@algomaster99
Copy link
Contributor

@bhatnagar444 10.2.0-beta-12 does not show up on maven central repository yet, but our README is showing the updated beta release. You can try updating spoon in your project.

@algomaster99
Copy link
Contributor

Following up on my conversation with @bhatnagar444 on LinkedIn, please try creating the launcher in your project like below before printing your code:

Launcher launcher = new Launcher();
launcher.getEnvironment().setPrettyPrinterCreator(() -> {
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(launcher.getEnvironment());
    printer.setMinimizeRoundBrackets(true);
    return printer;
});

You can use SniperJavaPrettyPrinter also if you would like.

@algomaster99
Copy link
Contributor

Since setMinimizeRoundBrackets is protected in DJPP, I thought I would stick to using SJPP as it sets minimizeRoundBrackets to true in its constructor. However, that did not work as well. I finally extended a new printer from DJPP and then set the attribute to true and that works.

class RoundBracketMinimiser extends DefaultJavaPrettyPrinter {
    public RoundBracketPrinter(Environment env) {
            super(env);
            this.setMinimizeRoundBrackets(true);
        }
    }

I used this printed to print the types and it works.

@slarse, have you considered making the setter in DJPP a public API?

@slarse
Copy link
Collaborator

slarse commented Aug 6, 2022

have you considered making the setter in DJPP a public API?

Not really. The DJPP is meant to above all else be correct. The round bracket minimization introduces some amount of risk for incorrectness (due to potential bugs), especially when going beyond the simple stuff of arithmetic expressions and you start venturing into casts and the like.

Honestly, I can't quite recall why I implemented it in the DJPP to begin with.

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 a pull request may close this issue.

3 participants