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: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);
    T t = constructor.newInstance(args);
    constructor.setAccessible(accessible);
    return t;//w  w w  .  j a  v  a  2  s . c  o  m
}

From source file:libepg.epg.section.descriptor.contentdescriptor.NIbbleMaker.java

public static final Nibble init(byte[] data) throws Throwable {
    try {//from w w w .  ja va2  s .c o m
        Object[] args = { data };
        Class<?>[] params = { byte[].class };
        Constructor<Nibble> constructor = Nibble.class.getDeclaredConstructor(params);
        constructor.setAccessible(true);
        Nibble target = constructor.newInstance(args);
        return target;
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException
            | SecurityException ex) {
        LOG.fatal(ex);
    } catch (InvocationTargetException ex) {
        throw ex.getCause();
    }
    return null;
}

From source file:Main.java

public static Object newInstance(String paramString, Object[] paramArrayOfObject,
        Class<?>[] paramArrayOfClass) {
    try {/* w ww .  ja va  2s .co m*/
        Constructor e = Class.forName(paramString).getDeclaredConstructor(paramArrayOfClass);
        e.setAccessible(true);
        return e.newInstance(paramArrayOfObject);
    } catch (Exception var4) {
        var4.printStackTrace();
        return null;
    }
}

From source file:libepg.common.SectionBodyMaker.java

public static final SectionBody init(TABLE_ID tableID, byte[] data) throws InvocationTargetException {
    try {//ww  w . j a v  a  2  s  .  c  o  m
        Object[] args = { tableID, data };
        Class<?>[] params = { TABLE_ID.class, byte[].class };
        Constructor<SectionBody> constructor = SectionBody.class.getDeclaredConstructor(params);
        constructor.setAccessible(true);
        SectionBody target = constructor.newInstance(args);
        return target;
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException
            | SecurityException ex) {
        LOG.fatal(ex);
    }
    return null;
}

From source file:marshalsec.gadgets.ToStringUtil.java

public static Object makeJohnzonToStringTrigger(Object o) throws Exception {
    Class<?> clz = Class.forName("org.apache.johnzon.core.JsonObjectImpl"); //$NON-NLS-1$
    Constructor<?> dec = clz.getDeclaredConstructor(Map.class);
    dec.setAccessible(true);
    HashMap<Object, Object> m = new HashMap<>();
    Object jo = dec.newInstance(m);
    m.put(o, o);//from  w ww  .jav  a  2  s  .c o m
    XString toStringTrig = new XString("");
    return Arrays.asList(jo, JDKUtil.makeMap(jo, toStringTrig));
}

From source file:io.cloudex.framework.cloud.api.ApiUtils.java

/**
 * Return an IOException from the metaData error
 * @param metaData// ww  w.ja va  2 s  .c  om
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static IOException exceptionFromCloudExError(VmMetaData metaData, String instanceId) {
    Class clazz = IOException.class;
    String message = metaData.getMessage();
    if (StringUtils.isNoneBlank(metaData.getException())) {
        try {
            clazz = Class.forName(metaData.getException());
        } catch (ClassNotFoundException e) {
            log.warn("failed to load exception class from evm");
        }
    }
    Exception cause;
    try {
        Constructor ctor = clazz.getDeclaredConstructor(String.class);
        ctor.setAccessible(true);
        cause = (Exception) ctor.newInstance(message);
    } catch (Exception e) {
        log.warn("failed to load exception class from evm");
        cause = new IOException(message);
    }
    return new ProcessorException(PROCESSOR_EXCEPTION + instanceId, cause, instanceId);
}

From source file:Main.java

public static Service createService(Context context, ServiceInfo serviceInfo) throws Exception {
    IBinder token = new Binder();

    Class<?> createServiceDataClass = Class.forName("android.app.ActivityThread$CreateServiceData");
    Constructor<?> constructor = createServiceDataClass.getDeclaredConstructor();
    constructor.setAccessible(true);
    Object createServiceData = constructor.newInstance();

    Field tokenField = createServiceDataClass.getDeclaredField("token");
    tokenField.setAccessible(true);//from  ww  w.ja v  a 2 s  .  c o m
    tokenField.set(createServiceData, token);

    serviceInfo.applicationInfo.packageName = context.getPackageName();
    Field infoField = createServiceDataClass.getDeclaredField("info");
    infoField.setAccessible(true);
    infoField.set(createServiceData, serviceInfo);

    Class<?> compatibilityClass = Class.forName("android.content.res.CompatibilityInfo");
    Field defaultCompatibilityField = compatibilityClass.getDeclaredField("DEFAULT_COMPATIBILITY_INFO");
    defaultCompatibilityField.setAccessible(true);
    Object defaultCompatibility = defaultCompatibilityField.get(null);
    defaultCompatibilityField.set(createServiceData, defaultCompatibility);

    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Method currentActivityThreadMethod = activityThreadClass.getDeclaredMethod("currentActivityThread");
    Object currentActivityThread = currentActivityThreadMethod.invoke(null);

    Method handleCreateServiceMethod = activityThreadClass.getDeclaredMethod("handleCreateService",
            createServiceDataClass);
    handleCreateServiceMethod.setAccessible(true);
    handleCreateServiceMethod.invoke(currentActivityThread, createServiceData);

    Field mServicesField = activityThreadClass.getDeclaredField("mServices");
    mServicesField.setAccessible(true);
    Map mServices = (Map) mServicesField.get(currentActivityThread);
    Service service = (Service) mServices.get(token);
    mServices.remove(token);

    return service;
}

From source file:Main.java

public static String getDefaultUserAgentString(Context context) {
    //      String userAgent = System.getProperty("http.agent");
    try {/*  www.  ja va  2 s . c om*/
        Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class,
                WebView.class);
        constructor.setAccessible(true);
        try {
            WebSettings settings = constructor.newInstance(context, null);
            return settings.getUserAgentString();
        } finally {
            constructor.setAccessible(false);
        }
    } catch (Exception e) {
        return new WebView(context).getSettings().getUserAgentString();
    }
}

From source file:Main.java

public static <T> T newInstance(Class<T> claxx)
        throws IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor<?>[] cons = claxx.getDeclaredConstructors();
    for (Constructor<?> c : cons) {
        Class[] cls = c.getParameterTypes();
        if (cls.length == 0) {
            c.setAccessible(true);
            return (T) c.newInstance();
        } else {//from ww  w .  ja va  2s  . c  om
            Object[] objs = new Object[cls.length];
            for (int i = 0; i < cls.length; i++) {
                objs[i] = getDefaultPrimiticeValue(cls[i]);
            }
            c.setAccessible(true);
            return (T) c.newInstance(objs);
        }
    }
    return null;
}

From source file:Main.java

@NonNull
public static PreferenceManager newInstance(@NonNull Activity activity, int firstRequestCode) {
    try {//from  ww w  .j av a 2s. com
        Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class,
                int.class);
        c.setAccessible(true);
        return c.newInstance(activity, firstRequestCode);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}