View Javadoc
1   package org.apache.maven.shared.transfer.artifact.install.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 java.lang.reflect.InvocationTargetException;
23  
24  import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
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 Object invoke( Object object, String method )
37          throws ArtifactInstallerException
38      {
39          return invoke( object.getClass(), object, method );
40      }
41  
42      public static Object invoke( Class<?> objectClazz, Object object, String method )
43          throws ArtifactInstallerException
44      {
45          try
46          {
47              return objectClazz.getMethod( method ).invoke( object );
48          }
49          catch ( IllegalAccessException e )
50          {
51              throw new ArtifactInstallerException( e.getMessage(), e );
52          }
53          catch ( InvocationTargetException e )
54          {
55              throw new ArtifactInstallerException( e.getMessage(), e );
56          }
57          catch ( NoSuchMethodException e )
58          {
59              throw new ArtifactInstallerException( e.getMessage(), e );
60          }
61      }
62  
63      public static Object invoke( Object object, String method, Class<?> argClazz, Object arg )
64          throws ArtifactInstallerException
65      {
66          try
67          {
68              final Class<?> objectClazz = object.getClass();
69              return objectClazz.getMethod( method, argClazz ).invoke( object, arg );
70          }
71          catch ( IllegalAccessException e )
72          {
73              throw new ArtifactInstallerException( e.getMessage(), e );
74          }
75          catch ( InvocationTargetException e )
76          {
77              throw new ArtifactInstallerException( e.getMessage(), e );
78          }
79          catch ( NoSuchMethodException e )
80          {
81              throw new ArtifactInstallerException( e.getMessage(), e );
82          }
83      }
84  
85      public static Object invoke( Class<?> objectClazz, String staticMethod, Class<?> argClazz, Object arg )
86          throws ArtifactInstallerException
87      {
88          try
89          {
90              return objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
91          }
92          catch ( IllegalAccessException e )
93          {
94              throw new ArtifactInstallerException( e.getMessage(), e );
95          }
96          catch ( InvocationTargetException e )
97          {
98              throw new ArtifactInstallerException( e.getMessage(), e );
99          }
100         catch ( NoSuchMethodException e )
101         {
102             throw new ArtifactInstallerException( e.getMessage(), e );
103         }
104     }
105 }