forked from apache/maven-help-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed class, formatter can be made functional with maven 3.x
- Loading branch information
1 parent
0d03b43
commit 3073624
Showing
8 changed files
with
187 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/main/java/org/apache/maven/plugins/help/ImportedFromLocationFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.maven.plugins.help; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.apache.maven.model.Dependency; | ||
import org.apache.maven.model.InputLocation; | ||
import org.apache.maven.model.InputSource; | ||
import org.apache.maven.project.MavenProject; | ||
import org.codehaus.plexus.util.StringUtils; | ||
|
||
/** | ||
* Implementation of {@link InputLocation.StringFormatter}. Enhances the default implementation with support for | ||
* following "references" (caused by e.g. dependency management imports). | ||
*/ | ||
public class ImportedFromLocationFormatter extends InputLocation.StringFormatter { | ||
private final Method getImportedFromMethod; | ||
private final MavenProject project; | ||
|
||
public ImportedFromLocationFormatter(final Method getImportedFromMethod, final MavenProject project) { | ||
this.getImportedFromMethod = getImportedFromMethod; | ||
this.project = project; | ||
} | ||
|
||
@Override | ||
public String toString(InputLocation location) { | ||
InputSource source = location.getSource(); | ||
|
||
String s = source.getModelId(); // by default, display modelId | ||
|
||
if (StringUtils.isBlank(s) || s.contains("[unknown-version]")) { | ||
// unless it is blank or does not provide version information | ||
s = source.toString(); | ||
} | ||
|
||
InputLocation importedFrom = getImportedFrom(location); | ||
|
||
StringBuilder p = new StringBuilder(); | ||
|
||
while (importedFrom != null) { | ||
p.append(" from ").append(importedFrom.getSource().getModelId()); | ||
importedFrom = getImportedFrom(importedFrom); | ||
} | ||
|
||
return '}' + s + ((location.getLineNumber() >= 0) ? ", line " + location.getLineNumber() : "") + p; | ||
} | ||
|
||
protected InputLocation getImportedFrom(final InputLocation location) { | ||
try { | ||
InputLocation result = (InputLocation) getImportedFromMethod.invoke(location); | ||
|
||
if (result == null && project != null) { | ||
for (Dependency dependency : project.getDependencyManagement().getDependencies()) { | ||
// Until a new maven api model is released, we need to use reflection to access the locations | ||
Set<?> locationKeys = getLocationKeys(dependency); | ||
for (Object key : locationKeys) { | ||
if (!(key instanceof String)) { | ||
throw new RuntimeException( | ||
"Expected a String, got " + key.getClass().getName()); | ||
} | ||
|
||
InputLocation dependencyLocation = dependency.getLocation(key); | ||
if (dependencyLocation != null | ||
&& dependencyLocation.toString().equals(location.toString())) { | ||
result = (InputLocation) Dependency.class | ||
.getMethod("getImportedFrom") | ||
.invoke(dependency); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} catch (IllegalAccessException | ||
| InvocationTargetException | ||
| NoSuchMethodException | ||
| NoSuchFieldException | ||
| ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private Set<?> getLocationKeys(Dependency dependency) | ||
throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { | ||
Field delegateField = Class.forName("org.apache.maven.model.BaseObject").getDeclaredField("delegate"); | ||
delegateField.setAccessible(true); | ||
Object delegate = delegateField.get(dependency); | ||
delegateField.setAccessible(false); | ||
|
||
Field locationsField = delegate.getClass().getDeclaredField("locations"); | ||
locationsField.setAccessible(true); | ||
Object locations = locationsField.get(delegate); | ||
locationsField.setAccessible(false); | ||
|
||
if (!(locations instanceof Map)) { | ||
throw new RuntimeException( | ||
"Expected a Map, got " + locations.getClass().getName()); | ||
} | ||
|
||
return ((Map<?, ?>) locations).keySet(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 0 additions & 134 deletions
134
src/main/java/org/apache/maven/plugins/help/Maven4InputLocationFormatter.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.