⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelbuilder.java

📁 该源代码为一些通用的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if (parent == null) {
                continue;
            }
            final ClassDescription superCD = model.get(parent);
            if (superCD != null) {
                cd.setSuperClass(superCD.getObjectClass());
            }
        }
    }

    /**
     * Updates the model to contain the given classes.
     *
     * @param classes  a list of classes which should be part of the model.
     * @param model  the model which is updated
     * 
     * @return A list of super classes which should also be contained in the model.
     */
    private Class[] fillModel(final Class[] classes, final DescriptionModel model) {
        // first check all direct matches from the source collector.
        // but make sure that we also detect external superclasses -
        // we have to get all properties ...
        final ArrayList superClasses = new ArrayList();
        for (int i = 0; i < classes.length; i++) {

            Class superClass = classes[i].getSuperclass();
            if (superClass != null) {
                if (!Object.class.equals(superClass) 
                    && !contains(classes, superClass) 
                    && !superClasses.contains(superClass)) {
                    superClasses.add(superClass);
                }
            }
            else {
                superClass = Object.class;
            }

            try {
                final BeanInfo bi = Introspector.getBeanInfo(classes[i], superClass);
                final ClassDescription parent = model.get(classes[i]);
                final ClassDescription cd = createClassDescription(bi, parent);
                if (cd != null) {
                    model.addClassDescription(cd);
                }
            }
            catch (IntrospectionException ie) {
                // swallowed....
            }
        }
        return (Class[]) superClasses.toArray(new Class[0]);
    }

    /**
     * Creates a {@link ClassDescription} object for the specified bean info.
     * 
     * @param beanInfo  the bean info.
     * @param parent  the parent class description.
     * 
     * @return The class description.
     */
    private ClassDescription createClassDescription (final BeanInfo beanInfo, final ClassDescription parent) {
        final PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
        final ArrayList properties = new ArrayList();
        for (int i = 0; i < props.length; i++) {
            final PropertyDescriptor propertyDescriptor = props[i];
            PropertyInfo pi;
            if (parent != null) {
                pi = parent.getProperty(propertyDescriptor.getName());
                if (pi != null) {
                    // Property already found, don't touch it
//                    Log.info (new Log.SimpleMessage
//                        ("Ignore predefined property: ", propertyDescriptor.getName()));
                    properties.add(pi);
                    continue;
                }
            }

            if (props[i] instanceof IndexedPropertyDescriptor) {
                // this would handle lists and array access. We don't support
                // this in the direct approach. We will need some cheating:
                // <Chart>
                //    <Subtitle-list>
                //         <title1 ..>
                //         <title2 ..>
                // pi = createIndexedPropertyInfo((IndexedPropertyDescriptor) props[i]);
            }
            else {
                pi = createSimplePropertyInfo(props[i]);
                if (pi != null) {
                    properties.add(pi);
                }
            }
        }

        final PropertyInfo[] propArray = (PropertyInfo[])
            properties.toArray(new PropertyInfo[properties.size()]);

        final ClassDescription cd;
        if (parent != null) {
            cd = parent;
        }
        else {
            cd = new ClassDescription(beanInfo.getBeanDescriptor().getBeanClass());
            cd.setDescription(beanInfo.getBeanDescriptor().getShortDescription());
        }

        cd.setProperties(propArray);
        return cd;
    }

    /**
     * Checks, whether the given method can be called from the generic object factory.
     *
     * @param method the method descriptor
     * @return true, if the method is not null and public, false otherwise.
     */
    public static boolean isValidMethod(final Method method) {
        if (method == null) {
            return false;
        }
        if (!Modifier.isPublic(method.getModifiers())) {
            return false;
        }
        return true;
    }

    /**
     * Creates a {@link PropertyInfo} object from a {@link PropertyDescriptor}.
     * 
     * @param pd  the property descriptor.
     * 
     * @return the property info (<code>null</code> possible).
     */
    public PropertyInfo createSimplePropertyInfo(final PropertyDescriptor pd) {

        final boolean readMethod = isValidMethod(pd.getReadMethod());
        final boolean writeMethod = isValidMethod(pd.getWriteMethod());
        if (!writeMethod || !readMethod) {
            // a property is useless for our purposes without having a read or write method.
            return null;
        }

        final PropertyInfo pi = new PropertyInfo(pd.getName(), pd.getPropertyType());
        pi.setConstrained(pd.isConstrained());
        pi.setDescription(pd.getShortDescription());
        pi.setNullable(true);
        pi.setPreserve(false);
        pi.setReadMethodAvailable(readMethod);
        pi.setWriteMethodAvailable(writeMethod);
        pi.setXmlName(pd.getName());
        if (isAttributeProperty(pd.getPropertyType())) {
            pi.setPropertyType(PropertyType.ATTRIBUTE);
            pi.setXmlHandler(getHandlerClass(pd.getPropertyType()));
        }
        else {
            pi.setPropertyType(PropertyType.ELEMENT);
        }
        return pi;
    }

    /**
     * Checks, whether the given class can be handled as attribute.
     * All primitive types can be attributes as well as all types which have
     * a custom attribute handler defined.
     *
     * @param c the class which should be checked
     * @return true, if the class can be handled as attribute, false otherwise.
     */
    private boolean isAttributeProperty(final Class c) {
        if (BasicTypeSupport.isBasicDataType(c)) {
            return true;
        }
        return this.handlerMapping.containsKey(c.getName());
    }

    /**
     * Returns the class name for the attribute handler for a property of the specified class.
     *
     * @param c the class for which to search an attribute handler
     * @return the handler class or null, if this class cannot be handled
     * as attribute.
     */
    private String getHandlerClass(final Class c) {
        if (BasicTypeSupport.isBasicDataType(c)) {
            final String handler = BasicTypeSupport.getHandlerClass(c);
            if (handler != null) {
                return handler;
            }
        }
        return this.handlerMapping.getProperty(c.getName());
    }

    /**
     * Checks, whether the class <code>c</code> is contained in the given
     * class array.
     *
     * @param cAll the list of all classes
     * @param c the class to be searched
     * @return true, if the class is contained in the array, false otherwise.
     */
    private boolean contains(final Class[] cAll, final Class c) {
        for (int i = 0; i < cAll.length; i++) {
            if (cAll[i].equals(c)) {
                return true;
            }
        }
        return false;
    }


//  private PropertyInfo createIndexedPropertyInfo(IndexedPropertyDescriptor prop)
//  {
//
//    MethodInfo readMethod = createMethodInfo(prop.getIndexedReadMethod());
//    MethodInfo writeMethod = createMethodInfo(prop.getIndexedWriteMethod());
//    if (writeMethod == null)
//    {
//      return null;
//    }
//    IndexedPropertyInfo pi = new IndexedPropertyInfo(prop.getName());
//    pi.setConstrained(prop.isConstrained());
//    pi.setDescription(prop.getShortDescription());
//    pi.setNullable(true);
//    pi.setPreserve(false);
//    pi.setType(prop.getIndexedPropertyType());
//    pi.setReadMethod(readMethod);
//    pi.setWriteMethod(writeMethod);
//
//    TypeInfo keyInfo = new TypeInfo("index");
//    keyInfo.setType(Integer.TYPE);
//    keyInfo.setNullable(false);
//    keyInfo.setConstrained(true); // throws indexoutofboundsexception
//    keyInfo.setDescription("Generic index value");
//    KeyDescription kd = new KeyDescription(new TypeInfo[]{keyInfo});
//    pi.setKey(kd);
//    return pi;
//  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -