Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.stephenerialize;
 
 import java.util.Set;
 
 
 public class SimpleStephenerializer implements Stephenerializer {
 	
 	private static final String ERROR_MSG = "An error occurred while performing stepherialization.";
 
 
 	public void stephenerialize(Object object,
 		final Class<?> clazz = object.getClass();
 		final Stephenerializable annotation = clazz.getAnnotation(Stephenerializable.class);
 		if (annotation == null) {
 			throw new StephenerializationException("The class " + clazz.getName() + " cannot be Stephenerialized "
 					+ "because it lacks a Stephenerializable annotation.");
 		} else {
 			final int version = annotation.version();
 			final Set<StephenerializableFieldfields = .generateFields(clazzversion);
 			streamer.writeInt(version); // write the version out to the stream
 			for (StephenerializableField field : fields) {
 				try {
 					writeField(objectclazzfield.getField(), streamer);
 				} catch (Exception e) {
 				}
 			}
 		}
 	}
 	
 	private void writeField(Object objectClass<?> clazzField fieldStephenerializationStream streamerthrows IllegalArgumentExceptionIllegalAccessException {
 		field.setAccessible(true);
 		final StephenerializableType type = StephenerializableType.lookup(clazz);
 		switch (type) {
 			case  :
 				streamer.writeByte(field.getByte(object));
 				break;
 			case  :
 				streamer.writeShort(field.getShort(object));
 				break;
 			case  :
 				streamer.writeInt(field.getInt(object));
 				break;
 			case  :
 				streamer.writeLong(field.getLong(object));
 				break;
 			case  :
 				streamer.writeFloat(field.getFloat(object));
 				break;
 			case  :
 				streamer.writeDouble(field.getDouble(object));
 				break;
 			case  :
 				streamer.writeBoolean(field.getBoolean(object));
 				break;
 			case  :
 				streamer.writeChar(field.getChar(object));
 				break;
 			case  :
 				streamer.writeObject(field.get(object));
 				break;
 			default :
 				throw new StephenerializationException("Unsupported stephenerialization type: " + type);
 		}
 	}

Parameters:
fieldFactory the fieldFactory to set
 
 	public void setFieldFactory(StephenerializableFieldFactory fieldFactory) {
		this. = fieldFactory;
	}
New to GrepCode? Check out our FAQ X