|
| 1 | +/* |
| 2 | + * This file is a part of BSL Language Server. |
| 3 | + * |
| 4 | + * Copyright (c) 2018-2022 |
| 5 | + * Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 8 | + * |
| 9 | + * BSL Language Server is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 3.0 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * BSL Language Server is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with BSL Language Server. |
| 21 | + */ |
| 22 | +package com.github._1c_syntax.bsl.languageserver.diagnostics; |
| 23 | + |
| 24 | +import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; |
| 25 | +import com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol; |
| 26 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata; |
| 27 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticScope; |
| 28 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity; |
| 29 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag; |
| 30 | +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType; |
| 31 | +import com.github._1c_syntax.bsl.languageserver.references.ReferenceIndex; |
| 32 | +import com.github._1c_syntax.bsl.types.MDOType; |
| 33 | +import com.github._1c_syntax.bsl.types.ModuleType; |
| 34 | +import com.github._1c_syntax.mdclasses.mdo.AbstractMDObjectBase; |
| 35 | +import com.github._1c_syntax.mdclasses.mdo.MDCommonModule; |
| 36 | +import com.github._1c_syntax.mdclasses.mdo.MDScheduledJob; |
| 37 | + |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Comparator; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +@DiagnosticMetadata( |
| 45 | + type = DiagnosticType.ERROR, |
| 46 | + severity = DiagnosticSeverity.CRITICAL, |
| 47 | + minutesToFix = 5, |
| 48 | + tags = { |
| 49 | + DiagnosticTag.ERROR |
| 50 | + }, |
| 51 | + scope = DiagnosticScope.BSL |
| 52 | +) |
| 53 | + |
| 54 | +public class ScheduledJobHandlerDiagnostic extends AbstractMetadataDiagnostic { |
| 55 | + |
| 56 | + private static final String DIAGNOSTIC_MESSAGE = "diagnosticMessage"; |
| 57 | + private static final String MISSING_MODULE_MESSAGE = "missingModule"; |
| 58 | + private static final String NON_SERVER_MODULE_MESSAGE = "nonServerModule"; |
| 59 | + private static final String NON_EXPORT_METHOD_MESSAGE = "nonExportMethod"; |
| 60 | + private static final String METHOD_WITH_PARAMETERS_MESSAGE = "methodWithParameters"; |
| 61 | + private static final String EMPTY_METHOD_MESSAGE = "emptyMethod"; |
| 62 | + private static final String DOUBLE_MESSAGE = "doubleMessage"; |
| 63 | + |
| 64 | + private final ReferenceIndex referenceIndex; |
| 65 | + private final Map<String, List<MDScheduledJob>> scheduledJobHandlers = new HashMap<>(); |
| 66 | + |
| 67 | + private static String getFullName(MDCommonModule mdCommonModule, String methodName) { |
| 68 | + return getFullName(mdCommonModule.getName(), methodName); |
| 69 | + } |
| 70 | + |
| 71 | + private static String getFullName(String commonModuleName, String methodName) { |
| 72 | + return commonModuleName.concat(".").concat(methodName); |
| 73 | + } |
| 74 | + |
| 75 | + public ScheduledJobHandlerDiagnostic(ReferenceIndex referenceIndex) { |
| 76 | + super(List.of(MDOType.SCHEDULED_JOB)); |
| 77 | + this.referenceIndex = referenceIndex; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void check() { |
| 82 | + super.check(); |
| 83 | + checkHandlerDoubles(); |
| 84 | + } |
| 85 | + |
| 86 | + private void checkHandlerDoubles() { |
| 87 | + scheduledJobHandlers.values().stream() |
| 88 | + .filter(mdScheduledJobs -> mdScheduledJobs.size() > 1) |
| 89 | + .map((List<MDScheduledJob> mdScheduledJobs) -> { |
| 90 | + mdScheduledJobs.sort(Comparator.comparing(AbstractMDObjectBase::getName)); |
| 91 | + return mdScheduledJobs; |
| 92 | + }) |
| 93 | + .forEach(this::fireIssueForDoubles); |
| 94 | + scheduledJobHandlers.clear(); |
| 95 | + } |
| 96 | + |
| 97 | + private void fireIssueForDoubles(List<MDScheduledJob> mdScheduledJobs) { |
| 98 | + final var scheduleJobNames = mdScheduledJobs.stream() |
| 99 | + .map(AbstractMDObjectBase::getName) |
| 100 | + .reduce((s, s2) -> s.concat(", ").concat(s2)) |
| 101 | + .orElseThrow(); |
| 102 | + final var mdScheduledJob = mdScheduledJobs.get(0).getHandler(); |
| 103 | + final var methodPath = getFullName(mdScheduledJob.getModuleName(), mdScheduledJob.getMethodName()); |
| 104 | + |
| 105 | + addDiagnostic(info.getResourceString(DOUBLE_MESSAGE, methodPath, scheduleJobNames)); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected void checkMetadata(AbstractMDObjectBase mdo) { |
| 110 | + final var scheduleJob = (MDScheduledJob) mdo; |
| 111 | + final var handler = scheduleJob.getHandler(); |
| 112 | + if (handler.isEmpty()) { |
| 113 | + addDiagnostic(scheduleJob); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + final var moduleName = handler.getModuleName(); |
| 118 | + |
| 119 | + final var commonModuleOptional = |
| 120 | + documentContext.getServerContext().getConfiguration().getCommonModule(moduleName); |
| 121 | + if (commonModuleOptional.isEmpty()) { |
| 122 | + addDiagnostic(MISSING_MODULE_MESSAGE, scheduleJob, moduleName); |
| 123 | + return; |
| 124 | + } |
| 125 | + final var mdCommonModule = commonModuleOptional.orElseThrow(); |
| 126 | + if (!mdCommonModule.isServer()) { |
| 127 | + addDiagnostic(NON_SERVER_MODULE_MESSAGE, scheduleJob, moduleName); |
| 128 | + return; |
| 129 | + } |
| 130 | + checkMethod(scheduleJob, mdCommonModule, handler.getMethodName()); |
| 131 | + } |
| 132 | + |
| 133 | + private void checkMethod(MDScheduledJob scheduleJob, MDCommonModule mdCommonModule, String methodName) { |
| 134 | + final var fullName = getFullName(mdCommonModule, methodName); |
| 135 | + scheduledJobHandlers.computeIfAbsent(fullName, k -> new ArrayList<>()).add(scheduleJob); |
| 136 | + |
| 137 | + documentContext.getServerContext().getDocument( |
| 138 | + mdCommonModule.getMdoReference().getMdoRef(), ModuleType.CommonModule) |
| 139 | + .ifPresent((DocumentContext commonModuleContext) -> { |
| 140 | + var method = commonModuleContext.getSymbolTree().getMethods().stream() |
| 141 | + .filter(methodSymbol -> methodSymbol.getName().equalsIgnoreCase(methodName)) |
| 142 | + .findFirst(); |
| 143 | + if (method.isEmpty()) { |
| 144 | + addDiagnostic(DIAGNOSTIC_MESSAGE, scheduleJob, fullName); |
| 145 | + return; |
| 146 | + } |
| 147 | + method.ifPresent((MethodSymbol methodSymbol) -> checkMethod(scheduleJob, fullName, methodSymbol)); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + private void checkMethod(MDScheduledJob scheduleJob, String fullName, MethodSymbol methodSymbol) { |
| 152 | + if (!methodSymbol.isExport()) { |
| 153 | + addDiagnostic(NON_EXPORT_METHOD_MESSAGE, scheduleJob, fullName); |
| 154 | + } |
| 155 | + if (scheduleJob.isPredefined() && !methodSymbol.getParameters().isEmpty()) { |
| 156 | + addDiagnostic(METHOD_WITH_PARAMETERS_MESSAGE, scheduleJob, fullName); |
| 157 | + } |
| 158 | + if (isEmptyMethodBody(methodSymbol)) { |
| 159 | + addDiagnostic(EMPTY_METHOD_MESSAGE, scheduleJob, fullName); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private boolean isEmptyMethodBody(MethodSymbol methodSymbol) { |
| 164 | + // В методе регламентного задания точно будут или переменные или вызов внешнего метода. |
| 165 | + // Если их нет, значит, метод пустой |
| 166 | + if (!methodSymbol.getChildren().isEmpty()) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + return referenceIndex.getReferencesFrom(methodSymbol).isEmpty(); |
| 170 | + } |
| 171 | + |
| 172 | + private void addDiagnostic(String messageString, MDScheduledJob scheduleJob, String text) { |
| 173 | + addDiagnostic(info.getResourceString(messageString, text, scheduleJob.getName())); |
| 174 | + } |
| 175 | + |
| 176 | + private void addDiagnostic(MDScheduledJob scheduleJob) { |
| 177 | + addDiagnostic(info.getMessage("", scheduleJob.getName())); |
| 178 | + } |
| 179 | +} |
0 commit comments