{"action":"created","comment":{"url":"https://api.github.com/repos/openhab/static-code-analysis/pulls/comments/181328488","pull_request_review_id":111934821,"id":181328488,"diff_hunk":"@@ -0,0 +1,125 @@\n+/**\n+ * Copyright (c) 2010-2018 by the respective copyright holders.\n+ *\n+ * All rights reserved. This program and the accompanying materials\n+ * are made available under the terms of the Eclipse Public License v1.0\n+ * which accompanies this distribution, and is available at\n+ * http://www.eclipse.org/legal/epl-v10.html\n+ */\n+package org.openhab.tools.analysis.pmd;\n+\n+import java.util.logging.Level;\n+import java.util.logging.Logger;\n+\n+import net.sourceforge.pmd.lang.ast.Node;\n+import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;\n+import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;\n+import net.sourceforge.pmd.lang.java.ast.ASTType;\n+import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;\n+import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;\n+\n+/**\n+ * Checks if a logger other than the one provided by slf4j is used\n+ *\n+ * @author Lyubomir Papazov - Initial contribution\n+ *\n+ */\n+public class UseSLF4JLoggerRule extends AbstractJavaRule {\n+\n+    //There is a very similar check created by PMD that checks if only one logger is used:\n+    //https://github.com/pmd/pmd/blob/pmd/5.8.x/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/logging/MoreThanOneLoggerRule.java\n+\n+    private static final String lOG_LITERAL = \"Log\";\n+    private static final String LOGGER_LITERAL = \"Logger\";\n+    private static final String CLASS_FOR_NAME_FAILED_WARNING = \"Unable to create class object for that name\";\n+\n+    private static final Class<?> LOG4J_LOGGER;\n+\n+    private static final Class<?> JAVA_LOGGER;\n+\n+    private static final Class<?> SLF4J_LOGGER;\n+\n+    private static final Class<?> COMMON_LOGGING_LOG;\n+\n+    private static final Class<?> LOGBACK_LOGGER;\n+\n+    private static Logger logger = Logger.getLogger(UseSLF4JLoggerRule.class.getName());\n+\n+    static {\n+        Class<?> clazz;\n+        try {\n+            clazz = Class.forName(\"org.apache.log4j.Logger\");\n+        } catch (Throwable t) {\n+            logger.log(Level.WARNING, CLASS_FOR_NAME_FAILED_WARNING, t);\n+            clazz = null;\n+        }\n+        LOG4J_LOGGER = clazz;\n+        try {\n+            clazz = Class.forName(\"java.util.logging.Logger\");\n+        } catch (Throwable t) {\n+            logger.log(Level.WARNING, CLASS_FOR_NAME_FAILED_WARNING, t);\n+            clazz = null;\n+        }\n+        JAVA_LOGGER = clazz;\n+        try {\n+            clazz = Class.forName(\"org.slf4j.Logger\");\n+        } catch (Throwable t) {\n+            logger.log(Level.WARNING, CLASS_FOR_NAME_FAILED_WARNING, t);\n+            clazz = null;\n+        }\n+        SLF4J_LOGGER = clazz;\n+        try {\n+            clazz = Class.forName(\"org.apache.commons.logging.Log\");\n+        } catch (Throwable t) {\n+            logger.log(Level.WARNING, CLASS_FOR_NAME_FAILED_WARNING, t);\n+            clazz = null;\n+        }\n+        COMMON_LOGGING_LOG = clazz;\n+        try {\n+            clazz = Class.forName(\"ch.qos.logback.classic.Logger\");\n+        } catch (Throwable t) {\n+            logger.log(Level.WARNING, CLASS_FOR_NAME_FAILED_WARNING, t);\n+            clazz = null;\n+        }\n+        LOGBACK_LOGGER = clazz;\n+    }\n+\n+    @Override\n+    public Object visit(ASTVariableDeclarator node, Object data) {\n+        Node type = node.jjtGetParent().getFirstChildOfType(ASTType.class);\n+        if (type != null) {\n+            Node reftypeNode = type.jjtGetChild(0);\n+            if (reftypeNode instanceof ASTReferenceType) {\n+                Node classOrIntType = reftypeNode.jjtGetChild(0);\n+                if (classOrIntType instanceof ASTClassOrInterfaceType) {\n+                    Class<?> clazzType = ((ASTClassOrInterfaceType) classOrIntType).getType();\n+                    if (clazzType != null && clazzType.equals(SLF4J_LOGGER)) {\n+                        return super.visit(node, data);\n+                    }\n+                    // If the clazzType is the same as the calss of any of the loggers that we don't want\n+                    // or if the classOrInterfeaceType name ends with \"Logger\" or \"Log\".\n+                    // The classOrInterfeaceType could use the full class name and that's why we check if it ends with \"Logger\" or \"Log\"\n+                    //\n+                    // Similar approach is used here:\n+                    // https://github.com/pmd/pmd/blob/pmd/5.8.x/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/logging/MoreThanOneLoggerRule.java#L99\n+                    if (checkDifferentLogger(clazzType) || classOrIntTypeIsLogger(classOrIntType.getImage())) {\n+                        addViolation(data, node);\n+                    }\n+                }\n+            }\n+        }\n+        return super.visit(node, data);\n+    }\n+\n+    private boolean classOrIntTypeIsLogger(String image) {\n+        return image.endsWith(lOG_LITERAL) || image.endsWith(LOGGER_LITERAL);\n+    }\n+\n+    private boolean checkDifferentLogger(Class<?> clazzType) {\n+        if (clazzType == null) {\n+            return false;\n+        }\n+        return clazzType.equals(LOG4J_LOGGER) || clazzType.equals(JAVA_LOGGER) || clazzType.equals(COMMON_LOGGING_LOG) || clazzType.equals(LOGBACK_LOGGER);\n+    }\n+\n+}","path":"custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java","position":125,"original_position":125,"commit_id":"6eb8fbbf8b2bcff01401a150eec5e9fce8b267bf","original_commit_id":"6eb8fbbf8b2bcff01401a150eec5e9fce8b267bf","user":{"login":"martinvw","id":2625279,"avatar_url":"https://avatars3.githubusercontent.com/u/2625279?v=4","gravatar_id":"","url":"https://api.github.com/users/martinvw","html_url":"https://github.com/martinvw","followers_url":"https://api.github.com/users/martinvw/followers","following_url":"https://api.github.com/users/martinvw/following{/other_user}","gists_url":"https://api.github.com/users/martinvw/gists{/gist_id}","starred_url":"https://api.github.com/users/martinvw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martinvw/subscriptions","organizations_url":"https://api.github.com/users/martinvw/orgs","repos_url":"https://api.github.com/users/martinvw/repos","events_url":"https://api.github.com/users/martinvw/events{/privacy}","received_events_url":"https://api.github.com/users/martinvw/received_events","type":"User","site_admin":false},"body":"Newlines","created_at":"2018-04-13T09:08:52Z","updated_at":"2018-04-13T09:10:18Z","html_url":"https://github.com/openhab/static-code-analysis/pull/291#discussion_r181328488","pull_request_url":"https://api.github.com/repos/openhab/static-code-analysis/pulls/291","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/openhab/static-code-analysis/pulls/comments/181328488"},"html":{"href":"https://github.com/openhab/static-code-analysis/pull/291#discussion_r181328488"},"pull_request":{"href":"https://api.github.com/repos/openhab/static-code-analysis/pulls/291"}}},"pull_request":{"url":"https://api.github.com/repos/openhab/static-code-analysis/pulls/291","id":181414683,"html_url":"https://github.com/openhab/static-code-analysis/pull/291","diff_url":"https://github.com/openhab/static-code-analysis/pull/291.diff","patch_url":"https://github.com/openhab/static-code-analysis/pull/291.patch","issue_url":"https://api.github.com/repos/openhab/static-code-analysis/issues/291","number":291,"state":"open","locked":false,"title":"add pmd check to report if a logger different than slf4j is used","user":{"login":"lpapazow","id":7544898,"avatar_url":"https://avatars0.githubusercontent.com/u/7544898?v=4","gravatar_id":"","url":"https://api.github.com/users/lpapazow","html_url":"https://github.com/lpapazow","followers_url":"https://api.github.com/users/lpapazow/followers","following_url":"https://api.github.com/users/lpapazow/following{/other_user}","gists_url":"https://api.github.com/users/lpapazow/gists{/gist_id}","starred_url":"https://api.github.com/users/lpapazow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lpapazow/subscriptions","organizations_url":"https://api.github.com/users/lpapazow/orgs","repos_url":"https://api.github.com/users/lpapazow/repos","events_url":"https://api.github.com/users/lpapazow/events{/privacy}","received_events_url":"https://api.github.com/users/lpapazow/received_events","type":"User","site_admin":false},"body":"Create a pmd check that reports if a logger class, different than org.slf4j.logger.Logger is used.\r\n\r\nSigned-off-by: Lyubomir V. Papazov <lpapazow@gmail.com>","created_at":"2018-04-13T08:08:45Z","updated_at":"2018-04-13T09:10:18Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ac7f75d183f48fba721347fa67071f54a9743b0e","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/openhab/static-code-analysis/pulls/291/commits","review_comments_url":"https://api.github.com/repos/openhab/static-code-analysis/pulls/291/comments","review_comment_url":"https://api.github.com/repos/openhab/static-code-analysis/pulls/comments{/number}","comments_url":"https://api.github.com/repos/openhab/static-code-analysis/issues/291/comments","statuses_url":"https://api.github.com/repos/openhab/static-code-analysis/statuses/6eb8fbbf8b2bcff01401a150eec5e9fce8b267bf","head":{"label":"MusalaSoft:pmd-slf4j-warning","ref":"pmd-slf4j-warning","sha":"6eb8fbbf8b2bcff01401a150eec5e9fce8b267bf","user":{"login":"MusalaSoft","id":19668691,"avatar_url":"https://avatars0.githubusercontent.com/u/19668691?v=4","gravatar_id":"","url":"https://api.github.com/users/MusalaSoft","html_url":"https://github.com/MusalaSoft","followers_url":"https://api.github.com/users/MusalaSoft/followers","following_url":"https://api.github.com/users/MusalaSoft/following{/other_user}","gists_url":"https://api.github.com/users/MusalaSoft/gists{/gist_id}","starred_url":"https://api.github.com/users/MusalaSoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MusalaSoft/subscriptions","organizations_url":"https://api.github.com/users/MusalaSoft/orgs","repos_url":"https://api.github.com/users/MusalaSoft/repos","events_url":"https://api.github.com/users/MusalaSoft/events{/privacy}","received_events_url":"https://api.github.com/users/MusalaSoft/received_events","type":"Organization","site_admin":false},"repo":{"id":84063676,"name":"static-code-analysis","full_name":"MusalaSoft/static-code-analysis","owner":{"login":"MusalaSoft","id":19668691,"avatar_url":"https://avatars0.githubusercontent.com/u/19668691?v=4","gravatar_id":"","url":"https://api.github.com/users/MusalaSoft","html_url":"https://github.com/MusalaSoft","followers_url":"https://api.github.com/users/MusalaSoft/followers","following_url":"https://api.github.com/users/MusalaSoft/following{/other_user}","gists_url":"https://api.github.com/users/MusalaSoft/gists{/gist_id}","starred_url":"https://api.github.com/users/MusalaSoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MusalaSoft/subscriptions","organizations_url":"https://api.github.com/users/MusalaSoft/orgs","repos_url":"https://api.github.com/users/MusalaSoft/repos","events_url":"https://api.github.com/users/MusalaSoft/events{/privacy}","received_events_url":"https://api.github.com/users/MusalaSoft/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/MusalaSoft/static-code-analysis","description":"Maven tooling for static code analysis","fork":true,"url":"https://api.github.com/repos/MusalaSoft/static-code-analysis","forks_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/forks","keys_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/keys{/key_id}","collaborators_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/teams","hooks_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/hooks","issue_events_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/issues/events{/number}","events_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/events","assignees_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/assignees{/user}","branches_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/branches{/branch}","tags_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/tags","blobs_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/git/refs{/sha}","trees_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/git/trees{/sha}","statuses_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/statuses/{sha}","languages_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/languages","stargazers_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/stargazers","contributors_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/contributors","subscribers_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/subscribers","subscription_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/subscription","commits_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/commits{/sha}","git_commits_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/git/commits{/sha}","comments_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/comments{/number}","issue_comment_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/issues/comments{/number}","contents_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/contents/{+path}","compare_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/compare/{base}...{head}","merges_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/merges","archive_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/downloads","issues_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/issues{/number}","pulls_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/pulls{/number}","milestones_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/milestones{/number}","notifications_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/labels{/name}","releases_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/releases{/id}","deployments_url":"https://api.github.com/repos/MusalaSoft/static-code-analysis/deployments","created_at":"2017-03-06T11:07:28Z","updated_at":"2017-03-06T11:07:30Z","pushed_at":"2018-04-13T08:16:31Z","git_url":"git://github.com/MusalaSoft/static-code-analysis.git","ssh_url":"git@github.com:MusalaSoft/static-code-analysis.git","clone_url":"https://github.com/MusalaSoft/static-code-analysis.git","svn_url":"https://github.com/MusalaSoft/static-code-analysis","homepage":null,"size":862,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"openhab:master","ref":"master","sha":"06273bc26d4f1764a473f6bbd0df7ea46a9b22f2","user":{"login":"openhab","id":1007353,"avatar_url":"https://avatars0.githubusercontent.com/u/1007353?v=4","gravatar_id":"","url":"https://api.github.com/users/openhab","html_url":"https://github.com/openhab","followers_url":"https://api.github.com/users/openhab/followers","following_url":"https://api.github.com/users/openhab/following{/other_user}","gists_url":"https://api.github.com/users/openhab/gists{/gist_id}","starred_url":"https://api.github.com/users/openhab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openhab/subscriptions","organizations_url":"https://api.github.com/users/openhab/orgs","repos_url":"https://api.github.com/users/openhab/repos","events_url":"https://api.github.com/users/openhab/events{/privacy}","received_events_url":"https://api.github.com/users/openhab/received_events","type":"Organization","site_admin":false},"repo":{"id":83811108,"name":"static-code-analysis","full_name":"openhab/static-code-analysis","owner":{"login":"openhab","id":1007353,"avatar_url":"https://avatars0.githubusercontent.com/u/1007353?v=4","gravatar_id":"","url":"https://api.github.com/users/openhab","html_url":"https://github.com/openhab","followers_url":"https://api.github.com/users/openhab/followers","following_url":"https://api.github.com/users/openhab/following{/other_user}","gists_url":"https://api.github.com/users/openhab/gists{/gist_id}","starred_url":"https://api.github.com/users/openhab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openhab/subscriptions","organizations_url":"https://api.github.com/users/openhab/orgs","repos_url":"https://api.github.com/users/openhab/repos","events_url":"https://api.github.com/users/openhab/events{/privacy}","received_events_url":"https://api.github.com/users/openhab/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/openhab/static-code-analysis","description":"Maven tooling for static code analysis","fork":false,"url":"https://api.github.com/repos/openhab/static-code-analysis","forks_url":"https://api.github.com/repos/openhab/static-code-analysis/forks","keys_url":"https://api.github.com/repos/openhab/static-code-analysis/keys{/key_id}","collaborators_url":"https://api.github.com/repos/openhab/static-code-analysis/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/openhab/static-code-analysis/teams","hooks_url":"https://api.github.com/repos/openhab/static-code-analysis/hooks","issue_events_url":"https://api.github.com/repos/openhab/static-code-analysis/issues/events{/number}","events_url":"https://api.github.com/repos/openhab/static-code-analysis/events","assignees_url":"https://api.github.com/repos/openhab/static-code-analysis/assignees{/user}","branches_url":"https://api.github.com/repos/openhab/static-code-analysis/branches{/branch}","tags_url":"https://api.github.com/repos/openhab/static-code-analysis/tags","blobs_url":"https://api.github.com/repos/openhab/static-code-analysis/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/openhab/static-code-analysis/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/openhab/static-code-analysis/git/refs{/sha}","trees_url":"https://api.github.com/repos/openhab/static-code-analysis/git/trees{/sha}","statuses_url":"https://api.github.com/repos/openhab/static-code-analysis/statuses/{sha}","languages_url":"https://api.github.com/repos/openhab/static-code-analysis/languages","stargazers_url":"https://api.github.com/repos/openhab/static-code-analysis/stargazers","contributors_url":"https://api.github.com/repos/openhab/static-code-analysis/contributors","subscribers_url":"https://api.github.com/repos/openhab/static-code-analysis/subscribers","subscription_url":"https://api.github.com/repos/openhab/static-code-analysis/subscription","commits_url":"https://api.github.com/repos/openhab/static-code-analysis/commits{/sha}","git_commits_url":"https://api.github.com/repos/openhab/static-code-analysis/git/commits{/sha}","comments_url":"https://api.github.com/repos/openhab/static-code-analysis/comments{/number}","issue_comment_url":"https://api.github.com/repos/openhab/static-code-analysis/issues/comments{/number}","contents_url":"https://api.github.com/repos/openhab/static-code-analysis/contents/{+path}","compare_url":"https://api.github.com/repos/openhab/static-code-analysis/compare/{base}...{head}","merges_url":"https://api.github.com/repos/openhab/static-code-analysis/merges","archive_url":"https://api.github.com/repos/openhab/static-code-analysis/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/openhab/static-code-analysis/downloads","issues_url":"https://api.github.com/repos/openhab/static-code-analysis/issues{/number}","pulls_url":"https://api.github.com/repos/openhab/static-code-analysis/pulls{/number}","milestones_url":"https://api.github.com/repos/openhab/static-code-analysis/milestones{/number}","notifications_url":"https://api.github.com/repos/openhab/static-code-analysis/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/openhab/static-code-analysis/labels{/name}","releases_url":"https://api.github.com/repos/openhab/static-code-analysis/releases{/id}","deployments_url":"https://api.github.com/repos/openhab/static-code-analysis/deployments","created_at":"2017-03-03T15:12:39Z","updated_at":"2018-04-13T07:01:43Z","pushed_at":"2018-04-13T08:57:28Z","git_url":"git://github.com/openhab/static-code-analysis.git","ssh_url":"git@github.com:openhab/static-code-analysis.git","clone_url":"https://github.com/openhab/static-code-analysis.git","svn_url":"https://github.com/openhab/static-code-analysis","homepage":null,"size":752,"stargazers_count":9,"watchers_count":9,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":11,"mirror_url":null,"archived":false,"open_issues_count":30,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":11,"open_issues":30,"watchers":9,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/openhab/static-code-analysis/pulls/291"},"html":{"href":"https://github.com/openhab/static-code-analysis/pull/291"},"issue":{"href":"https://api.github.com/repos/openhab/static-code-analysis/issues/291"},"comments":{"href":"https://api.github.com/repos/openhab/static-code-analysis/issues/291/comments"},"review_comments":{"href":"https://api.github.com/repos/openhab/static-code-analysis/pulls/291/comments"},"review_comment":{"href":"https://api.github.com/repos/openhab/static-code-analysis/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/openhab/static-code-analysis/pulls/291/commits"},"statuses":{"href":"https://api.github.com/repos/openhab/static-code-analysis/statuses/6eb8fbbf8b2bcff01401a150eec5e9fce8b267bf"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"martinvw"}}