-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
WIP: Element Builder proposal #741
Conversation
3cd8af0
to
07c192e
Compare
Love it! I really need this! On Tue, Jul 5, 2016 at 10:25 AM, Thomas Durieux [email protected]
|
07c192e
to
35a5fbb
Compare
looks like javapoet for Spoon? |
May be, I did not check. |
@monperrus Yes but better, with love and for Spoon! <3 |
35a5fbb
to
8776689
Compare
public void simpleBinaryTest() { | ||
Factory factory = new Launcher().getFactory(); | ||
Builder B = factory.Builder(); | ||
CtBinaryOperator binary = B.Binary( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
infix notation? Lit(2).times(Lit(3))
I love this idea. It will be an excellent alternative for intercession in addition to the core intercession API, snippets and templates. |
8776689
to
7411209
Compare
generate the builder with https://github.com/google/FreeBuilder? |
What about API like this? Builder B = factory.Builder();
CtTry aTry = B.Try()
.inBody(B.Increment(x.Literal(1)))
.createCatch(B.Catch("e", IllegalArgumentException.class)
.inBody(x.Increment(x.Literal(1)))
).createCatch(B.Catch("e", Exception.class)
).inFinally(x.Decrement(x.Literal(1)))
.build(); the close() can be avoided by building whole catch content as a parameter of createCatch(...) |
Why not but there are a lot of parenthesis and call to the factory (that I was trying to avoid) |
I do not prefer any yet. Just giving ideas into this nice concept, to help to move it forward 👍 CtTry aTry = B.Try();
aTry.inBody(B.Increment(x.Literal(1)))
aTry.createCatch()
.parameter("e", IllegalArgumentException.class)
.inBody(x.Increment(x.Literal(1)))
aTry.createCatch()
.parameter("e", Exception.class)
aTry.inFinally(x.Decrement(x.Literal(1)))
aTry.build(); or just rename of methods? CtTry aTry = B.Try()
.inBody(B.Increment(x.Literal(1)))
.begingCatch()
.parameter("e", IllegalArgumentException.class)
.inBody(x.Increment(x.Literal(1)))
.end()
.beginCatch()
.parameter("e", Exception.class)
.end()
.inFinally(x.Decrement(x.Literal(1)))
.build(); |
Yes, it is very hard to create a good design for this builder.
I prefer this solution |
May be we can avoid other calls of factory too CtTry aTry = B.Try()
.beginTry()
.Increment(x.Literal(1)))
.endTry()
.beginCatch()... the But I agree that this idea is really wild. So parenthesis and use of factory is better in this case, because we are building another spoon model element. So the rule would be:
|
I would said: CtTry aTry = B.Try()
.beginBody()
.Increment(x.Literal(1)))
.endBody()
.beginCatch()... |
it is no problem, if code of builder will be generated. |
CtTry aTry = B.Try()
.beginBody()
.Increment(x.Literal(1)))
.endBody()
.beginCatch()
.parameter("e", IllegalArgumentException.class)
.beginBody()
.Increment(x.Literal(1)))
.endBody()
.endCatch() The problem of this API is "What is the return type of |
You are right, I did not think enough |
One more idea ... to use lambda expression to generate nested elements. Like this: CtTry aTry = factory.Builder().Try()
.inBody(body ->
body.Increment(x.Literal(1)))
.createCatch(IllegalArgumentException.class, e, body->
body.statement(st -> st.Increment(x.Literal(1)))
.statement(st -> st.invoke("java.lang.System.out.println()"))
).createCatch(Exception.class, "e")
.inFinally(body ->
body.statemenet(st -> st.Decrement(x.Literal(1)))
.build();
|
I can be a solution but it is only nice for Java 8. |
I created this PR in order to discuss the architecture of a possible new Element Builder in spoon.
The current factories are neither easy to use nor explicit. This new builder aims to solve these issues.
But I don't know yet the best solution.
This PR contains a POC of the builder. I would like to have your opinion.
Syntaxe
BinaryOperator
Try
Method
Limitations
CatchBuilder.close()
) in order to get back the parent Builder (hereTryBuilder
). It is not intuitive...inBody
. Unfortunately the inheritance will not solve this issue (in this case we need multi inheritance)