|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.diagnostics.analyzer; |
| 18 | + |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.springframework.boot.diagnostics.FailureAnalysis; |
| 23 | +import org.springframework.boot.diagnostics.FailureAnalyzer; |
| 24 | +import org.springframework.core.Ordered; |
| 25 | +import org.springframework.core.annotation.Order; |
| 26 | +import org.springframework.util.StringUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * {@link FailureAnalyzer} for exceptions caused by missing parameter names. This analyzer |
| 30 | + * is ordered last, if other analyzers wish to also report parameter actions they can use |
| 31 | + * the {@link #analyzeForMissingParameters(Throwable)} static method. |
| 32 | + * |
| 33 | + * @author Phillip Webb |
| 34 | + */ |
| 35 | +@Order(Ordered.LOWEST_PRECEDENCE) |
| 36 | +class MissingParameterNamesFailureAnalyzer implements FailureAnalyzer { |
| 37 | + |
| 38 | + private static final String USE_PARAMETERS_MESSAGE = "Ensure that the compiler uses the '-parameters' flag"; |
| 39 | + |
| 40 | + static final String POSSIBILITY = "This may be due to missing parameter name information"; |
| 41 | + |
| 42 | + static String ACTION = """ |
| 43 | + Ensure that your compiler is configured to use the '-parameters' flag. |
| 44 | + You may need to update both your build tool settings as well as your IDE. |
| 45 | + (See https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-6.x#parameter-name-retention) |
| 46 | + """; |
| 47 | + |
| 48 | + @Override |
| 49 | + public FailureAnalysis analyze(Throwable failure) { |
| 50 | + return analyzeForMissingParameters(failure); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Analyze the given failure for missing parameter name exceptions. |
| 55 | + * @param failure the failure to analyze |
| 56 | + * @return a failure analysis or {@code null} |
| 57 | + */ |
| 58 | + static FailureAnalysis analyzeForMissingParameters(Throwable failure) { |
| 59 | + return analyzeForMissingParameters(failure, failure, new HashSet<>()); |
| 60 | + } |
| 61 | + |
| 62 | + private static FailureAnalysis analyzeForMissingParameters(Throwable rootFailure, Throwable cause, |
| 63 | + Set<Throwable> seen) { |
| 64 | + if (cause != null && seen.add(cause)) { |
| 65 | + if (isSpringParametersException(cause)) { |
| 66 | + return getAnalysis(rootFailure, cause); |
| 67 | + } |
| 68 | + FailureAnalysis analysis = analyzeForMissingParameters(rootFailure, cause.getCause(), seen); |
| 69 | + if (analysis != null) { |
| 70 | + return analysis; |
| 71 | + } |
| 72 | + for (Throwable suppressed : cause.getSuppressed()) { |
| 73 | + analysis = analyzeForMissingParameters(rootFailure, suppressed, seen); |
| 74 | + if (analysis != null) { |
| 75 | + return analysis; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + private static boolean isSpringParametersException(Throwable failure) { |
| 83 | + String message = failure.getMessage(); |
| 84 | + return message != null && message.contains(USE_PARAMETERS_MESSAGE) && isSpringException(failure); |
| 85 | + } |
| 86 | + |
| 87 | + private static boolean isSpringException(Throwable failure) { |
| 88 | + StackTraceElement[] elements = failure.getStackTrace(); |
| 89 | + return elements.length > 0 && isSpringClass(elements[0].getClassName()); |
| 90 | + } |
| 91 | + |
| 92 | + private static boolean isSpringClass(String className) { |
| 93 | + return className != null && className.startsWith("org.springframework."); |
| 94 | + } |
| 95 | + |
| 96 | + private static FailureAnalysis getAnalysis(Throwable rootFailure, Throwable cause) { |
| 97 | + StringBuilder description = new StringBuilder(String.format("%s:%n", cause.getMessage())); |
| 98 | + if (rootFailure != cause) { |
| 99 | + description.append(String.format("%n Resulting Failure: %s", getExceptionTypeAndMessage(rootFailure))); |
| 100 | + } |
| 101 | + return new FailureAnalysis(description.toString(), ACTION, rootFailure); |
| 102 | + } |
| 103 | + |
| 104 | + private static String getExceptionTypeAndMessage(Throwable ex) { |
| 105 | + String message = ex.getMessage(); |
| 106 | + return ex.getClass().getName() + (StringUtils.hasText(message) ? ": " + message : ""); |
| 107 | + } |
| 108 | + |
| 109 | + public static void appendPossibility(StringBuilder description) { |
| 110 | + if (!description.toString().endsWith(System.lineSeparator())) { |
| 111 | + description.append("%n".formatted()); |
| 112 | + } |
| 113 | + description.append("%n%s".formatted(POSSIBILITY)); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments