From 7aae33867472515bcec1d5161e6fca8d31d6c0bd Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 3 Oct 2017 18:33:55 +0200 Subject: [PATCH] SONAR-9111 Remove Guava dependency from sonar-duplications --- sonar-duplications/pom.xml | 4 - .../sonar/duplications/index/CloneGroup.java | 7 +- .../duplications/index/MemoryCloneIndex.java | 23 ++-- .../internal/pmd/TokenizerBridge.java | 16 +-- .../duplications/internal/pmd/TokensLine.java | 5 +- .../block/BlockChunkerTestCase.java | 14 +- .../detector/DetectorTestCase.java | 16 +-- .../java/JavaDuplicationsFunctionalTest.java | 125 +++++++++--------- .../java/JavaTokenProducerTest.java | 23 ++-- .../matcher/ForgetLastTokenMatcherTest.java | 17 ++- 10 files changed, 123 insertions(+), 127 deletions(-) diff --git a/sonar-duplications/pom.xml b/sonar-duplications/pom.xml index fa392d8a9d8..80a99f34c80 100644 --- a/sonar-duplications/pom.xml +++ b/sonar-duplications/pom.xml @@ -21,10 +21,6 @@ org.codehaus.sonar sonar-channel - - com.google.guava - guava - com.google.code.findbugs jsr305 diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java index f7f8376d30d..c4b4128330e 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java @@ -19,10 +19,9 @@ */ 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. @@ -66,12 +65,12 @@ public class CloneGroup { } public Builder setParts(List 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; } diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java index 6f5dae640a5..02f71965b8e 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java @@ -19,34 +19,35 @@ */ 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 byResource = ArrayListMultimap.create(); - private Multimap byHash = ArrayListMultimap.create(); + private Map> byResource = new LinkedHashMap<>(); + private Map> byHash = new LinkedHashMap<>(); @Override public Collection getByResourceId(String resourceId) { - return byResource.get(resourceId); + return byResource.computeIfAbsent(resourceId, k -> new ArrayList<>()); } @Override public Collection 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 diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokenizerBridge.java b/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokenizerBridge.java index 0fc08827663..97337e5419f 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokenizerBridge.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokenizerBridge.java @@ -19,10 +19,8 @@ */ 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; @@ -54,8 +52,10 @@ public class TokenizerBridge { 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()); @@ -66,7 +66,7 @@ public class TokenizerBridge { * tokens ordered by occurrence in source code and last token is EOF. */ public static List convert(List tokens) { - ImmutableList.Builder result = ImmutableList.builder(); + List result = new ArrayList<>(); StringBuilder sb = new StringBuilder(); int startLine = Integer.MIN_VALUE; int startIndex = 0; @@ -85,10 +85,10 @@ public class TokenizerBridge { } } addNewTokensLine(result, startIndex, currentIndex, startLine, sb); - return result.build(); + return result; } - private static void addNewTokensLine(ImmutableList.Builder result, int startUnit, int endUnit, int startLine, StringBuilder sb) { + private static void addNewTokensLine(List 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); diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokensLine.java b/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokensLine.java index b32f5af1102..aeb8632fe87 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokensLine.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokensLine.java @@ -19,7 +19,6 @@ */ package org.sonar.duplications.internal.pmd; -import com.google.common.base.Preconditions; import org.sonar.duplications.CodeFragment; /** @@ -36,7 +35,9 @@ public class TokensLine implements 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; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java index 2a3576936a8..86716386be7 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java @@ -19,14 +19,16 @@ */ 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; /** @@ -131,7 +133,7 @@ public abstract class BlockChunkerTestCase { * Creates list of statements from Strings, each statement on a new line starting from 0. */ protected static List createStatementsFromStrings(String... values) { - List result = Lists.newArrayList(); + List result = new ArrayList<>(); for (int i = 0; i < values.length; i++) { result.add(new Statement(i, i, values[i])); } diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java index 29974b89a2b..ee3e8b32232 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java @@ -19,7 +19,10 @@ */ 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; @@ -30,10 +33,6 @@ import org.sonar.duplications.index.ClonePart; 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; @@ -367,8 +366,7 @@ public abstract class DetectorTestCase { 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 clones = detect(cloneIndex, fileBlocks); print(clones); @@ -399,7 +397,7 @@ public abstract class DetectorTestCase { 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() @@ -426,7 +424,7 @@ public abstract class DetectorTestCase { } protected static Block[] newBlocks(String resourceId, String hashes) { - List result = Lists.newArrayList(); + List 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); diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java index 694db8205e6..4f02f68ba83 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java @@ -19,7 +19,8 @@ */ 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; @@ -32,9 +33,8 @@ import org.sonar.duplications.statement.Statement; 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; @@ -55,18 +55,18 @@ public class JavaDuplicationsFunctionalTest { @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 duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); @@ -83,17 +83,17 @@ public class JavaDuplicationsFunctionalTest { @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 duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); @@ -104,18 +104,18 @@ public class JavaDuplicationsFunctionalTest { @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 duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(0)); } @@ -126,22 +126,21 @@ public class JavaDuplicationsFunctionalTest { @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 duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); @@ -150,7 +149,7 @@ public class JavaDuplicationsFunctionalTest { } private String source(String... lines) { - return Joiner.on('\n').join(lines); + return asList(lines).stream().collect(joining("\n")); } private static List detect2(String... fragments) { @@ -181,7 +180,7 @@ public class JavaDuplicationsFunctionalTest { private static BlockChunker BLOCK_CHUNKER = new BlockChunker(BLOCK_SIZE); private List detect(String... lines) { - String sourceCode = Joiner.on('\n').join(lines); + String sourceCode = asList(lines).stream().collect(joining("\n")); MemoryCloneIndex index = new MemoryCloneIndex(); List statements = STATEMENT_CHUNKER.chunk(TOKEN_CHUNKER.chunk(sourceCode)); List blocks = BLOCK_CHUNKER.chunk("resourceId", statements); @@ -206,25 +205,25 @@ public class JavaDuplicationsFunctionalTest { @Test public void chainOfCases() { List 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 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)); } diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java index 0468e5c2111..3ee629b73f9 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java @@ -19,24 +19,23 @@ */ 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; @@ -337,7 +336,9 @@ public class JavaTokenProducerTest { } private List chunk(String sourceCode) { - return Lists.newArrayList(chunker.chunk(sourceCode)); + List target = new ArrayList<>(); + chunker.chunk(sourceCode).forEach(target::add); + return target; } } diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java index 3acd69f7c6e..a0746cbdd4f 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java @@ -19,20 +19,19 @@ */ 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 { @@ -40,7 +39,7 @@ public class ForgetLastTokenMatcherTest { public void shouldMatch() { TokenQueue tokenQueue = spy(new TokenQueue()); Token token = new Token("a", 0, 0); - List output = Lists.newArrayList(token); + List output = new ArrayList<>(Arrays.asList(token)); ForgetLastTokenMatcher matcher = new ForgetLastTokenMatcher(); assertThat(matcher.matchToken(tokenQueue, output), is(true)); -- 2.39.5