| 1 |
|
package org.trails.descriptor.annotation; |
| 2 |
|
|
| 3 |
|
import java.beans.Introspector; |
| 4 |
|
import java.beans.PropertyDescriptor; |
| 5 |
|
import java.lang.annotation.Annotation; |
| 6 |
|
import java.lang.reflect.Field; |
| 7 |
|
import java.lang.reflect.Method; |
| 8 |
|
import java.util.ArrayList; |
| 9 |
|
import java.util.Collections; |
| 10 |
|
import java.util.Iterator; |
| 11 |
|
|
| 12 |
|
import ognl.Ognl; |
| 13 |
|
import org.trails.descriptor.DescriptorDecorator; |
| 14 |
|
import org.trails.descriptor.IClassDescriptor; |
| 15 |
|
import org.trails.descriptor.IDescriptor; |
| 16 |
|
import org.trails.descriptor.IPropertyDescriptor; |
| 17 |
|
import org.trails.descriptor.EmbeddedDescriptor; |
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
4 |
public class AnnotationDecorator implements DescriptorDecorator |
| 26 |
|
{ |
| 27 |
|
|
| 28 |
|
public IClassDescriptor decorate(IClassDescriptor descriptor) |
| 29 |
|
{ |
| 30 |
|
|
| 31 |
3 |
Annotation[] classAnnotations = descriptor.getType().getAnnotations(); |
| 32 |
3 |
IClassDescriptor decoratedDescriptor = (IClassDescriptor) decorateFromAnnotations(descriptor, classAnnotations); |
| 33 |
3 |
ArrayList decoratedPropertyDescriptors = new ArrayList(); |
| 34 |
14 |
for (Iterator iter = descriptor.getPropertyDescriptors().iterator(); iter.hasNext();) |
| 35 |
|
{ |
| 36 |
8 |
IPropertyDescriptor propertyDescriptor = (IPropertyDescriptor) iter.next(); |
| 37 |
8 |
IPropertyDescriptor clonedDescriptor = decoratePropertyDescriptor(propertyDescriptor); |
| 38 |
|
|
| 39 |
8 |
if (clonedDescriptor.isEmbedded()) |
| 40 |
|
{ |
| 41 |
1 |
clonedDescriptor = (EmbeddedDescriptor) decorate((EmbeddedDescriptor) clonedDescriptor); |
| 42 |
|
} |
| 43 |
8 |
decoratedPropertyDescriptors.add(clonedDescriptor); |
| 44 |
|
} |
| 45 |
3 |
decoratedDescriptor.setPropertyDescriptors(decoratedPropertyDescriptors); |
| 46 |
3 |
sortDescriptors(decoratedDescriptor); |
| 47 |
3 |
return decoratedDescriptor; |
| 48 |
|
} |
| 49 |
|
|
| 50 |
|
protected IPropertyDescriptor decoratePropertyDescriptor(IPropertyDescriptor propertyDescriptor) |
| 51 |
|
{ |
| 52 |
8 |
IPropertyDescriptor clonedDescriptor = (IPropertyDescriptor) propertyDescriptor.clone(); |
| 53 |
|
try |
| 54 |
|
{ |
| 55 |
16 |
Field propertyField = clonedDescriptor.getBeanType().getDeclaredField( |
| 56 |
8 |
propertyDescriptor.getName()); |
| 57 |
6 |
clonedDescriptor = (IPropertyDescriptor) decorateFromAnnotations(clonedDescriptor, propertyField.getAnnotations()); |
| 58 |
|
|
| 59 |
2 |
} catch (Exception ex) |
| 60 |
|
{ |
| 61 |
|
|
| 62 |
|
} |
| 63 |
|
try |
| 64 |
|
{ |
| 65 |
16 |
PropertyDescriptor beanPropDescriptor = (PropertyDescriptor) Ognl.getValue("propertyDescriptors.{? name == '" + propertyDescriptor.getName() + "'}[0]", |
| 66 |
8 |
Introspector.getBeanInfo(clonedDescriptor.getBeanType())); |
| 67 |
|
|
| 68 |
6 |
Method readMethod = beanPropDescriptor.getReadMethod(); |
| 69 |
6 |
clonedDescriptor = (IPropertyDescriptor) decorateFromAnnotations(clonedDescriptor, readMethod.getAnnotations()); |
| 70 |
|
} |
| 71 |
2 |
catch (Exception ex) |
| 72 |
|
{ |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
} |
| 77 |
8 |
return clonedDescriptor; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
private void sortDescriptors(IClassDescriptor descriptor) |
| 86 |
|
{ |
| 87 |
3 |
ArrayList sortedDescriptors = new ArrayList(); |
| 88 |
3 |
sortedDescriptors.addAll(descriptor.getPropertyDescriptors()); |
| 89 |
14 |
for (Iterator iter = descriptor.getPropertyDescriptors().iterator(); iter.hasNext();) |
| 90 |
|
{ |
| 91 |
8 |
IPropertyDescriptor propertyDescriptor = (IPropertyDescriptor) iter.next(); |
| 92 |
8 |
if (propertyDescriptor.getIndex() != IPropertyDescriptor.UNDEFINED_INDEX) |
| 93 |
|
{ |
| 94 |
8 |
Collections.swap(sortedDescriptors, |
| 95 |
4 |
propertyDescriptor.getIndex(), |
| 96 |
4 |
sortedDescriptors.indexOf(propertyDescriptor)); |
| 97 |
|
} |
| 98 |
|
} |
| 99 |
3 |
descriptor.setPropertyDescriptors(sortedDescriptors); |
| 100 |
|
|
| 101 |
3 |
} |
| 102 |
|
|
| 103 |
|
private IDescriptor decorateFromAnnotations(IDescriptor descriptor, Annotation[] annotations) |
| 104 |
|
{ |
| 105 |
15 |
IDescriptor clonedDescriptor = (IDescriptor) descriptor.clone(); |
| 106 |
20 |
for (int i = 0; i < annotations.length; i++) |
| 107 |
|
{ |
| 108 |
5 |
Annotation annotation = annotations[i]; |
| 109 |
|
|
| 110 |
5 |
DescriptorAnnotation handlerAnnotation = annotation.annotationType().getAnnotation(DescriptorAnnotation.class); |
| 111 |
5 |
if (handlerAnnotation != null) |
| 112 |
|
{ |
| 113 |
|
try |
| 114 |
|
{ |
| 115 |
5 |
DescriptorAnnotationHandler handler = handlerAnnotation.value().newInstance(); |
| 116 |
5 |
clonedDescriptor = handler.decorateFromAnnotation(annotation, clonedDescriptor); |
| 117 |
|
} |
| 118 |
0 |
catch (Exception ex) |
| 119 |
|
{ |
| 120 |
|
|
| 121 |
|
} |
| 122 |
|
} |
| 123 |
|
} |
| 124 |
15 |
return clonedDescriptor; |
| 125 |
|
} |
| 126 |
|
|
| 127 |
|
} |