Example usage for java.lang.reflect Constructor setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Document

A SecurityException is also thrown if this object is a Constructor object for the class Class and flag is true.

Usage

From source file:iristk.util.Record.java

private static Record parseJsonObject(JsonObject json) throws JsonToRecordException {
    try {//from  w  ww.j a  v a2  s  .  c o m
        Record record;
        if (json.get("class") != null) {
            Constructor<?> constructor = Class.forName(json.get("class").asString()).getDeclaredConstructor();
            constructor.setAccessible(true);
            record = (Record) constructor.newInstance(null);
        } else {
            record = new Record();
        }
        for (String name : json.names()) {
            if (!name.equals("class")) {
                JsonValue jvalue = json.get(name);
                record.put(name, parseJsonValue(jvalue));
            }
        }
        //System.out.println(json + " " + record);
        return record;
    } catch (ClassNotFoundException e) {
        throw new JsonToRecordException("Class not found: " + e.getMessage());
    } catch (InstantiationException e) {
        throw new JsonToRecordException("Could not create: " + e.getMessage());
    } catch (IllegalAccessException e) {
        throw new JsonToRecordException("Could not access: " + e.getMessage());
    } catch (IllegalArgumentException e) {
        throw new JsonToRecordException("Illegal argument: " + e.getMessage());
    } catch (InvocationTargetException e) {
        throw new JsonToRecordException("Invocation problem: " + e.getMessage());
    } catch (NoSuchMethodException e) {
        throw new JsonToRecordException("No such method: " + e.getMessage());
    } catch (SecurityException e) {
        throw new JsonToRecordException("Securiry problem: " + e.getMessage());
    }
}

From source file:marshalsec.gadgets.CommonsConfiguration.java

@Args(minArgs = 2, args = { "codebase", "class" }, defaultArgs = { MarshallerBase.defaultCodebase,
        MarshallerBase.defaultCodebaseClass })
@Primary/*from  www  .  j  av  a2  s. c o m*/
default Object makeConfigurationMap(UtilFactory uf, String[] args) throws Exception {
    Object jc = makeConfiguration(uf, args);
    Class<?> cl = Class.forName("org.apache.commons.configuration.ConfigurationMap");
    Constructor<?> cons = cl.getDeclaredConstructor(Configuration.class);
    cons.setAccessible(true);
    return uf.makeHashCodeTrigger(cons.newInstance(jc));
}

From source file:org.jbpm.instantiation.XmlInstantiator.java

public Object instantiate(Class clazz, String configuration) {
    Object newInstance = null;/*from   ww  w  .  j a  v  a2 s.  co m*/
    try {
        // parse the bean configuration
        Element configurationElement = parseConfiguration(configuration);

        Constructor constructor = clazz.getDeclaredConstructor(parameterTypes);
        constructor.setAccessible(true);
        newInstance = constructor.newInstance(new Object[] { configurationElement });
    } catch (Exception e) {
        log.error("couldn't instantiate '" + clazz.getName() + "'", e);
        throw new JbpmException(e);
    }
    return newInstance;
}

From source file:com.opengamma.masterdb.security.SecurityTestCase.java

private static <T> void intializeClass(final Class<T> clazz) {
    // call the default constructor to initialize the class
    try {//from ww  w .j  a  va 2s . com
        Constructor<T> defaultConstructor = getDefaultConstructor(clazz);
        if (defaultConstructor != null) {
            defaultConstructor.setAccessible(true);
            defaultConstructor.newInstance();
        }
    } catch (Exception ex) {
    }
}

From source file:org.onosproject.yang.compiler.utils.io.impl.FileSystemUtilTest.java

/**
 * A private constructor is tested./*from  w w  w  .ja  va 2 s.c o m*/
 *
 * @throws SecurityException         if any security violation is observed
 * @throws NoSuchMethodException     if when the method is not found
 * @throws IllegalArgumentException  if there is illegal argument found
 * @throws InstantiationException    if instantiation is provoked for the private constructor
 * @throws IllegalAccessException    if instance is provoked or a method is provoked
 * @throws InvocationTargetException when an exception occurs by the method or constructor
 */
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
        InstantiationException, IllegalAccessException, InvocationTargetException {

    Class<?>[] classesToConstruct = { FileSystemUtil.class };
    for (Class<?> clazz : classesToConstruct) {
        Constructor<?> constructor = clazz.getDeclaredConstructor();
        constructor.setAccessible(true);
        assertThat(null, not(constructor.newInstance()));
    }
}

From source file:org.apache.hadoop.hbase.client.RetryingCallerInterceptorFactory.java

/**
 * This builds the implementation of {@link RetryingCallerInterceptor} that we
 * specify in the conf and returns the same.
 * //from w ww .  jav  a  2 s .  c om
 * To use {@link PreemptiveFastFailInterceptor}, set HBASE_CLIENT_ENABLE_FAST_FAIL_MODE to true.
 * HBASE_CLIENT_FAST_FAIL_INTERCEPTOR_IMPL is defaulted to {@link PreemptiveFastFailInterceptor}
 * 
 * @return The factory build method which creates the
 *         {@link RetryingCallerInterceptor} object according to the
 *         configuration.
 */
public RetryingCallerInterceptor build() {
    RetryingCallerInterceptor ret = NO_OP_INTERCEPTOR;
    if (failFast) {
        try {
            Class<?> c = conf.getClass(HConstants.HBASE_CLIENT_FAST_FAIL_INTERCEPTOR_IMPL,
                    PreemptiveFastFailInterceptor.class);
            Constructor<?> constructor = c.getDeclaredConstructor(Configuration.class);
            constructor.setAccessible(true);
            ret = (RetryingCallerInterceptor) constructor.newInstance(conf);
        } catch (Exception e) {
            ret = new PreemptiveFastFailInterceptor(conf);
        }
    }
    LOG.trace("Using " + ret.toString() + " for intercepting the RpcRetryingCaller");
    return ret;
}

From source file:org.onosproject.yang.compiler.utils.io.impl.CopyrightHeaderTest.java

/**
 * Unit test for testing private constructor.
 *
 * @throws SecurityException if any security violation is observed
 * @throws NoSuchMethodException if when the method is not found
 * @throws IllegalArgumentException if there is illegal argument found
 * @throws InstantiationException if instantiation is provoked for the private constructor
 * @throws IllegalAccessException if instance is provoked or a method is provoked
 * @throws InvocationTargetException when an exception occurs by the method or constructor
 *//*from  w w  w .  j a  v a2  s.  co  m*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
        InstantiationException, IllegalAccessException, InvocationTargetException {

    Class<?>[] classesToConstruct = { CopyrightHeader.class };
    for (Class<?> clazz : classesToConstruct) {
        Constructor<?> constructor = clazz.getDeclaredConstructor();
        constructor.setAccessible(true);
        assertThat(null, not(constructor.newInstance()));
    }
}

From source file:com.justwayward.reader.base.BaseRVActivity.java

public Object createInstance(Class<?> cls) {
    Object obj;//ww w  .  j ava  2  s  .c om
    try {
        Constructor c1 = cls.getDeclaredConstructor(Context.class);
        c1.setAccessible(true);
        obj = c1.newInstance(mContext);
    } catch (Exception e) {
        obj = null;
    }
    return obj;
}

From source file:org.openmhealth.schema.service.SchemaClassServiceImpl.java

@PostConstruct
public void initializeSchemaIds() throws IllegalAccessException, InvocationTargetException,
        InstantiationException, NoSuchMethodException {

    Reflections reflections = new Reflections(SCHEMA_CLASS_PACKAGE);

    for (Class<? extends SchemaSupport> schemaClass : reflections.getSubTypesOf(SchemaSupport.class)) {

        if (schemaClass.isInterface() || Modifier.isAbstract(schemaClass.getModifiers())) {
            continue;
        }//from   ww  w. j a  v a  2s  .co m

        SchemaId schemaId;

        if (schemaClass.isEnum()) {
            schemaId = schemaClass.getEnumConstants()[0].getSchemaId();
        } else {
            Constructor<? extends SchemaSupport> constructor = schemaClass.getDeclaredConstructor();
            constructor.setAccessible(true);
            schemaId = constructor.newInstance().getSchemaId();
        }

        schemaIds.add(schemaId);
    }
}

From source file:org.apache.solr.sentry.SentrySingletonTestInstance.java

private SentrySingletonTestInstance() {
    try {/*from w w w .  j a  va2 s  . c o  m*/
        setupSentry();
        Constructor ctor = SentryIndexAuthorizationSingleton.class.getDeclaredConstructor(String.class);
        ctor.setAccessible(true);
        sentryInstance = (SentryIndexAuthorizationSingleton) ctor
                .newInstance(sentrySite.toURI().toURL().toString().substring("file:".length()));
        // ensure all SecureAdminHandlers use this instance
        SecureRequestHandlerUtil.testOverride = sentryInstance;
    } catch (Exception ex) {
        LOGGER.error("Unable to create SentrySingletonTestInstance", ex);
    }
}