@Test public void testRetrieveCustomCoder() throws CoderNotFoundException { Class fieldClass = String.class; SaveState mockedSaveState = mock(SaveState.class); when(mockedSaveState.value()).thenAnswer(new Answer<Class<? extends StateCoder>>() { @Override public Class<? extends StateCoder> answer(InvocationOnMock invocationOnMock) throws Throwable { return CustomStateCoder.class; } }); StateCoder retrieverCoder = mRetriever.getCoder(mockedSaveState, fieldClass); assertNotNull(retrieverCoder); assertEquals(retrieverCoder.getClass(), CustomStateCoder.class); }
@NonNull @Override public StateCoder getCoder(@NonNull SaveState saveState, @NonNull Class<?> annotatedFieldClass) throws CoderNotFoundException { StateCoder stateCoder; final Class<? extends StateCoder> stateSDClass = saveState.value(); if (stateSDClass == StateCoder.class) { stateCoder = mCachedCoders.get(annotatedFieldClass); if (stateCoder != null) return stateCoder; stateCoder = StateCoderUtils.getBasicCoderForClass(annotatedFieldClass); mCachedCoders.put(annotatedFieldClass, stateCoder); } else { try { Constructor<? extends StateCoder> constructor = stateSDClass.getConstructor(); boolean accessible = constructor.isAccessible(); if (!accessible) { constructor.setAccessible(true); } stateCoder = constructor.newInstance(); if (!accessible) { constructor.setAccessible(false); } } catch (Exception e) { throw new RuntimeException("Cannot instantiate a " + StateCoder.class.getSimpleName() + " of class " + stateSDClass.getName()); } } return stateCoder; }
DefaultCoderRetriever implements CoderRetriever { @NonNull @Override public StateCoder getCoder(@NonNull SaveState saveState, @NonNull Class<?> annotatedFieldClass) throws CoderNotFoundException { StateCoder stateCoder; final Class<? extends StateCoder> stateSDClass = saveState.value(); if (stateSDClass == StateCoder.class) { stateCoder = mCachedCoders.get(annotatedFieldClass); if (stateCoder != null) return stateCoder; stateCoder = StateCoderUtils.getBasicCoderForClass(annotatedFieldClass); mCachedCoders.put(annotatedFieldClass, stateCoder); } else { try { Constructor<? extends StateCoder> constructor = stateSDClass.getConstructor(); boolean accessible = constructor.isAccessible(); if (!accessible) { constructor.setAccessible(true); } stateCoder = constructor.newInstance(); if (!accessible) { constructor.setAccessible(false); } } catch (Exception e) { throw new RuntimeException("Cannot instantiate a " + StateCoder.class.getSimpleName() + " of class " + stateSDClass.getName()); } } return stateCoder; } }
DefaultCoderRetriever implements CoderRetriever { @NonNull @Override public StateCoder getCoder(@NonNull SaveState saveState, @NonNull Class<?> annotatedFieldClass) throws CoderNotFoundException { StateCoder stateCoder; final Class<? extends StateCoder> stateSDClass = saveState.value(); if (stateSDClass == StateCoder.class) { stateCoder = mCachedCoders.get(annotatedFieldClass); if (stateCoder != null) return stateCoder; stateCoder = StateCoderUtils.getBasicCoderForClass(annotatedFieldClass); mCachedCoders.put(annotatedFieldClass, stateCoder); } else { try { Constructor<? extends StateCoder> constructor = stateSDClass.getConstructor(); boolean accessible = constructor.isAccessible(); if (!accessible) { constructor.setAccessible(true); } stateCoder = constructor.newInstance(); if (!accessible) { constructor.setAccessible(false); } } catch (Exception e) { throw new RuntimeException("Cannot instantiate a " + StateCoder.class.getSimpleName() + " of class " + stateSDClass.getName()); } } return stateCoder; } DefaultCoderRetriever(); }
DefaultCoderRetriever implements CoderRetriever { @NonNull @Override public StateCoder getCoder(@NonNull SaveState saveState, @NonNull Class<?> annotatedFieldClass) throws CoderNotFoundException { StateCoder stateCoder; final Class<? extends StateCoder> stateSDClass = saveState.value(); if (stateSDClass == StateCoder.class) { stateCoder = mCachedCoders.get(annotatedFieldClass); if (stateCoder != null) return stateCoder; stateCoder = StateCoderUtils.getBasicCoderForClass(annotatedFieldClass); mCachedCoders.put(annotatedFieldClass, stateCoder); } else { try { Constructor<? extends StateCoder> constructor = stateSDClass.getConstructor(); boolean accessible = constructor.isAccessible(); if (!accessible) { constructor.setAccessible(true); } stateCoder = constructor.newInstance(); if (!accessible) { constructor.setAccessible(false); } } catch (Exception e) { throw new RuntimeException("Cannot instantiate a " + StateCoder.class.getSimpleName() + " of class " + stateSDClass.getName()); } } return stateCoder; } DefaultCoderRetriever(); @NonNull @Override StateCoder getCoder(@NonNull SaveState saveState, @NonNull Class<?> annotatedFieldClass); }
DefaultCoderRetriever implements CoderRetriever { @NonNull @Override public StateCoder getCoder(@NonNull SaveState saveState, @NonNull Class<?> annotatedFieldClass) throws CoderNotFoundException { StateCoder stateCoder; final Class<? extends StateCoder> stateSDClass = saveState.value(); if (stateSDClass == StateCoder.class) { stateCoder = mCachedCoders.get(annotatedFieldClass); if (stateCoder != null) return stateCoder; stateCoder = StateCoderUtils.getBasicCoderForClass(annotatedFieldClass); mCachedCoders.put(annotatedFieldClass, stateCoder); } else { try { Constructor<? extends StateCoder> constructor = stateSDClass.getConstructor(); boolean accessible = constructor.isAccessible(); if (!accessible) { constructor.setAccessible(true); } stateCoder = constructor.newInstance(); if (!accessible) { constructor.setAccessible(false); } } catch (Exception e) { throw new RuntimeException("Cannot instantiate a " + StateCoder.class.getSimpleName() + " of class " + stateSDClass.getName()); } } return stateCoder; } DefaultCoderRetriever(); @NonNull @Override StateCoder getCoder(@NonNull SaveState saveState, @NonNull Class<?> annotatedFieldClass);  }