@Override public FieldAnnotationAwareSerializer<Object> newSerializer(final Kryo kryo, final Class type) { return new FieldAnnotationAwareSerializer<Object>(kryo, type, marked, disregarding); } }
private boolean isRemove(final Field field) { return !isMarked(field) ^ disregarding; }
private void removeFields() { final CachedField[] cachedFields = getFields(); for (final CachedField cachedField : cachedFields) { final Field field = cachedField.getField(); if (isRemove(field)) { if (TRACE) { trace("kryo", String.format("Ignoring field %s tag: %s", disregarding ? "without" : "with", cachedField)); } super.removeField(field.getName()); } } }
/** * Creates a new field annotation aware serializer. * * @param kryo The {@link Kryo} instace. * @param type The type of the class being serialized. * @param marked The annotations this serializer considers for its serialization process. Be aware tha * a serializer with {@code disregarding} set to {@code false} will never be able to * serialize fields that are not annotated with any of these annotations since it is not * possible to add fields to a {@link FieldSerializer} once it is created. See the * documentation to {@link FieldAnnotationAwareSerializer#addAnnotation(Class)} and * {@link FieldAnnotationAwareSerializer#removeAnnotation(Class)} for further information. * @param disregarding If {@code true}, the serializer will ignore all annotated fields, * if set to {@code false} it will exclusively look at annotated fields. */ public FieldAnnotationAwareSerializer(final Kryo kryo, final Class<?> type, final Collection<Class<? extends Annotation>> marked, final boolean disregarding) { super(kryo, type); this.disregarding = disregarding; this.marked = new HashSet<Class<? extends Annotation>>(marked); removeFields(); }
/** * Adds an annotation to the annotations that are considered by this serializer. * <p/> * <b>Important</b>: This will not have an effect if the serializer was configured * to exclusively serialize annotated fields by setting {@code disregarding} to * {@code false}. This is similar to the contract of this serializer's superclass * {@link FieldSerializer} which does not allow to add fields that were formerly * removed. If this was possible, instances that were serialized before this field * was added could not longer be properly deserialized. In order to make this contract * break explicit, you need to create a new instance of this serializer if you want to * include new fields to a serializer that exclusively serializes annotated fields. * * @param clazz The annotation class to be added. * @return {@code true} if the method call had an effect. */ public boolean addAnnotation(final Class<? extends Annotation> clazz) { if (disregarding && marked.add(clazz)) { initializeCachedFields(); return true; } return false; }
private void removeFields() { final CachedField[] cachedFields = getFields(); for (final CachedField cachedField : cachedFields) { final Field field = cachedField.getField(); if (isRemove(field)) { if (TRACE) { trace("kryo", String.format("Ignoring field %s tag: %s", disregarding ? "without" : "with", cachedField)); } super.removeField(field.getName()); } } }
/** * Creates a new field annotation aware serializer. * * @param kryo The {@link Kryo} instace. * @param type The type of the class being serialized. * @param marked The annotations this serializer considers for its serialization process. Be aware tha * a serializer with {@code disregarding} set to {@code false} will never be able to * serialize fields that are not annotated with any of these annotations since it is not * possible to add fields to a {@link FieldSerializer} once it is created. See the * documentation to {@link FieldAnnotationAwareSerializer#addAnnotation(Class)} and * {@link FieldAnnotationAwareSerializer#removeAnnotation(Class)} for further information. * @param disregarding If {@code true}, the serializer will ignore all annotated fields, * if set to {@code false} it will exclusively look at annotated fields. */ public FieldAnnotationAwareSerializer(final Kryo kryo, final Class<?> type, final Collection<Class<? extends Annotation>> marked, final boolean disregarding) { super(kryo, type); this.disregarding = disregarding; this.marked = new HashSet<Class<? extends Annotation>>(marked); removeFields(); }
/** * Adds an annotation to the annotations that are considered by this serializer. * <p/> * <b>Important</b>: This will not have an effect if the serializer was configured * to exclusively serialize annotated fields by setting {@code disregarding} to * {@code false}. This is similar to the contract of this serializer's superclass * {@link FieldSerializer} which does not allow to add fields that were formerly * removed. If this was possible, instances that were serialized before this field * was added could not longer be properly deserialized. In order to make this contract * break explicit, you need to create a new instance of this serializer if you want to * include new fields to a serializer that exclusively serializes annotated fields. * * @param clazz The annotation class to be added. * @return {@code true} if the method call had an effect. */ public boolean addAnnotation(final Class<? extends Annotation> clazz) { if (disregarding && marked.add(clazz)) { initializeCachedFields(); return true; } return false; }
@Override public void updateFields() { // In order to avoid rebuilding the cached fields twice, the super constructor's call // to this method will be suppressed. This can be done by a simple check of the initialization // state of a property of this subclass. if (marked == null) { return; } super.updateFields(); removeFields(); }
/** * Removes an annotation to the annotations that are considered by this serializer. * <p/> * <b>Important</b>: This will not have an effect if the serializer was configured * to not serialize annotated fields by setting {@code disregarding} to * {@code true}. This is similar to the contract of this serializer's superclass * {@link FieldSerializer} which does not allow to add fields that were formerly * removed. If this was possible, instances that were serialized before this field * was added could not longer be properly deserialized. In order to make this contract * break explicit, you need to create a new instance of this serializer if you want to * include new fields to a serializer that ignores annotated fields for serialization. * * @param clazz The annotation class to be removed. * @return {@code true} if the method call had an effect. */ public boolean removeAnnotation(final Class<? extends Annotation> clazz) { if (!disregarding && marked.remove(clazz)) { initializeCachedFields(); return true; } return false; } }
@Override public FieldAnnotationAwareSerializer<Object> newSerializer(final Kryo kryo, final Class type) { return new FieldAnnotationAwareSerializer<Object>(kryo, type, marked, disregarding); } }
private boolean isRemove(final Field field) { return !isMarked(field) ^ disregarding; }
@Override public void updateFields() { // In order to avoid rebuilding the cached fields twice, the super constructor's call // to this method will be suppressed. This can be done by a simple check of the initialization // state of a property of this subclass. if (marked == null) { return; } super.updateFields(); removeFields(); }
/** * Removes an annotation to the annotations that are considered by this serializer. * <p/> * <b>Important</b>: This will not have an effect if the serializer was configured * to not serialize annotated fields by setting {@code disregarding} to * {@code true}. This is similar to the contract of this serializer's superclass * {@link FieldSerializer} which does not allow to add fields that were formerly * removed. If this was possible, instances that were serialized before this field * was added could not longer be properly deserialized. In order to make this contract * break explicit, you need to create a new instance of this serializer if you want to * include new fields to a serializer that ignores annotated fields for serialization. * * @param clazz The annotation class to be removed. * @return {@code true} if the method call had an effect. */ public boolean removeAnnotation(final Class<? extends Annotation> clazz) { if (!disregarding && marked.remove(clazz)) { initializeCachedFields(); return true; } return false; } }