|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2024 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.python.semantic.v2.typeshed; |
| 21 | + |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import javax.annotation.CheckForNull; |
| 28 | +import javax.annotation.Nullable; |
| 29 | +import org.sonar.plugins.python.api.ProjectPythonVersion; |
| 30 | +import org.sonar.plugins.python.api.PythonVersionUtils; |
| 31 | +import org.sonar.python.index.AmbiguousDescriptor; |
| 32 | +import org.sonar.python.index.Descriptor; |
| 33 | +import org.sonar.python.index.ModuleDescriptor; |
| 34 | +import org.sonar.python.types.protobuf.SymbolsProtos; |
| 35 | + |
| 36 | +public class ModuleSymbolToDescriptorConverter { |
| 37 | + private final ClassSymbolToDescriptorConverter classConverter; |
| 38 | + private final FunctionSymbolToDescriptorConverter functionConverter; |
| 39 | + private final VarSymbolToDescriptorConverter variableConverter; |
| 40 | + private final OverloadedFunctionSymbolToDescriptorConverter overloadedFunctionConverter; |
| 41 | + private final Set<String> supportedPythonVersions; |
| 42 | + |
| 43 | + public ModuleSymbolToDescriptorConverter() { |
| 44 | + functionConverter = new FunctionSymbolToDescriptorConverter(); |
| 45 | + variableConverter = new VarSymbolToDescriptorConverter(); |
| 46 | + overloadedFunctionConverter = new OverloadedFunctionSymbolToDescriptorConverter(functionConverter); |
| 47 | + classConverter = new ClassSymbolToDescriptorConverter(variableConverter, functionConverter, overloadedFunctionConverter); |
| 48 | + supportedPythonVersions = ProjectPythonVersion.currentVersionValues(); |
| 49 | + } |
| 50 | + |
| 51 | + @CheckForNull |
| 52 | + public ModuleDescriptor convert(@Nullable SymbolsProtos.ModuleSymbol moduleSymbol) { |
| 53 | + if (moduleSymbol == null) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + var name = moduleSymbol.getFullyQualifiedName(); |
| 58 | + var fullyQualifiedName = moduleSymbol.getFullyQualifiedName(); |
| 59 | + var members = getModuleDescriptors(moduleSymbol); |
| 60 | + |
| 61 | + return new ModuleDescriptor(name, fullyQualifiedName, members); |
| 62 | + } |
| 63 | + |
| 64 | + private Map<String, Descriptor> getModuleDescriptors(SymbolsProtos.ModuleSymbol moduleSymbol) { |
| 65 | + Map<String, Set<Descriptor>> protoSymbolsByName = new HashMap<>(); |
| 66 | + moduleSymbol.getClassesList() |
| 67 | + .stream() |
| 68 | + .filter(d -> isValidForProjectPythonVersion(d.getValidForList())) |
| 69 | + .map(classConverter::convert) |
| 70 | + .forEach(descriptor -> protoSymbolsByName.computeIfAbsent(descriptor.name(), d -> new HashSet<>()).add(descriptor)); |
| 71 | + moduleSymbol.getFunctionsList() |
| 72 | + .stream() |
| 73 | + .filter(d -> isValidForProjectPythonVersion(d.getValidForList())) |
| 74 | + .map(functionConverter::convert) |
| 75 | + .forEach(descriptor -> protoSymbolsByName.computeIfAbsent(descriptor.name(), d -> new HashSet<>()).add(descriptor)); |
| 76 | + moduleSymbol.getOverloadedFunctionsList() |
| 77 | + .stream() |
| 78 | + .filter(d -> isValidForProjectPythonVersion(d.getValidForList())) |
| 79 | + .map(overloadedFunctionConverter::convert) |
| 80 | + .forEach(descriptor -> protoSymbolsByName.computeIfAbsent(descriptor.name(), d -> new HashSet<>()).add(descriptor)); |
| 81 | + moduleSymbol.getVarsList() |
| 82 | + .stream() |
| 83 | + .filter(d -> isValidForProjectPythonVersion(d.getValidForList())) |
| 84 | + .map(variableConverter::convert) |
| 85 | + .forEach(descriptor -> protoSymbolsByName.computeIfAbsent(descriptor.name(), d -> new HashSet<>()).add(descriptor)); |
| 86 | + |
| 87 | + var descriptorsByName = new HashMap<String, Descriptor>(); |
| 88 | + |
| 89 | + protoSymbolsByName.forEach((name, descriptors) -> { |
| 90 | + Descriptor disambiguatedDescriptor = disambiguateSymbolsWithSameName(descriptors); |
| 91 | + descriptorsByName.put(name, disambiguatedDescriptor); |
| 92 | + }); |
| 93 | + |
| 94 | + return descriptorsByName; |
| 95 | + } |
| 96 | + |
| 97 | + private static Descriptor disambiguateSymbolsWithSameName(Set<Descriptor> descriptors) { |
| 98 | + if (descriptors.size() > 1) { |
| 99 | + return AmbiguousDescriptor.create(descriptors); |
| 100 | + } |
| 101 | + return descriptors.iterator().next(); |
| 102 | + } |
| 103 | + |
| 104 | + private boolean isValidForProjectPythonVersion(List<String> validForPythonVersions) { |
| 105 | + if (validForPythonVersions.isEmpty()) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + if (supportedPythonVersions.stream().allMatch(PythonVersionUtils.Version.V_312.serializedValue()::equals) |
| 109 | + && validForPythonVersions.contains(PythonVersionUtils.Version.V_311.serializedValue())) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + HashSet<String> intersection = new HashSet<>(validForPythonVersions); |
| 113 | + intersection.retainAll(supportedPythonVersions); |
| 114 | + return !intersection.isEmpty(); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments