{"action":"created","comment":{"url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/comments/24070680","id":24070680,"diff_hunk":"@@ -0,0 +1,114 @@\n+/*\n+ * SonarQube Java\n+ * Copyright (C) 2012 SonarSource\n+ * dev@sonar.codehaus.org\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.checks;\n+\n+import com.google.common.collect.ImmutableList;\n+import org.apache.commons.lang.BooleanUtils;\n+import org.sonar.check.BelongsToProfile;\n+import org.sonar.check.Priority;\n+import org.sonar.check.Rule;\n+import org.sonar.java.model.declaration.MethodTreeImpl;\n+import org.sonar.java.resolve.Symbol;\n+import org.sonar.plugins.java.api.tree.AssignmentExpressionTree;\n+import org.sonar.plugins.java.api.tree.BaseTreeVisitor;\n+import org.sonar.plugins.java.api.tree.ClassTree;\n+import org.sonar.plugins.java.api.tree.ExpressionTree;\n+import org.sonar.plugins.java.api.tree.IdentifierTree;\n+import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;\n+import org.sonar.plugins.java.api.tree.MethodTree;\n+import org.sonar.plugins.java.api.tree.Tree;\n+import org.sonar.plugins.java.api.tree.Tree.Kind;\n+\n+import java.util.List;\n+\n+@Rule(\n+  key = \"S2165\",\n+  priority = Priority.MAJOR,\n+  tags = {\"clumsy\", \"performance\"})\n+@BelongsToProfile(title = \"Sonar way\", priority = Priority.MAJOR)\n+public class FinalizeFieldsSetCheck extends SubscriptionBaseVisitor {\n+\n+  @Override\n+  public List<Kind> nodesToVisit() {\n+    return ImmutableList.of(Kind.METHOD);\n+  }\n+\n+  @Override\n+  public void visitNode(Tree tree) {\n+    MethodTree methodTree = (MethodTree) tree;\n+    if (isFinalizeDeclaration(methodTree)) {\n+      methodTree.accept(new AssignmentVisitor());\n+    }\n+  }\n+\n+  private boolean isFinalizeDeclaration(MethodTree tree) {\n+    return hasSemantic() && isMethodNamedFinalize(tree) && isOverriding(tree) && hasNoParameters(tree);\n+  }\n+\n+  private boolean isMethodNamedFinalize(MethodTree tree) {\n+    return \"finalize\".equals(tree.simpleName().name());\n+  }\n+\n+  private boolean isOverriding(MethodTree tree) {\n+    return BooleanUtils.isTrue(((MethodTreeImpl) tree).isOverriding());\n+  }\n+\n+  private boolean hasNoParameters(MethodTree tree) {\n+    return tree.parameters().isEmpty();\n+  }\n+\n+  private class AssignmentVisitor extends BaseTreeVisitor {\n+    @Override\n+    public void visitClass(ClassTree tree) {\n+      // Do not visit inner classes as their methods will be visited by main visitor\n+    }\n+\n+    @Override\n+    public void visitAssignmentExpression(AssignmentExpressionTree tree) {\n+      if (isFieldAssignment(tree) && isNullAssignment(tree)) {\n+        addIssue(tree, \"Remove this nullification of \\\"\" + getFieldName(tree) + \"\\\".\");\n+      }\n+    }\n+\n+    private boolean isFieldAssignment(AssignmentExpressionTree tree) {\n+      ExpressionTree variable = tree.variable();\n+      if (variable.is(Kind.MEMBER_SELECT)) {\n+        variable = ((MemberSelectExpressionTree) variable).expression();\n+      }\n+      if (variable.is(Kind.IDENTIFIER)) {\n+        Symbol variableSymbol = getSemanticModel().getReference((IdentifierTree) variable);\n+        return variableSymbol.owner().isKind(Symbol.TYP);","path":"java-checks/src/main/java/org/sonar/java/checks/FinalizeFieldsSetCheck.java","position":97,"original_position":97,"commit_id":"c21043a396b88e792d4c4fa682ec020969161569","original_commit_id":"c21043a396b88e792d4c4fa682ec020969161569","user":{"login":"benzonico","id":1199708,"avatar_url":"https://avatars.githubusercontent.com/u/1199708?v=3","gravatar_id":"","url":"https://api.github.com/users/benzonico","html_url":"https://github.com/benzonico","followers_url":"https://api.github.com/users/benzonico/followers","following_url":"https://api.github.com/users/benzonico/following{/other_user}","gists_url":"https://api.github.com/users/benzonico/gists{/gist_id}","starred_url":"https://api.github.com/users/benzonico/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benzonico/subscriptions","organizations_url":"https://api.github.com/users/benzonico/orgs","repos_url":"https://api.github.com/users/benzonico/repos","events_url":"https://api.github.com/users/benzonico/events{/privacy}","received_events_url":"https://api.github.com/users/benzonico/received_events","type":"User","site_admin":false},"body":"This is not sufficient if you have an expression refering a field of another instance  or from another class : \r\nclass A  {\r\n  private int field;\r\n  protected void finalize() {\r\n    new A().field = null;\r\n    new B().field = null;\r\n  }\r\n}","created_at":"2015-02-04T09:14:52Z","updated_at":"2015-02-04T09:14:52Z","html_url":"https://github.com/SonarSource/sonar-java/pull/41#discussion_r24070680","pull_request_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/41","_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/comments/24070680"},"html":{"href":"https://github.com/SonarSource/sonar-java/pull/41#discussion_r24070680"},"pull_request":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/41"}}},"pull_request":{"url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/41","id":28565406,"html_url":"https://github.com/SonarSource/sonar-java/pull/41","diff_url":"https://github.com/SonarSource/sonar-java/pull/41.diff","patch_url":"https://github.com/SonarSource/sonar-java/pull/41.patch","issue_url":"https://api.github.com/repos/SonarSource/sonar-java/issues/41","number":41,"state":"open","locked":false,"title":"SONARJAVA-867 RSPEC-2165 Finalize fields set check","user":{"login":"m-g-sonar","id":10588758,"avatar_url":"https://avatars.githubusercontent.com/u/10588758?v=3","gravatar_id":"","url":"https://api.github.com/users/m-g-sonar","html_url":"https://github.com/m-g-sonar","followers_url":"https://api.github.com/users/m-g-sonar/followers","following_url":"https://api.github.com/users/m-g-sonar/following{/other_user}","gists_url":"https://api.github.com/users/m-g-sonar/gists{/gist_id}","starred_url":"https://api.github.com/users/m-g-sonar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m-g-sonar/subscriptions","organizations_url":"https://api.github.com/users/m-g-sonar/orgs","repos_url":"https://api.github.com/users/m-g-sonar/repos","events_url":"https://api.github.com/users/m-g-sonar/events{/privacy}","received_events_url":"https://api.github.com/users/m-g-sonar/received_events","type":"User","site_admin":false},"body":"Finalize fields set check :\r\n- Added new rule to check `null` assignment on fields in `finalize` methods.\r\n- There is no point in setting class fields to `null` in a finalizer. If this this is a hint to the garbage collector, it is unnecessary - the object will be garbage collected anyway - and may actually cause extra work for the garbage collector.","created_at":"2015-02-03T16:17:54Z","updated_at":"2015-02-04T09:14:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c2e0682ac216bdca97a359b5eeaa4dd2535428e8","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/41/commits","review_comments_url":"https://api.github.com/repos/SonarSource/sonar-java/pulls/41/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/41/comments","statuses_url":"https://api.github.com/repos/SonarSource/sonar-java/statuses/c21043a396b88e792d4c4fa682ec020969161569","head":{"label":"m-g-sonar:SONARJAVA-867_RSPEC-2165_Finalize_fields_set_check","ref":"SONARJAVA-867_RSPEC-2165_Finalize_fields_set_check","sha":"c21043a396b88e792d4c4fa682ec020969161569","user":{"login":"m-g-sonar","id":10588758,"avatar_url":"https://avatars.githubusercontent.com/u/10588758?v=3","gravatar_id":"","url":"https://api.github.com/users/m-g-sonar","html_url":"https://github.com/m-g-sonar","followers_url":"https://api.github.com/users/m-g-sonar/followers","following_url":"https://api.github.com/users/m-g-sonar/following{/other_user}","gists_url":"https://api.github.com/users/m-g-sonar/gists{/gist_id}","starred_url":"https://api.github.com/users/m-g-sonar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m-g-sonar/subscriptions","organizations_url":"https://api.github.com/users/m-g-sonar/orgs","repos_url":"https://api.github.com/users/m-g-sonar/repos","events_url":"https://api.github.com/users/m-g-sonar/events{/privacy}","received_events_url":"https://api.github.com/users/m-g-sonar/received_events","type":"User","site_admin":false},"repo":{"id":29524789,"name":"sonar-java","full_name":"m-g-sonar/sonar-java","owner":{"login":"m-g-sonar","id":10588758,"avatar_url":"https://avatars.githubusercontent.com/u/10588758?v=3","gravatar_id":"","url":"https://api.github.com/users/m-g-sonar","html_url":"https://github.com/m-g-sonar","followers_url":"https://api.github.com/users/m-g-sonar/followers","following_url":"https://api.github.com/users/m-g-sonar/following{/other_user}","gists_url":"https://api.github.com/users/m-g-sonar/gists{/gist_id}","starred_url":"https://api.github.com/users/m-g-sonar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m-g-sonar/subscriptions","organizations_url":"https://api.github.com/users/m-g-sonar/orgs","repos_url":"https://api.github.com/users/m-g-sonar/repos","events_url":"https://api.github.com/users/m-g-sonar/events{/privacy}","received_events_url":"https://api.github.com/users/m-g-sonar/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/m-g-sonar/sonar-java","description":"","fork":true,"url":"https://api.github.com/repos/m-g-sonar/sonar-java","forks_url":"https://api.github.com/repos/m-g-sonar/sonar-java/forks","keys_url":"https://api.github.com/repos/m-g-sonar/sonar-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/m-g-sonar/sonar-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/m-g-sonar/sonar-java/teams","hooks_url":"https://api.github.com/repos/m-g-sonar/sonar-java/hooks","issue_events_url":"https://api.github.com/repos/m-g-sonar/sonar-java/issues/events{/number}","events_url":"https://api.github.com/repos/m-g-sonar/sonar-java/events","assignees_url":"https://api.github.com/repos/m-g-sonar/sonar-java/assignees{/user}","branches_url":"https://api.github.com/repos/m-g-sonar/sonar-java/branches{/branch}","tags_url":"https://api.github.com/repos/m-g-sonar/sonar-java/tags","blobs_url":"https://api.github.com/repos/m-g-sonar/sonar-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/m-g-sonar/sonar-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/m-g-sonar/sonar-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/m-g-sonar/sonar-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/m-g-sonar/sonar-java/statuses/{sha}","languages_url":"https://api.github.com/repos/m-g-sonar/sonar-java/languages","stargazers_url":"https://api.github.com/repos/m-g-sonar/sonar-java/stargazers","contributors_url":"https://api.github.com/repos/m-g-sonar/sonar-java/contributors","subscribers_url":"https://api.github.com/repos/m-g-sonar/sonar-java/subscribers","subscription_url":"https://api.github.com/repos/m-g-sonar/sonar-java/subscription","commits_url":"https://api.github.com/repos/m-g-sonar/sonar-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/m-g-sonar/sonar-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/m-g-sonar/sonar-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/m-g-sonar/sonar-java/issues/comments/{number}","contents_url":"https://api.github.com/repos/m-g-sonar/sonar-java/contents/{+path}","compare_url":"https://api.github.com/repos/m-g-sonar/sonar-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/m-g-sonar/sonar-java/merges","archive_url":"https://api.github.com/repos/m-g-sonar/sonar-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/m-g-sonar/sonar-java/downloads","issues_url":"https://api.github.com/repos/m-g-sonar/sonar-java/issues{/number}","pulls_url":"https://api.github.com/repos/m-g-sonar/sonar-java/pulls{/number}","milestones_url":"https://api.github.com/repos/m-g-sonar/sonar-java/milestones{/number}","notifications_url":"https://api.github.com/repos/m-g-sonar/sonar-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/m-g-sonar/sonar-java/labels{/name}","releases_url":"https://api.github.com/repos/m-g-sonar/sonar-java/releases{/id}","created_at":"2015-01-20T10:39:06Z","updated_at":"2015-01-20T14:47:13Z","pushed_at":"2015-02-04T09:03:02Z","git_url":"git://github.com/m-g-sonar/sonar-java.git","ssh_url":"git@github.com:m-g-sonar/sonar-java.git","clone_url":"https://github.com/m-g-sonar/sonar-java.git","svn_url":"https://github.com/m-g-sonar/sonar-java","homepage":"","size":9232,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"SonarSource:master","ref":"master","sha":"228f920f2644bb04e2a3a3a68d9f5df7b7eed6c9","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-02-03T16:14:59Z","pushed_at":"2015-02-03T16:14:58Z","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":24929,"stargazers_count":30,"watchers_count":30,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":58,"mirror_url":null,"open_issues_count":6,"forks":58,"open_issues":6,"watchers":30,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/41"},"html":{"href":"https://github.com/SonarSource/sonar-java/pull/41"},"issue":{"href":"https://api.github.com/repos/SonarSource/sonar-java/issues/41"},"comments":{"href":"https://api.github.com/repos/SonarSource/sonar-java/issues/41/comments"},"review_comments":{"href":"https://api.github.com/repos/SonarSource/sonar-java/pulls/41/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/41/commits"},"statuses":{"href":"https://api.github.com/repos/SonarSource/sonar-java/statuses/c21043a396b88e792d4c4fa682ec020969161569"}}}}