1 8 9 package com.sleepycat.persist.impl; 10 11 import java.io.Serializable ; 12 import java.lang.reflect.Field ; 13 import java.lang.reflect.Modifier ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import com.sleepycat.persist.raw.RawField; 19 20 25 class FieldInfo implements RawField, Serializable , Comparable <FieldInfo> { 26 27 private static final long serialVersionUID = 2062721100372306296L; 28 29 33 static List <FieldInfo> getInstanceFields(Class cls) { 34 Field [] declaredFields = cls.getDeclaredFields(); 35 List <FieldInfo> fields = 36 new ArrayList <FieldInfo>(declaredFields.length); 37 for (Field field : declaredFields) { 38 int mods = field.getModifiers(); 39 if (!Modifier.isTransient(mods) && !Modifier.isStatic(mods)) { 40 fields.add(new FieldInfo(field)); 41 } 42 } 43 return fields; 44 } 45 46 static FieldInfo getField(List <FieldInfo> fields, String fieldName) { 47 int i = getFieldIndex(fields, fieldName); 48 if (i >= 0) { 49 return fields.get(i); 50 } else { 51 return null; 52 } 53 } 54 55 static int getFieldIndex(List <FieldInfo> fields, String fieldName) { 56 for (int i = 0; i < fields.size(); i += 1) { 57 FieldInfo field = fields.get(i); 58 if (fieldName.equals(field.getName())) { 59 return i; 60 } 61 } 62 return -1; 63 } 64 65 private String name; 66 private String className; 67 private Format format; 68 private transient Class cls; 69 70 private FieldInfo(Field field) { 71 name = field.getName(); 72 cls = field.getType(); 73 className = cls.getName(); 74 } 75 76 void collectRelatedFormats(Catalog catalog, 77 Map <String ,Format> newFormats) { 78 format = catalog.createFormat(cls, newFormats); 79 } 80 81 void migrateFromBeta(Map <String ,Format> formatMap) { 82 if (format == null) { 83 format = formatMap.get(className); 84 if (format == null) { 85 throw new IllegalStateException (className); 86 } 87 } 88 } 89 90 void initialize(Catalog catalog) { 91 } 92 93 Class getFieldClass() { 94 if (cls == null) { 95 try { 96 cls = SimpleCatalog.classForName(className); 97 } catch (ClassNotFoundException e) { 98 throw new IllegalStateException (e); 99 } 100 } 101 return cls; 102 } 103 104 String getClassName() { 105 return className; 106 } 107 108 public String getName() { 109 return name; 110 } 111 112 public Format getType() { 113 return format; 114 } 115 116 public int compareTo(FieldInfo o) { 117 return name.compareTo(o.name); 118 } 119 120 @Override 121 public boolean equals(Object other) { 122 if (other instanceof FieldInfo) { 123 FieldInfo o = (FieldInfo) other; 124 return name.equals(o.name); 125 } else { 126 return false; 127 } 128 } 129 130 @Override 131 public int hashCode() { 132 return name.hashCode(); 133 } 134 135 @Override 136 public String toString() { 137 return "[Field name: " + name + " class: " + className + ']'; 138 } 139 } 140 | Popular Tags |