View Javadoc
1   package org.apache.maven.shared.transfer.dependencies.collect.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.shared.transfer.dependencies.collect.DependencyCollectorException;
23  
24  import java.lang.reflect.InvocationTargetException;
25  
26  /**
27   * Invokes method on objects using reflection.
28   */
29  final class Invoker
30  {
31      private Invoker()
32      {
33          // do not instantiate
34      }
35  
36      public static <T> T invoke( Object object, String method ) throws DependencyCollectorException
37      {
38          try
39          {
40              @SuppressWarnings( "unchecked" )
41              T invoke = (T) object.getClass().getMethod( method ).invoke( object );
42              return invoke;
43          }
44          catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
45          {
46              throw new DependencyCollectorException( e.getMessage(), e );
47          }
48      }
49  
50      public static <T> T invoke( Class<?> objectClazz, String staticMethod, Class<?> argClazz, Object arg )
51              throws DependencyCollectorException
52      {
53          try
54          {
55              @SuppressWarnings( "unchecked" )
56              T invoke = (T) objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
57              return invoke;
58          }
59          catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
60          {
61              throw new DependencyCollectorException( e.getMessage(), e );
62          }
63      }
64  
65      /**
66       * @param objectClazz  the class of the static method
67       * @param staticMethod the static method to call
68       * @param argClasses   the classes of the argument, used to select the right static method
69       * @param args         the actual arguments to be passed
70       * @return the result of the method invocation
71       * @throws DependencyCollectorException if any checked exception occurs
72       */
73      public static <T> T invoke( Class<?> objectClazz, String staticMethod, Class<?>[] argClasses, Object[] args )
74              throws DependencyCollectorException
75      {
76          if ( args.length != argClasses.length )
77          {
78              throw new IllegalArgumentException( "The number of elements in argClasses and args are not the same." );
79          }
80  
81          try
82          {
83              @SuppressWarnings( "unchecked" )
84              T invoke = (T) objectClazz.getMethod( staticMethod, argClasses ).invoke( null, args );
85              return invoke;
86          }
87          catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
88          {
89              throw new DependencyCollectorException( e.getMessage(), e );
90          }
91      }
92  
93  }