use of com.badlogic.gdx.utils.reflect.ReflectionException in project libgdx by libgdx.
the class Json method readFields.
public void readFields(Object object, JsonValue jsonMap) {
Class type = object.getClass();
ObjectMap<String, FieldMetadata> fields = getFields(type);
for (JsonValue child = jsonMap.child; child != null; child = child.next) {
FieldMetadata metadata = fields.get(child.name);
if (metadata == null) {
if (child.name.equals(typeName))
continue;
if (ignoreUnknownFields) {
if (debug)
System.out.println("Ignoring unknown field: " + child.name + " (" + type.getName() + ")");
continue;
} else {
SerializationException ex = new SerializationException("Field not found: " + child.name + " (" + type.getName() + ")");
ex.addTrace(child.trace());
throw ex;
}
}
Field field = metadata.field;
try {
field.set(object, readValue(field.getType(), metadata.elementType, child));
} catch (ReflectionException ex) {
throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
} catch (SerializationException ex) {
ex.addTrace(field.getName() + " (" + type.getName() + ")");
throw ex;
} catch (RuntimeException runtimeEx) {
SerializationException ex = new SerializationException(runtimeEx);
ex.addTrace(child.trace());
ex.addTrace(field.getName() + " (" + type.getName() + ")");
throw ex;
}
}
}
use of com.badlogic.gdx.utils.reflect.ReflectionException in project libgdx by libgdx.
the class Json method writeField.
/** Writes the specified field to the current JSON object.
* @param elementType May be null if the type is unknown. */
public void writeField(Object object, String fieldName, String jsonName, Class elementType) {
Class type = object.getClass();
ObjectMap<String, FieldMetadata> fields = getFields(type);
FieldMetadata metadata = fields.get(fieldName);
if (metadata == null)
throw new SerializationException("Field not found: " + fieldName + " (" + type.getName() + ")");
Field field = metadata.field;
if (elementType == null)
elementType = metadata.elementType;
try {
if (debug)
System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")");
writer.name(jsonName);
writeValue(field.get(object), field.getType(), elementType);
} catch (ReflectionException ex) {
throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
} catch (SerializationException ex) {
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
} catch (Exception runtimeEx) {
SerializationException ex = new SerializationException(runtimeEx);
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
}
}
use of com.badlogic.gdx.utils.reflect.ReflectionException in project Mindustry by Anuken.
the class ByteSerializer method read.
@Override
public Object read(ByteBuffer byteBuffer) {
try {
byte id = byteBuffer.get();
if (id == -2) {
return FrameworkSerializer.read(byteBuffer);
} else {
Class<?> type = Registrator.getByID(id);
Packet packet = (Packet) ClassReflection.newInstance(type);
packet.read(byteBuffer);
return packet;
}
} catch (ReflectionException e) {
throw new RuntimeException(e);
}
}
use of com.badlogic.gdx.utils.reflect.ReflectionException in project bladecoder-adventure-engine by bladecoder.
the class ActionDetector method detect.
public static void detect() {
actions = new HashMap<String, Class<?>>();
final TypeReporter reporter = new TypeReporter() {
@SuppressWarnings("unchecked")
@Override
public Class<? extends Annotation>[] annotations() {
return new Class[] { ActionDescription.class };
}
@SuppressWarnings("unchecked")
@Override
public void reportTypeAnnotation(Class<? extends Annotation> annotation, String className) {
Class<Action> c = null;
try {
c = ClassReflection.forName(className);
} catch (ReflectionException e) {
EditorLogger.printStackTrace(e);
}
String name = ActionUtils.getName(c);
if (!c.isAssignableFrom(EndAction.class) && !c.isAssignableFrom(DisableActionAction.class))
actions.put(name, c);
}
};
final AnnotationDetector cf = new AnnotationDetector(reporter);
try {
cf.detect();
} catch (IOException e) {
EditorLogger.printStackTrace(e);
}
}
use of com.badlogic.gdx.utils.reflect.ReflectionException in project bladecoder-adventure-engine by bladecoder.
the class BladeSkin method getJsonLoader.
/**
* Override BitmapFont.class serializer to support TTF fonts
*
* Also add the size parameter to support bitmaps font size in pt
*/
@Override
protected Json getJsonLoader(final FileHandle skinFile) {
Json json = super.getJsonLoader(skinFile);
final Skin skin = this;
json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {
public Skin read(Json json, JsonValue typeToValueMap, @SuppressWarnings("rawtypes") Class ignored) {
for (JsonValue valueMap = typeToValueMap.child; valueMap != null; valueMap = valueMap.next) {
try {
Class<?> type = json.getClass(valueMap.name());
if (type == null)
type = ClassReflection.forName(valueMap.name());
readNamedObjects(json, type, valueMap);
} catch (ReflectionException ex) {
throw new SerializationException(ex);
}
}
return skin;
}
private void readNamedObjects(Json json, Class<?> type, JsonValue valueMap) {
Class<?> addType = type == TintedDrawable.class ? Drawable.class : type;
for (JsonValue valueEntry = valueMap.child; valueEntry != null; valueEntry = valueEntry.next) {
Object object = json.readValue(type, valueEntry);
if (object == null)
continue;
try {
add(valueEntry.name, object, addType);
if (addType != Drawable.class && ClassReflection.isAssignableFrom(Drawable.class, addType))
add(valueEntry.name, object, Drawable.class);
} catch (Exception ex) {
throw new SerializationException("Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name, ex);
}
}
}
});
json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {
public BitmapFont read(Json json, JsonValue jsonData, @SuppressWarnings("rawtypes") Class type) {
String path = json.readValue("file", String.class, jsonData);
int scaledSize = json.readValue("scaledSize", int.class, -1, jsonData);
Boolean flip = json.readValue("flip", Boolean.class, false, jsonData);
int size = json.readValue("size", int.class, -1, jsonData);
FileHandle fontFile = skinFile.parent().child(path);
if (!FileUtils.exists(fontFile))
fontFile = Gdx.files.internal(path);
if (!FileUtils.exists(fontFile))
throw new SerializationException("Font file not found: " + fontFile);
BitmapFont font;
if (fontFile.extension().equalsIgnoreCase("ttf")) {
if (size == -1)
throw new SerializationException("'size' mandatory parameter for .ttf fonts");
long initTime = System.currentTimeMillis();
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = (int) (DPIUtils.dpToPixels(size) * DPIUtils.getSizeMultiplier());
parameter.incremental = json.readValue("incremental", boolean.class, true, jsonData);
parameter.borderWidth = json.readValue("borderWidth", int.class, 0, jsonData);
parameter.borderColor = json.readValue("borderColor", Color.class, Color.BLACK, jsonData);
parameter.borderStraight = json.readValue("borderStraight", boolean.class, false, jsonData);
parameter.shadowOffsetX = json.readValue("shadowOffsetX", int.class, 0, jsonData);
parameter.shadowOffsetY = json.readValue("shadowOffsetY", int.class, 0, jsonData);
parameter.shadowColor = json.readValue("shadowColor", Color.class, Color.BLACK, jsonData);
if (parameter.incremental)
parameter.characters = "";
// parameter.hinting = Hinting.Medium;
// parameter.mono = false;
font = generator.generateFont(parameter);
EngineLogger.debug(path + " TIME (ms): " + (System.currentTimeMillis() - initTime));
// TODO Dispose all generators.
} else {
// Use a region with the same name as the font, else use a
// PNG file in the same directory as the FNT file.
String regionName = fontFile.nameWithoutExtension();
try {
TextureRegion region = skin.optional(regionName, TextureRegion.class);
if (region != null)
font = new BitmapFont(fontFile, region, flip);
else {
FileHandle imageFile = fontFile.parent().child(regionName + ".png");
if (FileUtils.exists(imageFile))
font = new BitmapFont(fontFile, imageFile, flip);
else
font = new BitmapFont(fontFile, flip);
}
// font to.
if (scaledSize != -1)
font.getData().setScale(scaledSize / font.getCapHeight());
else if (// TODO set size in points (dpi
size != -1)
// independent)
font.getData().setScale((DPIUtils.dpToPixels(size) * DPIUtils.getSizeMultiplier()) / font.getCapHeight());
} catch (RuntimeException ex) {
throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
}
}
font.getData().markupEnabled = true;
return font;
}
});
for (Class<?> cls : TAGGED_STYLES) {
json.addClassTag(cls.getSimpleName(), cls);
}
return json;
}
Aggregations