@Test public void gettingAndSettingProtectedFieldIsSuccessful() throws Exception { new FieldUtils(); Object tc = new TestClass(); assertThat(FieldUtils.getProtectedFieldValue("protectedField", tc)).isEqualTo("x"); assertThat(FieldUtils.getFieldValue(tc, "nested.protectedField")).isEqualTo("z"); FieldUtils.setProtectedFieldValue("protectedField", tc, "y"); assertThat(FieldUtils.getProtectedFieldValue("protectedField", tc)).isEqualTo("y"); try { FieldUtils.getProtectedFieldValue("nonExistentField", tc); } catch (IllegalStateException expected) { } } }
public static Object getProtectedFieldValue(String protectedField, Object object) { Field field = FieldUtils.getField(object.getClass(), protectedField); try { field.setAccessible(true); return field.get(object); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); return null; // unreachable - previous line throws exception } }
public void putInCache(MutableAcl acl) { Assert.notNull(acl, "Acl required"); Assert.notNull(acl.getObjectIdentity(), "ObjectIdentity required"); Assert.notNull(acl.getId(), "ID required"); if (this.aclAuthorizationStrategy == null) { if (acl instanceof AclImpl) { this.aclAuthorizationStrategy = (AclAuthorizationStrategy) FieldUtils .getProtectedFieldValue("aclAuthorizationStrategy", acl); this.permissionGrantingStrategy = (PermissionGrantingStrategy) FieldUtils .getProtectedFieldValue("permissionGrantingStrategy", acl); } } if ((acl.getParentAcl() != null) && (acl.getParentAcl() instanceof MutableAcl)) { putInCache((MutableAcl) acl.getParentAcl()); } cache.put(new Element(acl.getObjectIdentity(), acl)); cache.put(new Element(acl.getId(), acl)); }
private static <T> T getFieldValue(Object bean, String fieldName) throws IllegalAccessException { return (T) FieldUtils.getFieldValue(bean, fieldName); } }
private MutableAcl initializeTransientFields(MutableAcl value) { if (value instanceof AclImpl) { FieldUtils.setProtectedFieldValue("aclAuthorizationStrategy", value, this.aclAuthorizationStrategy); FieldUtils.setProtectedFieldValue("permissionGrantingStrategy", value, this.permissionGrantingStrategy); } if (value.getParentAcl() != null) { initializeTransientFields((MutableAcl) value.getParentAcl()); } return value; }
@Test public void testDiskSerializationOfMutableAclObjectInstance() throws Exception { // Serialization test File file = File.createTempFile("SEC_TEST", ".object"); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(acl); oos.close(); FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); MutableAcl retrieved = (MutableAcl) ois.readObject(); ois.close(); assertThat(retrieved).isEqualTo(acl); Object retrieved1 = FieldUtils.getProtectedFieldValue("aclAuthorizationStrategy", retrieved); assertThat(retrieved1).isNull(); Object retrieved2 = FieldUtils.getProtectedFieldValue( "permissionGrantingStrategy", retrieved); assertThat(retrieved2).isNull(); }
private <T> T getFieldValue(Object target, String fieldName) { try { return (T) FieldUtils.getFieldValue(target, fieldName); } catch (Exception e) { throw new RuntimeException(e); } }
private MutableAcl initializeTransientFields(MutableAcl value) { if (value instanceof AclImpl) { FieldUtils.setProtectedFieldValue("aclAuthorizationStrategy", value, this.aclAuthorizationStrategy); FieldUtils.setProtectedFieldValue("permissionGrantingStrategy", value, this.permissionGrantingStrategy); } if (value.getParentAcl() != null) { initializeTransientFields((MutableAcl) value.getParentAcl()); } return value; }
public static void setProtectedFieldValue(String protectedField, Object object, Object newValue) { Field field = FieldUtils.getField(object.getClass(), protectedField); try { field.setAccessible(true); field.set(object, newValue); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } } }
/** * @return The shell's {@link ConsoleReader}. */ public ConsoleReader getReader() { // XXX Spring Shell doesn't expose the reader that we need to use to // read values from a terminal, so use reflection to pull it out. return (ConsoleReader) FieldUtils.getProtectedFieldValue("reader", shell); }
@Test public void groupQueryIsParsedCorrectly() throws Exception { setContext("<jdbc-user-service id='myUserService' " + "data-source-ref='dataSource' " + "group-authorities-by-username-query='blah blah'/>" + DATA_SOURCE); JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext .getBean("myUserService"); assertThat(FieldUtils.getFieldValue(mgr, "groupAuthoritiesByUsernameQuery")).isEqualTo("blah blah"); assertThat((Boolean) FieldUtils.getFieldValue(mgr, "enableGroups")).isTrue(); }
private MutableAcl initializeTransientFields(MutableAcl value) { if (value instanceof AclImpl) { FieldUtils.setProtectedFieldValue("aclAuthorizationStrategy", value, this.aclAuthorizationStrategy); FieldUtils.setProtectedFieldValue("permissionGrantingStrategy", value, this.permissionGrantingStrategy); } if (value.getParentAcl() != null) { initializeTransientFields((MutableAcl) value.getParentAcl()); } return value; }
public static Object getProtectedFieldValue(String protectedField, Object object) { Field field = FieldUtils.getField(object.getClass(), protectedField); try { field.setAccessible(true); return field.get(object); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); return null; // unreachable - previous line throws exception } }
private Session getCurrentSession() { DataDefinition dataDefinition = dataDefinitionService .get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_TECHNOLOGY); Object dataAccessService = FieldUtils.getProtectedFieldValue("dataAccessService", dataDefinition); Object hibernateService = FieldUtils.getProtectedFieldValue("hibernateService", dataAccessService); try { return (Session) MethodUtils.invokeExactMethod(hibernateService, "getCurrentSession", new Object[0]); } catch (NoSuchMethodException e) { throw new IllegalStateException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new IllegalStateException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new IllegalStateException(e.getMessage(), e); } } }
@Test public void usernameAndAuthorityQueriesAreParsedCorrectly() throws Exception { String userQuery = "select username, password, true from users where username = ?"; String authoritiesQuery = "select username, authority from authorities where username = ? and 1 = 1"; setContext("<jdbc-user-service id='myUserService' " + "data-source-ref='dataSource' " + "users-by-username-query='" + userQuery + "' " + "authorities-by-username-query='" + authoritiesQuery + "'/>" + DATA_SOURCE); JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext .getBean("myUserService"); assertThat(FieldUtils.getFieldValue(mgr, "usersByUsernameQuery")).isEqualTo(userQuery); assertThat(FieldUtils.getFieldValue(mgr, "authoritiesByUsernameQuery")).isEqualTo(authoritiesQuery); assertThat(mgr.loadUserByUsername("rod") != null).isTrue(); }
private MutableAcl initializeTransientFields(MutableAcl value) { if (value instanceof AclImpl) { FieldUtils.setProtectedFieldValue("aclAuthorizationStrategy", value, this.aclAuthorizationStrategy); FieldUtils.setProtectedFieldValue("permissionGrantingStrategy", value, this.permissionGrantingStrategy); } if (value.getParentAcl() != null) { initializeTransientFields((MutableAcl) value.getParentAcl()); } return value; }
/** * Returns the value of a (nested) field on a bean. Intended for testing. * @param bean the object * @param fieldName the field name, with "." separating nested properties * @return the value of the nested field */ public static Object getFieldValue(Object bean, String fieldName) throws IllegalAccessException { Assert.notNull(bean, "Bean cannot be null"); Assert.hasText(fieldName, "Field name required"); String[] nestedFields = StringUtils.tokenizeToStringArray(fieldName, "."); Class<?> componentClass = bean.getClass(); Object value = bean; for (String nestedField : nestedFields) { Field field = getField(componentClass, nestedField); field.setAccessible(true); value = field.get(value); if (value != null) { componentClass = value.getClass(); } } return value; }
public void putInCache(MutableAcl acl) { Assert.notNull(acl, "Acl required"); Assert.notNull(acl.getObjectIdentity(), "ObjectIdentity required"); Assert.notNull(acl.getId(), "ID required"); if (this.aclAuthorizationStrategy == null) { if (acl instanceof AclImpl) { this.aclAuthorizationStrategy = (AclAuthorizationStrategy) FieldUtils .getProtectedFieldValue("aclAuthorizationStrategy", acl); this.permissionGrantingStrategy = (PermissionGrantingStrategy) FieldUtils .getProtectedFieldValue("permissionGrantingStrategy", acl); } } if ((acl.getParentAcl() != null) && (acl.getParentAcl() instanceof MutableAcl)) { putInCache((MutableAcl) acl.getParentAcl()); } cache.put(new Element(acl.getObjectIdentity(), acl)); cache.put(new Element(acl.getId(), acl)); }
@Test public void eventsArePublishedByDefault() throws Exception { ConfigurableApplicationContext appContext = this.spring.context(CONTEXT) .getContext(); AuthListener listener = new AuthListener(); appContext.addApplicationListener(listener); ProviderManager pm = (ProviderManager) appContext .getBeansOfType(ProviderManager.class).values().toArray()[0]; Object eventPublisher = FieldUtils.getFieldValue(pm, "eventPublisher"); assertThat(eventPublisher).isNotNull(); assertThat(eventPublisher instanceof DefaultAuthenticationEventPublisher).isTrue(); pm.authenticate(new UsernamePasswordAuthenticationToken("bob", "bobspassword")); assertThat(listener.events).hasSize(1); }
public static void setProtectedFieldValue(String protectedField, Object object, Object newValue) { Field field = FieldUtils.getField(object.getClass(), protectedField); try { field.setAccessible(true); field.set(object, newValue); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } } }