View Javadoc
1   package com.github.davidmoten.rx.jdbc;
2   
3   import java.lang.reflect.Method;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import com.github.davidmoten.rx.jdbc.Util.Col;
8   import com.github.davidmoten.rx.jdbc.Util.IndexedCol;
9   import com.github.davidmoten.rx.jdbc.Util.NamedCol;
10  import com.github.davidmoten.rx.jdbc.annotations.Column;
11  import com.github.davidmoten.rx.jdbc.annotations.Index;
12  
13  class AutoMapCache {
14      final Map<String, Col> methodCols;
15      public Class<?> cls;
16  
17      AutoMapCache(Class<?> cls) {
18          this.cls = cls;
19          this.methodCols = getMethodCols(cls);
20      }
21  
22      private static Map<String, Col> getMethodCols(Class<?> cls) {
23          Map<String, Col> methodCols = new HashMap<String, Col>();
24          for (Method method : cls.getMethods()) {
25              String name = method.getName();
26              Column column = method.getAnnotation(Column.class);
27              if (column != null) {
28                  checkHasNoParameters(method);
29                  // TODO check method has a mappable return type
30                  String col = column.value();
31                  if (col.equals(Column.NOT_SPECIFIED))
32                      col = Util.camelCaseToUnderscore(name);
33                  methodCols.put(name, new NamedCol(col, method.getReturnType()));
34              } else {
35                  Index index = method.getAnnotation(Index.class);
36                  if (index != null) {
37                      // TODO check method has a mappable return type
38                      checkHasNoParameters(method);
39                      methodCols.put(name, new IndexedCol(index.value(), method.getReturnType()));
40                  }
41              }
42          }
43          return methodCols;
44      }
45  
46      private static void checkHasNoParameters(Method method) {
47          if (method.getParameterTypes().length > 0) {
48              throw new RuntimeException("mapped interface method cannot have parameters");
49          }
50      }
51  
52  }