|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.plugins.help; |
| 20 | + |
| 21 | +import java.lang.reflect.Field; |
| 22 | +import java.lang.reflect.InvocationTargetException; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.apache.maven.model.Dependency; |
| 28 | +import org.apache.maven.model.InputLocation; |
| 29 | +import org.apache.maven.model.InputSource; |
| 30 | +import org.apache.maven.project.MavenProject; |
| 31 | +import org.codehaus.plexus.util.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Implementation of {@link InputLocation.StringFormatter}. Enhances the default implementation with support for |
| 35 | + * following "references" (caused by e.g. dependency management imports). |
| 36 | + */ |
| 37 | +public class ImportedFromLocationFormatter extends InputLocation.StringFormatter { |
| 38 | + private final Method getImportedFromMethod; |
| 39 | + private final MavenProject project; |
| 40 | + |
| 41 | + public ImportedFromLocationFormatter(final Method getImportedFromMethod, final MavenProject project) { |
| 42 | + this.getImportedFromMethod = getImportedFromMethod; |
| 43 | + this.project = project; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String toString(InputLocation location) { |
| 48 | + InputSource source = location.getSource(); |
| 49 | + |
| 50 | + String s = source.getModelId(); // by default, display modelId |
| 51 | + |
| 52 | + if (StringUtils.isBlank(s) || s.contains("[unknown-version]")) { |
| 53 | + // unless it is blank or does not provide version information |
| 54 | + s = source.toString(); |
| 55 | + } |
| 56 | + |
| 57 | + InputLocation importedFrom = getImportedFrom(location); |
| 58 | + |
| 59 | + StringBuilder p = new StringBuilder(); |
| 60 | + |
| 61 | + while (importedFrom != null) { |
| 62 | + p.append(" from ").append(importedFrom.getSource().getModelId()); |
| 63 | + importedFrom = getImportedFrom(importedFrom); |
| 64 | + } |
| 65 | + |
| 66 | + return '}' + s + ((location.getLineNumber() >= 0) ? ", line " + location.getLineNumber() : "") + p; |
| 67 | + } |
| 68 | + |
| 69 | + protected InputLocation getImportedFrom(final InputLocation location) { |
| 70 | + try { |
| 71 | + InputLocation result = (InputLocation) getImportedFromMethod.invoke(location); |
| 72 | + |
| 73 | + if (result == null && project != null) { |
| 74 | + for (Dependency dependency : project.getDependencyManagement().getDependencies()) { |
| 75 | + // Until a new maven api model is released, we need to use reflection to access the locations |
| 76 | + Set<?> locationKeys = getLocationKeys(dependency); |
| 77 | + for (Object key : locationKeys) { |
| 78 | + if (!(key instanceof String)) { |
| 79 | + throw new RuntimeException( |
| 80 | + "Expected a String, got " + key.getClass().getName()); |
| 81 | + } |
| 82 | + |
| 83 | + InputLocation dependencyLocation = dependency.getLocation(key); |
| 84 | + if (dependencyLocation != null |
| 85 | + && dependencyLocation.toString().equals(location.toString())) { |
| 86 | + result = (InputLocation) Dependency.class |
| 87 | + .getMethod("getImportedFrom") |
| 88 | + .invoke(dependency); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return result; |
| 96 | + } catch (IllegalAccessException |
| 97 | + | InvocationTargetException |
| 98 | + | NoSuchMethodException |
| 99 | + | NoSuchFieldException |
| 100 | + | ClassNotFoundException e) { |
| 101 | + throw new RuntimeException(e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private Set<?> getLocationKeys(Dependency dependency) |
| 106 | + throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { |
| 107 | + Field delegateField = Class.forName("org.apache.maven.model.BaseObject").getDeclaredField("delegate"); |
| 108 | + delegateField.setAccessible(true); |
| 109 | + Object delegate = delegateField.get(dependency); |
| 110 | + delegateField.setAccessible(false); |
| 111 | + |
| 112 | + Field locationsField = delegate.getClass().getDeclaredField("locations"); |
| 113 | + locationsField.setAccessible(true); |
| 114 | + Object locations = locationsField.get(delegate); |
| 115 | + locationsField.setAccessible(false); |
| 116 | + |
| 117 | + if (!(locations instanceof Map)) { |
| 118 | + throw new RuntimeException( |
| 119 | + "Expected a Map, got " + locations.getClass().getName()); |
| 120 | + } |
| 121 | + |
| 122 | + return ((Map<?, ?>) locations).keySet(); |
| 123 | + } |
| 124 | +} |
0 commit comments