org.yaml.snakeyaml.introspector.MethodProperty Java Examples

The following examples show how to use org.yaml.snakeyaml.introspector.MethodProperty. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdvancedRepresenterTest.java    From waggle-dance with Apache License 2.0 6 votes vote down vote up
@Test
public void notNullMapProperty() {
  bean.setMapProperty(ImmutableMap.<String, Long>builder().put("first", 1L).put("second", 2L).build());
  Property property = new MethodProperty(getPropertyDescriptor("mapProperty"));
  NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getMapProperty(), null);
  assertThat(nodeTuple, is(notNullValue()));
  assertThat(nodeTuple.getKeyNode(), is(instanceOf(ScalarNode.class)));
  assertThat(((ScalarNode) nodeTuple.getKeyNode()).getValue(), is("map-property"));
  assertThat(nodeTuple.getValueNode(), is(instanceOf(MappingNode.class)));
  assertThat(((MappingNode) nodeTuple.getValueNode()).getValue().size(), is(2));
  assertThat(((MappingNode) nodeTuple.getValueNode()).getValue().get(0), is(instanceOf(NodeTuple.class)));
  assertThat(((ScalarNode) ((MappingNode) nodeTuple.getValueNode()).getValue().get(0).getKeyNode()).getValue(),
      is("first"));
  assertThat(((ScalarNode) ((MappingNode) nodeTuple.getValueNode()).getValue().get(0).getValueNode()).getValue(),
      is("1"));
  assertThat(((ScalarNode) ((MappingNode) nodeTuple.getValueNode()).getValue().get(1).getKeyNode()).getValue(),
      is("second"));
  assertThat(((ScalarNode) ((MappingNode) nodeTuple.getValueNode()).getValue().get(1).getValueNode()).getValue(),
      is("2"));
}
 
Example #2
Source File: AdvancedPropertyUtils.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
private synchronized boolean include(Property property) {
  boolean eligible = property.isReadable() && (allowReadOnlyProperties || property.isWritable());
  if (!eligible) {
    return false;
  }
  if (MethodProperty.class.isAssignableFrom(property.getClass())) {
    PropertyDescriptor propertyDescriptor = getPropertyDescriptor((MethodProperty) property);
    return propertyDescriptor == null || !isTransient(propertyDescriptor);
  }
  return true;
}
 
Example #3
Source File: AdvancedPropertyUtils.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
private synchronized PropertyDescriptor getPropertyDescriptor(MethodProperty methodProperty) {
  Field propertyField = null;
  try {
    propertyField = MethodProperty.class.getDeclaredField("property");
    propertyField.setAccessible(true);
    return (PropertyDescriptor) propertyField.get(methodProperty);
  } catch (Exception e) {
    return null;
  } finally {
    if (propertyField != null) {
      propertyField.setAccessible(false);
    }
  }
}
 
Example #4
Source File: AdvancedRepresenterTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void notNullProperty() throws Exception {
  bean.setProperty("value");
  Property property = new MethodProperty(getPropertyDescriptor("property"));
  NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getProperty(), null);
  assertThat(nodeTuple, is(notNullValue()));
  assertThat(nodeTuple.getKeyNode(), is(instanceOf(ScalarNode.class)));
  assertThat(((ScalarNode) nodeTuple.getKeyNode()).getValue(), is("property"));
  assertThat(nodeTuple.getValueNode(), is(instanceOf(ScalarNode.class)));
  assertThat(((ScalarNode) nodeTuple.getValueNode()).getValue(), is("value"));
}
 
Example #5
Source File: AdvancedRepresenterTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void notNullCollectionProperty() {
  bean.setCollectionProperty(ImmutableList.<String>builder().add("1").add("2").build());
  Property property = new MethodProperty(getPropertyDescriptor("collectionProperty"));
  NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getCollectionProperty(), null);
  assertThat(nodeTuple, is(notNullValue()));
  assertThat(nodeTuple.getKeyNode(), is(instanceOf(ScalarNode.class)));
  assertThat(((ScalarNode) nodeTuple.getKeyNode()).getValue(), is("collection-property"));
  assertThat(nodeTuple.getValueNode(), is(instanceOf(SequenceNode.class)));
  assertThat(((SequenceNode) nodeTuple.getValueNode()).getValue().size(), is(2));
  assertThat(((SequenceNode) nodeTuple.getValueNode()).getValue().get(0), is(instanceOf(ScalarNode.class)));
  assertThat(((ScalarNode) ((SequenceNode) nodeTuple.getValueNode()).getValue().get(0)).getValue(), is("1"));
  assertThat(((SequenceNode) nodeTuple.getValueNode()).getValue().get(1), is(instanceOf(ScalarNode.class)));
  assertThat(((ScalarNode) ((SequenceNode) nodeTuple.getValueNode()).getValue().get(1)).getValue(), is("2"));
}
 
Example #6
Source File: AdvancedRepresenterTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyCollectionProperty() {
  bean.setCollectionProperty(ImmutableList.of());
  Property property = new MethodProperty(getPropertyDescriptor("collectionProperty"));
  NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getCollectionProperty(), null);
  assertThat(nodeTuple, is(nullValue()));
}
 
Example #7
Source File: AdvancedRepresenterTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyMapProperty() {
  bean.setMapProperty(ImmutableMap.of());
  Property property = new MethodProperty(getPropertyDescriptor("mapProperty"));
  NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getMapProperty(), null);
  assertThat(nodeTuple, is(nullValue()));
}
 
Example #8
Source File: VersionedYamlDoc.java    From onedev with MIT License 4 votes vote down vote up
private static Representer newRepresenter() {
	Representer representer = new Representer() {
		
	    @SuppressWarnings("rawtypes")
		@Override
	    protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, 
	    		Object propertyValue,Tag customTag) {
	        if (propertyValue == null 
	        		|| propertyValue instanceof Collection && ((Collection) propertyValue).isEmpty()
	        		|| propertyValue instanceof Map && ((Map) propertyValue).isEmpty()) { 
	        	return null;
	        } else {
	        	return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
	        }
	    }

	};
	representer.setDefaultFlowStyle(FlowStyle.BLOCK);
	representer.setPropertyUtils(new PropertyUtils() {

		@Override
		protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) {
			List<Property> properties = new ArrayList<>();
			Map<String, Integer> orders = new HashMap<>();
			if (type.getAnnotation(Editable.class) != null) {
				for (Method getter: BeanUtils.findGetters(type)) {
					Editable editable = getter.getAnnotation(Editable.class);
					Method setter = BeanUtils.findSetter(getter);
					if (editable != null && setter != null) {
						String propertyName = BeanUtils.getPropertyName(getter);
						try {
							properties.add(new MethodProperty(new PropertyDescriptor(propertyName, getter, setter)));
						} catch (IntrospectionException e) {
							throw new RuntimeException(e);
						}
						orders.put(propertyName, editable.order());
					}
				}
			}
			Collections.sort(properties, new Comparator<Property>() {

				@Override
				public int compare(Property o1, Property o2) {
					return orders.get(o1.getName()) - orders.get(o2.getName());
				}
				
			});
			return new LinkedHashSet<>(properties);
		}
		
	});
	return representer;
}
 
Example #9
Source File: AdvancedRepresenterTest.java    From waggle-dance with Apache License 2.0 4 votes vote down vote up
@Test
public void nullProperty() {
  Property property = new MethodProperty(getPropertyDescriptor("property"));
  NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getProperty(), null);
  assertThat(nodeTuple, is(nullValue()));
}
 
Example #10
Source File: AdvancedRepresenterTest.java    From waggle-dance with Apache License 2.0 4 votes vote down vote up
@Test
public void nullCollectionProperty() {
  Property property = new MethodProperty(getPropertyDescriptor("collectionProperty"));
  NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getCollectionProperty(), null);
  assertThat(nodeTuple, is(nullValue()));
}
 
Example #11
Source File: AdvancedRepresenterTest.java    From waggle-dance with Apache License 2.0 4 votes vote down vote up
@Test
public void nullMapProperty() {
  Property property = new MethodProperty(getPropertyDescriptor("mapProperty"));
  NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getMapProperty(), null);
  assertThat(nodeTuple, is(nullValue()));
}