|
| 1 | +/* |
| 2 | + * SonarQube Java |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.java; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.EnumSource; |
| 28 | +import org.sonar.api.batch.fs.InputFile; |
| 29 | +import org.sonar.java.model.JNullabilityData; |
| 30 | +import org.sonar.java.model.JParser; |
| 31 | +import org.sonar.java.model.JParserConfig; |
| 32 | +import org.sonar.java.model.JParserConfig.Result; |
| 33 | +import org.sonar.java.model.JSymbolMetadataNullabilityHelper; |
| 34 | +import org.sonar.java.model.JUtils; |
| 35 | +import org.sonar.java.model.JavaTree; |
| 36 | +import org.sonar.java.model.Symbols; |
| 37 | +import org.sonar.plugins.java.api.diagnostics.Problem; |
| 38 | +import org.sonar.plugins.java.api.diagnostics.Warning; |
| 39 | +import org.sonar.plugins.java.api.semantic.Sema; |
| 40 | +import org.sonar.plugins.java.api.semantic.Symbol; |
| 41 | +import org.sonar.plugins.java.api.semantic.SymbolMetadata; |
| 42 | +import org.sonar.plugins.java.api.semantic.SymbolMetadata.NullabilityLevel; |
| 43 | +import org.sonar.plugins.java.api.semantic.SymbolMetadata.NullabilityTarget; |
| 44 | +import org.sonar.plugins.java.api.semantic.Type; |
| 45 | +import org.sonar.plugins.java.api.tree.ClassTree; |
| 46 | +import org.sonar.plugins.java.api.tree.VariableTree; |
| 47 | + |
| 48 | +import static org.assertj.core.api.Assertions.assertThat; |
| 49 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 50 | + |
| 51 | +class FrontendApiTest { |
| 52 | + |
| 53 | + @Test |
| 54 | + void original_single_file_call_resolves_semantics_through_frontend_interfaces() { |
| 55 | + var version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION; |
| 56 | + JParserConfig.ParserConfigConstructor constructor = JParserConfig.Mode.FILE_BY_FILE::create; |
| 57 | + JParserConfig config = constructor.apply(version, List.of(), false); |
| 58 | + JParser parser = config.astParser(); |
| 59 | + JavaTree.CompilationUnitTreeImpl tree = JParser.parse(parser, version.toString(), "Sample.java", "class Sample { String value; }"); |
| 60 | + try { |
| 61 | + ClassTree declaration = (ClassTree) tree.types().get(0); |
| 62 | + Symbol.TypeSymbol symbol = declaration.symbol(); |
| 63 | + Type type = symbol.type(); |
| 64 | + VariableTree field = (VariableTree) declaration.members().get(0); |
| 65 | + assertThat(symbol.name()).isEqualTo("Sample"); |
| 66 | + assertThat(type.isSubtypeOf("java.lang.Object")).isTrue(); |
| 67 | + assertThat(field.symbol().type().is("java.lang.String")).isTrue(); |
| 68 | + assertThat(tree.sema.getClassType("java.lang.String").isUnknown()).isFalse(); |
| 69 | + } finally { |
| 70 | + tree.getEnvironmentCleaner().run(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @ParameterizedTest |
| 75 | + @EnumSource(JParserConfig.Mode.class) |
| 76 | + void original_batch_callback_uses_nested_result(JParserConfig.Mode mode) { |
| 77 | + InputFile input = TestUtils.inputFile("src/test/files/metrics/Classes.java"); |
| 78 | + AtomicInteger callbacks = new AtomicInteger(); |
| 79 | + mode.create(JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION, List.of()) |
| 80 | + .parse(List.of(input), () -> false, new AnalysisProgress(1), (file, result) -> { |
| 81 | + assertThat(file).isSameAs(input); |
| 82 | + try { |
| 83 | + JavaTree.CompilationUnitTreeImpl tree = result.get(); |
| 84 | + assertThat(tree.types()).isNotEmpty(); |
| 85 | + tree.getEnvironmentCleaner().run(); |
| 86 | + } catch (Exception e) { |
| 87 | + throw new AssertionError(e); |
| 88 | + } |
| 89 | + callbacks.incrementAndGet(); |
| 90 | + }); |
| 91 | + assertThat(callbacks.get()).isEqualTo(1); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void nested_result_rethrows_original_exception() { |
| 96 | + Exception exception = new IOException("failed to read source"); |
| 97 | + Result result = Result.failure(exception); |
| 98 | + assertThatThrownBy(result::get).isSameAs(exception); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void original_utility_names_remain_available() { |
| 103 | + assertThat(JUtils.defaultValue(Symbol.MethodSymbol.UNKNOWN_METHOD)).isNull(); |
| 104 | + assertThat(JUtils.wrapTypeIfPrimitive(Type.UNKNOWN)).isSameAs(Type.UNKNOWN); |
| 105 | + assertThat(JNullabilityData.noNullabilityAnnotationAt(NullabilityLevel.CLASS)) |
| 106 | + .isSameAs(JSymbolMetadataNullabilityHelper.getNullabilityDataAtLevel(Symbols.EMPTY_METADATA, NullabilityTarget.METHOD, NullabilityLevel.CLASS)); |
| 107 | + assertThat(JNullabilityData.unknownNullabilityAt(NullabilityLevel.PACKAGE).level()).isEqualTo(NullabilityLevel.PACKAGE); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void frontend_contracts_are_interfaces() { |
| 112 | + assertThat(List.of(JParser.class, JParserConfig.class, Sema.class, Type.class, Symbol.class, |
| 113 | + Symbol.TypeSymbol.class, Symbol.MethodSymbol.class, Symbol.VariableSymbol.class, |
| 114 | + SymbolMetadata.class, Problem.class, Warning.class)).allMatch(Class::isInterface); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void shared_frontend_classes_do_not_depend_on_jdt() throws Exception { |
| 119 | + Path sonarClasses = Path.of(JParser.class.getProtectionDomain().getCodeSource().getLocation().toURI()).resolve("org/sonar"); |
| 120 | + try (var classes = Files.walk(sonarClasses)) { |
| 121 | + for (Path path : classes.filter(file -> file.toString().endsWith(".class")).toList()) { |
| 122 | + Path relative = sonarClasses.relativize(path); |
| 123 | + if (relative.startsWith(Path.of("java", "jdt"))) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + String bytecode = Files.readString(path, StandardCharsets.ISO_8859_1); |
| 127 | + assertThat(bytecode).as("%s", relative).doesNotContain("org/eclipse/jdt/", "org/eclipse/core/"); |
| 128 | + if (!relative.equals(Path.of("java", "model", "JParserConfig$Mode.class"))) { |
| 129 | + assertThat(bytecode).as("%s", relative).doesNotContain("org/sonar/java/jdt/"); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments