From e3947d50e30dbd6bcc2d334e5efeb2a0010e8091 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 5 Dec 2012 22:30:40 +0100 Subject: [PATCH] Fix some quality flaws --- .../sonar/duplications/block/ByteArray.java | 3 ++- .../detector/ContainsInComparator.java | 9 ++++--- .../detector/original/BlocksGroup.java | 9 ++++--- .../OriginalCloneDetectionAlgorithm.java | 3 ++- .../suffixtree/DuplicationsCollector.java | 3 ++- .../detector/suffixtree/Edge.java | 8 +++++-- .../detector/suffixtree/Search.java | 6 +++-- .../detector/suffixtree/SuffixTree.java | 3 ++- .../SuffixTreeCloneDetectionAlgorithm.java | 3 ++- .../duplications/java/JavaTokenProducer.java | 24 ++++++++++++------- .../duplications/token/TokenChannel.java | 6 +++-- 11 files changed, 52 insertions(+), 25 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 dc7d78f4665..0fe0ad05618 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 @@ -80,7 +80,8 @@ public final class ByteArray { } public int[] toIntArray() { - int size = (bytes.length / 4) + (bytes.length % 4 == 0 ? 0 : 1); // Pad the size to multiple of 4 + // Pad the size to multiple of 4 + int size = (bytes.length / 4) + (bytes.length % 4 == 0 ? 0 : 1); ByteBuffer bb = ByteBuffer.allocate(size * 4); bb.put(bytes); bb.rewind(); diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/ContainsInComparator.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/ContainsInComparator.java index 9c2ad3c295c..917a195a922 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/ContainsInComparator.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/ContainsInComparator.java @@ -76,12 +76,15 @@ public final class ContainsInComparator implements Comparator { if (c == 0) { if (part1.getUnitStart() <= part2.getUnitStart()) { if (part2.getUnitStart() + l2 <= part1.getUnitStart() + l1) { - return 0; // part1 contains part2 + // part1 contains part2 + return 0; } else { - return -1; // SortedListsUtils#contains should continue search + // SortedListsUtils#contains should continue search + return -1; } } else { - return 1; // unitStart of part1 is less than unitStart of part2 - SortedListsUtils#contains should stop search + // unitStart of part1 is less than unitStart of part2 - SortedListsUtils#contains should stop search + return 1; } } else { return c; diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/original/BlocksGroup.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/original/BlocksGroup.java index 0c3e259368b..ba2dc5c1887 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/original/BlocksGroup.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/original/BlocksGroup.java @@ -113,15 +113,18 @@ final class BlocksGroup { if (c == 0) { c = block1.getIndexInFile() + 1 - block2.getIndexInFile(); } - if (c == 0) { // list1[i] == list2[j] + if (c == 0) { + // list1[i] == list2[j] i++; j++; intersection.blocks.add(block2); } - if (c > 0) { // list1[i] > list2[j] + if (c > 0) { + // list1[i] > list2[j] j++; } - if (c < 0) { // list1[i] < list2[j] + if (c < 0) { + // list1[i] < list2[j] i++; } } diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/original/OriginalCloneDetectionAlgorithm.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/original/OriginalCloneDetectionAlgorithm.java index 84f3633f5f6..670a5c59495 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/original/OriginalCloneDetectionAlgorithm.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/original/OriginalCloneDetectionAlgorithm.java @@ -68,7 +68,8 @@ public final class OriginalCloneDetectionAlgorithm { int size = fileBlocks.size(); // Godin: create one group per unique hash - Map groupsByHash = Maps.newHashMap(); // TODO Godin: can we create map with expected size? + // TODO Godin: can we create map with expected size? + Map groupsByHash = Maps.newHashMap(); for (Block fileBlock : fileBlocks) { ByteArray hash = fileBlock.getBlockHash(); BlocksGroup sameHash = groupsByHash.get(hash); diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/DuplicationsCollector.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/DuplicationsCollector.java index b36f5b3c94f..12873e45af9 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/DuplicationsCollector.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/DuplicationsCollector.java @@ -95,7 +95,8 @@ public class DuplicationsCollector extends Search.Collector { lastBlock.getEndLine()); // TODO Godin: maybe use FastStringComparator here ? - if (originResourceId.equals(part.getResourceId())) { // part from origin + if (originResourceId.equals(part.getResourceId())) { + // part from origin if (origin == null) { origin = part; // To calculate length important to use the origin, because otherwise block may come from DB without required data diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/Edge.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/Edge.java index a6c756a2063..1c25e1da6ff 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/Edge.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/Edge.java @@ -21,10 +21,14 @@ package org.sonar.duplications.detector.suffixtree; public final class Edge { - private int beginIndex; // can't be changed + // can't be changed + private int beginIndex; + private int endIndex; private Node startNode; - private Node endNode; // can't be changed, could be used as edge id + + // can't be changed, could be used as edge id + private Node endNode; // each time edge is created, a new end node is created public Edge(int beginIndex, int endIndex, Node startNode) { diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/Search.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/Search.java index c2174c37bd9..2fea748404a 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/Search.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/Search.java @@ -68,11 +68,13 @@ public final class Search { while (!stack.isEmpty()) { Node node = stack.removeLast(); node.startSize = list.size(); - if (node.getEdges().isEmpty()) { // leaf + if (node.getEdges().isEmpty()) { + // leaf list.add(node.depth); node.endSize = list.size(); } else { - if (!node.equals(tree.getRootNode())) { // inner node = not leaf and not root + if (!node.equals(tree.getRootNode())) { + // inner node = not leaf and not root innerNodes.add(node); } for (Edge edge : node.getEdges()) { diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/SuffixTree.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/SuffixTree.java index 70c76aae86d..7bea7a8d32f 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/SuffixTree.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/SuffixTree.java @@ -103,7 +103,8 @@ public final class SuffixTree { active.canonize(); } updateSuffixNode(lastParentNode, parentNode); - active.incEndIndex(); // Now the endpoint is the next active point + active.incEndIndex(); + // Now the endpoint is the next active point active.canonize(); } diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/SuffixTreeCloneDetectionAlgorithm.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/SuffixTreeCloneDetectionAlgorithm.java index 99036f4c1bd..c7240c40d58 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/SuffixTreeCloneDetectionAlgorithm.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/SuffixTreeCloneDetectionAlgorithm.java @@ -55,7 +55,8 @@ public final class SuffixTreeCloneDetectionAlgorithm { String originResourceId = fileBlocks.iterator().next().getResourceId(); Map> fromIndex = retrieveFromIndex(index, originResourceId, hashes); - if (fromIndex.isEmpty() && hashes.size() == fileBlocks.size()) { // optimization for the case when there is no duplications + if (fromIndex.isEmpty() && hashes.size() == fileBlocks.size()) { + // optimization for the case when there is no duplications return null; } diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/java/JavaTokenProducer.java b/sonar-duplications/src/main/java/org/sonar/duplications/java/JavaTokenProducer.java index 588c0c3e3c8..6a7918082f1 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/java/JavaTokenProducer.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/java/JavaTokenProducer.java @@ -59,15 +59,23 @@ public final class JavaTokenProducer { // Identifiers, Keywords, Boolean Literals, The Null Literal .token("\\p{javaJavaIdentifierStart}++\\p{javaJavaIdentifierPart}*+") // Floating-Point Literals - .token("[0-9_]++\\.([0-9_]++)?+" + EXP + "?+" + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Decimal - .token("\\.[0-9_]++" + EXP + "?+" + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Decimal - .token("[0-9_]++" + EXP + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Decimal - .token("0[xX][0-9a-fA-F_]++\\.[0-9a-fA-F_]*+" + BINARY_EXP + "?+" + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Hexadecimal - .token("0[xX][0-9a-fA-F_]++" + BINARY_EXP + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Hexadecimal + // Decimal + .token("[0-9_]++\\.([0-9_]++)?+" + EXP + "?+" + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) + // Decimal + .token("\\.[0-9_]++" + EXP + "?+" + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) + // Decimal + .token("[0-9_]++" + EXP + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) + // Hexadecimal + .token("0[xX][0-9a-fA-F_]++\\.[0-9a-fA-F_]*+" + BINARY_EXP + "?+" + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) + // Hexadecimal + .token("0[xX][0-9a-fA-F_]++" + BINARY_EXP + FLOAT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Integer Literals - .token("0[xX][0-9a-fA-F_]++" + INT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Hexadecimal - .token("0[bB][01_]++" + INT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Binary (Java 7) - .token("[0-9_]++" + INT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Decimal and Octal + // Hexadecimal + .token("0[xX][0-9a-fA-F_]++" + INT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) + // Binary (Java 7) + .token("0[bB][01_]++" + INT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) + // Decimal and Octal + .token("[0-9_]++" + INT_SUFFIX + "?+", NORMALIZED_NUMERIC_LITERAL) // Any other character .token(".") .build(); diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChannel.java b/sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChannel.java index 99515f86921..9918dcb96e6 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChannel.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChannel.java @@ -44,13 +44,15 @@ class TokenChannel extends Channel { @Override public boolean consume(CodeReader code, TokenQueue output) { if (code.popTo(matcher, tmpBuilder) > 0) { - Cursor previousCursor = code.getPreviousCursor(); // see SONAR-2499 + // see SONAR-2499 + Cursor previousCursor = code.getPreviousCursor(); if (normalizationValue != null) { output.add(new Token(normalizationValue, previousCursor.getLine(), previousCursor.getColumn())); } else { output.add(new Token(tmpBuilder.toString(), previousCursor.getLine(), previousCursor.getColumn())); } - tmpBuilder.setLength(0); // Godin: note that other channels use method delete in order to do the same thing + // Godin: note that other channels use method delete in order to do the same thing + tmpBuilder.setLength(0); return true; } return false; -- 2.39.5