{"action":"created","comment":{"url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/comments/42491180","id":42491180,"diff_hunk":"@@ -0,0 +1,262 @@\n+/*\n+ * SonarQube Java\n+ * Copyright (C) 2012 SonarSource\n+ * sonarqube@googlegroups.com\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU Lesser General Public\n+ * License as published by the Free Software Foundation; either\n+ * version 3 of the License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+ * Lesser General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU Lesser General Public\n+ * License along with this program; if not, write to the Free Software\n+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02\n+ */\n+package org.sonar.java.se;\n+\n+import java.util.HashMap;\n+import java.util.Map;\n+\n+import javax.annotation.CheckForNull;\n+\n+import org.sonar.plugins.java.api.semantic.Symbol;\n+import org.sonar.plugins.java.api.tree.BinaryExpressionTree;\n+import org.sonar.plugins.java.api.tree.IdentifierTree;\n+import org.sonar.plugins.java.api.tree.InstanceOfTree;\n+import org.sonar.plugins.java.api.tree.LiteralTree;\n+import org.sonar.plugins.java.api.tree.ParenthesizedTree;\n+import org.sonar.plugins.java.api.tree.Tree;\n+import org.sonar.plugins.java.api.tree.TypeCastTree;\n+import org.sonar.plugins.java.api.tree.UnaryExpressionTree;\n+import org.sonar.plugins.java.api.tree.VariableTree;\n+\n+import com.google.common.collect.Maps;\n+\n+public class ConstraintManager {\n+\n+  private final Map<Tree, SymbolicValue> map = new HashMap<>();\n+  /**\n+   * Map to handle lookup of fields.\n+   * */\n+  private final Map<Symbol, SymbolicValue> symbolMap = new HashMap<>();\n+\n+  public SymbolicValue createSymbolicValue(Tree syntaxNode) {\n+    SymbolicValue result = map.get(syntaxNode);\n+    if (result == null && syntaxNode.is(Tree.Kind.IDENTIFIER)) {\n+      result = symbolMap.get(((IdentifierTree) syntaxNode).symbol());\n+    } else if (result == null && syntaxNode.is(Tree.Kind.VARIABLE)) {\n+      result = symbolMap.get(((VariableTree) syntaxNode).symbol());\n+    }\n+    if (result == null) {\n+      result = new SymbolicValue.ObjectSymbolicValue(map.size() + ProgramState.EMPTY_STATE.constraints.size());\n+      map.put(syntaxNode, result);\n+      if (syntaxNode.is(Tree.Kind.IDENTIFIER)) {\n+        symbolMap.put(((IdentifierTree) syntaxNode).symbol(), result);\n+      } else if (syntaxNode.is(Tree.Kind.VARIABLE)) {\n+        symbolMap.put(((VariableTree) syntaxNode).symbol(), result);\n+      }\n+    }\n+    return result;\n+  }\n+\n+  public SymbolicValue supersedeSymbolicValue(VariableTree variable) {\n+    SymbolicValue result = new SymbolicValue.ObjectSymbolicValue(map.size() + ProgramState.EMPTY_STATE.constraints.size());\n+    map.put(variable, result);\n+    symbolMap.put(variable.symbol(), result);\n+    return result;\n+  }\n+\n+  public SymbolicValue eval(ProgramState programState, Tree syntaxNode) {\n+    syntaxNode = skipTrivial(syntaxNode);\n+    switch (syntaxNode.kind()) {\n+      case NULL_LITERAL: {\n+        return SymbolicValue.NULL_LITERAL;\n+      }\n+      case BOOLEAN_LITERAL: {\n+        boolean value = Boolean.parseBoolean(((LiteralTree) syntaxNode).value());\n+        if (value) {\n+          return SymbolicValue.TRUE_LITERAL;\n+        }\n+        return SymbolicValue.FALSE_LITERAL;\n+      }\n+      case VARIABLE: {\n+        Symbol symbol = ((VariableTree) syntaxNode).symbol();\n+        SymbolicValue result = programState.values.get(symbol);\n+        if (result != null) {\n+          // symbolic value associated with local variable\n+          return result;\n+        }\n+        break;\n+      }\n+      case IDENTIFIER: {\n+        Symbol symbol = ((IdentifierTree) syntaxNode).symbol();\n+        SymbolicValue result = programState.values.get(symbol);\n+        if (result != null) {\n+          // symbolic value associated with local variable\n+          return result;\n+        }\n+        break;\n+      }\n+      case LOGICAL_COMPLEMENT: {\n+        UnaryExpressionTree unaryExpressionTree = (UnaryExpressionTree) syntaxNode;\n+        SymbolicValue val = eval(programState, unaryExpressionTree.expression());\n+        if (SymbolicValue.FALSE_LITERAL.equals(val)) {\n+          return SymbolicValue.TRUE_LITERAL;\n+        } else if (val.equals(SymbolicValue.TRUE_LITERAL)) {\n+          return SymbolicValue.FALSE_LITERAL;\n+        }\n+        // if not tied to a concrete value, create symbolic value with no constraint for now.\n+        // TODO : create constraint between expression and created symbolic value\n+      }\n+    }\n+    return createSymbolicValue(syntaxNode);\n+  }\n+\n+  /**\n+   * Remove parenthesis and type cast.\n+   */\n+  private static Tree skipTrivial(Tree expression) {\n+    do {\n+      switch (expression.kind()) {\n+        case PARENTHESIZED_EXPRESSION:\n+          expression = ((ParenthesizedTree) expression).expression();\n+          break;\n+        case TYPE_CAST:\n+          expression = ((TypeCastTree) expression).expression();","path":"java-squid/src/main/java/org/sonar/java/se/ConstraintManager.java","position":130,"original_position":130,"commit_id":"caf24678ebd4693b7f56197e7502c7b1e0739312","original_commit_id":"caf24678ebd4693b7f56197e7502c7b1e0739312","user":{"login":"SonarTech","id":1842438,"avatar_url":"https://avatars.githubusercontent.com/u/1842438?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarTech","html_url":"https://github.com/SonarTech","followers_url":"https://api.github.com/users/SonarTech/followers","following_url":"https://api.github.com/users/SonarTech/following{/other_user}","gists_url":"https://api.github.com/users/SonarTech/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarTech/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarTech/subscriptions","organizations_url":"https://api.github.com/users/SonarTech/orgs","repos_url":"https://api.github.com/users/SonarTech/repos","events_url":"https://api.github.com/users/SonarTech/events{/privacy}","received_events_url":"https://api.github.com/users/SonarTech/received_events","type":"User","site_admin":false},"body":"![MAJOR](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/severity-major.png) Introduce a new variable instead of reusing the parameter \"expression\". [![rule](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/rule.png)](https://dory.internal.sonarsource.com/coding_rules#rule_key=squid%3AS1226)\n","created_at":"2015-10-20T12:58:32Z","updated_at":"2015-10-20T12:58:32Z","html_url":"https://github.com/SonarSource/sonar-java/pull/445#discussion_r42491180","pull_request_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/445","_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/comments/42491180"},"html":{"href":"https://github.com/SonarSource/sonar-java/pull/445#discussion_r42491180"},"pull_request":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/445"}}},"pull_request":{"url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/445","id":48194983,"html_url":"https://github.com/SonarSource/sonar-java/pull/445","diff_url":"https://github.com/SonarSource/sonar-java/pull/445.diff","patch_url":"https://github.com/SonarSource/sonar-java/pull/445.patch","issue_url":"https://api.github.com/repos/SonarSource/sonar-java/issues/445","number":445,"state":"open","locked":false,"title":"Sonarjava 1339","user":{"login":"DidierBesset","id":14869932,"avatar_url":"https://avatars.githubusercontent.com/u/14869932?v=3","gravatar_id":"","url":"https://api.github.com/users/DidierBesset","html_url":"https://github.com/DidierBesset","followers_url":"https://api.github.com/users/DidierBesset/followers","following_url":"https://api.github.com/users/DidierBesset/following{/other_user}","gists_url":"https://api.github.com/users/DidierBesset/gists{/gist_id}","starred_url":"https://api.github.com/users/DidierBesset/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DidierBesset/subscriptions","organizations_url":"https://api.github.com/users/DidierBesset/orgs","repos_url":"https://api.github.com/users/DidierBesset/repos","events_url":"https://api.github.com/users/DidierBesset/events{/privacy}","received_events_url":"https://api.github.com/users/DidierBesset/received_events","type":"User","site_admin":false},"body":"Proper handling of synchronized blocks","created_at":"2015-10-20T12:42:08Z","updated_at":"2015-10-20T12:58:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/445/commits","review_comments_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/445/comments","review_comment_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/SonarSource/sonar-java/issues/445/comments","statuses_url":"https://api.github.com/repos/SonarSource/sonar-java/statuses/caf24678ebd4693b7f56197e7502c7b1e0739312","head":{"label":"SonarSource:SONARJAVA-1339","ref":"SONARJAVA-1339","sha":"caf24678ebd4693b7f56197e7502c7b1e0739312","user":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"repo":{"id":5674787,"name":"sonar-java","full_name":"SonarSource/sonar-java","owner":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SonarSource/sonar-java","description":"","fork":false,"url":"https://api.github.com/repos/SonarSource/sonar-java","forks_url":"https://api.github.com/repos/SonarSource/sonar-java/forks","keys_url":"https://api.github.com/repos/SonarSource/sonar-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SonarSource/sonar-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SonarSource/sonar-java/teams","hooks_url":"https://api.github.com/repos/SonarSource/sonar-java/hooks","issue_events_url":"https://api.github.com/repos/SonarSource/sonar-java/issues/events{/number}","events_url":"https://api.github.com/repos/SonarSource/sonar-java/events","assignees_url":"https://api.github.com/repos/SonarSource/sonar-java/assignees{/user}","branches_url":"https://api.github.com/repos/SonarSource/sonar-java/branches{/branch}","tags_url":"https://api.github.com/repos/SonarSource/sonar-java/tags","blobs_url":"https://api.github.com/repos/SonarSource/sonar-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SonarSource/sonar-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SonarSource/sonar-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/SonarSource/sonar-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SonarSource/sonar-java/statuses/{sha}","languages_url":"https://api.github.com/repos/SonarSource/sonar-java/languages","stargazers_url":"https://api.github.com/repos/SonarSource/sonar-java/stargazers","contributors_url":"https://api.github.com/repos/SonarSource/sonar-java/contributors","subscribers_url":"https://api.github.com/repos/SonarSource/sonar-java/subscribers","subscription_url":"https://api.github.com/repos/SonarSource/sonar-java/subscription","commits_url":"https://api.github.com/repos/SonarSource/sonar-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/SonarSource/sonar-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/SonarSource/sonar-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/SonarSource/sonar-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/SonarSource/sonar-java/contents/{+path}","compare_url":"https://api.github.com/repos/SonarSource/sonar-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SonarSource/sonar-java/merges","archive_url":"https://api.github.com/repos/SonarSource/sonar-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SonarSource/sonar-java/downloads","issues_url":"https://api.github.com/repos/SonarSource/sonar-java/issues{/number}","pulls_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls{/number}","milestones_url":"https://api.github.com/repos/SonarSource/sonar-java/milestones{/number}","notifications_url":"https://api.github.com/repos/SonarSource/sonar-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SonarSource/sonar-java/labels{/name}","releases_url":"https://api.github.com/repos/SonarSource/sonar-java/releases{/id}","created_at":"2012-09-04T15:53:13Z","updated_at":"2015-10-07T15:01:34Z","pushed_at":"2015-10-20T12:55:45Z","git_url":"git://github.com/SonarSource/sonar-java.git","ssh_url":"git@github.com:SonarSource/sonar-java.git","clone_url":"https://github.com/SonarSource/sonar-java.git","svn_url":"https://github.com/SonarSource/sonar-java","homepage":"","size":45617,"stargazers_count":68,"watchers_count":68,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":99,"mirror_url":null,"open_issues_count":10,"forks":99,"open_issues":10,"watchers":68,"default_branch":"master"}},"base":{"label":"SonarSource:master","ref":"master","sha":"31345a6f611f4e023f5c523b94e7390abfb026da","user":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"repo":{"id":5674787,"name":"sonar-java","full_name":"SonarSource/sonar-java","owner":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SonarSource/sonar-java","description":"","fork":false,"url":"https://api.github.com/repos/SonarSource/sonar-java","forks_url":"https://api.github.com/repos/SonarSource/sonar-java/forks","keys_url":"https://api.github.com/repos/SonarSource/sonar-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SonarSource/sonar-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SonarSource/sonar-java/teams","hooks_url":"https://api.github.com/repos/SonarSource/sonar-java/hooks","issue_events_url":"https://api.github.com/repos/SonarSource/sonar-java/issues/events{/number}","events_url":"https://api.github.com/repos/SonarSource/sonar-java/events","assignees_url":"https://api.github.com/repos/SonarSource/sonar-java/assignees{/user}","branches_url":"https://api.github.com/repos/SonarSource/sonar-java/branches{/branch}","tags_url":"https://api.github.com/repos/SonarSource/sonar-java/tags","blobs_url":"https://api.github.com/repos/SonarSource/sonar-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SonarSource/sonar-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SonarSource/sonar-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/SonarSource/sonar-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SonarSource/sonar-java/statuses/{sha}","languages_url":"https://api.github.com/repos/SonarSource/sonar-java/languages","stargazers_url":"https://api.github.com/repos/SonarSource/sonar-java/stargazers","contributors_url":"https://api.github.com/repos/SonarSource/sonar-java/contributors","subscribers_url":"https://api.github.com/repos/SonarSource/sonar-java/subscribers","subscription_url":"https://api.github.com/repos/SonarSource/sonar-java/subscription","commits_url":"https://api.github.com/repos/SonarSource/sonar-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/SonarSource/sonar-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/SonarSource/sonar-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/SonarSource/sonar-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/SonarSource/sonar-java/contents/{+path}","compare_url":"https://api.github.com/repos/SonarSource/sonar-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SonarSource/sonar-java/merges","archive_url":"https://api.github.com/repos/SonarSource/sonar-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SonarSource/sonar-java/downloads","issues_url":"https://api.github.com/repos/SonarSource/sonar-java/issues{/number}","pulls_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls{/number}","milestones_url":"https://api.github.com/repos/SonarSource/sonar-java/milestones{/number}","notifications_url":"https://api.github.com/repos/SonarSource/sonar-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SonarSource/sonar-java/labels{/name}","releases_url":"https://api.github.com/repos/SonarSource/sonar-java/releases{/id}","created_at":"2012-09-04T15:53:13Z","updated_at":"2015-10-07T15:01:34Z","pushed_at":"2015-10-20T12:55:45Z","git_url":"git://github.com/SonarSource/sonar-java.git","ssh_url":"git@github.com:SonarSource/sonar-java.git","clone_url":"https://github.com/SonarSource/sonar-java.git","svn_url":"https://github.com/SonarSource/sonar-java","homepage":"","size":45617,"stargazers_count":68,"watchers_count":68,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":99,"mirror_url":null,"open_issues_count":10,"forks":99,"open_issues":10,"watchers":68,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/445"},"html":{"href":"https://github.com/SonarSource/sonar-java/pull/445"},"issue":{"href":"https://api.github.com/repos/SonarSource/sonar-java/issues/445"},"comments":{"href":"https://api.github.com/repos/SonarSource/sonar-java/issues/445/comments"},"review_comments":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/445/comments"},"review_comment":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/445/commits"},"statuses":{"href":"https://api.github.com/repos/SonarSource/sonar-java/statuses/caf24678ebd4693b7f56197e7502c7b1e0739312"}}}}