Search in sources :

Example 11 with ReflectionException

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;
        }
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 12 with ReflectionException

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;
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 13 with ReflectionException

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);
    }
}
Also used : Packet(io.anuke.mindustry.net.Packet) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 14 with ReflectionException

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);
    }
}
Also used : TypeReporter(eu.infomas.annotation.AnnotationDetector.TypeReporter) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) EndAction(com.bladecoder.engine.actions.EndAction) Action(com.bladecoder.engine.actions.Action) DisableActionAction(com.bladecoder.engine.actions.DisableActionAction) ActionDescription(com.bladecoder.engine.actions.ActionDescription) AnnotationDetector(eu.infomas.annotation.AnnotationDetector) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation)

Example 15 with ReflectionException

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;
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) SerializationException(com.badlogic.gdx.utils.SerializationException) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) FileHandle(com.badlogic.gdx.files.FileHandle) JsonValue(com.badlogic.gdx.utils.JsonValue) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) Json(com.badlogic.gdx.utils.Json) SerializationException(com.badlogic.gdx.utils.SerializationException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Aggregations

ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)17 Field (com.badlogic.gdx.utils.reflect.Field)5 FileHandle (com.badlogic.gdx.files.FileHandle)4 JsonValue (com.badlogic.gdx.utils.JsonValue)4 SerializationException (com.badlogic.gdx.utils.SerializationException)4 IOException (java.io.IOException)4 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 Json (com.badlogic.gdx.utils.Json)3 Action (com.bladecoder.engine.actions.Action)3 AccessControlException (java.security.AccessControlException)3 Color (com.badlogic.gdx.graphics.Color)2 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)2 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)2 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)2 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)2 TiledDrawable (com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable)2 Array (com.badlogic.gdx.utils.Array)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 ReadOnlySerializer (com.badlogic.gdx.utils.Json.ReadOnlySerializer)2