Search in sources :

Example 1 with Symbolizable

use of org.sonar.api.source.Symbolizable in project sonarqube by SonarSource.

the class SymbolReferencesSensor method processFileSymbol.

private void processFileSymbol(InputFile inputFile, SensorContext context) {
    File ioFile = inputFile.file();
    File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION);
    if (symbolFile.exists()) {
        LOG.debug("Processing " + symbolFile.getAbsolutePath());
        try {
            List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name());
            int lineNumber = 0;
            Symbolizable symbolizable = perspectives.as(Symbolizable.class, inputFile);
            if (symbolizable != null) {
                Symbolizable.SymbolTableBuilder symbolTableBuilder = symbolizable.newSymbolTableBuilder();
                for (String line : lines) {
                    lineNumber++;
                    if (StringUtils.isBlank(line) || line.startsWith("#")) {
                        continue;
                    }
                    processLine(symbolFile, lineNumber, symbolTableBuilder, line);
                }
                symbolizable.setSymbolTable(symbolTableBuilder.build());
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Symbolizable(org.sonar.api.source.Symbolizable)

Example 2 with Symbolizable

use of org.sonar.api.source.Symbolizable in project sonarqube by SonarSource.

the class SymbolReferencesSensorTest method testExecution.

@Test
public void testExecution() throws IOException {
    File symbol = new File(baseDir, "src/foo.xoo.symbol");
    FileUtils.write(symbol, "1:4,7\n12:15,23:33\n\n#comment");
    InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
    fileSystem.add(inputFile);
    Symbolizable symbolizable = mock(Symbolizable.class);
    when(perspectives.as(Symbolizable.class, inputFile)).thenReturn(symbolizable);
    Symbolizable.SymbolTableBuilder symbolTableBuilder = mock(Symbolizable.SymbolTableBuilder.class);
    when(symbolizable.newSymbolTableBuilder()).thenReturn(symbolTableBuilder);
    Symbol symbol1 = mock(Symbol.class);
    when(symbolTableBuilder.newSymbol(1, 4)).thenReturn(symbol1);
    Symbol symbol2 = mock(Symbol.class);
    when(symbolTableBuilder.newSymbol(12, 15)).thenReturn(symbol2);
    sensor.execute(context);
    verify(symbolTableBuilder).newSymbol(1, 4);
    verify(symbolTableBuilder).newReference(symbol1, 7);
    verify(symbolTableBuilder).newSymbol(12, 15);
    verify(symbolTableBuilder).newReference(symbol2, 23, 33);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Symbol(org.sonar.api.source.Symbol) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Symbolizable(org.sonar.api.source.Symbolizable) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Aggregations

File (java.io.File)2 InputFile (org.sonar.api.batch.fs.InputFile)2 Symbolizable (org.sonar.api.source.Symbolizable)2 IOException (java.io.IOException)1 Test (org.junit.Test)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 Symbol (org.sonar.api.source.Symbol)1