View Javadoc
1   package org.apache.maven.shared.dependency.graph.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.dependency.graph.DependencyGraphBuilderException;
23  
24  /**
25   * Invokes method on objects using reflection.
26   */
27  final class Invoker
28  {
29      private Invoker()
30      {
31          // do not instantiate
32      }
33  
34      static <T extends Exception> Object invoke( Object object, String method, ExceptionHandler<T> exceptionHandler )
35          throws T
36      {
37          return invoke( object.getClass(), object, method, exceptionHandler );
38      }
39  
40      static <T extends Exception> Object invoke( Class<?> objectClazz, Object object, String method,
41                                                  ExceptionHandler<T> exceptionHandler )
42          throws T
43      {
44          try
45          {
46              return objectClazz.getMethod( method ).invoke( object );
47          }
48          catch ( ReflectiveOperationException e )
49          {
50              throw exceptionHandler.create( e.getMessage(), e );
51          }
52      }
53  
54      static Object invoke( Object object, String method, Class<?> clazz, Object arg )
55          throws DependencyGraphBuilderException
56      {
57          try
58          {
59              final Class<?> objectClazz = object.getClass();
60              return objectClazz.getMethod( method, clazz ).invoke( object, arg );
61          }
62          catch ( ReflectiveOperationException e )
63          {
64              throw new DependencyGraphBuilderException( e.getMessage(), e );
65          }
66      }
67      
68      static <T extends Exception> Object invoke( Class<?> objectClazz, String staticMethod,
69                                                                 Class<?> argClazz, Object arg,
70                                                                 ExceptionHandler<T> exceptionHandler )
71          throws T
72      {
73          try
74          {
75              return objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
76          }
77          catch ( ReflectiveOperationException e )
78          {
79              throw exceptionHandler.create( e.getMessage(), e );
80          }
81      }
82  
83      static <T extends Exception> Object invoke( Class<?> objectClazz, String staticMethod, Class<?> argClazz1,
84                                                  Class<?> argClazz2, Object arg1, Object arg2,
85                                                  ExceptionHandler<T> exceptionHandler )
86          throws T
87      {
88          try
89          {
90              return objectClazz.getMethod( staticMethod, argClazz1, argClazz2 ).invoke( null, arg1, arg2 );
91          }
92          catch ( ReflectiveOperationException e )
93          {
94              throw exceptionHandler.create( e.getMessage(), e );
95          }
96      }
97  }