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

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

Introduction

In this page you can find the example usage for com.google.common.io ByteArrayDataInput 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

public void readPacketToClass(ByteArrayDataInput in, Class<?> clazz, int iteration) {
    try {//from  ww w.java2 s. c om
        int fieldCount = in.readInt();
        for (int i = 0; i < fieldCount; i++) {
            try {
                String fieldName = in.readUTF();
                Field field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                String className = field.getType().getSimpleName();
                className = Character.toUpperCase(className.charAt(0)) + className.substring(1);
                if (field.getType().isAssignableFrom(String.class))
                    className = "UTF";
                if (className != null) {
                    if (IDataObject.class.isAssignableFrom(field.getType())) {
                        IDataObject data = (IDataObject) field.getType().newInstance();

                        data.readFromPacket(in);
                        field.set(this, data);
                    } else {
                        Method method = in.getClass().getMethod("read" + className);
                        method.setAccessible(true);
                        field.set(this, method.invoke(in));
                    }
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {

    }
}