Example usage for com.badlogic.gdx.utils.reflect Constructor setAccessible

List of usage examples for com.badlogic.gdx.utils.reflect Constructor setAccessible

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils.reflect Constructor setAccessible.

Prototype

public void setAccessible(boolean accessible) 

Source Link

Usage

From source file:com.vmilea.gdx.pool.AltReflectionPool.java

License:Apache License

private Constructor findConstructor(Class<T> pooledType) {
    Constructor constructor = null;

    try {/*from   w  ww  . j  a  v a2  s . co  m*/
        constructor = ClassReflection.getConstructor(pooledType, (Class[]) null);
    } catch (Exception e) {
        try {
            constructor = ClassReflection.getDeclaredConstructor(pooledType, (Class[]) null);
            constructor.setAccessible(true);
        } catch (ReflectionException re) {
            ArgCheck.fail("Default constructor for %s is not accessible", pooledType.getSimpleName());
        }
    }

    return constructor;
}

From source file:com.github.antag99.retinazer.Mapper.java

License:Open Source License

Mapper(Engine engine, Class<T> type, int typeIndex) {
    this.engine = engine;
    this.type = type;
    this.typeIndex = typeIndex;
    Constructor constructor;
    try {//from w w w  .j a va 2s. c  om
        constructor = ClassReflection.getConstructor(type);
        constructor.setAccessible(true);
    } catch (ReflectionException ex) {
        if (ex.getCause() instanceof RuntimeException)
            throw (RuntimeException) ex.getCause();
        constructor = null;
    }
    this.constructor = constructor;
    if (Component.Pooled.class.isAssignableFrom(type)) {
        assert constructor != null : "Pooled component MUST have no-arg constructor! (" + type + ")";
        componentPool = new ReflectionPool<T>(type);
    } else {
        componentPool = null;
    }
}

From source file:org.itas.common.json.Json.java

License:Apache License

private Object newInstance(Class type) {
    try {//from  w w w. ja v  a  2s  .  c  om
        return ClassReflection.newInstance(type);
    } catch (Exception ex) {
        try {
            // Try a private constructor.
            Constructor constructor = ClassReflection.getDeclaredConstructor(type);
            constructor.setAccessible(true);
            return constructor.newInstance();
        } catch (SecurityException ignored) {
        } catch (ReflectionException ignored) {
            if (type.isEnum()) {
                return type.getEnumConstants()[0];
            }
            if (type.isArray())
                throw new SerializationException(
                        "Encountered JSON object when expected array of type: " + type.getName(), ex);
            else if (ClassReflection.isMemberClass(type) && !ClassReflection.isStaticClass(type))
                throw new SerializationException(
                        "Class cannot be created (non-static member class): " + type.getName(), ex);
            else
                throw new SerializationException(
                        "Class cannot be created (missing no-arg constructor): " + type.getName(), ex);
        } catch (Exception privateConstructorException) {
            ex = privateConstructorException;
        }
        throw new SerializationException("Error constructing instance of class: " + type.getName(), ex);
    }
}

From source file:mt.Json.java

License:Apache License

protected Object newInstance(Class type) {
    try {//  w ww .  j  a  v a2s  . c o m
        return ClassReflection.newInstance(type);
    } catch (Exception ex) {
        try {
            // Try a private constructor.
            Constructor constructor = ClassReflection.getDeclaredConstructor(type);
            constructor.setAccessible(true);
            return constructor.newInstance();
        } catch (SecurityException ignored) {
        } catch (ReflectionException ignored) {
            if (type.isEnum()) {
                return type.getEnumConstants()[0];
            }
            if (type.isArray())
                throw new SerializationException(
                        "Encountered JSON object when expected array of type: " + type.getName(), ex);
            else if (ClassReflection.isMemberClass(type) && !ClassReflection.isStaticClass(type))
                throw new SerializationException(
                        "Class cannot be created (non-static member class): " + type.getName(), ex);
            else
                throw new SerializationException(
                        "Class cannot be created (missing no-arg constructor): " + type.getName(), ex);
        } catch (Exception privateConstructorException) {
            ex = privateConstructorException;
        }
        throw new SerializationException("Error constructing instance of class: " + type.getName(), ex);
    }
}