| 1 |
|
package org.trails.descriptor.annotation; |
| 2 |
|
|
| 3 |
|
import ognl.Ognl; |
| 4 |
|
import org.trails.descriptor.*; |
| 5 |
|
|
| 6 |
|
import java.beans.Introspector; |
| 7 |
|
import java.beans.PropertyDescriptor; |
| 8 |
|
import java.lang.annotation.Annotation; |
| 9 |
|
import java.lang.reflect.Field; |
| 10 |
|
import java.lang.reflect.Method; |
| 11 |
|
import java.util.Map; |
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
0 |
public class OgnlAnnotationsDecorator implements DescriptorDecorator |
| 18 |
|
{ |
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
private Map context; |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
public IClassDescriptor decorate(IClassDescriptor descriptor) |
| 29 |
|
{ |
| 30 |
0 |
decoratePropertyDescriptors(descriptor); |
| 31 |
0 |
return descriptor; |
| 32 |
|
} |
| 33 |
|
|
| 34 |
|
private void decoratePropertyDescriptors(IClassDescriptor descriptor) |
| 35 |
|
{ |
| 36 |
0 |
for (IPropertyDescriptor propertyDescriptor : descriptor.getPropertyDescriptors()) |
| 37 |
|
{ |
| 38 |
0 |
decoratePropertyDescriptor(propertyDescriptor); |
| 39 |
|
|
| 40 |
0 |
if (propertyDescriptor.isEmbedded()) |
| 41 |
|
{ |
| 42 |
0 |
decorate((EmbeddedDescriptor) propertyDescriptor); |
| 43 |
|
} |
| 44 |
|
} |
| 45 |
0 |
} |
| 46 |
|
|
| 47 |
|
private void decoratePropertyDescriptor(IPropertyDescriptor propertyDescriptor) |
| 48 |
|
{ |
| 49 |
|
try |
| 50 |
|
{ |
| 51 |
0 |
Field propertyField = propertyDescriptor.getBeanType().getDeclaredField(propertyDescriptor.getName()); |
| 52 |
0 |
decorateFromAnnotations(propertyDescriptor, propertyField.getAnnotations()); |
| 53 |
|
|
| 54 |
0 |
} catch (Exception ex) |
| 55 |
|
{ |
| 56 |
|
|
| 57 |
|
} |
| 58 |
|
try |
| 59 |
|
{ |
| 60 |
0 |
PropertyDescriptor beanPropDescriptor = (PropertyDescriptor) Ognl.getValue("propertyDescriptors.{? name == '" + propertyDescriptor.getName() + "'}[0]", |
| 61 |
0 |
Introspector.getBeanInfo(propertyDescriptor.getBeanType())); |
| 62 |
|
|
| 63 |
0 |
Method readMethod = beanPropDescriptor.getReadMethod(); |
| 64 |
0 |
decorateFromAnnotations(propertyDescriptor, readMethod.getAnnotations()); |
| 65 |
|
} |
| 66 |
0 |
catch (Exception ex) |
| 67 |
|
{ |
| 68 |
|
|
| 69 |
|
} |
| 70 |
0 |
} |
| 71 |
|
|
| 72 |
|
private void decorateFromAnnotations(IDescriptor descriptor, Annotation[] annotations) |
| 73 |
|
{ |
| 74 |
0 |
for (Annotation annotation : annotations) |
| 75 |
|
{ |
| 76 |
0 |
if (annotation instanceof InitialValue) |
| 77 |
|
{ |
| 78 |
0 |
InitialValueDescriptorExtension extension = new InitialValueDescriptorExtension(((InitialValue) annotation).value(), context); |
| 79 |
0 |
descriptor.addExtension(InitialValueDescriptorExtension.class.getName(), extension); |
| 80 |
0 |
} else if (annotation instanceof PossibleValues) |
| 81 |
|
{ |
| 82 |
0 |
PossibleValuesDescriptorExtension extension = new PossibleValuesDescriptorExtension(((PossibleValues) annotation).value(), context); |
| 83 |
0 |
descriptor.addExtension(PossibleValuesDescriptorExtension.class.getName(), extension); |
| 84 |
|
} |
| 85 |
|
} |
| 86 |
0 |
} |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
public void setContext(Map context) |
| 94 |
|
{ |
| 95 |
0 |
this.context = context; |
| 96 |
0 |
} |
| 97 |
|
} |