aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-duplications
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-07-25 16:31:45 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2014-07-30 18:03:36 +0200
commit6074164392edd3db2dfdfd21d05cd56c19e2b0e6 (patch)
treeb9314796d68c4c396dcf45a1ab689b06490fd4a2 /sonar-duplications
parent12f243728f42a5eb1e714ff15f0240109193f1d8 (diff)
downloadsonarqube-6074164392edd3db2dfdfd21d05cd56c19e2b0e6.tar.gz
sonarqube-6074164392edd3db2dfdfd21d05cd56c19e2b0e6.zip
SONAR-5389 New duplication API
Diffstat (limited to 'sonar-duplications')
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/block/ByteArray.java8
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/block/FileBlocks.java45
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java13
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/PmdBlockChunker.java21
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokenizerBridge.java2
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/TokensLine.java3
6 files changed, 75 insertions, 17 deletions
diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/block/ByteArray.java b/sonar-duplications/src/main/java/org/sonar/duplications/block/ByteArray.java
index aab2802c27b..a3deacff154 100644
--- a/sonar-duplications/src/main/java/org/sonar/duplications/block/ByteArray.java
+++ b/sonar-duplications/src/main/java/org/sonar/duplications/block/ByteArray.java
@@ -60,7 +60,7 @@ public final class ByteArray {
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
- (byte) value };
+ (byte) value};
}
public ByteArray(int value) {
@@ -68,7 +68,7 @@ public final class ByteArray {
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
- (byte) value };
+ (byte) value};
}
public ByteArray(int[] intArray) {
@@ -79,6 +79,10 @@ public final class ByteArray {
this.bytes = bb.array();
}
+ public byte[] getBytes() {
+ return bytes;
+ }
+
public int[] toIntArray() {
// Pad the size to multiple of 4
int size = (bytes.length / 4) + (bytes.length % 4 == 0 ? 0 : 1);
diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/block/FileBlocks.java b/sonar-duplications/src/main/java/org/sonar/duplications/block/FileBlocks.java
new file mode 100644
index 00000000000..7b43988efc2
--- /dev/null
+++ b/sonar-duplications/src/main/java/org/sonar/duplications/block/FileBlocks.java
@@ -0,0 +1,45 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.duplications.block;
+
+import java.util.List;
+
+/**
+ * Represents all blocks in a file.
+ */
+public final class FileBlocks {
+
+ private final String resourceId;
+ private final List<Block> blocks;
+
+ public FileBlocks(String resourceId, List<Block> blocks) {
+ this.resourceId = resourceId;
+ this.blocks = blocks;
+ }
+
+ public String resourceId() {
+ return resourceId;
+ }
+
+ public List<Block> blocks() {
+ return blocks;
+ }
+
+}
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 7c9ad2a8278..01b7273da1c 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
@@ -20,8 +20,10 @@
package org.sonar.duplications.index;
import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
+import java.util.ArrayList;
import java.util.List;
/**
@@ -32,6 +34,7 @@ public class CloneGroup {
private final ClonePart originPart;
private final int cloneLength;
private final List<ClonePart> parts;
+ private int length;
/**
* Cache for hash code.
@@ -52,7 +55,7 @@ public class CloneGroup {
private ClonePart origin;
private int length;
private int lengthInUnits;
- private List<ClonePart> parts;
+ private List<ClonePart> parts = new ArrayList<ClonePart>();
public Builder setLength(int length) {
this.length = length;
@@ -69,6 +72,12 @@ public class CloneGroup {
return this;
}
+ public Builder addPart(ClonePart part) {
+ Preconditions.checkNotNull(part);
+ this.parts.add(part);
+ return this;
+ }
+
public Builder setLengthInUnits(int length) {
this.lengthInUnits = length;
return this;
@@ -90,8 +99,6 @@ public class CloneGroup {
return originPart;
}
- private int length;
-
/**
* Length of duplication measured in original units, e.g. for token-based detection - in tokens.
*
diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/PmdBlockChunker.java b/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/PmdBlockChunker.java
index 1c7c0ee4437..b0ae419332a 100644
--- a/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/PmdBlockChunker.java
+++ b/sonar-duplications/src/main/java/org/sonar/duplications/internal/pmd/PmdBlockChunker.java
@@ -23,7 +23,7 @@ import com.google.common.collect.Lists;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.ByteArray;
-import java.util.Collections;
+import java.util.ArrayList;
import java.util.List;
/**
@@ -48,7 +48,10 @@ public class PmdBlockChunker {
this.power = pow;
}
- public List<Block> chunk(String resourceId, List<TokensLine> fragments) {
+ /**
+ * @return ArrayList as we need a serializable object
+ */
+ public ArrayList<Block> chunk(String resourceId, List<TokensLine> fragments) {
List<TokensLine> filtered = Lists.newArrayList();
int i = 0;
while (i < fragments.size()) {
@@ -66,10 +69,10 @@ public class PmdBlockChunker {
fragments = filtered;
if (fragments.size() < blockSize) {
- return Collections.emptyList();
+ return Lists.newArrayList();
}
TokensLine[] fragmentsArr = fragments.toArray(new TokensLine[fragments.size()]);
- List<Block> blocks = Lists.newArrayListWithCapacity(fragmentsArr.length - blockSize + 1);
+ ArrayList<Block> blocks = Lists.newArrayListWithCapacity(fragmentsArr.length - blockSize + 1);
long hash = 0;
int first = 0;
int last = 0;
@@ -84,11 +87,11 @@ public class PmdBlockChunker {
hash = hash * PRIME_BASE + lastFragment.getHashCode();
// create block
Block block = blockBuilder
- .setBlockHash(new ByteArray(hash))
- .setIndexInFile(first)
- .setLines(firstFragment.getStartLine(), lastFragment.getEndLine())
- .setUnit(firstFragment.getStartUnit(), lastFragment.getEndUnit())
- .build();
+ .setBlockHash(new ByteArray(hash))
+ .setIndexInFile(first)
+ .setLines(firstFragment.getStartLine(), lastFragment.getEndLine())
+ .setUnit(firstFragment.getStartUnit(), lastFragment.getEndUnit())
+ .build();
blocks.add(block);
// remove first statement from hash
hash -= power * firstFragment.getHashCode();
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 e8dcc4bad76..80fc8691717 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
@@ -69,7 +69,7 @@ public class TokenizerBridge {
* We expect that implementation of {@link Tokenizer} is correct:
* tokens ordered by occurrence in source code and last token is EOF.
*/
- private static List<TokensLine> convert(List<TokenEntry> tokens) {
+ public static List<TokensLine> convert(List<TokenEntry> tokens) {
ImmutableList.Builder<TokensLine> result = ImmutableList.builder();
StringBuilder sb = new StringBuilder();
int startLine = Integer.MIN_VALUE;
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 9c07b381240..d8f5a304af9 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
@@ -25,7 +25,7 @@ import org.sonar.duplications.CodeFragment;
/**
* Immutable code fragment, which formed from tokens of one line.
*/
-class TokensLine implements CodeFragment {
+public class TokensLine implements CodeFragment {
private final String value;
@@ -35,7 +35,6 @@ class TokensLine implements CodeFragment {
private final int startUnit;
private final int endUnit;
-
public TokensLine(int startUnit, int endUnit, int startLine, String value) {
Preconditions.checkArgument(startLine > 0);
// TODO do we have requirements for length and hashcode ?