diff --git a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java index cffba67..fe2604c 100644 --- a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java +++ b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java @@ -44,7 +44,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -214,7 +213,7 @@ public class PluginDescriptorGenerator if ( StringUtils.isEmpty( packageName ) ) { - packageName = discoverPackageName( pluginDescriptor ); + packageName = PluginUtils.discoverPackageName( pluginDescriptor ); } if ( StringUtils.isNotEmpty( packageName ) ) { @@ -272,55 +271,6 @@ public class PluginDescriptorGenerator return descriptor; } - /** - * Find the best package name, based on the number of hits of actual Mojo classes. - * - * @param pluginDescriptor not null - * @return the best name of the package for the generated mojo - */ - private static String discoverPackageName( PluginDescriptor pluginDescriptor ) - { - Map packageNames = new HashMap(); - @SuppressWarnings( "unchecked" ) - List descriptors = pluginDescriptor.getMojos(); - for ( MojoDescriptor descriptor : descriptors ) - { - String impl = descriptor.getImplementation(); - if ( impl.lastIndexOf( '.' ) != -1 ) - { - String name = impl.substring( 0, impl.lastIndexOf( '.' ) ); - if ( packageNames.get( name ) != null ) - { - int next = packageNames.get( name ).intValue() + 1; - packageNames.put( name, new Integer( next ) ); - } - else - { - packageNames.put( name, new Integer( 1 ) ); - } - } - else - { - packageNames.put( "", new Integer( 1 ) ); - } - } - - String packageName = ""; - int max = 0; - for ( Map.Entry entry : packageNames.entrySet() ) - { - String key = entry.getKey(); - int value = entry.getValue().intValue(); - if ( value > max ) - { - max = value; - packageName = key; - } - } - - return packageName; - } - protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w ) { processMojoDescriptor( mojoDescriptor, w, false ); @@ -696,7 +646,7 @@ public class PluginDescriptorGenerator protected String rewriteHelpClassToMojoPackage( PluginToolsRequest request ) throws GeneratorException { - String destinationPackage = PluginHelpGenerator.discoverPackageName( request.getPluginDescriptor() ); + String destinationPackage = PluginUtils.discoverPackageName( request.getPluginDescriptor() ); if ( StringUtils.isEmpty( destinationPackage ) ) { return null; diff --git a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java index 8f48f64..3a23e0e 100644 --- a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java +++ b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java @@ -24,6 +24,7 @@ import org.apache.maven.plugin.descriptor.Parameter; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.project.MavenProject; import org.apache.maven.tools.plugin.PluginToolsRequest; +import org.apache.maven.tools.plugin.util.PluginUtils; import org.apache.velocity.VelocityContext; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.logging.Logger; @@ -40,10 +41,7 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StringWriter; import java.io.Writer; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Properties; /** @@ -104,6 +102,7 @@ public class PluginHelpGenerator MojoDescriptor helpDescriptor = makeHelpDescriptor( pluginDescriptor ); + @SuppressWarnings( "unchecked" ) List mojoDescriptors = pluginDescriptor.getMojos(); if ( mojoDescriptors != null ) @@ -251,7 +250,7 @@ public class PluginHelpGenerator String packageName = helpPackageName; if ( StringUtils.isEmpty( packageName ) ) { - packageName = discoverPackageName( pluginDescriptor ); + packageName = PluginUtils.discoverPackageName( pluginDescriptor ); } if ( StringUtils.isNotEmpty( packageName ) ) { @@ -308,62 +307,4 @@ public class PluginHelpGenerator return descriptor; } - - /** - * Find the best package name, based on the number of hits of actual Mojo classes. - * - * @param pluginDescriptor not null - * @return the best name of the package for the generated mojo - */ - protected static String discoverPackageName( PluginDescriptor pluginDescriptor ) - { - Map packageNames = new HashMap(); - List mojoDescriptors = pluginDescriptor.getMojos(); - if ( mojoDescriptors == null ) - { - return ""; - } - for ( MojoDescriptor descriptor : mojoDescriptors ) - { - - String impl = descriptor.getImplementation(); - if ( StringUtils.equals( descriptor.getGoal(), "help" ) && StringUtils.equals( "HelpMojo", impl ) ) - { - continue; - } - if ( impl.lastIndexOf( '.' ) != -1 ) - { - String name = impl.substring( 0, impl.lastIndexOf( '.' ) ); - if ( packageNames.get( name ) != null ) - { - int next = ( packageNames.get( name ) ).intValue() + 1; - packageNames.put( name, new Integer( next ) ); - } - else - { - packageNames.put( name, new Integer( 1 ) ); - } - } - else - { - packageNames.put( "", new Integer( 1 ) ); - } - } - - String packageName = ""; - int max = 0; - for ( Iterator it = packageNames.keySet().iterator(); it.hasNext(); ) - { - String key = it.next().toString(); - int value = ( packageNames.get( key ) ).intValue(); - if ( value > max ) - { - max = value; - packageName = key; - } - } - - return packageName; - } - } diff --git a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/util/PluginUtils.java b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/util/PluginUtils.java index 462832d..efb6091 100644 --- a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/util/PluginUtils.java +++ b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/util/PluginUtils.java @@ -31,8 +31,10 @@ import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -181,6 +183,7 @@ public final class PluginUtils * false otherwise. * @throws IllegalArgumentException if any */ + @SuppressWarnings( "unchecked" ) public static boolean isMavenReport( String impl, MavenProject project ) throws IllegalArgumentException { @@ -724,4 +727,61 @@ public final class PluginUtils sb.append( text ); } } + + /** + * Find the best package name, based on the number of hits of actual Mojo classes. + * + * @param pluginDescriptor not null + * @return the best name of the package for the generated mojo + */ + public static String discoverPackageName( PluginDescriptor pluginDescriptor ) + { + Map packageNames = new HashMap(); + @SuppressWarnings( "unchecked" ) + List mojoDescriptors = pluginDescriptor.getMojos(); + if ( mojoDescriptors == null ) + { + return ""; + } + for ( MojoDescriptor descriptor : mojoDescriptors ) + { + + String impl = descriptor.getImplementation(); + if ( StringUtils.equals( descriptor.getGoal(), "help" ) && StringUtils.equals( "HelpMojo", impl ) ) + { + continue; + } + if ( impl.lastIndexOf( '.' ) != -1 ) + { + String name = impl.substring( 0, impl.lastIndexOf( '.' ) ); + if ( packageNames.get( name ) != null ) + { + int next = ( packageNames.get( name ) ).intValue() + 1; + packageNames.put( name, new Integer( next ) ); + } + else + { + packageNames.put( name, new Integer( 1 ) ); + } + } + else + { + packageNames.put( "", new Integer( 1 ) ); + } + } + + String packageName = ""; + int max = 0; + for ( Map.Entry entry : packageNames.entrySet() ) + { + int value = entry.getValue().intValue(); + if ( value > max ) + { + max = value; + packageName = entry.getKey(); + } + } + + return packageName; + } }