import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
-import org.sonar.api.batch.sensor.symbol.Symbol;
-import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
+import org.sonar.api.component.ResourcePerspectives;
+import org.sonar.api.source.Symbol;
+import org.sonar.api.source.Symbolizable;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.xoo.Xoo;
*/
public class SymbolReferencesSensor implements Sensor {
+ private ResourcePerspectives perspectives;
+
+ public SymbolReferencesSensor(ResourcePerspectives perspectives) {
+ this.perspectives = perspectives;
+ }
+
private static final Logger LOG = Loggers.get(SymbolReferencesSensor.class);
private static final String SYMBOL_EXTENSION = ".symbol";
try {
List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name());
int lineNumber = 0;
- SymbolTableBuilder symbolTableBuilder = context.symbolTableBuilder(inputFile);
+ Symbolizable symbolizable = perspectives.as(Symbolizable.class, inputFile);
+
+ Symbolizable.SymbolTableBuilder symbolTableBuilder = symbolizable.newSymbolTableBuilder();
for (String line : lines) {
lineNumber++;
if (StringUtils.isBlank(line) || line.startsWith("#")) {
}
processLine(symbolFile, lineNumber, symbolTableBuilder, line);
}
- symbolTableBuilder.done();
+ symbolizable.setSymbolTable(symbolTableBuilder.build());
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
- private void processLine(File symbolFile, int lineNumber, SymbolTableBuilder symbolTableBuilder, String line) {
+ private void processLine(File symbolFile, int lineNumber, Symbolizable.SymbolTableBuilder symbolTableBuilder, String line) {
try {
Iterator<String> split = Splitter.on(",").split(line).iterator();
int startOffset = Integer.parseInt(split.next());
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
-import org.sonar.api.batch.sensor.symbol.Symbol;
-import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
+import org.sonar.api.component.ResourcePerspectives;
+import org.sonar.api.source.Symbol;
+import org.sonar.api.source.Symbolizable;
import java.io.File;
import java.io.IOException;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
private File baseDir;
+ private ResourcePerspectives perspectives;
@Before
public void prepare() throws IOException {
baseDir = temp.newFolder();
- sensor = new SymbolReferencesSensor();
+ perspectives = mock(ResourcePerspectives.class);
+ sensor = new SymbolReferencesSensor(perspectives);
fileSystem = new DefaultFileSystem(baseDir.toPath());
when(context.fileSystem()).thenReturn(fileSystem);
}
FileUtils.write(symbol, "1,4,7\n12,15,23\n\n#comment");
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
fileSystem.add(inputFile);
- SymbolTableBuilder symbolTableBuilder = mock(SymbolTableBuilder.class);
- when(context.symbolTableBuilder(inputFile)).thenReturn(symbolTableBuilder);
+ 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);
import org.sonar.api.batch.sensor.duplication.Duplication;
import org.sonar.api.batch.sensor.duplication.internal.DefaultDuplication;
import org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule;
-import org.sonar.api.batch.sensor.symbol.Symbol;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
+import org.sonar.api.source.Symbol;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.KeyValueFormat;
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
-import org.sonar.api.batch.sensor.symbol.Symbol;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.measures.Measure;
+import org.sonar.api.source.Symbol;
import org.sonar.batch.dependency.DependencyCache;
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.batch.highlighting.SyntaxHighlightingData;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.dependency.NewDependency;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
import org.sonar.api.batch.sensor.measure.NewMeasure;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
-import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
import org.sonar.api.config.Settings;
import org.sonar.batch.index.ComponentDataCache;
-import org.sonar.batch.symbol.DefaultSymbolTableBuilder;
import java.io.Serializable;
return new DefaultHighlighting(sensorStorage);
}
- @Override
- public SymbolTableBuilder symbolTableBuilder(InputFile inputFile) {
- return new DefaultSymbolTableBuilder(((DefaultInputFile) inputFile).key(), componentDataCache);
- }
-
@Override
public NewDuplication newDuplication() {
return new DefaultDuplication(sensorStorage);
--- /dev/null
+/*
+ * 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.batch.source;
+
+import com.google.common.base.Objects;
+
+import java.io.Serializable;
+
+public class DefaultSymbol implements org.sonar.api.source.Symbol, Serializable {
+
+ private final int declarationStartOffset;
+ private final int declarationEndOffset;
+
+ public DefaultSymbol(int startOffset, int endOffset) {
+ this.declarationStartOffset = startOffset;
+ this.declarationEndOffset = endOffset;
+ }
+
+ @Override
+ public int getDeclarationStartOffset() {
+ return declarationStartOffset;
+ }
+
+ @Override
+ public int getDeclarationEndOffset() {
+ return declarationEndOffset;
+ }
+
+ @Override
+ public String getFullyQualifiedName() {
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return Objects.toStringHelper("Symbol")
+ .add("offset", String.format("%d-%d", declarationStartOffset, declarationEndOffset))
+ .toString();
+ }
+}
package org.sonar.batch.source;
-import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbol;
import org.sonar.api.source.Symbol;
import org.sonar.api.source.Symbolizable;
@Override
public List<Symbol> symbols() {
List<Symbol> result = new ArrayList<Symbol>();
- for (org.sonar.api.batch.sensor.symbol.Symbol symbol : referencesBySymbol.keySet()) {
+ for (Symbol symbol : referencesBySymbol.keySet()) {
result.add((Symbol) symbol);
}
return result;
package org.sonar.batch.symbol;
-import org.sonar.api.batch.sensor.symbol.Symbol;
-import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
-import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbol;
+import org.sonar.api.source.Symbol;
import org.sonar.batch.index.ComponentDataCache;
+import org.sonar.batch.source.DefaultSymbol;
import org.sonar.core.source.SnapshotDataTypes;
import java.io.Serializable;
import java.util.Set;
import java.util.TreeSet;
-public class DefaultSymbolTableBuilder implements SymbolTableBuilder {
+public class DefaultSymbolTableBuilder {
private final String componentKey;
private final ComponentDataCache cache;
this.cache = cache;
}
- @Override
public Symbol newSymbol(int fromOffset, int toOffset) {
org.sonar.api.source.Symbol symbol = new DefaultSymbol(fromOffset, toOffset);
referencesBySymbol.put(symbol, new TreeSet<Integer>());
return symbol;
}
- @Override
public void newReference(Symbol symbol, int fromOffset) {
if (!referencesBySymbol.containsKey(symbol)) {
throw new UnsupportedOperationException("Cannot add reference to a symbol in another file");
return new SymbolData(referencesBySymbol);
}
- @Override
public void done() {
cache.setData(componentKey, SnapshotDataTypes.SYMBOL_HIGHLIGHTING, build());
}
package org.sonar.batch.symbol;
-import org.sonar.api.batch.sensor.symbol.Symbol;
+import org.sonar.api.source.Symbol;
import org.sonar.batch.index.Data;
import java.util.Collection;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
+import org.sonar.api.source.Symbol;
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.batch.highlighting.SyntaxHighlightingData;
import org.sonar.batch.highlighting.SyntaxHighlightingDataBuilder;
@Test
public void applySymbolReferences() throws Exception {
DefaultSymbolTableBuilder symbolBuilder = new DefaultSymbolTableBuilder(inputFile.key(), null);
- org.sonar.api.batch.sensor.symbol.Symbol s1 = symbolBuilder.newSymbol(1, 2);
+ Symbol s1 = symbolBuilder.newSymbol(1, 2);
symbolBuilder.newReference(s1, 4);
symbolBuilder.newReference(s1, 11);
- org.sonar.api.batch.sensor.symbol.Symbol s2 = symbolBuilder.newSymbol(4, 6);
+ Symbol s2 = symbolBuilder.newSymbol(4, 6);
symbolBuilder.newReference(s2, 0);
symbolBuilder.newReference(s2, 7);
when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYMBOL_HIGHLIGHTING)).thenReturn(symbolBuilder.build());
@Test
public void applySymbolReferences_declaration_order_is_not_important() throws Exception {
DefaultSymbolTableBuilder symbolBuilder = new DefaultSymbolTableBuilder(inputFile.key(), null);
- org.sonar.api.batch.sensor.symbol.Symbol s2 = symbolBuilder.newSymbol(4, 6);
+ Symbol s2 = symbolBuilder.newSymbol(4, 6);
symbolBuilder.newReference(s2, 7);
symbolBuilder.newReference(s2, 0);
- org.sonar.api.batch.sensor.symbol.Symbol s1 = symbolBuilder.newSymbol(1, 2);
+ Symbol s1 = symbolBuilder.newSymbol(1, 2);
symbolBuilder.newReference(s1, 11);
symbolBuilder.newReference(s1, 4);
when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYMBOL_HIGHLIGHTING)).thenReturn(symbolBuilder.build());
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
-import org.sonar.api.batch.sensor.symbol.Symbol;
-import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
+import org.sonar.api.source.Symbol;
import org.sonar.batch.index.ComponentDataCache;
import org.sonar.core.source.SnapshotDataTypes;
@Test
public void should_write_symbol_and_references() throws Exception {
ComponentDataCache componentDataCache = mock(ComponentDataCache.class);
- SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache);
+ DefaultSymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache);
Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20);
symbolTableBuilder.newReference(firstSymbol, 32);
Symbol secondSymbol = symbolTableBuilder.newSymbol(84, 92);
public void should_serialize_unused_symbol() throws Exception {
ComponentDataCache componentDataCache = mock(ComponentDataCache.class);
- SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache);
+ DefaultSymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache);
symbolTableBuilder.newSymbol(10, 20);
symbolTableBuilder.done();
throwable.expect(UnsupportedOperationException.class);
ComponentDataCache componentDataCache = mock(ComponentDataCache.class);
- SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache);
+ DefaultSymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache);
Symbol symbol = symbolTableBuilder.newSymbol(10, 20);
symbolTableBuilder.newReference(symbol, 15);
}
throwable.expect(UnsupportedOperationException.class);
ComponentDataCache componentDataCache = mock(ComponentDataCache.class);
- SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache);
+ DefaultSymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache);
Symbol symbol = symbolTableBuilder.newSymbol(10, 20);
- SymbolTableBuilder symbolTableBuilder2 = new DefaultSymbolTableBuilder("foo2", componentDataCache);
+ DefaultSymbolTableBuilder symbolTableBuilder2 = new DefaultSymbolTableBuilder("foo2", componentDataCache);
Symbol symbol2 = symbolTableBuilder2.newSymbol(30, 40);
symbolTableBuilder.newReference(symbol2, 15);
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.CpdMapping;
import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.sensor.dependency.NewDependency;
import org.sonar.api.batch.sensor.duplication.NewDuplication;
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.batch.sensor.measure.Measure;
import org.sonar.api.batch.sensor.measure.NewMeasure;
-import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
import org.sonar.api.config.Settings;
import java.io.Serializable;
// ------------ SYMBOL REFERENCES ------------
- /**
- * Builder to define symbol references in a file.
- */
- SymbolTableBuilder symbolTableBuilder(InputFile inputFile);
+ // TODO
// ------------ DUPLICATIONS ------------
+++ /dev/null
-/*
- * 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.api.batch.sensor.symbol;
-
-import com.google.common.annotations.Beta;
-
-/**
- * Represent a symbol in a source file. Experimental, do not use
- * @since 4.5
- */
-@Beta
-public interface Symbol {
-
- int getDeclarationStartOffset();
-
- int getDeclarationEndOffset();
-
-}
+++ /dev/null
-/*
- * 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.api.batch.sensor.symbol;
-
-import com.google.common.annotations.Beta;
-
-/**
- * Experimental, do not use.
- * <p/>
- * Use this builder to create symbol references. For now only references
- * in the same file are supported.
- * @since 4.5
- */
-@Beta
-public interface SymbolTableBuilder {
-
- /**
- * Create a new symbol.
- * @param fromOffset Starting offset in a file for the symbol declaration. File starts at offset '0'.
- * @param toOffset Ending offset of symbol declaration.
- * @return a new Symbol that can be used later in {@link #newReference(Symbol, int)}
- */
- Symbol newSymbol(int fromOffset, int toOffset);
-
- /**
- * Records that a {@link Symbol} is referenced at another location in the same file.
- * @param symbol Symbol previously created with {@link #newSymbol(int, int)}
- * @param fromOffset Starting offset of the place symbol is referenced. No need for end offset here since we assume it is same length.
- */
- void newReference(Symbol symbol, int fromOffset);
-
- /**
- * Call this method only once when your are done with defining symbols of the file.
- */
- void done();
-}
+++ /dev/null
-/*
- * 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.api.batch.sensor.symbol.internal;
-
-import com.google.common.base.Objects;
-import org.sonar.api.batch.sensor.symbol.Symbol;
-
-import java.io.Serializable;
-
-public class DefaultSymbol implements Symbol, org.sonar.api.source.Symbol, Serializable {
-
- private final int declarationStartOffset;
- private final int declarationEndOffset;
-
- public DefaultSymbol(int startOffset, int endOffset) {
- this.declarationStartOffset = startOffset;
- this.declarationEndOffset = endOffset;
- }
-
- @Override
- public int getDeclarationStartOffset() {
- return declarationStartOffset;
- }
-
- @Override
- public int getDeclarationEndOffset() {
- return declarationEndOffset;
- }
-
- @Override
- public String getFullyQualifiedName() {
- return null;
- }
-
- @Override
- public String toString() {
- return Objects.toStringHelper("Symbol")
- .add("offset", String.format("%d-%d", declarationStartOffset, declarationEndOffset))
- .toString();
- }
-}
+++ /dev/null
-/*
- * 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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.batch.sensor.symbol.internal;
+++ /dev/null
-/*
- * 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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.batch.sensor.symbol;
package org.sonar.api.source;
-public interface Symbol extends org.sonar.api.batch.sensor.symbol.Symbol {
+public interface Symbol {
- @Override
int getDeclarationStartOffset();
- @Override
int getDeclarationEndOffset();
/**