Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.enragedginger.stephenerialization.preprocessing;
  
  
  import java.util.Set;
Generates fields for classes that are being preprocessed.

Author(s):
Stephen Hopper
 
Generates StephenerializationPreprocessorField for a class that is Stephenerializable and is being preprocessed.

Parameters:
classElement The element refering to the annotated class.
Returns:
The set of Stephenerializable fields in classElement.
 
         final Set<StephenerializationPreprocessorFieldinstanceFields = new TreeSet<StephenerializationPreprocessorField>();
         for(VariableElement instanceField : ElementFilter.fieldsIn(classElement.getEnclosedElements())) {
             if (!instanceField.getModifiers().contains(.) && instanceField.getAnnotation(Stephenerialize.class) != null) {
                 StephenerializationPreprocessorField preprocessorField = buildPreprocessorField(instanceField);
                 instanceFields.add(preprocessorField);
             }
         }
 
         return instanceFields;
     }

    
Builds a preprocessor field.

Parameters:
variableElement The element refering to the field.
Returns:
The StephenerializationPreprocessorField instance built from the variable element.
 
 
         final Stephenerialize stephenerialize = variableElement.getAnnotation(Stephenerialize.class);
         field.setOrder(stephenerialize.priority());
         field.setVersion(stephenerialize.minVersion());
 
         final String typeName = variableElement.asType().toString();
         final boolean isPrimitive = variableElement.asType().getKind().isPrimitive();
         field.setFieldTypeName(typeName);
         field.setPrimitive(isPrimitive);
         field.setObjectInputStreamMethod(buildInputObjectStreamMethodName(isPrimitivetypeName));
         field.setObjectOutputStreamMethod(buildObjectOutputStreamMethodName(isPrimitivetypeName));
 
         final String fieldName = variableElement.getSimpleName().toString();
         field.setFieldName(fieldName);
 
         final String prefix = variableElement.asType().toString().equals("boolean") ? "is" : "get";
         final String upperCasedFieldName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
         field.setGetterName(prefix + upperCasedFieldName);
         field.setSetterName("set" + upperCasedFieldName);
 
         return field;
     }

    
Builds the name of the method on an ObjectOutputStream that should be used to write this field.

Parameters:
isPrimitive True if the field's data type is a primitive Java type.
typeName The name of the type.
Returns:
The name of the method on an ObjectOutputStream that should be used to write this field.
 
     private String buildObjectOutputStreamMethodName(boolean isPrimitiveString typeName) {
         if (isPrimitive) {
             if ("int".equals(typeName)) {
                 return "writeInt";
             } else if ("long".equals(typeName)) {
                 return "writeLong";
             } else if ("short".equals(typeName)) {
                 return "writeShort";
             } else if ("byte".equals(typeName)) {
                 return "writeByte";
             } else if ("float".equals(typeName)) {
                 return "writeFloat";
             } else if ("double".equals(typeName)) {
                 return "writeDouble";
             } else if ("boolean".equals(typeName)) {
                 return "writeBoolean";
             } else if ("char".equals(typeName)) {
                 return "writeChar";
             } else {
                 throw new IllegalArgumentException("Type was supposedly primitive but was actually: " + typeName);
             }
         } else {
             return "writeObject";
         }
 
     }

    
Builds the name of the method on an ObjectInputStream that should be used to read this field.

Parameters:
isPrimitive True if the field's data type is a primitive Java type.
typeName The name of the type.
Returns:
The name of the method on an ObjectInputStream that should be used to read this field.
    private String buildInputObjectStreamMethodName(boolean isPrimitiveString typeName) {
        if (isPrimitive) {
            if ("int".equals(typeName)) {
                return "readInt";
            } else if ("long".equals(typeName)) {
                return "readLong";
            } else if ("short".equals(typeName)) {
                return "readShort";
            } else if ("byte".equals(typeName)) {
                return "readByte";
            } else if ("float".equals(typeName)) {
                return "readFloat";
            } else if ("double".equals(typeName)) {
                return "readDouble";
            } else if ("boolean".equals(typeName)) {
                return "readBoolean";
            } else if ("char".equals(typeName)) {
                return "readChar";
            } else {
                throw new IllegalArgumentException("Type was supposedly primitive but was actually: " + typeName);
            }
        } else {
            return "readObject";
        }
    }
New to GrepCode? Check out our FAQ X