Search in sources :

Example 46 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class UtilReflection method getField.

/**
 * Get the field by reflection.
 *
 * @param <T> The field type.
 * @param object The object to use (must not be <code>null</code>).
 * @param name The field name (must not be <code>null</code>).
 * @return The field found.
 * @throws LionEngineException If invalid parameters or field not found.
 */
public static <T> T getField(Object object, String name) {
    Check.notNull(object);
    Check.notNull(name);
    try {
        final Class<?> clazz = getClass(object);
        final Field field = getDeclaredFieldSuper(clazz, name);
        setAccessible(field, true);
        @SuppressWarnings("unchecked") final T value = (T) field.get(object);
        return value;
    } catch (final NoSuchFieldException | IllegalAccessException exception) {
        throw new LionEngineException(exception, ERROR_FIELD + name);
    }
}
Also used : Field(java.lang.reflect.Field) LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 47 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class UtilReflection method create.

/**
 * Create a class instance with its parameters.
 *
 * @param <T> The element type used.
 * @param type The class type to instantiate (must not be <code>null</code>).
 * @param constructor The constructor to use (must not be <code>null</code>).
 * @param params The constructor parameters (must not be <code>null</code>).
 * @return The class instance.
 * @throws LionEngineException If invalid parameters or unable to create the instance.
 */
private static <T> T create(Class<T> type, Constructor<T> constructor, Object... params) {
    Check.notNull(type);
    Check.notNull(constructor);
    Check.notNull(params);
    try {
        setAccessible(constructor, true);
        return constructor.newInstance(params);
    } catch (final IllegalArgumentException exception) {
        throw new LionEngineException(exception, ERROR_CONSTRUCTOR + type + " " + Arrays.asList(constructor.getParameterTypes()) + " with " + Arrays.asList(params));
    } catch (final InstantiationException | IllegalAccessException | InvocationTargetException exception) {
        throw new LionEngineException(exception, ERROR_CONSTRUCTOR + type);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 48 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class Sc68Format method loadLibrary.

/**
 * Load the library.
 *
 * @return The library binding.
 * @throws LionEngineException If error on loading.
 */
private static Sc68Binding loadLibrary() {
    try {
        Verbose.info("Load library: ", LIBRARY_NAME);
        final Sc68Binding binding = Native.loadLibrary(LIBRARY_NAME, Sc68Binding.class);
        Verbose.info("Library ", LIBRARY_NAME, " loaded");
        return binding;
    } catch (final LinkageError exception) {
        throw new LionEngineException(exception, ERROR_LOAD_LIBRARY + LIBRARY_NAME);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 49 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class FactoryGraphicAwt method saveImage.

@Override
public void saveImage(ImageBuffer image, Media media) {
    Check.notNull(media);
    final BufferedImage surface = image.getSurface();
    try (OutputStream output = media.getOutputStream()) {
        ToolsAwt.saveImage(surface, output);
    } catch (final IOException exception) {
        throw new LionEngineException(exception, ERROR_IMAGE_SAVE);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 50 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class ScreenAwtTest method testHeadless.

/**
 * Test headless screen.
 *
 * @param config The config reference.
 * @throws Exception If error.
 */
private void testHeadless(Config config) throws Exception {
    final Object old = UtilReflection.getField(GraphicsEnvironment.class, "headless");
    final Field field = GraphicsEnvironment.class.getDeclaredField("headless");
    UtilReflection.setAccessible(field, true);
    field.set(GraphicsEnvironment.class, Boolean.TRUE);
    try {
        Assert.assertNull(Graphics.createScreen(config));
    } catch (final LionEngineException exception) {
        Assert.assertTrue(exception.getMessage().equals("No available display !"));
    } finally {
        field.set(GraphicsEnvironment.class, old);
    }
}
Also used : Field(java.lang.reflect.Field) LionEngineException(com.b3dgs.lionengine.LionEngineException)

Aggregations

LionEngineException (com.b3dgs.lionengine.LionEngineException)75 IOException (java.io.IOException)13 File (java.io.File)8 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7 XmlReader (com.b3dgs.lionengine.XmlReader)6 Field (java.lang.reflect.Field)6 Test (org.junit.Test)6 Media (com.b3dgs.lionengine.Media)5 Configurer (com.b3dgs.lionengine.game.Configurer)4 UtilFile (com.b3dgs.lionengine.UtilFile)3 Graphic (com.b3dgs.lionengine.graphic.Graphic)3 UtilFile (com.b3dgs.lionengine.util.UtilFile)3 DisplayMode (java.awt.DisplayMode)3 BufferedImage (java.awt.image.BufferedImage)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 AfterAll (org.junit.jupiter.api.AfterAll)3 Animation (com.b3dgs.lionengine.Animation)2 AnimationConfig (com.b3dgs.lionengine.game.AnimationConfig)2 Feature (com.b3dgs.lionengine.game.Feature)2