jjkester commented on a change in pull request #37: URL: https://github.com/apache/maven-help-plugin/pull/37#discussion_r741280647
########## File path: src/it/projects/effective-pom-with-bom/invoker.properties ########## @@ -0,0 +1,20 @@ +# 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. + +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:effective-pom Review comment: There are some Windows line feeds from the test files I got in the ticket. I will convert them from CRLF to LF. ########## File path: src/it/projects/effective-pom-with-bom/test.properties ########## @@ -0,0 +1,19 @@ +# 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. + +output = result.txt Review comment: There are some Windows line feeds from the test files I got in the ticket. I will convert them from CRLF to LF. ########## 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 basically took the same approach as the other code in this class. I'll have a quick look around what the consequences are. ########## 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 Review comment: Will do. ########## 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). ########## 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 Review comment: This is actually just the case for the `NoSuchMethodException`. The other exceptions should never be thrown and reflect other issues. I would designate these "unexpected". -- 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]
