package org.jvnet.jaxb2_commons.util; import javax.xml.namespace.QName; import com.sun.tools.xjc.generator.bean.ClassOutlineImpl; import com.sun.tools.xjc.generator.bean.field.FieldRendererFactory; import com.sun.tools.xjc.model.CAttributePropertyInfo; import com.sun.tools.xjc.model.CCustomizations; import com.sun.tools.xjc.model.CNonElement; import com.sun.tools.xjc.model.CPropertyInfo; import com.sun.tools.xjc.outline.FieldOutline; /** * Field utilities. * * @author valikov */ public class FieldUtils { /** Hidden constructor. */ private FieldUtils() { } // // public static FieldItem[] getFieldItems(ClassContext classContext) { // final List fieldItems = (List) classContext.target // .visit(new FieldGatheringVisitor(classContext)); // return (FieldItem[]) fieldItems.toArray(new // FieldItem[fieldItems.size()]); // } // // public static String getFieldName(ClassContext classContext, FieldItem // fieldItem) { // return ClassUtils.getClassName(classContext.target) + "." + // fieldItem.name; // } // // public static String getFieldPropertyName(FieldItem fieldItem) { // return fieldItem.name; // } // // public static String getCommonFieldPropertyName(FieldItem fieldItem) { // final String draftName = fieldItem.name; // return Introspector.decapitalize(draftName); // } // // public static FieldItem getFieldItem(FieldUse fieldUse) { // if (fieldUse.items.isEmpty()) { // return null; // } // else { // return (FieldItem) fieldUse.items.iterator().next(); // } // } // // public static FieldUse getFieldUse(ClassContext classContext, FieldItem // fieldItem) { // return classContext.target.getField(fieldItem.name); // } // // /** // * Returns a new unique name of the field in the given class, based on the // given prefix. If required, the prefix will // * be appended with an integer to make it unique // * // * @param theClass class to create field in. // * @param prefix field name prefix. // * @return Unique name of the new field. // */ // public static String generateUniqueFieldName(final JDefinedClass // theClass, final String prefix) { // final String name; // if (null == getField(theClass, prefix)) { // name = prefix; // } // else { // int index = 0; // while (null != getField(theClass, prefix + index)) { // index++; // } // name = prefix + index; // } // return name; // } // // /** // * Retrievs a named field of the given class. // * // * @param theClass class to search a field in. // * @param name name of the field. // * @return Requested field of the given class. // */ // public static JVar getField(final JDefinedClass theClass, final String // name) { // JFieldVar result = null; // for (Iterator iterator = theClass.fields(); iterator.hasNext();) { // final JFieldVar var = (JFieldVar) iterator.next(); // if (name.equals(var.name())) { // result = var; // } // } // // todo : if not found??? // return result; // } // public static FieldOutline createAttributeField( ClassOutlineImpl classOutline, String name, final QName attName, final CNonElement typeInfo, final boolean required // , // final XSDatatype datatype, // final JType type ) { final CPropertyInfo propertyInfo = new CAttributePropertyInfo( // name name, // source null, new CCustomizations(), // locator null, // attName attName, // typeUse typeInfo, // typeName typeInfo.getTypeName(), // required required); propertyInfo.realization = new FieldRendererFactory().getDefault(); final FieldOutline fieldOutline = propertyInfo.realization.generate(classOutline, propertyInfo); return fieldOutline; } // public static boolean isConstantField(ClassContext classContext, // FieldItem fieldItem) { // return AccessorUtils.get(classContext, fieldItem) == null; // } // // public static FieldUse[] getFieldUses(final ClassItem classItem) { // if (classItem.getSuperClass() == null) // return classItem.getDeclaredFieldUses(); // else { // final FieldUse[] superFieldUses = // FieldUtils.getFieldUses(classItem.getSuperClass()); // final FieldUse[] declaredFieldUses = classItem.getDeclaredFieldUses(); // final FieldUse[] fieldUses = new FieldUse[superFieldUses.length + // declaredFieldUses.length]; // System.arraycopy(superFieldUses, 0, fieldUses, 0, superFieldUses.length); // System.arraycopy( // declaredFieldUses, // 0, // fieldUses, // superFieldUses.length, // declaredFieldUses.length); // return fieldUses; // } // } }