Example usage for java.lang.reflect Field set

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the field represented by this Field object on the specified object argument to the specified new value.

Usage

From source file:ddf.security.command.EncryptCommandTest.java

private void setPlainTextValueField(EncryptCommand encryptCommand, String value) {
    final String plainTextValueField = "plainTextValue";
    final Class<? extends EncryptCommand> clazz = encryptCommand.getClass();

    try {//  w ww  . j  a v  a2 s .  co  m
        final Field field = clazz.getDeclaredField(plainTextValueField);
        field.setAccessible(true);
        field.set(encryptCommand, value);
    } catch (SecurityException e) {
        LOGGER.debug("Security exception setting field value", e);
        fail(e.getMessage());
    } catch (NoSuchFieldException e) {
        LOGGER.debug("No such field exception setting field value", e);
        fail(e.getMessage());
    } catch (IllegalArgumentException e) {
        LOGGER.debug("Illegal argument exception setting field value", e);
        fail(e.getMessage());
    } catch (IllegalAccessException e) {
        LOGGER.debug("Illegal exception exception setting field value", e);
        fail(e.getMessage());
    }

}

From source file:com.thoughtworks.go.util.command.ConsoleResult.java

private void smudgeException(Throwable rawException) throws NoSuchFieldException, IllegalAccessException {
    Field messageField = Throwable.class.getDeclaredField("detailMessage");
    messageField.setAccessible(true);//from   ww w .  jav a2 s.co m
    messageField.set(rawException, replaceSecretInfo(rawException.getMessage()));
}

From source file:info.raack.appliancelabeler.datacollector.StepgreenTED5000ConfigurationLoaderTest.java

@Before
public void setup() throws SecurityException, NoSuchFieldException, IllegalArgumentException,
        IllegalAccessException, JAXBException, SAXException {
    dataService = mock(DataService.class);
    database = mock(Database.class);
    oauthRequestProcessor = mock(OAuthRequestProcessor.class);
    emailSender = mock(EmailSender.class);

    loader = new StepgreenTED5000ConfigurationLoader();
    Class<?> c = loader.getClass();
    Field f = c.getDeclaredField("dataService");
    f.setAccessible(true);// w  w  w.j av  a2  s  .co m
    f.set(loader, dataService);

    f = c.getDeclaredField("oAuthRequestProcessor");
    f.setAccessible(true);
    f.set(loader, oauthRequestProcessor);

    f = c.getDeclaredField("emailSender");
    f.setAccessible(true);
    f.set(loader, emailSender);

    f = c.getDeclaredField("energylabelerUrl");
    f.setAccessible(true);
    f.set(loader, "http://www.energylabelerurl.com");

    f = c.getDeclaredField("database");
    f.setAccessible(true);
    f.set(loader, database);
}

From source file:io.personium.jersey.engine.test.ExtensionScriptIntegrationTest.java

/**
 * ??????./*  w  w w  . ja v  a 2s .c o  m*/
 * @throws Exception ??????????
 */
@Before
public void before() throws Exception {
    // ? singleton?????????????
    // ? singleton??????????
    Field field = ExtensionJarLoader.class.getDeclaredField("singleton");
    field.setAccessible(true);
    field.set(null, null);
}

From source file:com.github.fcannizzaro.resourcer.Resourcer.java

/**
 * Create a JSONObject / JSONArray from a file
 *
 * @throws IllegalAccessException// www. j  a  v  a 2s.co  m
 */
private static void annotateJson(Field field, Json annotation, Object annotated) throws IllegalAccessException {

    String url = annotation.url(), key = annotation.key(), path = annotation.value(), content;

    boolean isUrl = !url.equals("null") || path.startsWith("http"),
            pathAbsolute = FileUtils.isPathAbsolute(path);

    if (hasDotNotation(annotation) && !isUrl || isUrl && !key.equals("null")) {
        annotateJsonDotNotation(field, annotation, annotated);
        return;
    }

    if (!isUrl)
        content = FileUtils.read(pathAbsolute ? path + ".json" : getPath(path + ".json", JSON_DIR));
    else
        content = RequestUtils.get(url.equals("null") ? path : url);

    if (content == null)
        return;

    if (is(field, JSONArray.class))
        field.set(annotated, new JSONArray(content));

    else if (is(field, JSONObject.class))
        field.set(annotated, new JSONObject(content));

}

From source file:com.google.feedserver.util.CommonsCliHelper.java

/**
 * Sets value in the supplied field to the given value.
 * //w ww  .j  av  a 2 s . c o m
 * @param field the flag field.
 * @param value the value, usually a Boolean or a String.
 * @throws RuntimeException if the field is mis-configured.
 */
private void setField(Field field, Object value) {
    try {
        field.set(null, value);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException("Field " + field.getName() + " must be a String", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Field " + field.getName() + " must be public", e);
    } catch (NullPointerException e) {
        throw new RuntimeException("Field " + field.getName() + " must be static");
    }
}

From source file:com.glaf.core.util.ReflectUtils.java

public static void setField(Field field, Object object, Object value) {
    try {/* w  w  w.j a va 2s.co  m*/
        field.setAccessible(true);
        field.set(object, value);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException("Could not set field " + field.toString(), e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Could not set field " + field.toString(), e);
    }
}

From source file:dk.netarkivet.viewerproxy.ViewerProxyTester.java

@After
public void tearDown() throws NoSuchFieldException, IllegalAccessException {
    if (proxy != null) {
        proxy.cleanup();// w  ww .ja va2  s  .  c  o  m
    }
    FileUtils.removeRecursively(TestInfo.WORKING_DIR);
    TestRemoteFile.removeRemainingFiles();
    Field f = ReflectUtils.getPrivateField(IndexRequestClient.class, "clients");
    f.set(null, new EnumMap<RequestType, IndexRequestClient>(RequestType.class));
    f = ReflectUtils.getPrivateField(IndexRequestClient.class, "synchronizer");
    f.set(null, null);
    rs.tearDown();
}

From source file:com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings.java

public void setServiceSettings(SpinnakerService.Type type, ServiceSettings settings) {
    Field serviceField = getServiceField(type.getCanonicalName());
    serviceField.setAccessible(true);/*  w w w .  j  a  v a2s  .  c  o m*/
    try {
        serviceField.set(services, settings);
    } catch (IllegalAccessException e) {
        throw new HalException(Problem.Severity.FATAL,
                "Can't access service field for " + type.toString() + ": " + e.getMessage());
    } finally {
        serviceField.setAccessible(false);
    }
}

From source file:de.keyle.mypet.npc.MyPetNpcPlugin.java

private void replaceLogger() {
    try {/*w  w w  . ja va 2 s .  c  o m*/
        Field logger = JavaPlugin.class.getDeclaredField("logger");
        logger.setAccessible(true);
        logger.set(this, new MyPetLogger(this));
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
}