<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-channel</artifactId>
</dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
*/
package org.sonar.duplications.index;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
/**
* Groups a set of related {@link ClonePart}s.
}
public Builder setParts(List<ClonePart> parts) {
- this.parts = ImmutableList.copyOf(parts);
+ this.parts = new ArrayList<>(parts);
return this;
}
public Builder addPart(ClonePart part) {
- Preconditions.checkNotNull(part);
+ Objects.requireNonNull(part);
this.parts.add(part);
return this;
}
*/
package org.sonar.duplications.index;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Multimap;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.ByteArray;
import org.sonar.duplications.index.PackedMemoryCloneIndex.ResourceBlocks;
-import java.util.Collection;
-import java.util.Iterator;
-
public class MemoryCloneIndex implements CloneIndex {
- private Multimap<String, Block> byResource = ArrayListMultimap.create();
- private Multimap<ByteArray, Block> byHash = ArrayListMultimap.create();
+ private Map<String, List<Block>> byResource = new LinkedHashMap<>();
+ private Map<ByteArray, List<Block>> byHash = new LinkedHashMap<>();
@Override
public Collection<Block> getByResourceId(String resourceId) {
- return byResource.get(resourceId);
+ return byResource.computeIfAbsent(resourceId, k -> new ArrayList<>());
}
@Override
public Collection<Block> getBySequenceHash(ByteArray sequenceHash) {
- return byHash.get(sequenceHash);
+ return byHash.computeIfAbsent(sequenceHash, k -> new ArrayList<>());
}
@Override
public void insert(Block block) {
- byResource.put(block.getResourceId(), block);
- byHash.put(block.getBlockHash(), block);
+ getByResourceId(block.getResourceId()).add(block);
+ getBySequenceHash(block.getBlockHash()).add(block);
}
@Override
*/
package org.sonar.duplications.internal.pmd;
-import com.google.common.base.Throwables;
-import com.google.common.collect.ImmutableList;
-import java.io.IOException;
import java.io.Reader;
+import java.util.ArrayList;
import java.util.List;
import net.sourceforge.pmd.cpd.SourceCode;
import net.sourceforge.pmd.cpd.TokenEntry;
TokenEntry.clearImages();
try {
tokenizer.tokenize(sourceCode, tokens);
- } catch (IOException e) {
- throw Throwables.propagate(e);
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
}
TokenEntry.clearImages();
return convert(tokens.getTokens());
* tokens ordered by occurrence in source code and last token is EOF.
*/
public static List<TokensLine> convert(List<TokenEntry> tokens) {
- ImmutableList.Builder<TokensLine> result = ImmutableList.builder();
+ List<TokensLine> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
int startLine = Integer.MIN_VALUE;
int startIndex = 0;
}
}
addNewTokensLine(result, startIndex, currentIndex, startLine, sb);
- return result.build();
+ return result;
}
- private static void addNewTokensLine(ImmutableList.Builder<TokensLine> result, int startUnit, int endUnit, int startLine, StringBuilder sb) {
+ private static void addNewTokensLine(List<TokensLine> result, int startUnit, int endUnit, int startLine, StringBuilder sb) {
if (sb.length() != 0) {
result.add(new TokensLine(startUnit, endUnit, startLine, sb.toString()));
sb.setLength(0);
*/
package org.sonar.duplications.internal.pmd;
-import com.google.common.base.Preconditions;
import org.sonar.duplications.CodeFragment;
/**
private final int endUnit;
public TokensLine(int startUnit, int endUnit, int startLine, String value) {
- Preconditions.checkArgument(startLine > 0);
+ if (startLine <= 0) {
+ throw new IllegalArgumentException("Start line should be strictly positive");
+ }
// TODO do we have requirements for length and hashcode ?
this.startLine = startLine;
this.value = value;
*/
package org.sonar.duplications.block;
-import com.google.common.collect.Lists;
-import org.junit.Test;
-import org.sonar.duplications.statement.Statement;
-
+import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import org.junit.Test;
+import org.sonar.duplications.statement.Statement;
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
/**
* Creates list of statements from Strings, each statement on a new line starting from 0.
*/
protected static List<Statement> createStatementsFromStrings(String... values) {
- List<Statement> result = Lists.newArrayList();
+ List<Statement> result = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
result.add(new Statement(i, i, values[i]));
}
*/
package org.sonar.duplications.detector;
-import com.google.common.collect.Lists;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.index.MemoryCloneIndex;
import org.sonar.duplications.junit.TestNamePrinter;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
public void problemWithEndOfFile() {
CloneIndex cloneIndex = createIndex(
newBlocks("b", "1 2 3 4"));
- Block[] fileBlocks =
- newBlocks("a", "1 2 3");
+ Block[] fileBlocks = newBlocks("a", "1 2 3");
List<CloneGroup> clones = detect(cloneIndex, fileBlocks);
print(clones);
Block.Builder block = Block.builder()
.setResourceId("a")
.setLines(0, 1);
- Block[] fileBlocks = new Block[]{
+ Block[] fileBlocks = new Block[] {
block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(0).build(),
block.setBlockHash(new ByteArray("2".getBytes())).setIndexInFile(1).build(),
block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(2).build()
}
protected static Block[] newBlocks(String resourceId, String hashes) {
- List<Block> result = Lists.newArrayList();
+ List<Block> result = new ArrayList<>();
int indexInFile = 0;
for (int i = 0; i < hashes.length(); i += 2) {
Block block = newBlock(resourceId, new ByteArray("0" + hashes.charAt(i)), indexInFile);
*/
package org.sonar.duplications.java;
-import com.google.common.base.Joiner;
+import java.util.Collection;
+import java.util.List;
import org.junit.Test;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.BlockChunker;
import org.sonar.duplications.statement.StatementChunker;
import org.sonar.duplications.token.TokenChunker;
-import java.util.Collection;
-import java.util.List;
-
+import static java.util.Arrays.asList;
+import static java.util.stream.Collectors.joining;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@Test
public void type1() {
String fragment0 = source(
- "if (a >= b) {",
- " c = d + b; // Comment1",
- " d = d + 1;}",
- "else",
- " c = d - a; // Comment2");
+ "if (a >= b) {",
+ " c = d + b; // Comment1",
+ " d = d + 1;}",
+ "else",
+ " c = d - a; // Comment2");
String fragment1 = source(
- "if (a>=b) {",
- " // Comment1",
- " c=d+b;",
- " d=d+1;",
- "} else // Comment2",
- " c=d-a;");
+ "if (a>=b) {",
+ " // Comment1",
+ " c=d+b;",
+ " d=d+1;",
+ "} else // Comment2",
+ " c=d-a;");
List<CloneGroup> duplications = detect2(fragment0, fragment1);
assertThat(duplications.size(), is(1));
ClonePart part = duplications.get(0).getOriginPart();
@Test
public void type2_literals() {
String fragment0 = source(
- "if (a >= b) {",
- " c = b + 1; // Comment1",
- " d = '1';}",
- "else",
- " c = d - a; // Comment2");
+ "if (a >= b) {",
+ " c = b + 1; // Comment1",
+ " d = '1';}",
+ "else",
+ " c = d - a; // Comment2");
String fragment1 = source(
- "if (a >= b) {",
- " c = b + 2; // Comment1",
- " d = '2';}",
- "else",
- " c = d - a; // Comment2");
+ "if (a >= b) {",
+ " c = b + 2; // Comment1",
+ " d = '2';}",
+ "else",
+ " c = d - a; // Comment2");
List<CloneGroup> duplications = detect2(fragment0, fragment1);
assertThat(duplications.size(), is(1));
ClonePart part = duplications.get(0).getOriginPart();
@Test
public void type2() {
String fragment0 = source(
- "if (a >= b) {",
- " c = d + b; // Comment1",
- " d = d + 1;}",
- "else",
- " c = d - a; // Comment2");
+ "if (a >= b) {",
+ " c = d + b; // Comment1",
+ " d = d + 1;}",
+ "else",
+ " c = d - a; // Comment2");
String fragment1 = source(
- "if (m >= n) {",
- " // Comment3",
- " y = x + n; // Comment1",
- " x = x + 5;}",
- "else",
- " y = x - m; // Comment2");
+ "if (m >= n) {",
+ " // Comment3",
+ " y = x + n; // Comment1",
+ " x = x + 5;}",
+ "else",
+ " y = x - m; // Comment2");
List<CloneGroup> duplications = detect2(fragment0, fragment1);
assertThat(duplications.size(), is(0));
}
@Test
public void type3() {
String fragment0 = source(
- "public int getSoLinger() throws SocketException {",
- " Object o = impl.getOption( SocketOptions.SO_LINGER);",
- " if (o instanceof Integer) {",
- " return((Integer) o).intValue();",
- " }",
- " else return -1;",
- "}");
+ "public int getSoLinger() throws SocketException {",
+ " Object o = impl.getOption( SocketOptions.SO_LINGER);",
+ " if (o instanceof Integer) {",
+ " return((Integer) o).intValue();",
+ " }",
+ " else return -1;",
+ "}");
String fragment1 = source(
- "public synchronized int getSoTimeout() throws SocketException {",
- " Object o = impl.getOption( SocketOptions.SO_TIMEOUT);",
- " if (o instanceof Integer) {",
- " return((Integer) o).intValue();",
- " }",
- " else return -0;",
- "}"
- );
+ "public synchronized int getSoTimeout() throws SocketException {",
+ " Object o = impl.getOption( SocketOptions.SO_TIMEOUT);",
+ " if (o instanceof Integer) {",
+ " return((Integer) o).intValue();",
+ " }",
+ " else return -0;",
+ "}");
List<CloneGroup> duplications = detect2(fragment0, fragment1);
assertThat(duplications.size(), is(1));
ClonePart part = duplications.get(0).getOriginPart();
}
private String source(String... lines) {
- return Joiner.on('\n').join(lines);
+ return asList(lines).stream().collect(joining("\n"));
}
private static List<CloneGroup> detect2(String... fragments) {
private static BlockChunker BLOCK_CHUNKER = new BlockChunker(BLOCK_SIZE);
private List<CloneGroup> detect(String... lines) {
- String sourceCode = Joiner.on('\n').join(lines);
+ String sourceCode = asList(lines).stream().collect(joining("\n"));
MemoryCloneIndex index = new MemoryCloneIndex();
List<Statement> statements = STATEMENT_CHUNKER.chunk(TOKEN_CHUNKER.chunk(sourceCode));
List<Block> blocks = BLOCK_CHUNKER.chunk("resourceId", statements);
@Test
public void chainOfCases() {
List<CloneGroup> duplications = detect(
- "switch (a) {",
- " case 'a': case 'b': case 'c':",
- " doSomething();",
- " case 'd': case 'e': case 'f':",
- " doSomethingElse();",
- "}");
+ "switch (a) {",
+ " case 'a': case 'b': case 'c':",
+ " doSomething();",
+ " case 'd': case 'e': case 'f':",
+ " doSomethingElse();",
+ "}");
assertThat(duplications.size(), is(0));
}
@Test
public void literalsNormalization() {
List<CloneGroup> duplications = detect(
- "String s = \"abc\";",
- "String s = \"def\";");
+ "String s = \"abc\";",
+ "String s = \"def\";");
assertThat(duplications.size(), is(1));
duplications = detect(
- "int i = 1;",
- "int i = 2;");
+ "int i = 1;",
+ "int i = 2;");
assertThat(duplications.size(), is(1));
}
*/
package org.sonar.duplications.java;
-import com.google.common.collect.Lists;
-import org.apache.commons.io.IOUtils;
-import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
-import org.junit.Test;
-import org.sonar.duplications.DuplicationsTestUtil;
-import org.sonar.duplications.token.Token;
-import org.sonar.duplications.token.TokenChunker;
-import org.sonar.duplications.token.TokenQueue;
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import org.apache.commons.io.IOUtils;
+import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+import org.sonar.duplications.DuplicationsTestUtil;
+import org.sonar.duplications.token.Token;
+import org.sonar.duplications.token.TokenChunker;
+import org.sonar.duplications.token.TokenQueue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
}
private List<Token> chunk(String sourceCode) {
- return Lists.newArrayList(chunker.chunk(sourceCode));
+ List<Token> target = new ArrayList<>();
+ chunker.chunk(sourceCode).forEach(target::add);
+ return target;
}
}
*/
package org.sonar.duplications.statement.matcher;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
-
import org.junit.Test;
import org.sonar.duplications.token.Token;
import org.sonar.duplications.token.TokenQueue;
-import com.google.common.collect.Lists;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
public class ForgetLastTokenMatcherTest {
public void shouldMatch() {
TokenQueue tokenQueue = spy(new TokenQueue());
Token token = new Token("a", 0, 0);
- List<Token> output = Lists.newArrayList(token);
+ List<Token> output = new ArrayList<>(Arrays.asList(token));
ForgetLastTokenMatcher matcher = new ForgetLastTokenMatcher();
assertThat(matcher.matchToken(tokenQueue, output), is(true));