View Javadoc

1   package org.jfrog.reflect.impl;
2   
3   import org.jfrog.reflect.FieldDefinition;
4   import org.jfrog.reflect.FieldPrefix;
5   import org.jfrog.reflect.ObjectMappingException;
6   
7   import java.lang.reflect.Field;
8   
9   /**
10   * Created by IntelliJ IDEA.
11   * User: freds
12   * Date: May 18, 2007
13   * Time: 9:23:33 AM
14   */
15  public class FieldDefinitionImpl extends AccessibleObjectDefinitionImpl implements FieldDefinition {
16      private String fieldName;
17      private Field reflectField;
18  
19      public FieldDefinitionImpl(Enum enumEntry) {
20          super(enumEntry);
21      }
22  
23      public FieldDefinitionImpl(Enum enumEntry, String fieldName) {
24          super(enumEntry);
25          this.fieldName = fieldName;
26      }
27  
28      public String getFieldName() {
29          if (fieldName == null) {
30              fieldName = enumEntry.name();
31              FieldPrefix fpAnn = (FieldPrefix) getModelClass().getAnnotation(FieldPrefix.class);
32              if (fpAnn != null) {
33                  fieldName = fpAnn.value() + fieldName;
34              }
35          }
36          return fieldName;
37      }
38  
39      public Field getField() {
40          if (reflectField == null) {
41              try {
42                  reflectField = getModelClass().getDeclaredField(getFieldName());
43              } catch (NoSuchFieldException e) {
44                  throw new ObjectMappingException("Field " + enumEntry + " with name " + fieldName +
45                          " does not exists in " + modelClass, e);
46              }
47          }
48          return reflectField;
49      }
50  }