@Override public int compare(T o1, T o2) { Comparable obj1 = null; Comparable obj2 = null; try { obj1 = (Comparable) PropertyUtils.getProperty(o1, this.propertyPath); obj2 = (Comparable) PropertyUtils.getProperty(o2, this.propertyPath); } catch (NestedNullException ignored) { // Ignored, als het property NULL is, dan vergelijken met NULL } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new IllegalArgumentException("Could not retrieve property " + this.propertyPath, e); } Comparator<Comparable<Object>> objectComparator = null; if ((obj1 != null && obj2 != null) && obj1 instanceof String) { obj1 = ((String) obj1).toLowerCase().trim(); obj2 = ((String) obj2).toLowerCase().trim(); if (!StringUtils.isEmpty((String) obj1) && !StringUtils.isEmpty((String) obj2)) { if (StringUtils.isNumeric((String) obj1) && StringUtils.isNumeric((String) obj2)) { objectComparator = Comparator.comparingDouble(o -> new Double(String.valueOf(o))); } } } if (objectComparator == null) { objectComparator = Comparator.naturalOrder(); } //noinspection unchecked return Comparator.nullsLast(objectComparator).compare(obj1, obj2); }
/** * Return whether or not an attribute is writeable. This method is aware that that Collections may be involved and * handles them * consistently with the way in which OJB handles specifying the attributes of elements of a Collection. * * @param object * @param property * @return * @throws IllegalArgumentException */ public static boolean isWriteable(Object object, String property, PersistenceStructureService persistenceStructureService) throws IllegalArgumentException { if (null == object || null == property) { throw new IllegalArgumentException("Cannot check writeable status with null arguments."); } // Try the easy way. try { if (!(PropertyUtils.isWriteable(object, property))) { // If that fails lets try to be a bit smarter, understanding that Collections may be involved. return isWriteableHelper(object, property, persistenceStructureService); } else { return true; } } catch (NestedNullException nestedNullException) { // If a NestedNullException is thrown then the property has a null // value. Call the helper to find the class of the property and // get a newInstance of it. return isWriteableHelper(object, property, persistenceStructureService); } }
public static Object getNestedProperty(Object data, String name) { try { if (!name.contains(".")) { return PropertyUtils.getProperty(data, name); } if (name.contains("[")) { String[] names = name.split("\\.", 2); Object obj = null; try { obj = PropertyUtils.getIndexedProperty(data, names[0]); } catch (NullPointerException npe) { // アクセスする配列オブジェクトがnullの場合 obj = null; } if (obj == null) { return null; } return getNestedProperty(obj, names[1]); } try { return PropertyUtils.getNestedProperty(data, name); } catch (NestedNullException nne) { // ネストしたプロパティの途中がnullの場合はエラーとせず // nullを返す return null; } } catch (Exception e) { throw new ApplicationFatalRuntimeException("invalid property : " + name, e); } }
private static boolean isPropertyReadable(Object bean, String name) { try { return PropertyUtils.isReadable(bean, name); } catch (NestedNullException e) { return false; } }
private static boolean isPropertyWritable(Object bean, String name) { try { return PropertyUtils.isWriteable(bean, name); } catch (NestedNullException e) { return false; } }
@Override public Object getValue() { try { final int propIndex = propertyProvider.getNestedIndex(propertyName); if (propIndex >= 0 && propIndex < nestedProps.size()) return nestedProps.get(propIndex); else return PropertyUtils.getNestedProperty(bean, propertyName); } catch (final NestedNullException ne) { return null; } catch (final Throwable e) { propagate(e); } return null; }
/** * Tries to get a property from map using the provided * key. If the Map contains the key, the associated property value * is obtained from the Map and returned. Otherwise, checks to see if * the Map contains a key matching the remainder of the keypath. If so, * the property value stored under the alternate key is returned. */ public Object getPropertyFromMap(Map map, String key, Keypath keypath) { String alternateKey = keypath.remainingKeypath(); if (!(map.containsKey(key) || map.containsKey(alternateKey))) throw new NestedNullException("No property named " + key + " or " + alternateKey); if (map.containsKey(key)) return map.get(key); if (map.containsKey(alternateKey)) keypath.invalidate(); return map.get(alternateKey); }
@Test public void testPopulateBusinessObjectFromMap() { PojoPlugin.initBeanUtils(); NestedBo nestedBo = new NestedBo(); Map<String, Object> values = new HashMap<String, Object>(); values.put("nestedImpl.value", "value"); FieldUtils.populateBusinessObjectFromMap(nestedBo, values); assertNotNull(nestedBo.nested); nestedBo.nested = null; values.clear(); values.put("nestedIntf.value", "value"); try { FieldUtils.populateBusinessObjectFromMap(nestedBo, values); fail("Expected to throw NestedNullException due to attempt to instantiate interface"); } catch (NestedNullException nne) { // expected } BOContainingPerson bo = new BOContainingPerson(); values.clear(); values.put("person.name", "value"); FieldUtils.populateBusinessObjectFromMap(bo, values); assertNotNull(bo.getPerson()); assertEquals("value", bo.getPerson().getName()); }
public Collection getCollection() { throw new NestedNullException(); }
@Test(dependsOnMethods="createComplex", groups = "searchComplex") public void searchAttributesParam() throws Exception{ int count=3; List<String> attrList=Arrays.asList("name.familyName", "active"); logger.debug("Searching at most {} users using POST verb", count); logger.debug("Sorted by family name descending"); logger.debug("Retrieving only the attributes {}", attrList); SearchRequest sr=new SearchRequest(); sr.setFilter("name.familyName pr"); sr.setSortBy("name.familyName"); sr.setSortOrder("descending"); //Generate a string with the attributes desired to be returned separated by comma sr.setAttributes(attrList.toString().replaceFirst("\\[","").replaceFirst("]","")); sr.setCount(count); Response response=client.searchUsersPost(sr); assertEquals(response.getStatus(), OK.getStatusCode()); ListResponse listResponse=response.readEntity(ListResponse.class); if (listResponse.getResources().size()<count) logger.warn("Less than {} users satisfying the criteria. TESTER please check manually", count); else{ //Obtain an array of results UserResource users[]=listResponse.getResources().stream().map(usrClass::cast) .collect(Collectors.toList()).toArray(new UserResource[]{}); assertEquals(users.length, count); //Build a set of all attributes that should not appear in the response Set<String> check=new HashSet<>(); check.addAll(IntrospectUtil.allAttrs.get(usrClass)); //Remove from the ALL list, those requested plus its "parents" for (String attr : attrList){ String part=attr; for (int i=part.length(); i>0; i = part.lastIndexOf(".")){ part=part.substring(0, i); check.remove(part); } } //Remove those that are ALWAYS present (per spec) check.removeAll(IntrospectUtil.alwaysCoreAttrs.get(usrClass).keySet()); //Confirm for every user, those attributes are not there for (UserResource user : users) for (String path : check){ String val=null; try { val=BeanUtils.getProperty(user, path); } catch (NestedNullException nne){ //Intentionally left empty } finally { assertNull(val); } } for (int i=1;i<users.length;i++) { String familyName=users[i-1].getName().getFamilyName().toLowerCase(); String familyName2=users[i].getName().getFamilyName().toLowerCase(); //Check if first string is greater or equal than second assertFalse(familyName.compareTo(familyName2)<0); } } }