Search in sources :

Example 1 with PropertyAdapter

use of org.apache.tapestry5.commons.services.PropertyAdapter in project tapestry-5 by apache.

the class ExceptionAnalyzerImpl method extractData.

private ExceptionData extractData(Throwable t) {
    Map<String, Object> properties = CollectionFactory.newMap();
    ClassPropertyAdapter adapter = propertyAccess.getAdapter(t);
    Throwable cause = null;
    for (String name : adapter.getPropertyNames()) {
        PropertyAdapter pa = adapter.getPropertyAdapter(name);
        if (!pa.isRead())
            continue;
        if (cause == null && Throwable.class.isAssignableFrom(pa.getType())) {
            // Ignore the property, but track it as the cause.
            Throwable nestedException = (Throwable) pa.get(t);
            // Handle the case where an exception is its own cause (avoid endless loop!)
            if (t != nestedException)
                cause = nestedException;
            continue;
        }
        if (throwableProperties.contains(name))
            continue;
        Object value = pa.get(t);
        if (value == null)
            continue;
        // An interesting property, let's save it for the analysis.
        properties.put(name, value);
    }
    // Provide the stack trace only at the deepest exception.
    List<StackTraceElement> stackTrace = Collections.emptyList();
    if (cause == null)
        stackTrace = Arrays.asList(t.getStackTrace());
    ExceptionInfo info = new ExceptionInfoImpl(t, properties, stackTrace);
    return new ExceptionData(info, cause);
}
Also used : ExceptionInfo(org.apache.tapestry5.ioc.services.ExceptionInfo)

Example 2 with PropertyAdapter

use of org.apache.tapestry5.commons.services.PropertyAdapter in project tapestry-5 by apache.

the class BeanModelSourceImpl method orderProperties.

/**
 * @param classAdapter  defines the bean that contains the properties
 * @param propertyNames the initial set of property names, which will be rebuilt in the correct order
 */
private void orderProperties(ClassPropertyAdapter classAdapter, List<String> propertyNames) {
    List<PropertyOrder> properties = CollectionFactory.newList();
    for (String name : propertyNames) {
        PropertyAdapter pa = classAdapter.getPropertyAdapter(name);
        Method readMethod = pa.getReadMethod();
        Location location = readMethod == null ? null : proxyFactory.getMethodLocation(readMethod);
        int line = location == null ? -1 : location.getLine();
        properties.add(new PropertyOrder(name, computeDepth(pa), line));
    }
    Collections.sort(properties);
    propertyNames.clear();
    for (PropertyOrder po : properties) {
        propertyNames.add(po.propertyName);
    }
}
Also used : ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) PropertyAdapter(org.apache.tapestry5.commons.services.PropertyAdapter) Method(java.lang.reflect.Method) Location(org.apache.tapestry5.commons.Location)

Example 3 with PropertyAdapter

use of org.apache.tapestry5.commons.services.PropertyAdapter in project tapestry-5 by apache.

the class BeanModelSourceImpl method create.

public <T> BeanModel<T> create(Class<T> beanClass, boolean filterReadOnlyProperties, Messages messages) {
    assert beanClass != null;
    assert messages != null;
    ClassPropertyAdapter adapter = propertyAccess.getAdapter(beanClass);
    BeanModel<T> model = new BeanModelImpl<T>(beanClass, propertyConduitSource, typeCoercer, messages, locator);
    for (final String propertyName : adapter.getPropertyNames()) {
        PropertyAdapter pa = adapter.getPropertyAdapter(propertyName);
        if (!pa.isRead()) {
            continue;
        }
        if (isStaticFieldProperty(pa)) {
            continue;
        }
        if (pa.getAnnotation(NonVisual.class) != null) {
            continue;
        }
        if (filterReadOnlyProperties && !pa.isUpdate()) {
            continue;
        }
        final String dataType = dataTypeAnalyzer.identifyDataType(pa);
        if (dataType == null) {
            continue;
        }
        model.add(propertyName).dataType(dataType);
    }
    // First, order the properties based on the location of the getter method
    // within the class.
    List<String> propertyNames = model.getPropertyNames();
    orderProperties(adapter, propertyNames);
    model.reorder(propertyNames.toArray(new String[propertyNames.size()]));
    // Next, check for an annotation with specific ordering information.
    ReorderProperties reorderAnnotation = beanClass.getAnnotation(ReorderProperties.class);
    if (reorderAnnotation != null) {
        BeanModelUtils.reorder(model, reorderAnnotation.value());
    }
    return model;
}
Also used : BeanModelImpl(org.apache.tapestry5.beanmodel.internal.beanmodel.BeanModelImpl) NonVisual(org.apache.tapestry5.beaneditor.NonVisual) ReorderProperties(org.apache.tapestry5.beaneditor.ReorderProperties) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) PropertyAdapter(org.apache.tapestry5.commons.services.PropertyAdapter)

Example 4 with PropertyAdapter

use of org.apache.tapestry5.commons.services.PropertyAdapter in project tapestry-5 by apache.

the class ComponentDefaultProviderImplTest method default_property_exists.

@Test
public void default_property_exists() {
    String parameterName = "myparam";
    String id = "mycomponentid";
    ComponentResources resources = mockComponentResources();
    Component container = mockComponent();
    PropertyAccess access = mockPropertyAccess();
    ClassPropertyAdapter classPropertyAdapter = mockClassPropertyAdapter();
    PropertyAdapter propertyAdapter = mockPropertyAdapter();
    BindingSource bindingSource = mockBindingSource();
    Binding binding = mockBinding();
    ComponentResources containerResources = mockComponentResources();
    train_getId(resources, id);
    train_getContainer(resources, container);
    train_getAdapter(access, container, classPropertyAdapter);
    train_getPropertyAdapter(classPropertyAdapter, id, propertyAdapter);
    train_getContainerResources(resources, containerResources);
    train_newBinding(bindingSource, "default myparam", containerResources, BindingConstants.PROP, id, binding);
    replay();
    ComponentDefaultProvider source = new ComponentDefaultProviderImpl(access, bindingSource, null, null, null);
    assertSame(source.defaultBinding(parameterName, resources), binding);
    verify();
}
Also used : Binding(org.apache.tapestry5.Binding) BindingSource(org.apache.tapestry5.services.BindingSource) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) PropertyAdapter(org.apache.tapestry5.commons.services.PropertyAdapter) Component(org.apache.tapestry5.runtime.Component) ComponentResources(org.apache.tapestry5.ComponentResources) PropertyAccess(org.apache.tapestry5.commons.services.PropertyAccess) ComponentDefaultProvider(org.apache.tapestry5.services.ComponentDefaultProvider) Test(org.testng.annotations.Test)

Example 5 with PropertyAdapter

use of org.apache.tapestry5.commons.services.PropertyAdapter in project tapestry-5 by apache.

the class PropertyValueLabelProvider method getLabel.

@SuppressWarnings({ "unchecked", "rawtypes" })
public String getLabel(Object object) {
    final ClassPropertyAdapter classPropertyAdapter = this.propertyAccess.getAdapter(object);
    final PropertyAdapter propertyAdapter = classPropertyAdapter.getPropertyAdapter(labelProperty);
    final ValueEncoder encoder = this.valueEncoderSource.getValueEncoder(propertyAdapter.getType());
    final Object label = propertyAdapter.get(object);
    return encoder.toClient(label);
}
Also used : ValueEncoder(org.apache.tapestry5.ValueEncoder) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) PropertyAdapter(org.apache.tapestry5.commons.services.PropertyAdapter) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter)

Aggregations

PropertyAdapter (org.apache.tapestry5.commons.services.PropertyAdapter)6 ClassPropertyAdapter (org.apache.tapestry5.commons.services.ClassPropertyAdapter)4 Test (org.testng.annotations.Test)3 AnnotationDataTypeAnalyzer (org.apache.tapestry5.commons.internal.services.AnnotationDataTypeAnalyzer)2 DataTypeAnalyzer (org.apache.tapestry5.commons.services.DataTypeAnalyzer)2 Method (java.lang.reflect.Method)1 Binding (org.apache.tapestry5.Binding)1 ComponentResources (org.apache.tapestry5.ComponentResources)1 ValueEncoder (org.apache.tapestry5.ValueEncoder)1 NonVisual (org.apache.tapestry5.beaneditor.NonVisual)1 ReorderProperties (org.apache.tapestry5.beaneditor.ReorderProperties)1 BeanModelImpl (org.apache.tapestry5.beanmodel.internal.beanmodel.BeanModelImpl)1 Location (org.apache.tapestry5.commons.Location)1 PropertyAccess (org.apache.tapestry5.commons.services.PropertyAccess)1 ExceptionInfo (org.apache.tapestry5.ioc.services.ExceptionInfo)1 Component (org.apache.tapestry5.runtime.Component)1 BindingSource (org.apache.tapestry5.services.BindingSource)1 ComponentDefaultProvider (org.apache.tapestry5.services.ComponentDefaultProvider)1