本文整理了Java中org.sonar.duplications.block.Block.getBlockHash()
方法的一些代码示例,展示了Block.getBlockHash()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Block.getBlockHash()
方法的具体详情如下:
包路径:org.sonar.duplications.block.Block
类名称:Block
方法名:getBlockHash
暂无
代码示例来源:origin: SonarSource/sonarqube
@Override
public Object symbolAt(int index) {
Object obj = super.symbolAt(index);
if (obj instanceof Block) {
return ((Block) obj).getBlockHash();
}
return obj;
}
代码示例来源:origin: SonarSource/sonarqube
public String getHashHex() {
return getBlockHash().toString();
}
代码示例来源:origin: SonarSource/sonarqube
private static TextSet createTextSet(CloneIndex index, Collection<Block> fileBlocks) {
Set<ByteArray> hashes = new HashSet<>();
for (Block fileBlock : fileBlocks) {
hashes.add(fileBlock.getBlockHash());
}
String originResourceId = fileBlocks.iterator().next().getResourceId();
Map<String, List<Block>> fromIndex = retrieveFromIndex(index, originResourceId, hashes);
if (fromIndex.isEmpty() && hashes.size() == fileBlocks.size()) {
// optimization for the case when there is no duplications
return null;
}
return createTextSet(fileBlocks, fromIndex);
}
代码示例来源:origin: SonarSource/sonarqube
@Override
public void insert(Block block) {
getByResourceId(block.getResourceId()).add(block);
getBySequenceHash(block.getBlockHash()).add(block);
}
代码示例来源:origin: SonarSource/sonarqube
ByteArray hash = fileBlock.getBlockHash();
BlocksGroup sameHash = groupsByHash.get(hash);
if (sameHash == null) {
ByteArray hash = fileBlock.getBlockHash();
int i = fileBlock.getIndexInFile() + 1;
代码示例来源:origin: SonarSource/sonarqube
@Test
public void testHashes() {
List<Statement> statements = createStatementsFromStrings("1", "2", "1", "2");
BlockChunker chunker = createChunkerWithBlockSize(2);
List<Block> blocks = chunker.chunk("resource", statements);
assertThat("blocks 0 and 2 should have same hash", blocks.get(0).getBlockHash(), equalTo(blocks.get(2).getBlockHash()));
assertThat("blocks 0 and 1 should have different hash", blocks.get(0).getBlockHash(), not(equalTo(blocks.get(1).getBlockHash())));
}
代码示例来源:origin: SonarSource/sonarqube
/**
* TODO Godin: should we allow empty statements in general?
*/
@Test
public void testEmptyStatements() {
List<Statement> statements = createStatementsFromStrings("1", "", "1", "1", "");
BlockChunker chunker = createChunkerWithBlockSize(3);
List<Block> blocks = chunker.chunk("resource", statements);
assertThat("first and last block should have different hashes", blocks.get(0).getBlockHash(), not(equalTo(blocks.get(blocks.size() - 1).getBlockHash())));
}
代码示例来源:origin: SonarSource/sonarqube
/**
* Rolling hash must produce exactly the same values as without rolling behavior.
* Moreover those values must always be the same (without dependency on JDK).
*/
@Test
public void shouldCalculateHashes() {
List<Statement> statements = createStatementsFromStrings("aaaaaa", "bbbbbb", "cccccc", "dddddd", "eeeeee");
BlockChunker blockChunker = createChunkerWithBlockSize(3);
List<Block> blocks = blockChunker.chunk("resource", statements);
assertThat(blocks.get(0).getBlockHash(), equalTo(hash("aaaaaa", "bbbbbb", "cccccc")));
assertThat(blocks.get(1).getBlockHash(), equalTo(hash("bbbbbb", "cccccc", "dddddd")));
assertThat(blocks.get(2).getBlockHash(), equalTo(hash("cccccc", "dddddd", "eeeeee")));
assertThat(blocks.get(0).getBlockHash().toString(), is("fffffeb6ae1af4c0"));
assertThat(blocks.get(1).getBlockHash().toString(), is("fffffebd8512d120"));
assertThat(blocks.get(2).getBlockHash().toString(), is("fffffec45c0aad80"));
}
代码示例来源:origin: SonarSource/sonarqube
/**
* Given:
* <pre>
* String[][] data = {
* {"a", "a"},
* {"a", "a"},
* {"a"},
* {"a", "a"},
* {"a", "a"}
* };
*
* Statements (where L - literal, C - comma): "LCL", "C", "LCL", "C", "L", "C", "LCL", "C", "LCL"
* Block size is 5.
* First block: "LCL", "C", "LCL", "C", "L"
* Last block: "L", "C", "LCL", "C", "LCL"
* </pre>
* Expected: different hashes for first and last blocks
*/
@Test
public void testSameChars() {
List<Statement> statements = createStatementsFromStrings("LCL", "C", "LCL", "C", "L", "C", "LCL", "C", "LCL");
BlockChunker chunker = createChunkerWithBlockSize(5);
List<Block> blocks = chunker.chunk("resource", statements);
assertThat("first and last block should have different hashes", blocks.get(0).getBlockHash(), not(equalTo(blocks.get(blocks.size() - 1).getBlockHash())));
}
代码示例来源:origin: SonarSource/sonarqube
/**
* {@inheritDoc}
* <p>
* <strong>Note that this implementation allows insertion of two blocks with same index for one resource.</strong>
* </p>
*/
@Override
public void insert(Block block) {
sorted = false;
ensureCapacity();
resourceIds[size] = block.getResourceId();
int[] hash = block.getBlockHash().toIntArray();
if (hash.length != hashInts) {
throw new IllegalArgumentException("Expected " + hashInts + " ints in hash, but got " + hash.length);
}
int offset = size * blockInts;
for (int i = 0; i < hashInts; i++) {
blockData[offset++] = hash[i];
}
blockData[offset++] = block.getIndexInFile();
blockData[offset++] = block.getStartLine();
blockData[offset++] = block.getEndLine();
blockData[offset++] = block.getStartUnit();
blockData[offset] = block.getEndUnit();
size++;
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void shouldBuildBlocks() {
TokensLine line1 = new TokensLine(0, 9, 1, Character.toString((char) 1));
TokensLine line2 = new TokensLine(10, 19, 2, Character.toString((char) 2));
TokensLine line3 = new TokensLine(20, 29, 3, Character.toString((char) 3));
List<Block> blocks = new PmdBlockChunker(2).chunk("resourceId", Arrays.asList(line1, line2, line3));
assertThat(blocks.size(), is(2));
Block block = blocks.get(0);
// assertThat(block.getLengthInUnits(), is(11));
assertThat(block.getStartLine(), is(1));
assertThat(block.getEndLine(), is(2));
assertThat(block.getBlockHash(), is(new ByteArray(1L * 31 + 2)));
block = blocks.get(1);
// assertThat(block.getLengthInUnits(), is(33));
assertThat(block.getStartLine(), is(2));
assertThat(block.getEndLine(), is(3));
assertThat(block.getBlockHash(), is(new ByteArray(2L * 31 + 3)));
}
代码示例来源:origin: SonarSource/sonarqube
public void insert(InputFile inputFile, Collection<Block> blocks) {
if (settings.isCrossProjectDuplicationEnabled()) {
int id = ((DefaultInputFile) inputFile).scannerId();
if (publisher.getWriter().hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, id)) {
throw new UnsupportedOperationException("Trying to save CPD tokens twice for the same file is not supported: " + inputFile.absolutePath());
}
final ScannerReport.CpdTextBlock.Builder builder = ScannerReport.CpdTextBlock.newBuilder();
publisher.getWriter().writeCpdTextBlocks(id, blocks.stream().map(block -> {
builder.clear();
builder.setStartLine(block.getStartLine());
builder.setEndLine(block.getEndLine());
builder.setStartTokenIndex(block.getStartUnit());
builder.setEndTokenIndex(block.getEndUnit());
builder.setHash(block.getBlockHash().toHexString());
return builder.build();
}).collect(Collectors.toList()));
}
for (Block block : blocks) {
mem.insert(block);
}
if (blocks.isEmpty()) {
LOG.debug("Not enough content in '{}' to have CPD blocks, it will not be part of the duplication detection", inputFile.relativePath());
}
indexedFiles.add(inputFile);
}
代码示例来源:origin: SonarSource/sonarqube
/**
* When: query by a hash value.
* Expected: all blocks should have same hash, which presented in the form of the same object.
*/
@Test
public void should_construct_blocks_with_normalized_hash() {
index.insert(newBlock("a", 1));
index.insert(newBlock("b", 1));
index.insert(newBlock("c", 1));
ByteArray requestedHash = new ByteArray(1L);
Collection<Block> blocks = index.getBySequenceHash(requestedHash);
assertThat(blocks.size(), is(3));
for (Block block : blocks) {
assertThat(block.getBlockHash(), sameInstance(requestedHash));
}
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void testBuilder() {
ByteArray hash = new ByteArray(1);
Block block = Block.builder()
.setResourceId("resource")
.setBlockHash(hash)
.setIndexInFile(1)
.setLines(2, 3)
.setUnit(4, 5)
.build();
assertThat(block.getResourceId(), is("resource"));
assertThat(block.getBlockHash(), sameInstance(hash));
assertThat(block.getIndexInFile(), is(1));
assertThat(block.getStartLine(), is(2));
assertThat(block.getEndLine(), is(3));
assertThat(block.getStartUnit(), is(4));
assertThat(block.getEndUnit(), is(5));
}
代码示例来源:origin: org.codehaus.sonar/sonar-duplications
@Override
public void insert(Block block) {
byResource.put(block.getResourceId(), block);
byHash.put(block.getBlockHash(), block);
}
代码示例来源:origin: org.codehaus.sonar/sonar-duplications
@Override
public Object symbolAt(int index) {
Object obj = super.symbolAt(index);
if (obj instanceof Block) {
return ((Block) obj).getBlockHash();
}
return obj;
}
代码示例来源:origin: org.codehaus.sonar/sonar-duplications
public String getHashHex() {
return getBlockHash().toString();
}
代码示例来源:origin: org.codehaus.sonar/sonar-duplications
private static TextSet createTextSet(CloneIndex index, Collection<Block> fileBlocks) {
Set<ByteArray> hashes = Sets.newHashSet();
for (Block fileBlock : fileBlocks) {
hashes.add(fileBlock.getBlockHash());
}
String originResourceId = fileBlocks.iterator().next().getResourceId();
Map<String, List<Block>> fromIndex = retrieveFromIndex(index, originResourceId, hashes);
if (fromIndex.isEmpty() && hashes.size() == fileBlocks.size()) {
// optimization for the case when there is no duplications
return null;
}
return createTextSet(fileBlocks, fromIndex);
}
代码示例来源:origin: org.codehaus.sonar/sonar-batch
public void insert(InputFile inputFile, Collection<Block> blocks) {
int resourceSnapshotId = getSnapshotIdFor(inputFile);
// TODO Godin: maybe remove conversion of blocks to units?
List<DuplicationUnitDto> units = Lists.newArrayList();
for (Block block : blocks) {
DuplicationUnitDto unit = new DuplicationUnitDto(
currentProjectSnapshotId,
resourceSnapshotId,
block.getBlockHash().toString(),
block.getIndexInFile(),
block.getStartLine(),
block.getEndLine());
units.add(unit);
}
dao.insert(units);
}
代码示例来源:origin: org.sonarsource.sonarqube/sonar-batch
@Override
public BatchReport.CpdTextBlock apply(Block input) {
builder.clear();
builder.setStartLine(input.getStartLine());
builder.setEndLine(input.getEndLine());
builder.setStartTokenIndex(input.getStartUnit());
builder.setEndTokenIndex(input.getEndUnit());
builder.setHash(input.getBlockHash().toHexString());
return builder.build();
}
}));
内容来源于网络,如有侵权,请联系作者删除!