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:io.smalldata.ohmageomh.data.service.DataPointServiceImpl.java

@Override
public void setUserId(DataPointHeader header, String endUserId) {
    // this is currently implemented using reflection, until we see other use cases where mutability would be useful
    try {/*from   w w w.j av a  2  s.com*/
        Field userIdField = header.getClass().getDeclaredField("userId");
        userIdField.setAccessible(true);
        userIdField.set(header, endUserId);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new IllegalStateException("A user identifier property can't be changed in the data point header.",
                e);
    }
}

From source file:de.hska.ld.content.dto.DocumentListItemDto.java

public DocumentListItemDto(Document document) {
    try {/*from w  ww  .j  a  v  a 2 s. c o  m*/
        // extract and set values per reflection
        Class subclass = document.getClass();
        Class superclass = subclass.getSuperclass();
        while (superclass != null) {
            Field[] declaredFields = superclass.getDeclaredFields();
            for (Field documentfield : declaredFields) {
                documentfield.setAccessible(true);
                Object obj = documentfield.get(document);
                documentfield.set(this, obj);
            }
            superclass = superclass.getSuperclass();
        }
        Field[] declaredFields = document.getClass().getDeclaredFields();
        for (Field documentfield : declaredFields) {
            documentfield.setAccessible(true);
            Object obj = documentfield.get(document);
            documentfield.set(this, obj);
        }
        this.setId(document.getId());
    } catch (IllegalAccessException e) {
        //
    }
}

From source file:de.hska.ld.content.dto.FolderDto.java

public FolderDto(Folder folder) {
    try {/*from ww  w .j a  v a2  s.c  om*/
        Class subclass = folder.getClass();
        Class superclass = subclass.getSuperclass();
        while (superclass != null) {
            Field[] declaredFields = superclass.getDeclaredFields();
            for (Field folderfield : declaredFields) {
                folderfield.setAccessible(true);
                Object obj = folderfield.get(folder);
                folderfield.set(this, obj);
            }
            superclass = superclass.getSuperclass();
        }
        // extract and set values per reflection
        for (Field folderfield : folder.getClass().getDeclaredFields()) {
            folderfield.setAccessible(true);
            Object obj = folderfield.get(folder);
            folderfield.set(this, obj);
        }
        this.setId(folder.getId());
    } catch (IllegalAccessException e) {
        //
    }
    this.getJsonParentId();
}

From source file:jef.tools.security.EncrypterUtil.java

/**
 * ???//w w w.  jav  a 2  s.  c om
 * @return
 */
public static boolean removeCryptographyRestrictions() {
    if (!isRestrictedCryptography()) {
        return false;
    }
    try {
        /*
         * Do the following, but with reflection to bypass access checks:
         * 
         * JceSecurity.isRestricted = false;
         * JceSecurity.defaultPolicy.perms.clear();
         * JceSecurity.defaultPolicy.add(CryptoAllPermission.INSTANCE);
         */
        final Class<?> jceSecurity = Class.forName("javax.crypto.JceSecurity");
        final Class<?> cryptoPermissions = Class.forName("javax.crypto.CryptoPermissions");
        final Class<?> cryptoAllPermission = Class.forName("javax.crypto.CryptoAllPermission");

        final Field isRestrictedField = jceSecurity.getDeclaredField("isRestricted");
        isRestrictedField.setAccessible(true);
        final Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(isRestrictedField, isRestrictedField.getModifiers() & ~Modifier.FINAL);
        isRestrictedField.set(null, false);

        final Field defaultPolicyField = jceSecurity.getDeclaredField("defaultPolicy");
        defaultPolicyField.setAccessible(true);
        final PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null);

        final Field perms = cryptoPermissions.getDeclaredField("perms");
        perms.setAccessible(true);
        ((Map<?, ?>) perms.get(defaultPolicy)).clear();

        final Field instance = cryptoAllPermission.getDeclaredField("INSTANCE");
        instance.setAccessible(true);
        defaultPolicy.add((Permission) instance.get(null));
        return true;
    } catch (final Exception e) {
        LogUtil.error("Failed to remove cryptography restrictions", e);
        return false;
    }
}

From source file:com.jroossien.boxx.nms.item.ItemUtils_V1_10_R1.java

@Override
public SkullMeta setSkullTexture(SkullMeta meta, String skinUrl) {
    if (meta == null) {
        return meta;
    }//from  w w  w  .  jav a 2  s .c o  m

    GameProfile profile = new GameProfile(UUID.randomUUID(), null);
    profile.getProperties().put("textures",
            new Property("textures", new String(Utils.getSkullTexture(skinUrl))));

    try {
        Field profileField = meta.getClass().getDeclaredField("profile");
        profileField.setAccessible(true);
        profileField.set(meta, profile);

    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return meta;
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestControllerTest.java

@Before
public void before()
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    controller = new BaseRestController();
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    spyOnLog = spy(LogFactory.getLog(BaseRestController.class));
    // Need to get the logger using reflection
    Field log;/*from w w  w  .j a v  a  2s.c  o  m*/
    log = controller.getClass().getDeclaredField("log");
    log.setAccessible(true);

    log.set(controller, spyOnLog);

}

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

private void setActiveLoaders(String loaderName) throws NoSuchFieldException, IllegalAccessException {
    Field f = loader.getClass().getDeclaredField("activeLoaders");
    f.setAccessible(true);/*from  w ww  .j av  a 2 s .  c om*/
    f.set(loader, loaderName);
}

From source file:org.appverse.web.framework.backend.api.helpers.log.AutowiredLoggerBeanPostProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
    ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {

        @Override//from   www .j  av  a2s  .  c o  m
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (field.isAnnotationPresent(AutowiredLogger.class)) {
                field.setAccessible(true);
                field.set(bean, LoggerFactory.getLogger(bean.getClass()));
            }

        }
    });
    return bean;
}

From source file:org.openmrs.module.radiology.RadiologyActivatorComponentTest.java

/**
 * @see RadiologyActivator#stopDicomOrderFiller()
 * @verifies should throw exception when unable to stop the dicom order filler
 *//*  w  w  w . j a v a  2  s . c om*/
@Test
public void stopDicomOrderFiller_shouldThrowExceptionWhenUnableToStopTheDicomOrderFiller() throws Exception {

    Field dicomOrderFillerField = RadiologyActivator.class.getDeclaredField("dicomOrderFiller");
    dicomOrderFillerField.setAccessible(true);
    dicomOrderFillerField.set(radiologyActivator, null);

    expectedException.expect(NullPointerException.class);
    radiologyActivator.stopDicomOrderFiller();
}

From source file:de.hska.ld.content.dto.CommentDto.java

public CommentDto(Comment comment) {
    try {/*  w  ww . j a va 2 s  .  co m*/
        // extract and set values per reflection
        Class subclass = comment.getClass();
        Class superclass = subclass.getSuperclass();
        while (superclass != null) {
            Field[] declaredFields = superclass.getDeclaredFields();
            for (Field commentfield : declaredFields) {
                commentfield.setAccessible(true);
                Object obj = commentfield.get(comment);
                commentfield.set(this, obj);
            }
            superclass = superclass.getSuperclass();
        }
        Field[] declaredFields = comment.getClass().getDeclaredFields();
        for (Field commentfield : declaredFields) {
            commentfield.setAccessible(true);
            Object obj = commentfield.get(comment);
            commentfield.set(this, obj);
        }
        this.setId(comment.getId());
    } catch (IllegalAccessException e) {
        //
    }
    this.getJsonParentId();
}