Example usage for com.google.common.io ByteArrayDataOutput getClass

List of usage examples for com.google.common.io ByteArrayDataOutput getClass

Introduction

In this page you can find the example usage for com.google.common.io ByteArrayDataOutput getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:de.paleocrafter.pmfw.tileentity.TileEntityWithData.java

private void writePacketFromClass(ByteArrayDataOutput out, Class<?> clazz, int iteration) {
    int fieldCount = 0;
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);//from ww  w  . jav a 2s .  c  o  m
        Annotation anno = field.getAnnotation(TileData.class);
        if (anno != null)
            fieldCount++;
    }
    out.writeInt(fieldCount);
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        Annotation anno = field.getAnnotation(TileData.class);
        if (anno != null) {
            if (((TileData) anno).storeToPacket()) {
                try {
                    String className = null;
                    className = field.getType().getSimpleName();
                    className = Character.toUpperCase(className.charAt(0)) + className.substring(1);
                    if (field.getType().isAssignableFrom(String.class))
                        className = "UTF";

                    if (className != null) {
                        Object value = field.get(this);
                        Class<?> type = field.getType();
                        if (Primitives.wrap(type) != null || value instanceof String
                                || value instanceof IDataObject) {
                            if (value instanceof Byte || value.getClass() == byte.class) {
                                type = int.class;
                                value = (byte) value;
                            }
                            if (value instanceof IDataObject) {
                                out.writeUTF(field.getName());
                                ((IDataObject) value).writeToPacket(out);
                            } else {
                                Method method = out.getClass().getMethod("write" + className, type);
                                out.writeUTF(field.getName());
                                method.setAccessible(true);
                                method.invoke(out, value);
                            }
                        }
                    }

                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}