View Javadoc

1   /**
2    * 
3    */
4   package org.sourceforge.jemmrpc.shared;
5   
6   import java.lang.reflect.Method;
7   
8   /**
9    * Utility methods for interface handling.
10   * 
11   * @author Rory Graves
12   * 
13   */
14  public final class IFUtilities
15  {
16      /**
17       * Private constructor to prevent creation.
18       */
19      private IFUtilities()
20      {
21      }
22  
23      /**
24       * Validates that an interface for RPCClient/Server handling meets the specification - namely
25       * that it is not a subclass of another interface, and void methods use annotations to declare
26       * whether they are
27       * 
28       * @param ifClass The RPC interface class to validate.
29       */
30      public static void validateInterface(Class<?> ifClass)
31      {
32          if (!ifClass.isInterface())
33              throw new IllegalArgumentException("" + ifClass + " - is not an interface");
34  
35          if (ifClass.getInterfaces().length != 0 || ifClass.getSuperclass() != null)
36              throw new IllegalArgumentException("" + ifClass
37                      + " - must not implement other be a sub-interface");
38  
39          final Method methods[] = ifClass.getDeclaredMethods();
40          for (final Method method : methods)
41          {
42  
43              boolean hasAsyncAnnot = method.getAnnotation(AsynchronousCall.class) != null;
44              boolean hasSyncAnnot = method.getAnnotation(SynchronousCall.class) != null;
45              boolean isVoidMethod = method.getReturnType().equals(void.class);
46  
47              if (isVoidMethod)
48              {
49                  if (!hasAsyncAnnot && !hasSyncAnnot)
50                  {
51                      String classMethod = "" + ifClass + "." + method.getName();
52                      throw new IllegalArgumentException(
53                              classMethod
54                              + " -  void must have @AsynchronousCall or @SynchronousCall annotation");
55                      
56                  }
57              } else if (hasAsyncAnnot || hasSyncAnnot)
58              {
59                  throw new IllegalArgumentException(
60                          ""
61                                  + ifClass
62                                  + "."
63                                  + method.getName()
64                                  + ""
65                                  + " - methods with return types are always synchronous and must not have  @AsynchronousCall or @SynchronousCall annotation");
66              }
67          }
68      }
69  
70  }