
dorkbox.util.serialization.FieldAnnotationAwareSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Network-Dorkbox-Util Show documentation
Show all versions of Network-Dorkbox-Util Show documentation
Utilities for the Network project.
/*
* Copyright 2010 Martin Grotzke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package dorkbox.util.serialization;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.factories.SerializerFactory;
import com.esotericsoftware.kryo.serializers.FieldSerializer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* A kryo {@link FieldSerializer} that allows to exclusively include or exclude fields that
* are attributed with user-specific annotations. This can be for example useful when serializing beans that carry
* references to a dependency injection framework. As an example for Spring:
*
*
* {@code
* Set> marks = new HashSet<>();
* marks.add(Autowired.class);
* SerializerFactory disregardingFactory = new FieldAnnotationAwareSerializer.Factory(marks, true);
* Kryo kryo = new Kryo();
* kryo.setDefaultSerializer(factory);
* }
*
*
* The resulting {@link Kryo} instance would ignore all fields that are annotated with Spring's {@code @Autowired}
* annotation.
*
* Similarly, it is possible to created a serializer which does the opposite such that the resulting serializer
* would only serialize fields that are annotated with the specified annotations.
*
* @author Rafael Winterhalter
* @author Martin Grotzke
*/
public class FieldAnnotationAwareSerializer extends FieldSerializer {
/**
* A factory for creating instances of {@link FieldAnnotationAwareSerializer}.
*/
public static class Factory implements SerializerFactory {
private final Collection> marked;
private final boolean disregarding;
/**
* Creates a new factory. See {@link FieldAnnotationAwareSerializer#FieldAnnotationAwareSerializer(
*com.esotericsoftware.kryo.Kryo, Class, java.util.Collection, boolean)}
* for additional information on the constructor parameters.
*
* @param marked The annotations that will be considered of the resulting converter.
* @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 Factory(final Collection> marked, final boolean disregarding) {
this.marked = marked;
this.disregarding = disregarding;
}
@Override
public Serializer> makeSerializer(final Kryo kryo, final Class> type) {
return new FieldAnnotationAwareSerializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy