Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016 higherfrequencytrading.com
*
* 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 net.openhft.chronicle.bytes;
import net.openhft.chronicle.core.ClassLocal;
import net.openhft.chronicle.core.util.ObjectUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Supplier;
/**
* Created by Peter on 19/04/2016.
*/
public class BytesMarshaller {
public static final ClassLocal BYTES_MARSHALLER_CL
= ClassLocal.withInitial(BytesMarshaller::new);
private final FieldAccess[] fields;
public BytesMarshaller(Class tClass) {
Map map = new LinkedHashMap<>();
getAllField(tClass, map);
fields = map.values().stream()
.map(FieldAccess::create).toArray(FieldAccess[]::new);
}
public static void getAllField(Class clazz, Map map) {
if (clazz != Object.class)
getAllField(clazz.getSuperclass(), map);
for (Field field : clazz.getDeclaredFields()) {
if ((field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0)
continue;
field.setAccessible(true);
map.put(field.getName(), field);
}
}
public void readMarshallable(ReadBytesMarshallable t, BytesIn in) {
for (FieldAccess field : fields) {
field.read(t, in);
}
}
public void writeMarshallable(WriteBytesMarshallable t, BytesOut out) {
for (FieldAccess field : fields) {
field.write(t, out);
}
}
static abstract class FieldAccess {
final Field field;
FieldAccess(Field field) {
this.field = field;
}
public static Object create(Field field) {
Class> type = field.getType();
switch (type.getName()) {
case "boolean":
return new BooleanFieldAccess(field);
case "byte":
return new ByteFieldAccess(field);
case "short":
return new ShortFieldAccess(field);
case "int":
return new IntegerFieldAccess(field);
case "float":
return new FloatFieldAccess(field);
case "long":
return new LongFieldAccess(field);
case "double":
return new DoubleFieldAccess(field);
default:
if (type.isArray())
return new ArrayFieldAccess(field);
if (Collection.class.isAssignableFrom(type))
return new CollectionFieldAccess(field);
if (Map.class.isAssignableFrom(type))
return new MapFieldAccess(field);
if (BytesStore.class.isAssignableFrom(type))
return new BytesFieldAccess(field);
if (BytesMarshallable.class.isAssignableFrom(type))
return new BytesMarshallableFieldAccess(field);
return new ScalarFieldAccess(field);
}
}
static Class extractClass(Type type0) {
if (type0 instanceof Class)
return (Class) type0;
else if (type0 instanceof ParameterizedType)
return (Class) ((ParameterizedType) type0).getRawType();
else
return Object.class;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"field=" + field +
'}';
}
void write(Object o, BytesOut write) {
try {
getValue(o, write);
} catch (IllegalAccessException iae) {
throw new AssertionError(iae);
}
}
protected abstract void getValue(Object o, BytesOut write) throws IllegalAccessException;
void read(Object o, BytesIn read) {
try {
setValue(o, read);
} catch (IllegalAccessException iae) {
throw new AssertionError(iae);
}
}
protected abstract void setValue(Object o, BytesIn read) throws IllegalAccessException;
}
static class ScalarFieldAccess extends FieldAccess