/**
* Copyright (C) 2012 Stephen M. Hopper
*
* 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 com.enragedginger.stephenerialization.fields;
import java.lang.reflect.Field;
import java.util.Set;
import java.util.TreeSet;
import com.enragedginger.stephenerialization.annotations.Stephenerializable;
import com.enragedginger.stephenerialization.annotations.Stephenerialize;
/**
* Simple implementation of {@link StephenerializableFieldFactory}.
* @author Stephen Hopper
*
*/
public class SimpleStephenerializableFieldFactory implements StephenerializableFieldFactory {
/**
* {@inheritDoc}
*/
public Set<StephenerializableField> generateFields(Class<?> clazz, int maxVersion) {
final Set<StephenerializableField> fields = new TreeSet<StephenerializableField>();
final Class<?> superClazz = clazz.getSuperclass();
if (superClazz != null) {
fields.addAll(generateFields(superClazz, maxVersion));
}
fields.addAll(generateLocalFields(clazz, maxVersion));
return fields;
}
/**
* Generates the fields declared by clazz.
* @param clazz The clazz.
* @param version The max version.
* @return A set of fields representing the declared fields in clazz.
*/
private Set<StephenerializableField> generateLocalFields(Class<?> clazz, int maxVersion) {
final Set<StephenerializableField> fields = new TreeSet<StephenerializableField>();
final Stephenerializable annotation = clazz.getAnnotation(Stephenerializable.class);
if (annotation != null) {
final int superCount = annotation.superCount();
for(Field field : clazz.getDeclaredFields()) {
final Stephenerialize stephenerialize = field.getAnnotation(Stephenerialize.class);
if (stephenerialize != null && stephenerialize.minVersion() <= maxVersion) {
final StephenerializableField stephenField = new StephenerializableField();
stephenField.setField(field);
stephenField.setVersion(stephenerialize.minVersion());
stephenField.setSubClassLevel(superCount);
stephenField.setOrder(stephenerialize.priority());
fields.add(stephenField);
}
}
}
return fields;
}
}