Example usage for java.lang.reflect Constructor isAccessible

List of usage examples for java.lang.reflect Constructor isAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Constructor isAccessible.

Prototype

@Deprecated(since = "9")
public boolean isAccessible() 

Source Link

Document

Get the value of the accessible flag for this reflected object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("c:/mysql-connector-java-5.1.18-bin.jar");
    URLClassLoader urlCl = new URLClassLoader(new URL[] { f.toURL() }, System.class.getClassLoader());

    Class mySqlDriver = urlCl.loadClass("com.mysql.jdbc.Driver");
    System.out.println(mySqlDriver.newInstance());
    System.out.println("Is this interface? = " + mySqlDriver.isInterface());

    Class interfaces[] = mySqlDriver.getInterfaces();
    int i = 1;//from   w  ww  .  j a v  a  2  s . c o m
    for (Class _interface : interfaces) {
        System.out.println("Implemented Interface Name " + (i++) + " = " + _interface.getName());
    }

    Constructor constructors[] = mySqlDriver.getConstructors();
    for (Constructor constructor : constructors) {
        System.out.println("Constructor Name = " + constructor.getName());
        System.out.println("Is Constructor Accessible? = " + constructor.isAccessible());
    }
}

From source file:Main.java

public static <T> T newInstance(Class<T> clazz) {
    try {/*from w w  w .  java 2s  .  c o  m*/
        final Constructor<T> constructor = clazz.getDeclaredConstructor();
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        return constructor.newInstance();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static <T extends Object> T newInstance(Class<T> cl, Object... args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor<T> constructor = cl.getDeclaredConstructor(new Class[0]);
    boolean accessible = constructor.isAccessible();
    constructor.setAccessible(true);//from  w w w  . j  a  v  a2  s  . co  m
    T t = constructor.newInstance(args);
    constructor.setAccessible(accessible);
    return t;
}

From source file:com.dianping.squirrel.client.util.ClassUtils.java

/**
 * construct instance violently//from  ww  w .  j a  va 2  s. c  om
 * 
 * @param <T>
 * @param clazz
 * @param parameters
 * @return
 */
public static <T> T newInstance(Class<T> clazz, Object... parameters) {
    try {
        if (parameters == null) {
            parameters = new Object[0];
        }
        int paramLen = parameters.length;
        Class<?>[] parameterTypes = new Class<?>[paramLen];
        for (int i = 0; i < paramLen; i++) {
            parameterTypes[i] = parameters[i].getClass();
        }
        Constructor<T> constructor = getMatchingDeclaredConstructor(clazz, parameterTypes);
        boolean accessible = constructor.isAccessible();
        if (accessible) {
            return constructor.newInstance(parameters);
        } else {
            synchronized (constructor) {
                try {
                    constructor.setAccessible(true);
                    return constructor.newInstance(parameters);
                } finally {
                    constructor.setAccessible(accessible);
                }
            }
        }
    } catch (Exception e) {
        ReflectionUtils.rethrowRuntimeException(e);
    }
    throw new IllegalStateException("Should never get here");
}

From source file:com.github.jinahya.codec.commons.AbstractEncoderProxy.java

/**
 * Creates a new proxy instance.//from  www.  j  a v  a2s  .c o  m
 *
 * @param <P> proxy type parameter
 * @param <T> encoder type parameter
 * @param loader class loader
 * @param interfaces interfaces
 * @param proxyType proxy type
 * @param encoderType encoder type
 * @param encoder encoder
 *
 * @return a new proxy instance.
 */
protected static <P extends AbstractEncoderProxy<T>, T> Object newInstance(final ClassLoader loader,
        final Class<?>[] interfaces, final Class<P> proxyType, final Class<T> encoderType, final T encoder) {

    if (loader == null) {
        throw new NullPointerException("loader");
    }

    if (interfaces == null) {
        throw new NullPointerException("interfaces");
    }

    if (proxyType == null) {
        throw new NullPointerException("proxyType");
    }

    if (encoderType == null) {
        throw new NullPointerException("encoderType");
    }

    if (encoder == null) {
        //throw new NullPointerException("encoder");
    }

    try {
        final Constructor<P> constructor = proxyType.getDeclaredConstructor(encoderType);
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        try {
            return Proxy.newProxyInstance(loader, interfaces, constructor.newInstance(encoder));
        } catch (final IllegalAccessException iae) {
            throw new RuntimeException(iae);
        } catch (final InstantiationException ie) {
            throw new RuntimeException(ie);
        } catch (final InvocationTargetException ite) {
            throw new RuntimeException(ite);
        }
    } catch (final NoSuchMethodException nsme) {
        throw new RuntimeException(nsme);
    }
}

From source file:com.github.jinahya.codec.commons.AbstractDecoderProxy.java

/**
 * Creates a new proxy instance.//  w  w  w  .  j  a va  2s .  co m
 *
 * @param <P> proxy type parameter
 * @param <T> decoder type parameter
 * @param loader class loader
 * @param interfaces interfaces
 * @param proxyType proxy type
 * @param decoderType decoder type
 * @param decoder decoder
 *
 * @return a new proxy instance.
 */
protected static <P extends AbstractDecoderProxy<T>, T> Object newInstance(final ClassLoader loader,
        final Class<?>[] interfaces, final Class<P> proxyType, final Class<T> decoderType, final T decoder) {

    if (loader == null) {
        throw new NullPointerException("loader");
    }

    if (interfaces == null) {
        throw new NullPointerException("interfaces");
    }

    if (proxyType == null) {
        throw new NullPointerException("proxyType");
    }

    if (decoderType == null) {
        throw new NullPointerException("decoderType");
    }

    if (decoder == null) {
        //throw new NullPointerException("decoder");
    }

    try {
        final Constructor<P> constructor = proxyType.getDeclaredConstructor(decoderType);
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        try {
            return Proxy.newProxyInstance(loader, interfaces, constructor.newInstance(decoder));
        } catch (final InstantiationException ie) {
            throw new RuntimeException(ie);
        } catch (final IllegalAccessException iae) {
            throw new RuntimeException(iae);
        } catch (final InvocationTargetException ite) {
            throw new RuntimeException(ite);
        }
    } catch (final NoSuchMethodException nsme) {
        throw new RuntimeException(nsme);
    }
}

From source file:tech.sirwellington.alchemy.generator.ObjectGenerators.java

private static <T> T instantiate(Class<T> classOfT) throws NoSuchMethodException, InstantiationException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Constructor<T> defaultConstructor = classOfT.getDeclaredConstructor();
    boolean originalAccessibility = defaultConstructor.isAccessible();
    try {/*from ww  w .  ja v  a  2  s.c o  m*/
        defaultConstructor.setAccessible(true);
        return defaultConstructor.newInstance();
    } finally {
        defaultConstructor.setAccessible(originalAccessibility);
    }
}

From source file:org.bremersee.common.exception.ExceptionRegistry.java

private static Throwable findThrowableByDefaultConstructor(final Class<? extends Throwable> clazz,
        final String message, final Throwable cause) {
    try {//from w  w  w .  j av  a2 s .co m
        Constructor<? extends Throwable> constructor = clazz.getDeclaredConstructor();
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        Throwable t = constructor.newInstance();
        putMessage(t, message);
        if (cause != null) {
            t.initCause(cause); // NOSONAR
        }
        return t;

    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException // NOSONAR
            | InvocationTargetException e) { // NOSONAR
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finding throwable by default constructor failed. Class [" + clazz.getName()
                    + "] has no such constructor: " + e.getMessage() + " (" + e.getClass().getName() + ").");
        }
        return null;
    }
}

From source file:org.bremersee.common.exception.ExceptionRegistry.java

private static Throwable findThrowableByMessage(final Class<? extends Throwable> clazz, final String message,
        final Throwable cause) {
    try {/*w  ww.  j a v a 2 s  .c om*/
        Constructor<? extends Throwable> constructor = clazz.getDeclaredConstructor(String.class);
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        Throwable t = constructor.newInstance(message);
        if (cause != null) {
            t.initCause(cause);
        }
        return t;

    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException // NOSONAR
            | InvocationTargetException e) { // NOSONAR
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finding throwable by message failed. Class [" + clazz.getName()
                    + "] has no suitable constructor: " + e.getMessage() + " (" + e.getClass().getName()
                    + ").");
        }
        return null;
    }
}

From source file:org.bremersee.common.exception.ExceptionRegistry.java

private static Throwable findThrowableByCause(final Class<? extends Throwable> clazz, final String message,
        final Throwable cause) {
    try {//w  w  w.j a  v  a 2  s .  com
        Constructor<? extends Throwable> constructor = clazz.getDeclaredConstructor(Throwable.class);
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        Throwable t = constructor.newInstance(cause);
        putMessage(t, message);
        return t;

    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException // NOSONAR
            | InvocationTargetException e) { // NOSONAR
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finding throwable by cause failed. Class [" + clazz.getName()
                    + "] has no suitable constructor: " + e.getMessage() + " (" + e.getClass().getName()
                    + ").");
        }
        return null;
    }
}