}
}
// check for a Java 5 enumeration
boolean isenum = false;
IClass sclas = m_typeClass;
while ((sclas = sclas.getSuperClass()) != null) {
if (sclas.getName().equals("java.lang.Enum")) {
isenum = true;
break;
}
}
// find the enum value method, if specified
if (m_enumValueName != null) {
if (isenum) {
if (m_serializerName == null &&
m_deserializerName == null) {
m_enumValueItem = m_typeClass.getMethod(m_enumValueName,
ENUM_VALUE_METHOD_SIGNATURE);
if (m_enumValueItem == null) {
vctx.addError("Nonstatic 'enum-value-method' " +
m_enumValueName + " not found in class " +
m_typeClass.getName());
}
} else {
vctx.addError("'enum-value-method' cannot be used with 'serializer' or 'deserializer'");
}
} else {
vctx.addError("'enum-value-method' may only be used with Java 5 enum classes");
}
}
// check specified serializer and deserializer
String tname = m_typeClass.getName();
if (vctx.isOutBinding()) {
if (m_serializerName == null) {
if (m_enumValueItem == null) {
// try to find an inherited serializer
FormatElement ances = m_baseFormat;
while (ances != null) {
m_serializerItem = ances.getSerializer();
if (m_serializerItem == null) {
ances = ances.getBaseFormat();
} else {
break;
}
}
if (m_serializerItem == null) {
IClassItem item = m_typeClass.getMethod("toString",
"()Ljava/lang/String;");
if (item == null) {
vctx.addError("toString method not found");
}
}
}
} else {
// build all possible signature variations
String[] tsigs = ClassUtils.
getSignatureVariants(tname, vctx);
int vcnt = SERIALIZER_SIGNATURE_VARIANTS.length;
String[] msigs = new String[tsigs.length * vcnt];
for (int i = 0; i < tsigs.length; i++) {
for (int j = 0; j < vcnt; j++) {
msigs[i*vcnt + j] = "(" + tsigs[i] +
SERIALIZER_SIGNATURE_VARIANTS[j] +
")Ljava/lang/String;";
}
}
// find a matching static method
m_serializerItem = ClassUtils.
findStaticMethod(m_serializerName, msigs, vctx);
if (m_serializerItem == null) {
if (m_serializerName.indexOf('.') > 0) {
vctx.addError("Static serializer method " + m_serializerName + " not found");
} else {
vctx.addError("Need class name for static method " + m_serializerName);
}
}
}
}
if (vctx.isInBinding() || m_defaultText != null) {
if (m_deserializerName == null) {
if (isenum) {
if (m_enumValueItem == null) {
m_deserializerItem = m_typeClass.
getMethod("valueOf", "(Ljava/lang/String;)");
}
} else {
// try to find an inherited deserializer
FormatElement ances = m_baseFormat;
while (ances != null) {
m_deserializerItem = ances.getDeserializer();
if (m_deserializerItem == null) {
ances = ances.getBaseFormat();
} else {
break;
}
}
if (m_deserializerItem == null) {
// try to find constructor from string as last resort
m_deserializerItem = m_typeClass.
getInitializerMethod(STRING_CONSTRUCTOR_SIGNATURE);
if (m_deserializerItem == null) {
// error unless predefined formats
if (vctx.getNestingDepth() > 0) {
StringBuffer buff = new StringBuffer();
buff.append("Need deserializer or constructor from string");
if (!vctx.isInBinding()) {
buff.append(" for default value of type ");
buff.append(tname);
} else {
buff.append(" for type ");
buff.append(tname);
}
vctx.addError(buff.toString());
}
}
}
}
} else {
// find a matching static method
m_deserializerItem = ClassUtils.
findStaticMethod(m_deserializerName,
DESERIALIZER_SIGNATURES, vctx);
if (m_deserializerItem == null) {
if (m_deserializerName.indexOf('.') > 0) {
vctx.addError("Static deserializer method " + m_deserializerName + " not found");
} else {
vctx.addError("Need class name for static method " + m_deserializerName);
}
} else {
String result = m_deserializerItem.getTypeName();
if (!ClassUtils.isAssignable(result, tname, vctx)) {
vctx.addError("Static deserializer method " +
m_deserializerName +
" has incompatible result type");
}
}
}
}
// check for default value to be converted
if (m_defaultText != null && m_deserializerItem != null) {
// first load the class to handle conversion
IClass iclas = m_deserializerItem.getOwningClass();
Class clas = iclas.loadClass();
Exception ex = null;
boolean construct = false;
try {
if (clas == null) {
vctx.addError("Unable to load class " +
iclas.getName() +
" for converting default value of type " + tname);
} else if (m_deserializerItem.isInitializer()) {
// invoke constructor to process default value
construct = true;