InternalQueryableDatatypeProxy.java

  1. package nz.co.gregs.dbvolution.datatypes;

  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import nz.co.gregs.dbvolution.exceptions.DBRuntimeException;
  5. import nz.co.gregs.dbvolution.internal.properties.PropertyWrapperDefinition;

  6. /**
  7.  * Internal class.Do not use.Used internally to bridge between packages.Makes it possible to hide
  8.  internal methods on the QueryableDatatype so that they don't pollute the API
  9.  or JavaDocs, while still providing access to the internal methods from other
  10.  packages within DBvolution. For example QueryableDatatype.setPropertyWrapper() is set to package-private,
  11.  so the only way of calling it from other packages is via this class. If
  12.  QueryableDatatype.setPropertyWrapper() was public, then this class wouldn't
  13.  be needed, but it would pollute the public API.
  14.  * @param <BASETYPE> the type returned by the QDT's getValue method
  15.  */
  16. public class InternalQueryableDatatypeProxy<BASETYPE> {

  17.     private final QueryableDatatype<BASETYPE> qdt;

  18.     /**
  19.      * Internal class, do not use.
  20.      *
  21.      * @param qdt   qdt
  22.      */
  23.     public InternalQueryableDatatypeProxy(QueryableDatatype<BASETYPE> qdt) {
  24.         this.qdt = qdt;
  25.     }

  26.     /**
  27.      * Internal class, do not use.
  28.      * <p>
  29.      * Injects the PropertyWrapper into the QDT.
  30.      * <p>
  31.      * For use with QDT types that need meta-data only available via property
  32.      * wrappers.
  33.      *
  34.      * @param propertyWrapperDefn   propertyWrapperDefn
  35.      */
  36.     public void setPropertyWrapper(PropertyWrapperDefinition<?,BASETYPE> propertyWrapperDefn) {
  37.         qdt.setPropertyWrapper(propertyWrapperDefn);
  38.     }

  39.     /**
  40.      * Internal class, do not use.
  41.      * <p>
  42.      * Hides the generic setValue(Object) method within QueryableDatatype while
  43.      * allowing it to be used.
  44.      *
  45.      * @param obj   obj
  46.      */
  47.     public void setValue(Object obj) {
  48.         try {
  49.             if (obj == null) {
  50.                 qdt.setToNull();
  51.             } else {
  52.                 Method method = qdt.getClass().getMethod("setValue", obj.getClass());
  53.                 method.invoke(qdt, obj);
  54.             }
  55.         } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
  56.             throw new DBRuntimeException("Synchronisation Failed:" + ex.getMessage(), ex);
  57.         }

  58.     }

  59.     /**
  60.      * Internal class, do not use.
  61.      * <p>
  62.      * Hides the generic setValue(Object) method within QueryableDatatype while
  63.      * allowing it to be used.
  64.      *
  65.      * @param obj   obj
  66.      */
  67.     public void setValueFromDatabase(Object obj) {
  68.         try {
  69.             if (obj == null) {
  70.                 qdt.setToNull();
  71.             } else {
  72.                 Method method = null;
  73.                 Class<?> qdtClass = qdt.getClass();
  74.                 NoSuchMethodException nsmEx = null;
  75.                 while (method == null && !qdtClass.equals(Object.class)) {
  76.                     try {
  77.                         method = qdtClass.getDeclaredMethod("setValueFromDatabase", obj.getClass());
  78.                     } catch (NoSuchMethodException ex) {
  79.                         try {
  80.                             method = qdtClass.getDeclaredMethod("setValueFromDatabase", Object.class);
  81.                         } catch (NoSuchMethodException ex2) {
  82.                             nsmEx = ex;
  83.                         }
  84.                     }
  85.                     qdtClass = qdtClass.getSuperclass();
  86.                 }
  87.                 if (method != null) {
  88.                     method.setAccessible(true);
  89.                     method.invoke(qdt, obj);
  90.                 } else {
  91.                     throw nsmEx;
  92.                 }
  93.             }
  94.         } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
  95.             throw new DBRuntimeException("Synchronisation Failed:" + ex.getMessage(), ex);
  96.         }

  97.     }
  98. }