You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultSymbolTable.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.batch.sensor.symbol.internal;
  21. import java.util.Collection;
  22. import java.util.LinkedHashMap;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.TreeSet;
  26. import org.sonar.api.batch.fs.InputFile;
  27. import org.sonar.api.batch.fs.TextRange;
  28. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  29. import org.sonar.api.batch.sensor.internal.DefaultStorable;
  30. import org.sonar.api.batch.sensor.internal.SensorStorage;
  31. import org.sonar.api.batch.sensor.symbol.NewSymbol;
  32. import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
  33. import static java.util.Objects.requireNonNull;
  34. import static org.sonar.api.utils.Preconditions.checkArgument;
  35. import static org.sonar.api.utils.Preconditions.checkState;
  36. public class DefaultSymbolTable extends DefaultStorable implements NewSymbolTable {
  37. private final Map<TextRange, Set<TextRange>> referencesBySymbol;
  38. private DefaultInputFile inputFile;
  39. public DefaultSymbolTable(SensorStorage storage) {
  40. super(storage);
  41. referencesBySymbol = new LinkedHashMap<>();
  42. }
  43. public Map<TextRange, Set<TextRange>> getReferencesBySymbol() {
  44. return referencesBySymbol;
  45. }
  46. @Override
  47. public DefaultSymbolTable onFile(InputFile inputFile) {
  48. requireNonNull(inputFile, "file can't be null");
  49. this.inputFile = (DefaultInputFile) inputFile;
  50. return this;
  51. }
  52. public InputFile inputFile() {
  53. return inputFile;
  54. }
  55. @Override
  56. public NewSymbol newSymbol(int startLine, int startLineOffset, int endLine, int endLineOffset) {
  57. checkInputFileNotNull();
  58. TextRange declarationRange;
  59. try {
  60. declarationRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset);
  61. } catch (Exception e) {
  62. throw new IllegalArgumentException("Unable to create symbol on file " + inputFile, e);
  63. }
  64. return newSymbol(declarationRange);
  65. }
  66. @Override
  67. public NewSymbol newSymbol(TextRange range) {
  68. checkInputFileNotNull();
  69. TreeSet<TextRange> references = new TreeSet<>((o1, o2) -> o1.start().compareTo(o2.start()));
  70. referencesBySymbol.put(range, references);
  71. return new DefaultSymbol(inputFile, range, references);
  72. }
  73. private static class DefaultSymbol implements NewSymbol {
  74. private final Collection<TextRange> references;
  75. private final DefaultInputFile inputFile;
  76. private final TextRange declaration;
  77. public DefaultSymbol(DefaultInputFile inputFile, TextRange declaration, Collection<TextRange> references) {
  78. this.inputFile = inputFile;
  79. this.declaration = declaration;
  80. this.references = references;
  81. }
  82. @Override
  83. public NewSymbol newReference(int startLine, int startLineOffset, int endLine, int endLineOffset) {
  84. TextRange referenceRange;
  85. try {
  86. referenceRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset);
  87. } catch (Exception e) {
  88. throw new IllegalArgumentException("Unable to create symbol reference on file " + inputFile, e);
  89. }
  90. return newReference(referenceRange);
  91. }
  92. @Override
  93. public NewSymbol newReference(TextRange range) {
  94. requireNonNull(range, "Provided range is null");
  95. checkArgument(!declaration.overlap(range), "Overlapping symbol declaration and reference for symbol at %s", declaration);
  96. references.add(range);
  97. return this;
  98. }
  99. }
  100. @Override
  101. protected void doSave() {
  102. checkInputFileNotNull();
  103. storage.store(this);
  104. }
  105. private void checkInputFileNotNull() {
  106. checkState(inputFile != null, "Call onFile() first");
  107. }
  108. }