@Test public void findParserWithReflection() { Parser<DummyMessage> parser = ProtobufSerializationProvider.reflectionParserFor(DummyMessage.class); assertThat(parser, sameInstance(testMessage.getParserForType())); }
@SuppressWarnings("unchecked") static <T> Parser<T> reflectionParserFor(final Class<T> classToDeSerialize) { requireMessageLite(classToDeSerialize); try { Field field = classToDeSerialize.getDeclaredField("PARSER"); field.setAccessible(true); Object object = field.get(null); if (object instanceof Parser) { return (Parser<T>) object; } throw new SerializationException("'PARSER' field from " + classToDeSerialize.getName() + " was not an instance of " + Parser.class.getName()); } catch (NoSuchFieldException e) { throw new SerializationException("Could not find static field 'PARSER' from " + classToDeSerialize.getName()); } catch (IllegalAccessException e) { throw new SerializationException("'PARSER' field on " + classToDeSerialize.getName() + " was not publicly accessible"); } }
ProtobufSerializationProvider implements SerializationProvider { @SuppressWarnings("unchecked") static <T> Parser<T> reflectionParserFor(final Class<T> classToDeSerialize) { requireMessageLite(classToDeSerialize); try { Field field = classToDeSerialize.getDeclaredField("PARSER"); field.setAccessible(true); Object object = field.get(null); if (object instanceof Parser) { return (Parser<T>) object; } throw new SerializationException("'PARSER' field from " + classToDeSerialize.getName() + " was not an instance of " + Parser.class.getName()); } catch (NoSuchFieldException e) { throw new SerializationException("Could not find static field 'PARSER' from " + classToDeSerialize.getName()); } catch (IllegalAccessException e) { throw new SerializationException("'PARSER' field on " + classToDeSerialize.getName() + " was not publicly accessible"); } } }
ProtobufSerializationProvider implements SerializationProvider { @SuppressWarnings("unchecked") static <T> Parser<T> reflectionParserFor(final Class<T> classToDeSerialize) { requireMessageLite(classToDeSerialize); try { Field field = classToDeSerialize.getDeclaredField("PARSER"); field.setAccessible(true); Object object = field.get(null); if (object instanceof Parser) { return (Parser<T>) object; } throw new SerializationException("'PARSER' field from " + classToDeSerialize.getName() + " was not an instance of " + Parser.class.getName()); } catch (NoSuchFieldException e) { throw new SerializationException("Could not find static field 'PARSER' from " + classToDeSerialize.getName()); } catch (IllegalAccessException e) { throw new SerializationException("'PARSER' field on " + classToDeSerialize.getName() + " was not publicly accessible"); } } ProtobufSerializationProvider();  ProtobufSerializationProvider(final Function<Class<?>, Parser<?>> parserForClass); }
ProtobufSerializationProvider implements SerializationProvider { @SuppressWarnings("unchecked") static <T> Parser<T> reflectionParserFor(final Class<T> classToDeSerialize) { requireMessageLite(classToDeSerialize); try { Field field = classToDeSerialize.getDeclaredField("PARSER"); field.setAccessible(true); Object object = field.get(null); if (object instanceof Parser) { return (Parser<T>) object; } throw new SerializationException("'PARSER' field from " + classToDeSerialize.getName() + " was not an instance of " + Parser.class.getName()); } catch (NoSuchFieldException e) { throw new SerializationException("Could not find static field 'PARSER' from " + classToDeSerialize.getName()); } catch (IllegalAccessException e) { throw new SerializationException("'PARSER' field on " + classToDeSerialize.getName() + " was not publicly accessible"); } } ProtobufSerializationProvider();  ProtobufSerializationProvider(final Function<Class<?>, Parser<?>> parserForClass); @Override StreamingSerializer getSerializer(final Class<T> classToSerialize); @Override StreamingSerializer getSerializer(final TypeHolder<T> typeToSerialize); @Override @SuppressWarnings("unchecked") StreamingDeserializer<T> getDeserializer(final Class<T> classToDeSerialize); @Override StreamingDeserializer<T> getDeserializer(final TypeHolder<T> typeToDeserialize); }
ProtobufSerializationProvider implements SerializationProvider { @SuppressWarnings("unchecked") static <T> Parser<T> reflectionParserFor(final Class<T> classToDeSerialize) { requireMessageLite(classToDeSerialize); try { Field field = classToDeSerialize.getDeclaredField("PARSER"); field.setAccessible(true); Object object = field.get(null); if (object instanceof Parser) { return (Parser<T>) object; } throw new SerializationException("'PARSER' field from " + classToDeSerialize.getName() + " was not an instance of " + Parser.class.getName()); } catch (NoSuchFieldException e) { throw new SerializationException("Could not find static field 'PARSER' from " + classToDeSerialize.getName()); } catch (IllegalAccessException e) { throw new SerializationException("'PARSER' field on " + classToDeSerialize.getName() + " was not publicly accessible"); } } ProtobufSerializationProvider();  ProtobufSerializationProvider(final Function<Class<?>, Parser<?>> parserForClass); @Override StreamingSerializer getSerializer(final Class<T> classToSerialize); @Override StreamingSerializer getSerializer(final TypeHolder<T> typeToSerialize); @Override @SuppressWarnings("unchecked") StreamingDeserializer<T> getDeserializer(final Class<T> classToDeSerialize); @Override StreamingDeserializer<T> getDeserializer(final TypeHolder<T> typeToDeserialize);  }