package org.sonar.batch.source;
+import com.google.common.base.Objects;
import org.sonar.api.source.Symbol;
-import javax.annotation.Nullable;
-
public class DefaultSymbol implements Symbol {
private final int declarationStartOffset;
private final int declarationEndOffset;
- private final String fullyQualifiedName;
- public DefaultSymbol(int startOffset, int endOffset, @Nullable String fullyQualifiedName) {
+ public DefaultSymbol(int startOffset, int endOffset) {
this.declarationStartOffset = startOffset;
this.declarationEndOffset = endOffset;
- this.fullyQualifiedName = fullyQualifiedName;
}
public int getDeclarationStartOffset() {
}
public String getFullyQualifiedName() {
- return fullyQualifiedName;
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return Objects.toStringHelper("Symbol")
+ .add("offset", String.format("%d-%d", declarationStartOffset, declarationEndOffset))
+ .toString();
}
}
@Override
public Symbol newSymbol(int fromOffset, int toOffset) {
- Symbol symbol = new DefaultSymbol(fromOffset, toOffset, null);
+ Symbol symbol = new DefaultSymbol(fromOffset, toOffset);
referencesBySymbol.put(symbol, symbol.getDeclarationStartOffset());
return symbol;
}
@Override
public void newReference(Symbol symbol, int fromOffset) {
if (fromOffset >= symbol.getDeclarationStartOffset() && fromOffset < symbol.getDeclarationEndOffset()) {
- throw new UnsupportedOperationException("Cannot add reference overlapping the symbol declaration");
+ throw new UnsupportedOperationException("Cannot add reference (" + fromOffset + ") overlapping " + symbol);
}
referencesBySymbol.put(symbol, fromOffset);
}
@Test
public void should_order_symbol_and_references() throws Exception {
-
Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder();
Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20);
symbolTableBuilder.newReference(firstSymbol, 32);
@Test
public void should_reject_reference_conflicting_with_declaration() throws Exception {
-
throwable.expect(UnsupportedOperationException.class);
Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder();
Symbol symbol = symbolTableBuilder.newSymbol(10, 20);
symbolTableBuilder.newReference(symbol, 15);
}
+
+ @Test
+ public void test_toString() throws Exception {
+ Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder();
+ Symbol symbol = symbolTableBuilder.newSymbol(10, 20);
+
+ assertThat(symbol.toString()).isEqualTo("Symbol{offset=10-20}");
+ }
}
int getDeclarationEndOffset();
+ /**
+ * @since unused
+ * @deprecated in 4.3
+ */
+ @Deprecated
String getFullyQualifiedName();
}