18
18
19
19
import org .slf4j .Logger ;
20
20
import org .slf4j .LoggerFactory ;
21
+ import org .springframework .core .annotation .AnnotatedElementUtils ;
21
22
import org .springframework .ldap .odm .annotations .Entry ;
22
23
import org .springframework .ldap .odm .annotations .Id ;
23
24
import org .springframework .ldap .support .LdapUtils ;
25
+ import org .springframework .util .ReflectionUtils ;
24
26
import org .springframework .util .StringUtils ;
25
27
26
28
import javax .naming .Name ;
@@ -85,29 +87,33 @@ public AttributeMetaData getAttribute(Field field) {
85
87
return fieldToAttribute .get (field );
86
88
}
87
89
88
- public ObjectMetaData (Class <?> clazz ) {
90
+ public ObjectMetaData (final Class <?> clazz ) {
89
91
if (LOG .isDebugEnabled ()) {
90
92
LOG .debug (String .format ("Extracting metadata from %1$s" , clazz ));
91
93
}
92
94
93
95
// Get object class metadata - the @Entity annotation
94
- Entry entity = clazz .getAnnotation (Entry .class );
95
- if (entity != null ) {
96
+ // findAllMergedAnnotations will return set of inherited annotations and apply them from superclass down to subclass
97
+ Set <Entry > entities = AnnotatedElementUtils .findAllMergedAnnotations (clazz ,Entry .class );
98
+ if (entities != null && !entities .isEmpty ()) {
96
99
// Default objectclass name to the class name unless it's specified
97
100
// in @Entity(name={objectclass1, objectclass2});
98
- String [] localObjectClasses = entity .objectClasses ();
99
- if (localObjectClasses != null && localObjectClasses .length > 0 && localObjectClasses [0 ].length () > 0 ) {
100
- for (String localObjectClass :localObjectClasses ) {
101
- objectClasses .add (new CaseIgnoreString (localObjectClass ));
101
+ for (Entry entity : entities ) {
102
+ String [] localObjectClasses = entity .objectClasses ();
103
+ if (localObjectClasses .length > 0 && localObjectClasses [0 ].length () > 0 ) {
104
+ for (String localObjectClass : localObjectClasses ) {
105
+ objectClasses .add (new CaseIgnoreString (localObjectClass ));
106
+ }
102
107
}
103
- } else {
108
+ String base = entity .base ();
109
+ if (StringUtils .hasText (base )) {
110
+ this .base = LdapUtils .newLdapName (base );
111
+ }
112
+ }
113
+ if (objectClasses .isEmpty ()) {
104
114
objectClasses .add (new CaseIgnoreString (clazz .getSimpleName ()));
105
115
}
106
116
107
- String base = entity .base ();
108
- if (StringUtils .hasText (base )) {
109
- this .base = LdapUtils .newLdapName (base );
110
- }
111
117
} else {
112
118
throw new MetaDataException (String .format ("Class %1$s must have a class level %2$s annotation" , clazz ,
113
119
Entry .class ));
@@ -119,31 +125,34 @@ public ObjectMetaData(Class<?> clazz) {
119
125
}
120
126
121
127
// Get field meta-data - the @Attribute annotation
122
- Field [] fields = clazz .getDeclaredFields ();
123
- for (Field field : fields ) {
124
- // So we can write to private fields
125
- field .setAccessible (true );
126
-
127
- // Skip synthetic or static fields
128
- if (Modifier .isStatic (field .getModifiers ()) || field .isSynthetic ()) {
129
- continue ;
130
- }
131
-
132
- AttributeMetaData currentAttributeMetaData =new AttributeMetaData (field );
133
- if (currentAttributeMetaData .isId ()) {
134
- if (idAttribute !=null ) {
135
- // There can be only one id field
136
- throw new MetaDataException (
137
- String .format ("You man have only one field with the %1$s annotation in class %2$s" , Id .class , clazz ));
128
+ ReflectionUtils .doWithFields (clazz , new ReflectionUtils .FieldCallback () {
129
+ @ Override
130
+ public void doWith (Field field ) throws IllegalArgumentException {
131
+ // So we can write to private fields
132
+ field .setAccessible (true );
133
+
134
+ // Skip synthetic or static fields
135
+ if (Modifier .isStatic (field .getModifiers ()) || field .isSynthetic ()) {
136
+ return ;
137
+ }
138
+
139
+ AttributeMetaData currentAttributeMetaData =new AttributeMetaData (field );
140
+ if (currentAttributeMetaData .isId ()) {
141
+ if (idAttribute !=null ) {
142
+ // There can be only one id field
143
+ throw new MetaDataException (
144
+ String .format ("You man have only one field with the %1$s annotation in class %2$s" , Id .class , clazz ));
145
+ }
146
+ idAttribute =currentAttributeMetaData ;
147
+ }
148
+ fieldToAttribute .put (field , currentAttributeMetaData );
149
+
150
+ if (currentAttributeMetaData .isDnAttribute ()) {
151
+ dnAttributes .add (currentAttributeMetaData );
152
+ }
138
153
}
139
- idAttribute =currentAttributeMetaData ;
140
- }
141
- fieldToAttribute .put (field , currentAttributeMetaData );
142
-
143
- if (currentAttributeMetaData .isDnAttribute ()) {
144
- dnAttributes .add (currentAttributeMetaData );
145
154
}
146
- }
155
+ );
147
156
148
157
if (idAttribute == null ) {
149
158
throw new MetaDataException (
0 commit comments