001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: FieldInfo.java,v 1.19.2.4 2008/01/07 15:14:19 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.impl;
010:
011: import java.io.Serializable;
012: import java.lang.reflect.Field;
013: import java.lang.reflect.Modifier;
014: import java.util.ArrayList;
015: import java.util.List;
016: import java.util.Map;
017:
018: import com.sleepycat.persist.raw.RawField;
019:
020: /**
021: * A field definition used by ComplexFormat and CompositeKeyFormat.
022: *
023: * <p>Note that the equals(), compareTo() and hashCode() methods only use the
024: * name field in this class. Comparing two FieldInfo objects is only done when
025: * both are declared in the same class, so comparing the field name is
026: * sufficient.</p>
027: *
028: * @author Mark Hayes
029: */
030: class FieldInfo implements RawField, Serializable,
031: Comparable<FieldInfo> {
032:
033: private static final long serialVersionUID = 2062721100372306296L;
034:
035: /**
036: * Returns a list of all non-transient non-static fields that are declared
037: * in the given class.
038: */
039: static List<FieldInfo> getInstanceFields(Class cls) {
040: Field[] declaredFields = cls.getDeclaredFields();
041: List<FieldInfo> fields = new ArrayList<FieldInfo>(
042: declaredFields.length);
043: for (Field field : declaredFields) {
044: int mods = field.getModifiers();
045: if (!Modifier.isTransient(mods) && !Modifier.isStatic(mods)) {
046: fields.add(new FieldInfo(field));
047: }
048: }
049: return fields;
050: }
051:
052: static FieldInfo getField(List<FieldInfo> fields, String fieldName) {
053: int i = getFieldIndex(fields, fieldName);
054: if (i >= 0) {
055: return fields.get(i);
056: } else {
057: return null;
058: }
059: }
060:
061: static int getFieldIndex(List<FieldInfo> fields, String fieldName) {
062: for (int i = 0; i < fields.size(); i += 1) {
063: FieldInfo field = fields.get(i);
064: if (fieldName.equals(field.getName())) {
065: return i;
066: }
067: }
068: return -1;
069: }
070:
071: private String name;
072: private String className;
073: private Format format;
074: private transient Class cls;
075:
076: private FieldInfo(Field field) {
077: name = field.getName();
078: cls = field.getType();
079: className = cls.getName();
080: }
081:
082: void collectRelatedFormats(Catalog catalog,
083: Map<String, Format> newFormats) {
084: format = catalog.createFormat(cls, newFormats);
085: }
086:
087: void migrateFromBeta(Map<String, Format> formatMap) {
088: if (format == null) {
089: format = formatMap.get(className);
090: if (format == null) {
091: throw new IllegalStateException(className);
092: }
093: }
094: }
095:
096: void initialize(Catalog catalog, int initVersion) {
097: }
098:
099: Class getFieldClass() {
100: if (cls == null) {
101: try {
102: cls = SimpleCatalog.classForName(className);
103: } catch (ClassNotFoundException e) {
104: throw new IllegalStateException(e);
105: }
106: }
107: return cls;
108: }
109:
110: String getClassName() {
111: return className;
112: }
113:
114: public String getName() {
115: return name;
116: }
117:
118: public Format getType() {
119: return format;
120: }
121:
122: public int compareTo(FieldInfo o) {
123: return name.compareTo(o.name);
124: }
125:
126: @Override
127: public boolean equals(Object other) {
128: if (other instanceof FieldInfo) {
129: FieldInfo o = (FieldInfo) other;
130: return name.equals(o.name);
131: } else {
132: return false;
133: }
134: }
135:
136: @Override
137: public int hashCode() {
138: return name.hashCode();
139: }
140:
141: @Override
142: public String toString() {
143: return "[Field name: " + name + " class: " + className + ']';
144: }
145: }
|