pmd-cs: checkstyle / formatting

This commit is contained in:
Andreas Dangel
2016-12-02 09:23:15 +01:00
parent edcd4e5ccb
commit ead29c2350
4 changed files with 55 additions and 77 deletions

View File

@ -1,6 +1,7 @@
/** /**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/ */
package net.sourceforge.pmd.cpd; package net.sourceforge.pmd.cpd;
import java.util.Properties; import java.util.Properties;
@ -20,7 +21,7 @@ public class CsLanguage extends AbstractLanguage {
} }
public final void setProperties(Properties properties) { public final void setProperties(Properties properties) {
CsTokenizer tokenizer = (CsTokenizer)getTokenizer(); CsTokenizer tokenizer = (CsTokenizer) getTokenizer();
tokenizer.setProperties(properties); tokenizer.setProperties(properties);
} }
} }

View File

@ -1,6 +1,7 @@
/** /**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/ */
package net.sourceforge.pmd.cpd; package net.sourceforge.pmd.cpd;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -30,30 +31,30 @@ public class CsTokenizer implements Tokenizer {
@Override @Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) { public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
Tokenizer tokenizer = Tokenizer tokenizer = new Tokenizer(sourceCode.getCodeBuffer().toString());
new Tokenizer(sourceCode.getCodeBuffer().toString());
Token token = tokenizer.getNextToken(); Token token = tokenizer.getNextToken();
while (!token.equals(Token.EOF)) { while (!token.equals(Token.EOF)) {
Token lookAhead = tokenizer.getNextToken(); Token lookAhead = tokenizer.getNextToken();
// Ignore using directives // Ignore using directives
// Only using directives should be ignored, because these are used to import namespaces // Only using directives should be ignored, because these are used
// to import namespaces
// //
// Using directive: 'using System.Math;' // Using directive: 'using System.Math;'
// Using statement: 'using (Font font1 = new Font(..)) { .. }' // Using statement: 'using (Font font1 = new Font(..)) { .. }'
if (ignoreUsings && if (ignoreUsings && "using".equals(token.image) && !"(".equals(lookAhead.image)) {
"using".equals(token.image) && // We replace the 'using' token by a random token, because it
!"(".equals(lookAhead.image) // should not be part of
) { // any duplication block. When we omit it from the token stream,
// We replace the 'using' token by a random token, because it should not be part of // there is a change that
// any duplication block. When we omit it from the token stream, there is a change that // we get a duplication block that starts before the 'using'
// we get a duplication block that starts before the 'using' directives and ends afterwards. // directives and ends afterwards.
String randomTokenText = String randomTokenText = RandomStringUtils.randomAlphanumeric(20);
RandomStringUtils.randomAlphanumeric(20);
token = new Token(randomTokenText, token.lineNumber); token = new Token(randomTokenText, token.lineNumber);
//Skip all other tokens of the using directive to prevent a partial matching // Skip all other tokens of the using directive to prevent a
// partial matching
while (!";".equals(lookAhead.image) && !lookAhead.equals(Token.EOF)) { while (!";".equals(lookAhead.image) && !lookAhead.equals(Token.EOF)) {
lookAhead = tokenizer.getNextToken(); lookAhead = tokenizer.getNextToken();
} }
@ -71,13 +72,12 @@ public class CsTokenizer implements Tokenizer {
this.ignoreUsings = ignoreUsings; this.ignoreUsings = ignoreUsings;
} }
private static class Tokenizer implements Closeable { private static class Tokenizer implements Closeable {
private boolean endOfFile; private boolean endOfFile;
private int line; private int line;
private final PushbackReader reader; private final PushbackReader reader;
public Tokenizer(String sourceCode) { Tokenizer(String sourceCode) {
endOfFile = false; endOfFile = false;
line = 1; line = 1;
reader = new PushbackReader(new BufferedReader(new CharArrayReader(sourceCode.toCharArray()))); reader = new PushbackReader(new BufferedReader(new CharArrayReader(sourceCode.toCharArray())));
@ -120,7 +120,7 @@ public class CsTokenizer implements Tokenizer {
} else if (ic == c) { } else if (ic == c) {
ic = reader.read(); ic = reader.read();
if (ic == '=') { if (ic == '=') {
return new Token(c + c + "=", line); return new Token(c + c + "=", line);
} else { } else {
reader.unread(ic); reader.unread(ic);
return new Token(String.valueOf(c) + c, line); return new Token(String.valueOf(c) + c, line);
@ -130,7 +130,7 @@ public class CsTokenizer implements Tokenizer {
return new Token(String.valueOf(c), line); return new Token(String.valueOf(c), line);
} }
// = == & &= && | |= || + += ++ - -= -- // = == & &= && | |= || + += ++ - -= --
case '=': case '=':
case '&': case '&':
case '|': case '|':
@ -144,7 +144,7 @@ public class CsTokenizer implements Tokenizer {
return new Token(String.valueOf(c), line); return new Token(String.valueOf(c), line);
} }
// ! != * *= % %= ^ ^= ~ ~= // ! != * *= % %= ^ ^= ~ ~=
case '!': case '!':
case '*': case '*':
case '%': case '%':
@ -158,7 +158,7 @@ public class CsTokenizer implements Tokenizer {
return new Token(String.valueOf(c), line); return new Token(String.valueOf(c), line);
} }
// strings & chars // strings & chars
case '"': case '"':
case '\'': case '\'':
int beginLine = line; int beginLine = line;
@ -189,9 +189,11 @@ public class CsTokenizer implements Tokenizer {
// / /= /*...*/ //... // / /= /*...*/ //...
case '/': case '/':
switch (c = (char) (ic = reader.read())) { ic = reader.read();
c = (char) ic;
switch (c) {
case '*': case '*':
//int beginLine = line; // int beginLine = line;
int state = 1; int state = 1;
b = new StringBuilder(); b = new StringBuilder();
b.append("/*"); b.append("/*");
@ -251,30 +253,31 @@ public class CsTokenizer implements Tokenizer {
b = new StringBuilder(); b = new StringBuilder();
do { do {
b.append(c); b.append(c);
c = (char) (ic = reader.read()); ic = reader.read();
c = (char) ic;
} while (Character.isJavaIdentifierPart(c)); } while (Character.isJavaIdentifierPart(c));
reader.unread(ic); reader.unread(ic);
return new Token(b.toString(), line); return new Token(b.toString(), line);
} } else if (Character.isDigit(c) || c == '.') {
// numbers // numbers
else if (Character.isDigit(c) || c == '.') {
b = new StringBuilder(); b = new StringBuilder();
do { do {
b.append(c); b.append(c);
if (c == 'e' || c == 'E') { if (c == 'e' || c == 'E') {
c = (char) (ic = reader.read()); ic = reader.read();
c = (char) ic;
if ("1234567890-".indexOf(c) == -1) { if ("1234567890-".indexOf(c) == -1) {
break; break;
} }
b.append(c); b.append(c);
} }
c = (char) (ic = reader.read()); ic = reader.read();
c = (char) ic;
} while ("1234567890.iIlLfFdDsSuUeExX".indexOf(c) != -1); } while ("1234567890.iIlLfFdDsSuUeExX".indexOf(c) != -1);
reader.unread(ic); reader.unread(ic);
return new Token(b.toString(), line); return new Token(b.toString(), line);
} } else {
// anything else // anything else
else {
return new Token(String.valueOf(c), line); return new Token(String.valueOf(c), line);
} }
} }
@ -298,7 +301,7 @@ public class CsTokenizer implements Tokenizer {
public final String image; public final String image;
public final int lineNumber; public final int lineNumber;
public Token(String image, int lineNumber) { Token(String image, int lineNumber) {
this.image = image; this.image = image;
this.lineNumber = lineNumber; this.lineNumber = lineNumber;
} }

View File

@ -1,6 +1,7 @@
/** /**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/ */
package net.sourceforge.pmd.lang.cs; package net.sourceforge.pmd.lang.cs;
import net.sourceforge.pmd.lang.BaseLanguageModule; import net.sourceforge.pmd.lang.BaseLanguageModule;

View File

@ -44,13 +44,8 @@ public class CsTokenizerTest {
@Test @Test
public void testSimpleClassMethodMultipleLines() { public void testSimpleClassMethodMultipleLines() {
tokenizer.tokenize(toSourceCode( tokenizer.tokenize(toSourceCode("class Foo {\n" + " public String foo(int a) {\n" + " int i = a;\n"
"class Foo {\n" + " return \"x\" + a;\n" + " }\n" + "}"), tokens);
+ " public String foo(int a) {\n"
+ " int i = a;\n"
+ " return \"x\" + a;\n"
+ " }\n"
+ "}"), tokens);
assertEquals(22, tokens.size()); assertEquals(22, tokens.size());
List<TokenEntry> tokenList = tokens.getTokens(); List<TokenEntry> tokenList = tokens.getTokens();
assertEquals(1, tokenList.get(0).getBeginLine()); assertEquals(1, tokenList.get(0).getBeginLine());
@ -70,7 +65,6 @@ public class CsTokenizerTest {
assertEquals(5, tokens.size()); assertEquals(5, tokens.size());
} }
@Test @Test
public void testCommentsIgnored1() { public void testCommentsIgnored1() {
tokenizer.tokenize(toSourceCode("class Foo { /* class * ** X */ }"), tokens); tokenizer.tokenize(toSourceCode("class Foo { /* class * ** X */ }"), tokens);
@ -91,35 +85,22 @@ public class CsTokenizerTest {
@Test @Test
public void testMoreTokens() { public void testMoreTokens() {
tokenizer.tokenize(toSourceCode( tokenizer
"class Foo {\n" .tokenize(
+ " void bar() {\n" toSourceCode("class Foo {\n" + " void bar() {\n" + " int a = 1 >> 2; \n" + " a += 1; \n"
+ " int a = 1 >> 2; \n" + " a++; \n" + " a /= 3e2; \n" + " float f = -3.1; \n" + " f *= 2; \n"
+ " a += 1; \n" + " bool b = ! (f == 2.0 || f >= 1.0 && f <= 2.0) \n" + " }\n" + "}"),
+ " a++; \n" tokens);
+ " a /= 3e2; \n"
+ " float f = -3.1; \n"
+ " f *= 2; \n"
+ " bool b = ! (f == 2.0 || f >= 1.0 && f <= 2.0) \n"
+ " }\n"
+ "}"
), tokens);
assertEquals(50, tokens.size()); assertEquals(50, tokens.size());
} }
@Test @Test
public void testLineNumberAfterMultilineComment() { public void testLineNumberAfterMultilineComment() {
tokenizer.tokenize(toSourceCode( tokenizer
"/* This is a multiline comment \n" .tokenize(
+ " * \n" toSourceCode("/* This is a multiline comment \n" + " * \n" + " * Lorem ipsum dolor sit amet, \n"
+ " * Lorem ipsum dolor sit amet, \n" + " * consectetur adipiscing elit \n" + " */\n" + "\n" + "class Foo {\n" + "\n" + "}"),
+ " * consectetur adipiscing elit \n" tokens);
+ " */\n"
+ "\n"
+ "class Foo {\n"
+ "\n"
+ "}"
), tokens);
assertEquals(5, tokens.size()); assertEquals(5, tokens.size());
assertEquals(7, tokens.getTokens().get(0).getBeginLine()); assertEquals(7, tokens.getTokens().get(0).getBeginLine());
} }
@ -127,15 +108,9 @@ public class CsTokenizerTest {
@Test @Test
public void testLineNumberAfterMultilineString() { public void testLineNumberAfterMultilineString() {
tokenizer.tokenize(toSourceCode( tokenizer.tokenize(toSourceCode(
"class Foo {\n" "class Foo {\n" + " void bar() {\n" + " String query = \n" + " @\"SELECT foo, bar\n"
+ " void bar() {\n" + " FROM table \n" + " WHERE id = 42\"; \n" + " }\n" + "}"),
+ " String query = \n" tokens);
+ " @\"SELECT foo, bar\n"
+ " FROM table \n"
+ " WHERE id = 42\"; \n"
+ " }\n"
+ "}"
), tokens);
assertEquals(16, tokens.size()); assertEquals(16, tokens.size());
assertEquals(8, tokens.getTokens().get(14).getBeginLine()); assertEquals(8, tokens.getTokens().get(14).getBeginLine());
} }
@ -152,10 +127,8 @@ public class CsTokenizerTest {
public void testUsingStatementsAreNotIgnored() { public void testUsingStatementsAreNotIgnored() {
tokenizer.setIgnoreUsings(true); tokenizer.setIgnoreUsings(true);
tokenizer.tokenize(toSourceCode( tokenizer.tokenize(toSourceCode(
"using (Font font1 = new Font(\"Arial\", 10.0f)) {\n" "using (Font font1 = new Font(\"Arial\", 10.0f)) {\n" + " byte charset = font1.GdiCharSet;\n" + "}\n"),
+ " byte charset = font1.GdiCharSet;\n" tokens);
+ "}\n"
), tokens);
assertEquals("using", tokens.getTokens().get(0).toString()); assertEquals("using", tokens.getTokens().get(0).toString());
} }