⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectconverter.java

📁 通过对bufflao改造
💻 JAVA
字号:
package net.buffalo.protocal.converters.map;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import net.buffalo.protocal.AccessFieldException;
import net.buffalo.protocal.converters.Converter;
import net.buffalo.protocal.io.MarshallingContext;
import net.buffalo.protocal.io.StreamReader;
import net.buffalo.protocal.io.StreamWriter;
import net.buffalo.protocal.io.UnmarshallingContext;

public class ObjectConverter extends AbstractMapConverter implements Converter {

	public boolean canConvert(Class type) {
		if (type == null)
			return false;
		return Object.class.isAssignableFrom(type);
	}

	protected void marshalMapObject(Object value, MarshallingContext context, StreamWriter streamWriter) {
		Class clazz = value.getClass();
		for (; clazz != null; clazz = clazz.getSuperclass()) {
			Field[] fields = clazz.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];

				if (Modifier.isTransient(field.getModifiers())
						|| Modifier.isStatic(field.getModifiers()))
					continue;
				field.setAccessible(true);
				context.convertAnother(field.getName());
				try {
					context.convertAnother(field.get(value));
				} catch (Exception e) {
					throw new AccessFieldException("error accessing property ["+field.getName()
							+"] of class ["+clazz+"]",e);
				}
			}
		}		
	}

	public Object unmarshal(StreamReader reader, UnmarshallingContext unmarshallingContext) {
		throw new UnsupportedOperationException("the MapConverter has done it already");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -