[maven-artifact-transfer] branch MSHARED-864 updated (24359e0 -> fe56741)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[maven-artifact-transfer] branch MSHARED-864 updated (24359e0 -> fe56741)

khmarbaise
This is an automated email from the ASF dual-hosted git repository.

khmarbaise pushed a change to branch MSHARED-864
in repository https://gitbox.apache.org/repos/asf/maven-artifact-transfer.git.


    from 24359e0  Reformatting
     new a0b3913  Further refactoring.
     new fe56741  improved code.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../transfer/artifact/deploy/internal/Invoker.java |  34 ++----
 .../deploy/internal/Maven30ArtifactDeployer.java   |   2 -
 .../deploy/internal/Maven31ArtifactDeployer.java   |   2 +-
 .../artifact/install/internal/Invoker.java         |  12 ++-
 .../artifact/resolve/internal/Invoker.java         |  43 ++------
 .../resolve/internal/Maven31ArtifactResolver.java  |   8 +-
 .../transfer/collection/internal/Invoker.java      |  49 ++-------
 .../dependencies/collect/internal/Invoker.java     | 119 +++++++++------------
 .../internal/Maven30DependencyCollector.java       |  86 +++++++--------
 .../internal/Maven31DependencyCollector.java       |  87 +++++++--------
 .../dependencies/resolve/internal/Invoker.java     |  70 +++---------
 .../resolve/internal/Maven30ArtifactResult.java    |   8 +-
 .../install/internal/DefaultProjectInstaller.java  |   4 +-
 .../transfer/repository/internal/Invoker.java      |  48 ++-------
 .../internal/Maven31RepositoryManager.java         |   2 +-
 15 files changed, 194 insertions(+), 380 deletions(-)

Reply | Threaded
Open this post in threaded view
|

[maven-artifact-transfer] 01/02: Further refactoring.

khmarbaise
This is an automated email from the ASF dual-hosted git repository.

khmarbaise pushed a commit to branch MSHARED-864
in repository https://gitbox.apache.org/repos/asf/maven-artifact-transfer.git

commit a0b39133645974688f7eff4dfa22daff04765acc
Author: Karl Heinz Marbaise <[hidden email]>
AuthorDate: Sun Mar 22 13:51:54 2020 +0100

    Further refactoring.
---
 .../transfer/artifact/deploy/internal/Invoker.java |  34 ++----
 .../artifact/install/internal/Invoker.java         |  12 ++-
 .../artifact/resolve/internal/Invoker.java         |  43 ++------
 .../transfer/collection/internal/Invoker.java      |  49 ++-------
 .../dependencies/collect/internal/Invoker.java     | 119 +++++++++------------
 .../internal/Maven30DependencyCollector.java       |  86 +++++++--------
 .../internal/Maven31DependencyCollector.java       |  87 +++++++--------
 .../dependencies/resolve/internal/Invoker.java     |  70 +++---------
 .../resolve/internal/Maven30ArtifactResult.java    |   8 +-
 .../install/internal/DefaultProjectInstaller.java  |   4 +-
 .../transfer/repository/internal/Invoker.java      |   8 +-
 11 files changed, 179 insertions(+), 341 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Invoker.java b/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Invoker.java
index be93619..485e58d 100644
--- a/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Invoker.java
@@ -36,25 +36,13 @@ final class Invoker
     public static <T> T invoke( Object object, String method )
         throws ArtifactDeployerException
     {
-        return (T) invoke( object.getClass(), object, method );
-    }
-
-    public static <T> T invoke( Class<?> objectClazz, Object object, String method )
-        throws ArtifactDeployerException
-    {
         try
         {
-            return (T) objectClazz.getMethod( method ).invoke( object );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new ArtifactDeployerException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new ArtifactDeployerException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) object.getClass().getMethod( method ).invoke( object );
+            return invoke;
         }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new ArtifactDeployerException( e.getMessage(), e );
         }
@@ -65,17 +53,11 @@ final class Invoker
     {
         try
         {
-            return (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new ArtifactDeployerException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new ArtifactDeployerException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+            return invoke;
         }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new ArtifactDeployerException( e.getMessage(), e );
         }
diff --git a/src/main/java/org/apache/maven/shared/transfer/artifact/install/internal/Invoker.java b/src/main/java/org/apache/maven/shared/transfer/artifact/install/internal/Invoker.java
index d1389ec..20df360 100644
--- a/src/main/java/org/apache/maven/shared/transfer/artifact/install/internal/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/transfer/artifact/install/internal/Invoker.java
@@ -36,15 +36,17 @@ final class Invoker
     public static <T> T invoke( Object object, String method )
         throws ArtifactInstallerException
     {
-        return (T) invoke( object.getClass(), object, method );
+        return invoke( object.getClass(), object, method );
     }
 
-    public static <T> T invoke( Class<?> objectClazz, Object object, String method )
+    private static <T> T invoke( Class<?> objectClazz, Object object, String method )
         throws ArtifactInstallerException
     {
         try
         {
-            return (T) objectClazz.getMethod( method ).invoke( object );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( method ).invoke( object );
+            return invoke;
         }
         catch ( IllegalAccessException e )
         {
@@ -65,7 +67,9 @@ final class Invoker
     {
         try
         {
-            return (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+            return invoke;
         }
         catch ( IllegalAccessException e )
         {
diff --git a/src/main/java/org/apache/maven/shared/transfer/artifact/resolve/internal/Invoker.java b/src/main/java/org/apache/maven/shared/transfer/artifact/resolve/internal/Invoker.java
index ebbea34..e46c199 100644
--- a/src/main/java/org/apache/maven/shared/transfer/artifact/resolve/internal/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/transfer/artifact/resolve/internal/Invoker.java
@@ -28,6 +28,7 @@ import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverExcepti
  */
 final class Invoker
 {
+
     private Invoker()
     {
         // do not instantiate
@@ -36,15 +37,17 @@ final class Invoker
     public static <T> T invoke( Object object, String method )
         throws ArtifactResolverException
     {
-        return (T) invoke( object.getClass(), object, method );
+        return invoke( object.getClass(), object, method );
     }
 
-    public static <T> T invoke( Class<?> objectClazz, Object object, String method )
+    private static <T> T invoke( Class<?> objectClazz, Object object, String method )
         throws ArtifactResolverException
     {
         try
         {
-            return (T) objectClazz.getMethod( method ).invoke( object );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( method ).invoke( object );
+            return invoke;
         }
         catch ( IllegalAccessException e )
         {
@@ -65,7 +68,9 @@ final class Invoker
     {
         try
         {
-            return (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+            return invoke;
         }
         catch ( IllegalAccessException e )
         {
@@ -81,34 +86,4 @@ final class Invoker
         }
     }
 
-    /**
-     * <strong>Note:</strong> Ensure that argClasses and args have the same number of elements
-     *
-     * @param objectClazz the class of the static method
-     * @param staticMethod the static method to call
-     * @param argClasses the classes of the argument, used to select the right static method
-     * @param args the actual arguments to be passed
-     * @return the result of the method invocation
-     * @throws ArtifactResolverException if any checked exception occurs
-     */
-    public static Object invoke( Class<?> objectClazz, String staticMethod, Class<?>[] argClasses, Object[] args )
-        throws ArtifactResolverException
-    {
-        try
-        {
-            return objectClazz.getMethod( staticMethod, argClasses ).invoke( null, args );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new ArtifactResolverException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new ArtifactResolverException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
-        {
-            throw new ArtifactResolverException( e.getMessage(), e );
-        }
-    }
 }
diff --git a/src/main/java/org/apache/maven/shared/transfer/collection/internal/Invoker.java b/src/main/java/org/apache/maven/shared/transfer/collection/internal/Invoker.java
index 5e07349..c5290ac 100644
--- a/src/main/java/org/apache/maven/shared/transfer/collection/internal/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/transfer/collection/internal/Invoker.java
@@ -20,10 +20,9 @@ package org.apache.maven.shared.transfer.collection.internal;
  */
 
 import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 
-import org.apache.maven.shared.transfer.dependencies.collect.DependencyCollectorException;
 import org.apache.maven.shared.transfer.collection.DependencyCollectionException;
+import org.apache.maven.shared.transfer.dependencies.collect.DependencyCollectorException;
 
 /**
  * Invokes method on objects using reflection.
@@ -38,28 +37,13 @@ final class Invoker
     public static <T> T invoke( Object object, String method )
         throws DependencyCollectionException
     {
-        @SuppressWarnings( "unchecked" )
-        T invoke = (T) invoke( object.getClass(), object, method );
-        return invoke;
-    }
-
-    @SuppressWarnings( "unchecked" )
-    public static <T> T invoke( Class<?> objectClazz, Object object, String method )
-        throws DependencyCollectionException
-    {
         try
         {
-            return (T) objectClazz.getMethod( method ).invoke( object );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyCollectionException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyCollectionException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) object.getClass().getMethod( method ).invoke( object );
+            return invoke;
         }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyCollectionException( e.getMessage(), e );
         }
@@ -70,20 +54,11 @@ final class Invoker
     {
         try
         {
-            Method method = objectClazz.getMethod( staticMethod, argClazz );
             @SuppressWarnings( "unchecked" )
-            T invoke = (T) method.invoke( null, arg );
+            T invoke = (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
             return invoke;
         }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyCollectionException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyCollectionException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyCollectionException( e.getMessage(), e );
         }
@@ -108,15 +83,7 @@ final class Invoker
             T invoke = (T) objectClazz.getMethod( staticMethod, argClasses ).invoke( null, args );
             return invoke;
         }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyCollectionException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyCollectionException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyCollectionException( e.getMessage(), e );
         }
diff --git a/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Invoker.java b/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Invoker.java
index 5a03346..6540fae 100644
--- a/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Invoker.java
@@ -33,105 +33,86 @@ final class Invoker
         // do not instantiate
     }
 
-    public static <T> T invoke( Object object, String method )
-        throws DependencyCollectorException
-    {
-        T invoke = invoke( object.getClass(), object, method );
-        return (T) invoke;
-    }
-
-    public static <T> T invoke( Class<?> objectClazz, Object object, String method )
-        throws DependencyCollectorException
+    public static <T> T invoke( Object object, String method ) throws DependencyCollectorException
     {
         try
         {
             @SuppressWarnings( "unchecked" )
-            T invoke = (T) objectClazz.getMethod( method ).invoke( object );
+            T invoke = (T) object.getClass().getMethod( method ).invoke( object );
             return invoke;
         }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyCollectorException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyCollectorException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyCollectorException( e.getMessage(), e );
         }
     }
 
-    public static Object invoke( Object object, String method, Class<?> argClazz, Object arg )
-        throws DependencyCollectorException
-    {
-        try
-        {
-            final Class<?> objectClazz = object.getClass();
-            return objectClazz.getMethod( method, argClazz ).invoke( object, arg );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyCollectorException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyCollectorException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
-        {
-            throw new DependencyCollectorException( e.getMessage(), e );
-        }
-    }
-    
     public static <T> T invoke( Class<?> objectClazz, String staticMethod, Class<?> argClazz, Object arg )
-                    throws DependencyCollectorException
+            throws DependencyCollectorException
     {
         try
         {
-            return (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyCollectorException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyCollectorException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+            return invoke;
         }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyCollectorException( e.getMessage(), e );
         }
     }
-    
+    //
+    //    public interface ExceptionFactory<E extends Exception>
+    //    {
+    //        E create(String message);
+    //    }
+    //
+    //    public static <T, E extends Exception> T invokeX( Class<?> objectClazz,
+    //   String staticMethod, Class<?> argClazz, Object arg )
+    //            throws E
+    //    {
+    //        try
+    //        {
+    //            return (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+    //        }
+    //        catch ( IllegalAccessException e )
+    //        {
+    //
+    //            throw new DependencyCollectorException( e.getMessage(), e );
+    //        }
+    //        catch ( InvocationTargetException e )
+    //        {
+    //            throw new DependencyCollectorException( e.getMessage(), e );
+    //        }
+    //        catch ( NoSuchMethodException e )
+    //        {
+    //            throw new DependencyCollectorException( e.getMessage(), e );
+    //        }
+    //    }
+
     /**
-     * <strong>Note:</strong> Ensure that argClasses and args have the same number of elements
-     *
-     * @param objectClazz the class of the static method
+     * @param objectClazz  the class of the static method
      * @param staticMethod the static method to call
-     * @param argClasses the classes of the argument, used to select the right static method
-     * @param args the actual arguments to be passed
+     * @param argClasses   the classes of the argument, used to select the right static method
+     * @param args         the actual arguments to be passed
      * @return the result of the method invocation
      * @throws DependencyCollectorException if any checked exception occurs
      */
-    public static Object invoke( Class<?> objectClazz, String staticMethod, Class<?>[] argClasses, Object[] args )
-                    throws DependencyCollectorException
+    public static <T> T invoke( Class<?> objectClazz, String staticMethod, Class<?>[] argClasses, Object[] args )
+            throws DependencyCollectorException
     {
-        try
+        if ( args.length != argClasses.length )
         {
-            return objectClazz.getMethod( staticMethod, argClasses ).invoke( null, args );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyCollectorException( e.getMessage(), e );
+            throw new IllegalArgumentException( "The number of elements in argClasses and args is not the same." );
         }
-        catch ( InvocationTargetException e )
+
+        try
         {
-            throw new DependencyCollectorException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( staticMethod, argClasses ).invoke( null, args );
+            return invoke;
         }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyCollectorException( e.getMessage(), e );
         }
diff --git a/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Maven30DependencyCollector.java b/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Maven30DependencyCollector.java
index 057e0bd..1a33fbc 100644
--- a/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Maven30DependencyCollector.java
+++ b/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Maven30DependencyCollector.java
@@ -42,23 +42,21 @@ import org.sonatype.aether.util.artifact.DefaultArtifact;
 
 /**
  * Maven 3.0 implementation of the {@link DependencyCollector}
- *
- * @author Robert Scholte
  *
+ * @author Robert Scholte
  */
-class Maven30DependencyCollector
-    implements MavenDependencyCollector
+class Maven30DependencyCollector implements MavenDependencyCollector
 {
     private final RepositorySystem repositorySystem;
 
     private final ArtifactHandlerManager artifactHandlerManager;
 
     private final RepositorySystemSession session;
-    
+
     private final List<RemoteRepository> aetherRepositories;
-    
+
     Maven30DependencyCollector( RepositorySystem repositorySystem, ArtifactHandlerManager artifactHandlerManager,
-                                RepositorySystemSession session, List<RemoteRepository> aetherRepositories )
+            RepositorySystemSession session, List<RemoteRepository> aetherRepositories )
     {
         super();
         this.repositorySystem = repositorySystem;
@@ -67,13 +65,22 @@ class Maven30DependencyCollector
         this.aetherRepositories = aetherRepositories;
     }
 
+    private static Dependency toDependency( org.apache.maven.model.Dependency mavenDependency,
+            ArtifactTypeRegistry typeRegistry ) throws DependencyCollectorException
+    {
+        Class<?>[] argClasses = new Class<?>[] {org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class};
+
+        Object[] args = new Object[] {mavenDependency, typeRegistry};
+
+        return (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args );
+    }
+
     @Override
     public CollectorResult collectDependencies( org.apache.maven.model.Dependency root )
-        throws DependencyCollectorException
+            throws DependencyCollectorException
     {
-        ArtifactTypeRegistry typeRegistry =
-                        (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
-                                                               ArtifactHandlerManager.class, artifactHandlerManager );
+        ArtifactTypeRegistry typeRegistry = Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
+                ArtifactHandlerManager.class, artifactHandlerManager );
 
         CollectRequest request = new CollectRequest();
         request.setRoot( toDependency( root, typeRegistry ) );
@@ -82,42 +89,39 @@ class Maven30DependencyCollector
     }
 
     @Override
-    public CollectorResult collectDependencies( DependableCoordinate root )
-        throws DependencyCollectorException
+    public CollectorResult collectDependencies( DependableCoordinate root ) throws DependencyCollectorException
     {
         ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( root.getType() );
-        
+
         String extension = artifactHandler != null ? artifactHandler.getExtension() : null;
-        
+
         Artifact aetherArtifact = new DefaultArtifact( root.getGroupId(), root.getArtifactId(), root.getClassifier(),
-                                                       extension, root.getVersion() );
-        
+                extension, root.getVersion() );
+
         CollectRequest request = new CollectRequest();
         request.setRoot( new Dependency( aetherArtifact, null ) );
 
         return collectDependencies( request );
     }
-    
+
     @Override
-    public CollectorResult collectDependencies( Model root )
-        throws DependencyCollectorException
+    public CollectorResult collectDependencies( Model root ) throws DependencyCollectorException
     {
         // Are there examples where packaging and type are NOT in sync
         ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( root.getPackaging() );
-        
+
         String extension = artifactHandler != null ? artifactHandler.getExtension() : null;
-        
-        Artifact aetherArtifact =
-            new DefaultArtifact( root.getGroupId(), root.getArtifactId(), extension, root.getVersion() );
-        
+
+        Artifact aetherArtifact = new DefaultArtifact( root.getGroupId(), root.getArtifactId(), extension,
+                root.getVersion() );
+
         CollectRequest request = new CollectRequest();
         request.setRoot( new Dependency( aetherArtifact, null ) );
 
-        ArtifactTypeRegistry typeRegistry =
-                        (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
-                                                               ArtifactHandlerManager.class, artifactHandlerManager );
+        ArtifactTypeRegistry typeRegistry = Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
+                ArtifactHandlerManager.class, artifactHandlerManager );
 
-        List<Dependency> aetherDependencies = new ArrayList<Dependency>( root.getDependencies().size() );
+        List<Dependency> aetherDependencies = new ArrayList<>( root.getDependencies().size() );
         for ( org.apache.maven.model.Dependency mavenDependency : root.getDependencies() )
         {
             aetherDependencies.add( toDependency( mavenDependency, typeRegistry ) );
@@ -126,22 +130,21 @@ class Maven30DependencyCollector
 
         if ( root.getDependencyManagement() != null )
         {
-            List<Dependency> aetherManagerDependencies =
-                new ArrayList<Dependency>( root.getDependencyManagement().getDependencies().size() );
-            
+            List<Dependency> aetherManagerDependencies = new ArrayList<>(
+                    root.getDependencyManagement().getDependencies().size() );
+
             for ( org.apache.maven.model.Dependency mavenDependency : root.getDependencyManagement().getDependencies() )
             {
                 aetherManagerDependencies.add( toDependency( mavenDependency, typeRegistry ) );
             }
-            
+
             request.setManagedDependencies( aetherManagerDependencies );
         }
-        
+
         return collectDependencies( request );
     }
 
-    private CollectorResult collectDependencies( CollectRequest request )
-        throws DependencyCollectorException
+    private CollectorResult collectDependencies( CollectRequest request ) throws DependencyCollectorException
     {
         request.setRepositories( aetherRepositories );
 
@@ -154,15 +157,4 @@ class Maven30DependencyCollector
             throw new DependencyCollectorException( e.getMessage(), e );
         }
     }
-
-    private static Dependency toDependency( org.apache.maven.model.Dependency mavenDependency,
-                                            ArtifactTypeRegistry typeRegistry )
-        throws DependencyCollectorException
-    {
-        Class<?>[] argClasses = new Class<?>[] { org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class };
-
-        Object[] args = new Object[] { mavenDependency, typeRegistry };
-
-        return (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args );
-    }
 }
diff --git a/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Maven31DependencyCollector.java b/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Maven31DependencyCollector.java
index 0ee0385..de98210 100644
--- a/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Maven31DependencyCollector.java
+++ b/src/main/java/org/apache/maven/shared/transfer/dependencies/collect/internal/Maven31DependencyCollector.java
@@ -42,23 +42,21 @@ import org.eclipse.aether.repository.RemoteRepository;
 
 /**
  * Maven 3.1+ implementation of the {@link DependencyCollector}
- *
- * @author Robert Scholte
  *
+ * @author Robert Scholte
  */
-class Maven31DependencyCollector
-    implements MavenDependencyCollector
+class Maven31DependencyCollector implements MavenDependencyCollector
 {
     private final RepositorySystem repositorySystem;
 
     private final ArtifactHandlerManager artifactHandlerManager;
-    
+
     private final RepositorySystemSession session;
-    
+
     private final List<RemoteRepository> aetherRepositories;
-    
+
     Maven31DependencyCollector( RepositorySystem repositorySystem, ArtifactHandlerManager artifactHandlerManager,
-                                RepositorySystemSession session, List<RemoteRepository> aetherRepositories )
+            RepositorySystemSession session, List<RemoteRepository> aetherRepositories )
     {
         super();
         this.repositorySystem = repositorySystem;
@@ -67,13 +65,22 @@ class Maven31DependencyCollector
         this.aetherRepositories = aetherRepositories;
     }
 
+    private static Dependency toDependency( org.apache.maven.model.Dependency mavenDependency,
+            ArtifactTypeRegistry typeRegistry ) throws DependencyCollectorException
+    {
+        Class<?>[] argClasses = new Class<?>[] {org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class};
+
+        Object[] args = new Object[] {mavenDependency, typeRegistry};
+
+        return (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args );
+    }
+
     @Override
     public CollectorResult collectDependencies( org.apache.maven.model.Dependency root )
-        throws DependencyCollectorException
+            throws DependencyCollectorException
     {
-        ArtifactTypeRegistry typeRegistry =
-                        (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
-                                                               ArtifactHandlerManager.class, artifactHandlerManager );
+        ArtifactTypeRegistry typeRegistry = Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
+                ArtifactHandlerManager.class, artifactHandlerManager );
 
         CollectRequest request = new CollectRequest();
         request.setRoot( toDependency( root, typeRegistry ) );
@@ -82,42 +89,39 @@ class Maven31DependencyCollector
     }
 
     @Override
-    public CollectorResult collectDependencies( DependableCoordinate root )
-        throws DependencyCollectorException
+    public CollectorResult collectDependencies( DependableCoordinate root ) throws DependencyCollectorException
     {
         ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( root.getType() );
-        
+
         String extension = artifactHandler != null ? artifactHandler.getExtension() : null;
-        
+
         Artifact aetherArtifact = new DefaultArtifact( root.getGroupId(), root.getArtifactId(), root.getClassifier(),
-                                                       extension, root.getVersion() );
-        
+                extension, root.getVersion() );
+
         CollectRequest request = new CollectRequest();
         request.setRoot( new Dependency( aetherArtifact, null ) );
 
         return collectDependencies( request );
     }
-    
+
     @Override
-    public CollectorResult collectDependencies( Model root )
-        throws DependencyCollectorException
+    public CollectorResult collectDependencies( Model root ) throws DependencyCollectorException
     {
         // Are there examples where packaging and type are NOT in sync
         ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( root.getPackaging() );
-        
+
         String extension = artifactHandler != null ? artifactHandler.getExtension() : null;
-        
-        Artifact aetherArtifact =
-            new DefaultArtifact( root.getGroupId(), root.getArtifactId(), extension, root.getVersion() );
-        
+
+        Artifact aetherArtifact = new DefaultArtifact( root.getGroupId(), root.getArtifactId(), extension,
+                root.getVersion() );
+
         CollectRequest request = new CollectRequest();
         request.setRoot( new Dependency( aetherArtifact, null ) );
-        
-        ArtifactTypeRegistry typeRegistry =
-                        (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
-                                                               ArtifactHandlerManager.class, artifactHandlerManager );
 
-        List<Dependency> aetherDependencies = new ArrayList<Dependency>( root.getDependencies().size() );
+        ArtifactTypeRegistry typeRegistry = Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
+                ArtifactHandlerManager.class, artifactHandlerManager );
+
+        List<Dependency> aetherDependencies = new ArrayList<>( root.getDependencies().size() );
         for ( org.apache.maven.model.Dependency mavenDependency : root.getDependencies() )
         {
             aetherDependencies.add( toDependency( mavenDependency, typeRegistry ) );
@@ -126,22 +130,21 @@ class Maven31DependencyCollector
 
         if ( root.getDependencyManagement() != null )
         {
-            List<Dependency> aetherManagerDependencies =
-                new ArrayList<Dependency>( root.getDependencyManagement().getDependencies().size() );
-            
+            List<Dependency> aetherManagerDependencies = new ArrayList<>(
+                    root.getDependencyManagement().getDependencies().size() );
+
             for ( org.apache.maven.model.Dependency mavenDependency : root.getDependencyManagement().getDependencies() )
             {
                 aetherManagerDependencies.add( toDependency( mavenDependency, typeRegistry ) );
             }
-            
+
             request.setManagedDependencies( aetherManagerDependencies );
         }
 
         return collectDependencies( request );
     }
 
-    private CollectorResult collectDependencies( CollectRequest request )
-        throws DependencyCollectorException
+    private CollectorResult collectDependencies( CollectRequest request ) throws DependencyCollectorException
     {
         request.setRepositories( aetherRepositories );
 
@@ -154,14 +157,4 @@ class Maven31DependencyCollector
             throw new DependencyCollectorException( e.getMessage(), e );
         }
     }
-
-    private static Dependency toDependency( org.apache.maven.model.Dependency root, ArtifactTypeRegistry typeRegistry )
-                    throws DependencyCollectorException
-    {
-        Class<?>[] argClasses = new Class<?>[] { org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class };
-
-        Object[] args = new Object[] { root, typeRegistry };
-
-        return (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args );
-    }
 }
diff --git a/src/main/java/org/apache/maven/shared/transfer/dependencies/resolve/internal/Invoker.java b/src/main/java/org/apache/maven/shared/transfer/dependencies/resolve/internal/Invoker.java
index e20ea96..9055f14 100644
--- a/src/main/java/org/apache/maven/shared/transfer/dependencies/resolve/internal/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/transfer/dependencies/resolve/internal/Invoker.java
@@ -36,47 +36,13 @@ final class Invoker
     public static <T> T invoke( Object object, String method )
         throws DependencyResolverException
     {
-        return (T) invoke( object.getClass(), object, method );
-    }
-
-    public static <T> T invoke( Class<?> objectClazz, Object object, String method )
-        throws DependencyResolverException
-    {
-        try
-        {
-            return (T) objectClazz.getMethod( method ).invoke( object );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
-        }
-    }
-
-    public static Object invoke( Object object, String method, Class<?> argClazz, Object arg )
-        throws DependencyResolverException
-    {
         try
         {
-            final Class<?> objectClazz = object.getClass();
-            return objectClazz.getMethod( method, argClazz ).invoke( object, arg );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) object.getClass().getMethod( method ).invoke( object );
+            return invoke;
         }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyResolverException( e.getMessage(), e );
         }
@@ -87,17 +53,11 @@ final class Invoker
     {
         try
         {
-            return (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+            return invoke;
         }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyResolverException( e.getMessage(), e );
         }
@@ -118,17 +78,11 @@ final class Invoker
     {
         try
         {
-            return (T) objectClazz.getMethod( staticMethod, argClasses ).invoke( null, args );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new DependencyResolverException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( staticMethod, argClasses ).invoke( null, args );
+            return invoke;
         }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new DependencyResolverException( e.getMessage(), e );
         }
diff --git a/src/main/java/org/apache/maven/shared/transfer/dependencies/resolve/internal/Maven30ArtifactResult.java b/src/main/java/org/apache/maven/shared/transfer/dependencies/resolve/internal/Maven30ArtifactResult.java
index 9ba5af8..b9f37d9 100644
--- a/src/main/java/org/apache/maven/shared/transfer/dependencies/resolve/internal/Maven30ArtifactResult.java
+++ b/src/main/java/org/apache/maven/shared/transfer/dependencies/resolve/internal/Maven30ArtifactResult.java
@@ -26,12 +26,11 @@ import org.sonatype.aether.resolution.ArtifactResult;
 
 /**
  * {@link org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult} wrapper for {@link ArtifactResult}
- *
+ *
  * @author Robert Scholte
  * @since 3.0
  */
-class Maven30ArtifactResult
-    implements org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult
+class Maven30ArtifactResult implements org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult
 {
     private final ArtifactResult artifactResult;
 
@@ -48,8 +47,7 @@ class Maven30ArtifactResult
     {
         try
         {
-            return Invoker.invoke( RepositoryUtils.class, "toArtifact",
-                                                                        Artifact.class, artifactResult.getArtifact() );
+            return Invoker.invoke( RepositoryUtils.class, "toArtifact", Artifact.class, artifactResult.getArtifact() );
         }
         catch ( DependencyResolverException e )
         {
diff --git a/src/main/java/org/apache/maven/shared/transfer/project/install/internal/DefaultProjectInstaller.java b/src/main/java/org/apache/maven/shared/transfer/project/install/internal/DefaultProjectInstaller.java
index dd6de0d..02ec826 100644
--- a/src/main/java/org/apache/maven/shared/transfer/project/install/internal/DefaultProjectInstaller.java
+++ b/src/main/java/org/apache/maven/shared/transfer/project/install/internal/DefaultProjectInstaller.java
@@ -149,10 +149,8 @@ class DefaultProjectInstaller
         }
     }
 
-    // CHECKSTYLE_OFF: LineLength
     private void addMetaDataFilesForArtifact( ProjectBuildingRequest buildingRequest, Artifact artifact,
-                                              Collection<File> targetMetadataFiles )
-    // CHECKSTYLE_ON: LineLength
+            Collection<File> targetMetadataFiles )
     {
         Collection<ArtifactMetadata> metadatas = artifact.getMetadataList();
         if ( metadatas != null )
diff --git a/src/main/java/org/apache/maven/shared/transfer/repository/internal/Invoker.java b/src/main/java/org/apache/maven/shared/transfer/repository/internal/Invoker.java
index 2da84bc..6f566e0 100644
--- a/src/main/java/org/apache/maven/shared/transfer/repository/internal/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/transfer/repository/internal/Invoker.java
@@ -36,15 +36,9 @@ final class Invoker
     public static Object invoke( Object object, String method )
         throws RepositoryManagerException
     {
-        return invoke( object.getClass(), object, method );
-    }
-
-    public static Object invoke( Class<?> objectClazz, Object object, String method )
-        throws RepositoryManagerException
-    {
         try
         {
-            return objectClazz.getMethod( method ).invoke( object );
+            return object.getClass().getMethod( method ).invoke( object );
         }
         catch ( IllegalAccessException e )
         {

Reply | Threaded
Open this post in threaded view
|

[maven-artifact-transfer] 02/02: improved code.

khmarbaise
In reply to this post by khmarbaise
This is an automated email from the ASF dual-hosted git repository.

khmarbaise pushed a commit to branch MSHARED-864
in repository https://gitbox.apache.org/repos/asf/maven-artifact-transfer.git

commit fe5674105faedbbc5402a27ed0165e038fdc4113
Author: Karl Heinz Marbaise <[hidden email]>
AuthorDate: Thu Mar 26 17:17:48 2020 +0100

    improved code.
---
 .../deploy/internal/Maven30ArtifactDeployer.java   |  2 --
 .../deploy/internal/Maven31ArtifactDeployer.java   |  2 +-
 .../resolve/internal/Maven31ArtifactResolver.java  |  8 ++---
 .../transfer/repository/internal/Invoker.java      | 42 ++++++----------------
 .../internal/Maven31RepositoryManager.java         |  2 +-
 5 files changed, 16 insertions(+), 40 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Maven30ArtifactDeployer.java b/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Maven30ArtifactDeployer.java
index 06a7c93..611eb4c 100644
--- a/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Maven30ArtifactDeployer.java
+++ b/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Maven30ArtifactDeployer.java
@@ -131,11 +131,9 @@ class Maven30ArtifactDeployer
     private RemoteRepository getRemoteRepository( RepositorySystemSession session, ArtifactRepository remoteRepository )
         throws ArtifactDeployerException
     {
-        // CHECKSTYLE_OFF: LineLength
         RemoteRepository aetherRepo = Invoker.invoke( RepositoryUtils.class, "toRepo",
                                                                          ArtifactRepository.class,
                                                                          remoteRepository );
-        // CHECKSTYLE_ON: LineLength
 
         if ( aetherRepo.getAuthentication() == null )
         {
diff --git a/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Maven31ArtifactDeployer.java b/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Maven31ArtifactDeployer.java
index f3bf542..8865c69 100644
--- a/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Maven31ArtifactDeployer.java
+++ b/src/main/java/org/apache/maven/shared/transfer/artifact/deploy/internal/Maven31ArtifactDeployer.java
@@ -102,7 +102,7 @@ class Maven31ArtifactDeployer
                     Artifact pomArtifact = new SubArtifact( aetherArtifact, "", "pom" );
                     pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
                     request.addArtifact( pomArtifact );
-                }
+                 }
                 else if ( // metadata instanceof SnapshotArtifactRepositoryMetadata ||
                 metadata instanceof ArtifactRepositoryMetadata )
                 {
diff --git a/src/main/java/org/apache/maven/shared/transfer/artifact/resolve/internal/Maven31ArtifactResolver.java b/src/main/java/org/apache/maven/shared/transfer/artifact/resolve/internal/Maven31ArtifactResolver.java
index ba1a419..6308ddb 100644
--- a/src/main/java/org/apache/maven/shared/transfer/artifact/resolve/internal/Maven31ArtifactResolver.java
+++ b/src/main/java/org/apache/maven/shared/transfer/artifact/resolve/internal/Maven31ArtifactResolver.java
@@ -56,13 +56,11 @@ class Maven31ArtifactResolver
     }
 
     @Override
-    // CHECKSTYLE_OFF: LineLength
-    public org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( org.apache.maven.artifact.Artifact mavenArtifact )
-                                                                                        throws ArtifactResolverException
-    // CHECKSTYLE_ON: LineLength
+    public org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact(
+            org.apache.maven.artifact.Artifact mavenArtifact ) throws ArtifactResolverException
     {
         Artifact aetherArtifact = Invoker.invoke( RepositoryUtils.class, "toArtifact",
-                                                             org.apache.maven.artifact.Artifact.class, mavenArtifact );
+                org.apache.maven.artifact.Artifact.class, mavenArtifact );
 
         return resolveArtifact( aetherArtifact );
     }
diff --git a/src/main/java/org/apache/maven/shared/transfer/repository/internal/Invoker.java b/src/main/java/org/apache/maven/shared/transfer/repository/internal/Invoker.java
index 6f566e0..48342dc 100644
--- a/src/main/java/org/apache/maven/shared/transfer/repository/internal/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/transfer/repository/internal/Invoker.java
@@ -33,44 +33,32 @@ final class Invoker
         // do not instantiate
     }
 
-    public static Object invoke( Object object, String method )
+    public static <T> T invoke( Object object, String method )
         throws RepositoryManagerException
     {
         try
         {
-            return object.getClass().getMethod( method ).invoke( object );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) object.getClass().getMethod( method ).invoke( object );
+            return invoke;
         }
-        catch ( IllegalAccessException e )
-        {
-            throw new RepositoryManagerException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new RepositoryManagerException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new RepositoryManagerException( e.getMessage(), e );
         }
     }
 
-    public static Object invoke( Object object, String method, Class<?> argClazz, Object arg )
+    public static <T> T invoke( Object object, String method, Class<?> argClazz, Object arg )
         throws RepositoryManagerException
     {
         try
         {
             final Class<?> objectClazz = object.getClass();
-            return objectClazz.getMethod( method, argClazz ).invoke( object, arg );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new RepositoryManagerException( e.getMessage(), e );
+            @SuppressWarnings( "unchecked" )
+            T invoke = (T) objectClazz.getMethod( method, argClazz ).invoke( object, arg );
+            return invoke;
         }
-        catch ( InvocationTargetException e )
-        {
-            throw new RepositoryManagerException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new RepositoryManagerException( e.getMessage(), e );
         }
@@ -83,15 +71,7 @@ final class Invoker
         {
             return objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
         }
-        catch ( IllegalAccessException e )
-        {
-            throw new RepositoryManagerException( e.getMessage(), e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            throw new RepositoryManagerException( e.getMessage(), e );
-        }
-        catch ( NoSuchMethodException e )
+        catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
         {
             throw new RepositoryManagerException( e.getMessage(), e );
         }
diff --git a/src/main/java/org/apache/maven/shared/transfer/repository/internal/Maven31RepositoryManager.java b/src/main/java/org/apache/maven/shared/transfer/repository/internal/Maven31RepositoryManager.java
index 86ae613..6f486a0 100644
--- a/src/main/java/org/apache/maven/shared/transfer/repository/internal/Maven31RepositoryManager.java
+++ b/src/main/java/org/apache/maven/shared/transfer/repository/internal/Maven31RepositoryManager.java
@@ -103,7 +103,7 @@ class Maven31RepositoryManager
         RepositorySystemSession session;
         try
         {
-            session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
+            session = Invoker.invoke( buildingRequest, "getRepositorySession" );
         }
         catch ( RepositoryManagerException e )
         {