diff --git a/pmd-scala/pmd-scala-checkstyle-suppressions.xml b/pmd-scala/pmd-scala-checkstyle-suppressions.xml new file mode 100644 index 0000000000..32d8cb105b --- /dev/null +++ b/pmd-scala/pmd-scala-checkstyle-suppressions.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/pmd-scala/pom.xml b/pmd-scala/pom.xml index 5d19352923..fc74802669 100644 --- a/pmd-scala/pom.xml +++ b/pmd-scala/pom.xml @@ -56,6 +56,14 @@ + + + org.apache.maven.plugins + maven-checkstyle-plugin + + pmd-scala-checkstyle-suppressions.xml + + diff --git a/pmd-scala/src/main/java/net/sourceforge/pmd/cpd/ScalaLanguage.java b/pmd-scala/src/main/java/net/sourceforge/pmd/cpd/ScalaLanguage.java index 2f34086344..94cc14b223 100644 --- a/pmd-scala/src/main/java/net/sourceforge/pmd/cpd/ScalaLanguage.java +++ b/pmd-scala/src/main/java/net/sourceforge/pmd/cpd/ScalaLanguage.java @@ -1,6 +1,7 @@ /** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ + package net.sourceforge.pmd.cpd; import org.sonar.plugins.scala.cpd.ScalaTokenizer; diff --git a/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ScalaLanguageModule.java b/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ScalaLanguageModule.java index 29e1af0beb..135b626ebd 100644 --- a/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ScalaLanguageModule.java +++ b/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ScalaLanguageModule.java @@ -1,6 +1,7 @@ /** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ + package net.sourceforge.pmd.lang.scala; import net.sourceforge.pmd.lang.BaseLanguageModule; diff --git a/pmd-scala/src/main/java/org/sonar/plugins/scala/cpd/ScalaTokenizer.java b/pmd-scala/src/main/java/org/sonar/plugins/scala/cpd/ScalaTokenizer.java index a28f1bc942..3ff303f195 100644 --- a/pmd-scala/src/main/java/org/sonar/plugins/scala/cpd/ScalaTokenizer.java +++ b/pmd-scala/src/main/java/org/sonar/plugins/scala/cpd/ScalaTokenizer.java @@ -17,19 +17,20 @@ * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ + package org.sonar.plugins.scala.cpd; import java.util.List; +import org.sonar.plugins.scala.compiler.Lexer; +import org.sonar.plugins.scala.compiler.Token; + import net.sourceforge.pmd.cpd.SourceCode; import net.sourceforge.pmd.cpd.TokenEntry; import net.sourceforge.pmd.cpd.Tokenizer; import net.sourceforge.pmd.cpd.Tokens; import net.sourceforge.pmd.lang.ast.TokenMgrError; -import org.sonar.plugins.scala.compiler.Lexer; -import org.sonar.plugins.scala.compiler.Token; - /** * Scala tokenizer for PMD CPD. * @@ -42,10 +43,9 @@ public final class ScalaTokenizer implements Tokenizer { try { Lexer lexer = new Lexer(); - List tokens = lexer.getTokensOfFile(filename); + List tokens = lexer.getTokensOfFile(filename); for (Token token : tokens) { - String tokenVal = - token.tokenVal() != null ? token.tokenVal() : Integer.toString(token.tokenType()); + String tokenVal = token.tokenVal() != null ? token.tokenVal() : Integer.toString(token.tokenType()); TokenEntry cpdToken = new TokenEntry(tokenVal, filename, token.line()); cpdTokens.add(cpdToken); @@ -53,11 +53,13 @@ public final class ScalaTokenizer implements Tokenizer { cpdTokens.add(TokenEntry.getEOF()); } catch (RuntimeException e) { e.printStackTrace(); - // Wrap exceptions of the Scala tokenizer in a TokenMgrError, so they are correctly handled - // when CPD is executed with the '--skipLexicalErrors' command line option + // Wrap exceptions of the Scala tokenizer in a TokenMgrError, so + // they are correctly handled + // when CPD is executed with the '--skipLexicalErrors' command line + // option throw new TokenMgrError( - "Lexical error in file " + filename + ". The scala tokenizer exited with error: " + e.getMessage(), - TokenMgrError.LEXICAL_ERROR); + "Lexical error in file " + filename + ". The scala tokenizer exited with error: " + e.getMessage(), + TokenMgrError.LEXICAL_ERROR); } } diff --git a/pmd-scala/src/main/java/org/sonar/plugins/scala/language/Comment.java b/pmd-scala/src/main/java/org/sonar/plugins/scala/language/Comment.java index bfbb992847..2f91c9a088 100644 --- a/pmd-scala/src/main/java/org/sonar/plugins/scala/language/Comment.java +++ b/pmd-scala/src/main/java/org/sonar/plugins/scala/language/Comment.java @@ -17,6 +17,7 @@ * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ + package org.sonar.plugins.scala.language; import java.io.IOException; @@ -28,92 +29,89 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import org.sonar.plugins.scala.util.StringUtils; /** - * This class implements a Scala comment and the computation - * of several base metrics for a comment. + * This class implements a Scala comment and the computation of several base + * metrics for a comment. * * @author Felix Müller * @since 0.1 */ public class Comment { - private final CommentType type; - private final List lines; + private final CommentType type; + private final List lines; - public Comment(String content, CommentType type) throws IOException { - lines = StringUtils.convertStringToListOfLines(content); - this.type = type; - } + public Comment(String content, CommentType type) throws IOException { + lines = StringUtils.convertStringToListOfLines(content); + this.type = type; + } - public int getNumberOfLines() { - return lines.size() - getNumberOfBlankLines() - getNumberOfCommentedOutLinesOfCode(); - } + public int getNumberOfLines() { + return lines.size() - getNumberOfBlankLines() - getNumberOfCommentedOutLinesOfCode(); + } - public int getNumberOfBlankLines() { - int numberOfBlankLines = 0; - for (String comment : lines) { - boolean isBlank = true; + public int getNumberOfBlankLines() { + int numberOfBlankLines = 0; + for (String comment : lines) { + boolean isBlank = true; - for (int i = 0; isBlank && i < comment.length(); i++) { - char character = comment.charAt(i); - if (!Character.isWhitespace(character) && character != '*' && character != '/') { - isBlank = false; + for (int i = 0; isBlank && i < comment.length(); i++) { + char character = comment.charAt(i); + if (!Character.isWhitespace(character) && character != '*' && character != '/') { + isBlank = false; + } + } + + if (isBlank) { + numberOfBlankLines++; + } } - } - - if (isBlank) { - numberOfBlankLines++; - } - } - return numberOfBlankLines; - } - - public int getNumberOfCommentedOutLinesOfCode() { - if (isDocComment()) { - return 0; + return numberOfBlankLines; } - int numberOfCommentedOutLinesOfCode = 0; - for (String line : lines) { - String strippedLine = org.apache.commons.lang3.StringUtils.strip(line, " /*"); - if (CodeDetector.hasDetectedCode(strippedLine)) { - numberOfCommentedOutLinesOfCode++; - } - } - return numberOfCommentedOutLinesOfCode; - } + public int getNumberOfCommentedOutLinesOfCode() { + if (isDocComment()) { + return 0; + } - public boolean isDocComment() { - return type == CommentType.DOC; - } - - public boolean isHeaderComment() { - return type == CommentType.HEADER; - } - - @Override - public int hashCode() { - return new HashCodeBuilder().append(type).append(lines).toHashCode(); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Comment)) { - return false; + int numberOfCommentedOutLinesOfCode = 0; + for (String line : lines) { + String strippedLine = org.apache.commons.lang3.StringUtils.strip(line, " /*"); + if (CodeDetector.hasDetectedCode(strippedLine)) { + numberOfCommentedOutLinesOfCode++; + } + } + return numberOfCommentedOutLinesOfCode; } - Comment other = (Comment) obj; - return new EqualsBuilder().append(type, other.type).append(lines, other.lines).isEquals(); - } + public boolean isDocComment() { + return type == CommentType.DOC; + } - @Override - public String toString() { - final String firstLine = lines.isEmpty() ? "" : lines.get(0); - final String lastLine = lines.isEmpty() ? "" : lines.get(lines.size() - 1); - return new ToStringBuilder(this).append("type", type) - .append("firstLine", firstLine) - .append("lastLine", lastLine) - .append("numberOfLines", getNumberOfLines()) - .append("numberOfCommentedOutLinesOfCode", getNumberOfCommentedOutLinesOfCode()) - .toString(); - } -} \ No newline at end of file + public boolean isHeaderComment() { + return type == CommentType.HEADER; + } + + @Override + public int hashCode() { + return new HashCodeBuilder().append(type).append(lines).toHashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Comment)) { + return false; + } + + Comment other = (Comment) obj; + return new EqualsBuilder().append(type, other.type).append(lines, other.lines).isEquals(); + } + + @Override + public String toString() { + final String firstLine = lines.isEmpty() ? "" : lines.get(0); + final String lastLine = lines.isEmpty() ? "" : lines.get(lines.size() - 1); + return new ToStringBuilder(this).append("type", type).append("firstLine", firstLine) + .append("lastLine", lastLine).append("numberOfLines", getNumberOfLines()) + .append("numberOfCommentedOutLinesOfCode", getNumberOfCommentedOutLinesOfCode()).toString(); + } +} diff --git a/pmd-scala/src/main/java/org/sonar/plugins/scala/language/CommentType.java b/pmd-scala/src/main/java/org/sonar/plugins/scala/language/CommentType.java index bf0b19db4c..df3710828c 100644 --- a/pmd-scala/src/main/java/org/sonar/plugins/scala/language/CommentType.java +++ b/pmd-scala/src/main/java/org/sonar/plugins/scala/language/CommentType.java @@ -17,18 +17,17 @@ * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ + package org.sonar.plugins.scala.language; /** - * This enum is a helper to distinguish between the - * different types of comments in Sonar. + * This enum is a helper to distinguish between the different types of comments + * in Sonar. * * @author Felix Müller * @since 0.1 */ public enum CommentType { - NORMAL, - DOC, - HEADER; -} \ No newline at end of file + NORMAL, DOC, HEADER; +} diff --git a/pmd-scala/src/main/java/org/sonar/plugins/scala/util/StringUtils.java b/pmd-scala/src/main/java/org/sonar/plugins/scala/util/StringUtils.java index bf32b4afa9..8027437555 100644 --- a/pmd-scala/src/main/java/org/sonar/plugins/scala/util/StringUtils.java +++ b/pmd-scala/src/main/java/org/sonar/plugins/scala/util/StringUtils.java @@ -17,6 +17,7 @@ * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ + package org.sonar.plugins.scala.util; import java.io.BufferedReader; @@ -28,22 +29,22 @@ import java.util.List; import org.apache.commons.io.IOUtils; public final class StringUtils { - private StringUtils() { - // to prevent instantiation - } - - public static List convertStringToListOfLines(String string) throws IOException { - final List lines = new ArrayList<>(); - BufferedReader reader = null; - try { - reader = new BufferedReader(new StringReader(string)); - String line = null; - while ((line = reader.readLine()) != null) { - lines.add(line); - } - } finally { - IOUtils.closeQuietly(reader); + private StringUtils() { + // to prevent instantiation } - return lines; - } -} \ No newline at end of file + + public static List convertStringToListOfLines(String string) throws IOException { + final List lines = new ArrayList<>(); + BufferedReader reader = null; + try { + reader = new BufferedReader(new StringReader(string)); + String line = null; + while ((line = reader.readLine()) != null) { + lines.add(line); + } + } finally { + IOUtils.closeQuietly(reader); + } + return lines; + } +} diff --git a/pmd-scala/src/test/java/net/sourceforge/pmd/LanguageVersionTest.java b/pmd-scala/src/test/java/net/sourceforge/pmd/LanguageVersionTest.java index af7a228025..1a45a40e5f 100644 --- a/pmd-scala/src/test/java/net/sourceforge/pmd/LanguageVersionTest.java +++ b/pmd-scala/src/test/java/net/sourceforge/pmd/LanguageVersionTest.java @@ -1,17 +1,18 @@ /** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ + package net.sourceforge.pmd; import java.util.Arrays; import java.util.Collection; +import org.junit.runners.Parameterized.Parameters; + import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.LanguageVersion; import net.sourceforge.pmd.lang.scala.ScalaLanguageModule; -import org.junit.runners.Parameterized.Parameters; - public class LanguageVersionTest extends AbstractLanguageVersionTest { public LanguageVersionTest(String name, String terseName, String version, LanguageVersion expected) { @@ -20,8 +21,7 @@ public class LanguageVersionTest extends AbstractLanguageVersionTest { @Parameters public static Collection data() { - return Arrays.asList(new Object[][] { - { ScalaLanguageModule.NAME, ScalaLanguageModule.TERSE_NAME, "", LanguageRegistry.getLanguage(ScalaLanguageModule.NAME).getDefaultVersion() } - }); + return Arrays.asList(new Object[][] { { ScalaLanguageModule.NAME, ScalaLanguageModule.TERSE_NAME, "", + LanguageRegistry.getLanguage(ScalaLanguageModule.NAME).getDefaultVersion(), }, }); } } diff --git a/pmd-scala/src/test/java/net/sourceforge/pmd/cpd/ScalaTokenizerTest.java b/pmd-scala/src/test/java/net/sourceforge/pmd/cpd/ScalaTokenizerTest.java index d3fc905927..ecc4830143 100644 --- a/pmd-scala/src/test/java/net/sourceforge/pmd/cpd/ScalaTokenizerTest.java +++ b/pmd-scala/src/test/java/net/sourceforge/pmd/cpd/ScalaTokenizerTest.java @@ -1,13 +1,12 @@ /** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ + package net.sourceforge.pmd.cpd; import java.io.File; import java.io.IOException; -import net.sourceforge.pmd.testframework.AbstractTokenizerTest; - import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.junit.After; @@ -15,6 +14,7 @@ import org.junit.Before; import org.junit.Test; import org.sonar.plugins.scala.cpd.ScalaTokenizer; +import net.sourceforge.pmd.testframework.AbstractTokenizerTest; public class ScalaTokenizerTest extends AbstractTokenizerTest {