Example usage for java.lang.reflect Field setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

public static void main(String[] st) throws Exception {
    Private p = new Private();
    Field[] fs = p.getClass().getDeclaredFields();
    for (Field f : fs) {
        f.setAccessible(true);
        System.out.println(f.get(p));
    }// w  ww  . j  a  v a  2s .  co m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Set<String> set = new HashSet<String>();
    String str = "java2s.com";
    set.add(str);// w  w w.ja  v a  2 s  . co m

    Field stringValue = String.class.getDeclaredField("value");
    stringValue.setAccessible(true);
    stringValue.set(str, str.toUpperCase().toCharArray());

    System.out.println("New value: " + str);

    String copy = new String(str); // force a copy
    System.out.println("Contains orig: " + set.contains(str));
    System.out.println("Contains copy: " + set.contains(copy));
}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> my = MyClass.class;
    try {//from www .  j  a va  2 s .  co  m
        MyClass p = my.newInstance();
        Field nameField = my.getDeclaredField("name");
        nameField.setAccessible(true);
        String nameValue = (String) nameField.get(p);
        System.out.println("Current name is " + nameValue);
        nameField.set(p, "abc");
        nameValue = (String) nameField.get(p);
        System.out.println("New name is " + nameValue);
    } catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
            | IllegalArgumentException e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

public static void main(String... args) {
    Main ft = new Main();
    try {//  www .j  ava 2  s  .c o  m
        Class<?> c = ft.getClass();
        Field f = c.getDeclaredField("b");
        f.setAccessible(true); // solution
        f.setBoolean(ft, Boolean.FALSE); // IllegalAccessException

        // production code should handle these exceptions more gracefully
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    } catch (IllegalArgumentException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];

    field.setAccessible(true);
    constructor.setAccessible(true);/*from   w w w  . j  a  v a  2s  .  c o m*/
    method.setAccessible(true);

}

From source file:E0.java

public static void main(String... args) {
    try {/* w w  w.  java2 s.  c om*/
        ETest test = new ETest();
        Field f = test.getClass().getDeclaredField("fld");
        f.setAccessible(true);
        f.set(test, E1.A); // IllegalArgumentException

        // production code should handle these exceptions more gracefully
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:TraceLevel.java

public static void main(String... args) {
    TraceLevel newLevel = TraceLevel.valueOf(args[0]);

    try {//from   w  w  w .ja v a 2s . c  o  m
        MyServer svr = new MyServer();
        Class<?> c = svr.getClass();
        Field f = c.getDeclaredField("level");
        f.setAccessible(true);
        TraceLevel oldLevel = (TraceLevel) f.get(svr);
        out.format("Original trace level:  %s%n", oldLevel);

        if (oldLevel != newLevel) {
            f.set(svr, newLevel);
            out.format("    New  trace level:  %s%n", f.get(svr));
        }

        // production code should handle these exceptions more gracefully
    } catch (IllegalArgumentException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static void main(String... args) {
    Constructor[] ctors = Console.class.getDeclaredConstructors();
    Constructor ctor = null;//from w  w  w.ja  va 2s . c  om
    for (int i = 0; i < ctors.length; i++) {
        ctor = ctors[i];
        if (ctor.getGenericParameterTypes().length == 0)
            break;
    }

    try {
        ctor.setAccessible(true);
        Console c = (Console) ctor.newInstance();
        Field f = c.getClass().getDeclaredField("cs");
        f.setAccessible(true);
        out.format("Console charset         :  %s%n", f.get(c));
        out.format("Charset.defaultCharset():  %s%n", Charset.defaultCharset());

        // production code should handle these exceptions more gracefully
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (InvocationTargetException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    }
}

From source file:GrowBufferedReader.java

public static void main(String... args) {
    try {/* w w  w  .j av a 2  s  .c o  m*/
        BufferedReader br = new BufferedReader(car);

        Class<?> c = br.getClass();
        Field f = c.getDeclaredField("cb");

        // cb is a private field
        f.setAccessible(true);
        char[] cbVal = char[].class.cast(f.get(br));

        char[] newVal = Arrays.copyOf(cbVal, cbVal.length * 2);
        if (args.length > 0 && args[0].equals("grow"))
            f.set(br, newVal);

        for (int i = 0; i < srcBufSize; i++)
            br.read();

        // see if the new backing array is being used
        if (newVal[srcBufSize - 1] == src[srcBufSize - 1])
            out.format("Using new backing array, size=%d%n", newVal.length);
        else
            out.format("Using original backing array, size=%d%n", cbVal.length);

        // production code should handle these exceptions more gracefully
    } catch (FileNotFoundException x) {
        x.printStackTrace();
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (IOException x) {
        x.printStackTrace();
    }
}

From source file:com.heliosdecompiler.helios.Helios.java

public static void main(String[] args) {
    try {//from   ww  w.j  a va  2 s  . c o  m
        LanguageController languageController = new LanguageController(); // blehhhhhh
        Message.init(languageController);

        GraphicsProvider launcher = getGraphicsProvider().newInstance();

        launcher.startSplash();
        launcher.updateSplash(Message.STARTUP_PREPARING_ENVIRONMENT);

        Field defaultCharset = Charset.class.getDeclaredField("defaultCharset");
        defaultCharset.setAccessible(true);
        defaultCharset.set(null, StandardCharsets.UTF_8);
        if (!Charset.defaultCharset().equals(StandardCharsets.UTF_8))
            throw new RuntimeException("Charset: " + Charset.defaultCharset());
        if (!Constants.DATA_DIR.exists() && !Constants.DATA_DIR.mkdirs())
            throw new RuntimeException("Could not create data directory");
        if (!Constants.ADDONS_DIR.exists() && !Constants.ADDONS_DIR.mkdirs())
            throw new RuntimeException("Could not create addons directory");
        if (Constants.DATA_DIR.isFile())
            throw new RuntimeException("Data directory is file");
        if (Constants.ADDONS_DIR.isFile())
            throw new RuntimeException("Addons directory is file");

        EventBus eventBus = new AsyncEventBus(Executors.newCachedThreadPool());

        Configuration configuration = loadConfiguration();
        Class<? extends UserInterfaceController> uiController = getUIControllerImpl();

        Injector mainInjector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                bind(MessageHandler.class).to(launcher.getMessageHandlerImpl());
                bind(UserInterfaceController.class).to(uiController);
                bind(Configuration.class).toInstance(configuration);
                bind(EventBus.class).toInstance(eventBus);
            }
        });

        mainInjector.getInstance(UserInterfaceController.class).initialize();

        launcher.updateSplash(Message.STARTUP_LOADING_GRAPHICS);
        launcher.prepare(mainInjector);

        launcher.updateSplash(Message.STARTUP_DONE);
        launcher.start();

        mainInjector.getInstance(PathController.class).reload();
        mainInjector.getInstance(UpdateController.class).doUpdate();
        handleCommandLine(args, mainInjector);
    } catch (Throwable t) {
        displayError(t);
        System.exit(1);
    }
}