jjkester commented on a change in pull request #37:
URL: https://github.com/apache/maven-help-plugin/pull/37#discussion_r741297138
##########
File path: src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
##########
@@ -298,8 +300,49 @@ else if ( e.getTargetException() instanceof
RuntimeException )
private static String toString( InputLocation location )
{
- InputSource source = location.getSource();
+ String source = toString( location.getSource() );
+ String hierarchy = toHierarchyString( location.getSource() );
+
+ return '}' + source + ( ( location.getLineNumber() >= 0 ) ? ", line "
+ location.getLineNumber() : "" )
+ + hierarchy + ' ';
+ }
+
+ private static String toHierarchyString( InputSource source )
+ {
+ StringBuilder sb = new StringBuilder();
+
+ try
+ {
+ // InputSource#getImportedBy() added in maven-model 4.0.0-alpha-1
+ Method getImportedBy = InputSource.class.getDeclaredMethod(
"getImportedBy" );
+ InputSource importedBy = (InputSource) getImportedBy.invoke(
source );
+
+ while ( importedBy != null )
+ {
+ sb.append( " via " ).append( toString( importedBy ) );
+ importedBy = (InputSource) getImportedBy.invoke( importedBy );
+ }
+ return sb.toString();
+ }
+ catch ( NoSuchMethodException | SecurityException |
IllegalArgumentException | IllegalAccessException
+ | ClassCastException e )
+ {
+ // Just continue without hierarchy
+ }
+ catch ( InvocationTargetException e )
+ {
+ if ( e.getTargetException() instanceof RuntimeException )
+ {
+ throw (RuntimeException) e.getTargetException();
Review comment:
I am unable to throw a `MojoExecutionException` because this is a
checked exception, and the `InputLocationStringFormatter` does not allow me to
throw it.
I can however create a custom `RuntimeException` and catch it in the
`execute()` method. However, it is not likely that any exception will be thrown
from the reflectively invoked method, so rethrowing the target exception may be
the most appropriate (it does reflect the behavior of a normal non-reflective
method call).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]