Java Code Examples for io.protostuff.Tag#value()
The following examples show how to use
io.protostuff.Tag#value() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnumIO.java From protostuff with Apache License 2.0 | 5 votes |
public EnumIO(Class<E> enumClass, IdStrategy strategy) { this.enumClass = enumClass; this.strategy = strategy; genericElementSchema = new ArraySchemas.EnumArray(strategy, null, this); Field[] fields = enumClass.getFields(); int n = fields.length; alias = new String[n]; tag = new int[n]; valueByAliasMap = new HashMap<String, E>(n * 2); valueByTagMap = new HashMap<Integer, E>(n * 2); for (E instance : enumClass.getEnumConstants()) { int ordinal = instance.ordinal(); try { Field field = enumClass.getField(instance.name()); if (field.isAnnotationPresent(Tag.class)) { Tag annotation = field.getAnnotation(Tag.class); tag[ordinal] = annotation.value(); alias[ordinal] = annotation.alias(); valueByTagMap.put(annotation.value(), instance); valueByAliasMap.put(annotation.alias(), instance); } else { tag[ordinal] = ordinal; alias[ordinal] = field.getName(); valueByTagMap.put(ordinal, instance); valueByAliasMap.put(field.getName(), instance); } } catch (NoSuchFieldException e) { throw new IllegalStateException(e); } } }
Example 2
Source File: RuntimeSchema.java From protostuff with Apache License 2.0 | 4 votes |
/** * Generates a schema from the given class with the exclusion of certain fields. */ public static <T> RuntimeSchema<T> createFrom(Class<T> typeClass, Set<String> exclusions, IdStrategy strategy) { if (typeClass.isInterface() || Modifier.isAbstract(typeClass.getModifiers())) { throw new RuntimeException( "The root object can neither be an abstract " + "class nor interface: \"" + typeClass.getName()); } final Map<String, java.lang.reflect.Field> fieldMap = findInstanceFields(typeClass); final ArrayList<Field<T>> fields = new ArrayList<Field<T>>( fieldMap.size()); int i = 0; boolean annotated = false; for (java.lang.reflect.Field f : fieldMap.values()) { if (!exclusions.contains(f.getName())) { if (f.getAnnotation(Deprecated.class) != null) { // this field should be ignored by ProtoStuff. // preserve its field number for backward-forward compat i++; continue; } final Tag tag = f.getAnnotation(Tag.class); final int fieldMapping; final String name; if (tag == null) { // Fields gets assigned mapping tags according to their // definition order if (annotated) { String className = typeClass.getCanonicalName(); String fieldName = f.getName(); String message = String.format("%s#%s is not annotated with @Tag", className, fieldName); throw new RuntimeException(message); } fieldMapping = ++i; name = f.getName(); } else { // Fields gets assigned mapping tags according to their // annotation if (!annotated && !fields.isEmpty()) { throw new RuntimeException( "When using annotation-based mapping, " + "all fields must be annotated with @" + Tag.class.getSimpleName()); } annotated = true; fieldMapping = tag.value(); if (fieldMapping < MIN_TAG_VALUE || fieldMapping > MAX_TAG_VALUE) { throw new IllegalArgumentException(ERROR_TAG_VALUE + ": " + fieldMapping + " on " + typeClass); } name = tag.alias().isEmpty() ? f.getName() : tag.alias(); } final Field<T> field = RuntimeFieldFactory.getFieldFactory( f.getType(), strategy).create(fieldMapping, name, f, strategy); fields.add(field); } } return new RuntimeSchema<T>(typeClass, fields, RuntimeEnv.newInstantiator(typeClass)); }