|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 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 | + * http://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.io.PrintWriter; |
| 20 | +import java.io.StringWriter; |
| 21 | +import java.net.URL; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; |
| 26 | +import org.springframework.boot.diagnostics.FailureAnalysis; |
| 27 | +import org.springframework.util.ClassUtils; |
| 28 | + |
| 29 | +/** |
| 30 | + * An {@link AbstractFailureAnalyzer} that analyzes {@link NoSuchMethodError |
| 31 | + * NoSuchMethodErrors}. |
| 32 | + * |
| 33 | + * @author Andy Wilkinson |
| 34 | + */ |
| 35 | +class NoSuchMethodFailureAnalyzer extends AbstractFailureAnalyzer<NoSuchMethodError> { |
| 36 | + |
| 37 | + @Override |
| 38 | + protected FailureAnalysis analyze(Throwable rootFailure, NoSuchMethodError cause) { |
| 39 | + String className = extractClassName(cause); |
| 40 | + if (className == null) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + List<URL> candidates = findCandidates(className); |
| 44 | + if (candidates == null) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + URL actual = getActual(className); |
| 48 | + if (actual == null) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + StringWriter description = new StringWriter(); |
| 52 | + PrintWriter writer = new PrintWriter(description); |
| 53 | + writer.print("An attempt was made to call the method "); |
| 54 | + writer.print(cause.getMessage()); |
| 55 | + writer.print(" but it does not exist. Its class, "); |
| 56 | + writer.print(className); |
| 57 | + writer.println(", is available from the following locations:"); |
| 58 | + writer.println(); |
| 59 | + for (URL candidate : candidates) { |
| 60 | + writer.print(" "); |
| 61 | + writer.println(candidate); |
| 62 | + } |
| 63 | + writer.println(); |
| 64 | + writer.println("It was loaded from the following location:"); |
| 65 | + writer.println(); |
| 66 | + writer.print(" "); |
| 67 | + writer.println(actual); |
| 68 | + return new FailureAnalysis(description.toString(), |
| 69 | + "Correct the classpath of your application so that it contains a single," |
| 70 | + + " compatible version of " + className, |
| 71 | + cause); |
| 72 | + } |
| 73 | + |
| 74 | + private String extractClassName(NoSuchMethodError cause) { |
| 75 | + int descriptorIndex = cause.getMessage().indexOf('('); |
| 76 | + if (descriptorIndex == -1) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + String classAndMethodName = cause.getMessage().substring(0, descriptorIndex); |
| 80 | + int methodNameIndex = classAndMethodName.lastIndexOf('.'); |
| 81 | + if (methodNameIndex == -1) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + return classAndMethodName.substring(0, methodNameIndex); |
| 85 | + } |
| 86 | + |
| 87 | + private List<URL> findCandidates(String className) { |
| 88 | + try { |
| 89 | + return Collections.list((NoSuchMethodFailureAnalyzer.class.getClassLoader() |
| 90 | + .getResources(ClassUtils.convertClassNameToResourcePath(className) |
| 91 | + + ".class"))); |
| 92 | + } |
| 93 | + catch (Throwable ex) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private URL getActual(String className) { |
| 99 | + try { |
| 100 | + return getClass().getClassLoader().loadClass(className).getProtectionDomain() |
| 101 | + .getCodeSource().getLocation(); |
| 102 | + } |
| 103 | + catch (Throwable ex) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments