]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9111 Remove Guava dependency from sonar-duplications
authorJulien HENRY <henryju@yahoo.fr>
Tue, 3 Oct 2017 16:33:55 +0000 (18:33 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 4 Oct 2017 11:48:58 +0000 (13:48 +0200)
sonar-duplications/pom.xml
sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java
sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java
sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokenizerBridge.java
sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokensLine.java
sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java
sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java
sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java
sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java
sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java

index fa392d8a9d890fdca4f542e59241c43750b352b0..80a99f34c803507549d01c77a0d1a947f36d339f 100644 (file)
       <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>
index f7f8376d30db1126bfa1b119f1e33c4f868e3cd4..c4b4128330e4eca194d2cec9ecd2ff4da660906b 100644 (file)
  */
 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<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;
     }
index 6f5dae640a5d6b710c718dfdc24cbc8edeca702c..02f71965b8eea4eda935165b03e0cb333b96aa3c 100644 (file)
  */
 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
index 0fc08827663e1a813516528c69758b1be1c11a84..97337e5419f6ba7e65dc5c3e1c3177e3141763d4 100644 (file)
  */
 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<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;
@@ -85,10 +85,10 @@ public class TokenizerBridge {
       }
     }
     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);
index b32f5af11025d5bc74ed3637144f8c72c58a65a1..aeb8632fe877fe4228f7e234111fd87c0bb5bd96 100644 (file)
@@ -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;
index 2a3576936a8b7c7dcd90aee0d816c9b9533da4e5..86716386be7b988a751902bd9f75a72a29fe47ae 100644 (file)
  */
 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<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]));
     }
index 29974b89a2b3f7ba7fd6506477e998ecd5c0ef9b..ee3e8b32232b699ec61fc58716352291b424e280 100644 (file)
  */
 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<CloneGroup> 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<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);
index 694db8205e6a53d78411d61626a17fa3e4e15fcf..4f02f68ba8399d8ccbf7d30da006062bc2981313 100644 (file)
@@ -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<CloneGroup> 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<CloneGroup> 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<CloneGroup> 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<CloneGroup> 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<CloneGroup> detect2(String... fragments) {
@@ -181,7 +180,7 @@ public class JavaDuplicationsFunctionalTest {
   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);
@@ -206,25 +205,25 @@ public class JavaDuplicationsFunctionalTest {
   @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));
   }
 
index 0468e5c21115fb85141f39c1e00cb893bd08a875..3ee629b73f9820712cb33bfb7e34f8d84b5319f8 100644 (file)
  */
 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<Token> chunk(String sourceCode) {
-    return Lists.newArrayList(chunker.chunk(sourceCode));
+    List<Token> target = new ArrayList<>();
+    chunker.chunk(sourceCode).forEach(target::add);
+    return target;
   }
 
 }
index 3acd69f7c6e3ef7c522175e76f8a345304faf740..a0746cbdd4f66b490458ba61c869c78259a04164 100644 (file)
  */
 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<Token> output = Lists.newArrayList(token);
+    List<Token> output = new ArrayList<>(Arrays.asList(token));
     ForgetLastTokenMatcher matcher = new ForgetLastTokenMatcher();
 
     assertThat(matcher.matchToken(tokenQueue, output), is(true));