{"action":"created","issue":{"url":"https://api.github.com/repos/INRIA/spoon/issues/741","repository_url":"https://api.github.com/repos/INRIA/spoon","labels_url":"https://api.github.com/repos/INRIA/spoon/issues/741/labels{/name}","comments_url":"https://api.github.com/repos/INRIA/spoon/issues/741/comments","events_url":"https://api.github.com/repos/INRIA/spoon/issues/741/events","html_url":"https://github.com/INRIA/spoon/pull/741","id":163807393,"number":741,"title":"WIP: Element Builder proposal","user":{"login":"tdurieux","id":5577568,"avatar_url":"https://avatars.githubusercontent.com/u/5577568?v=3","gravatar_id":"","url":"https://api.github.com/users/tdurieux","html_url":"https://github.com/tdurieux","followers_url":"https://api.github.com/users/tdurieux/followers","following_url":"https://api.github.com/users/tdurieux/following{/other_user}","gists_url":"https://api.github.com/users/tdurieux/gists{/gist_id}","starred_url":"https://api.github.com/users/tdurieux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tdurieux/subscriptions","organizations_url":"https://api.github.com/users/tdurieux/orgs","repos_url":"https://api.github.com/users/tdurieux/repos","events_url":"https://api.github.com/users/tdurieux/events{/privacy}","received_events_url":"https://api.github.com/users/tdurieux/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-07-05T09:25:36Z","updated_at":"2016-07-05T11:50:25Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/INRIA/spoon/pulls/741","html_url":"https://github.com/INRIA/spoon/pull/741","diff_url":"https://github.com/INRIA/spoon/pull/741.diff","patch_url":"https://github.com/INRIA/spoon/pull/741.patch"},"body":"I created this PR in order to discuss the architecture of a possible new Element Builder in spoon.\r\n\r\nThe current factories are neither easy to use nor explicit. This new builder aims to solve these issues.\r\nBut I don't know yet the best solution.\r\n\r\nThis PR contains a POC of the builder. I would like to have your opinion.\r\n\r\n# Syntaxe\r\n\r\n## BinaryOperator\r\n```java\r\nBuilder B = factory.Builder();\r\nCtBinaryOperator binary = B.Binary(\r\n\t\tB.Literal(2))\r\n\t\t.plus(B.Literal(1))\r\n\t\t.lower(\r\n\t\t\t\tB.Binary(B.Literal(10))\r\n\t\t\t\t\t\t.minus(B.Literal(5))\r\n\t\t).build();\r\nAssert.assertEquals(\"(2 + 1) < (10 - 5)\", binary.toString());\r\n```\r\n\r\n## Try\r\n\r\n```java\r\nBuilder B = factory.Builder();\r\nCtTry aTry = B.Try()\r\n\t\t.inBody(B.Increment(x.Literal(1)))\r\n\t\t.createCatch()\r\n\t\t\t.parameter(\"e\", IllegalArgumentException.class)\r\n\t\t\t.inBody(x.Increment(x.Literal(1)))\r\n\t\t\t.close()\r\n\t\t.createCatch()\r\n\t\t\t.parameter(\"e\", Exception.class)\r\n\t\t\t.close()\r\n\t\t.inFinally(x.Decrement(x.Literal(1)))\r\n.build();\r\nAssert.assertEquals(\"try {\\n\"\r\n\t\t+ \"    1++;\\n\"\r\n\t\t+ \"} catch (java.lang.IllegalArgumentException e) {\\n\"\r\n\t\t+ \"    1++;\\n\"\r\n\t\t+ \"} catch (java.lang.Exception e) {\\n\"\r\n\t\t+ \"} finally {\\n\"\r\n\t\t+ \"    1--;\\n\"\r\n\t\t+ \"}\", aTry.toString());\r\n```\r\n## Method\r\n\r\n```java\r\nBuilder x = factory.Builder();\r\n\r\n\t\tCtReturn<Object> aReturn = factory.Core().createReturn();\r\n\t\taReturn.setReturnedExpression(x.Literal(1).build());\r\n\t\tCtMethod m = x.Method(\"m\")\r\n\t\t\t\t.type(int.class)\r\n\t\t\t\t.setPrivate()\r\n\t\t\t\t.setStatic()\r\n\t\t\t\t.addParam(int.class, \"x\")\r\n\t\t\t\t.inBody(aReturn).build();\r\n\t\tAssert.assertEquals(\"private static int m(int x) {\\n\"\r\n\t\t\t\t+ \"    return 1;\\n\"\r\n\t\t\t\t+ \"}\", m.toString());\r\n```\r\n# Limitations\r\n1. The builder requires a method to indicate the end of the element (here `CatchBuilder.close()`) in order to get back the parent Builder (here `TryBuilder`). It is not intuitive... \r\n\r\n2. This architecture will introduce a lot of duplicate codes. Example: all elements that have a body will have the method `inBody`. Unfortunately the inheritance will not solve this issue (in this case we need multi inheritance)\r\n"},"comment":{"url":"https://api.github.com/repos/INRIA/spoon/issues/comments/230457321","html_url":"https://github.com/INRIA/spoon/pull/741#issuecomment-230457321","issue_url":"https://api.github.com/repos/INRIA/spoon/issues/741","id":230457321,"user":{"login":"monperrus","id":803666,"avatar_url":"https://avatars.githubusercontent.com/u/803666?v=3","gravatar_id":"","url":"https://api.github.com/users/monperrus","html_url":"https://github.com/monperrus","followers_url":"https://api.github.com/users/monperrus/followers","following_url":"https://api.github.com/users/monperrus/following{/other_user}","gists_url":"https://api.github.com/users/monperrus/gists{/gist_id}","starred_url":"https://api.github.com/users/monperrus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/monperrus/subscriptions","organizations_url":"https://api.github.com/users/monperrus/orgs","repos_url":"https://api.github.com/users/monperrus/repos","events_url":"https://api.github.com/users/monperrus/events{/privacy}","received_events_url":"https://api.github.com/users/monperrus/received_events","type":"User","site_admin":false},"created_at":"2016-07-05T11:50:24Z","updated_at":"2016-07-05T11:50:24Z","body":"I love this idea. It will be an excellent alternative for intercession in addition to the core intercession API, snippets and templates. "}}
{"actor":{"display_login":"monperrus"}}