classcustom.java

来自「对xml很好的java处理引擎,编译中绑定xml」· Java 代码 · 共 679 行 · 第 1/2 页

JAVA
679
字号
        for (int i = 0; i < methods.length; i++) {            IClassItem item = methods[i];            String name = item.getName();            if (item.getArgumentCount() == 1 && name.startsWith("set") &&                item.getTypeName().equals("void")) {                                // have what appears to be a setter, check if it should be used                String memb = MemberCustom.memberNameFromSetMethod(name);                boolean use = true;                if (inclset != null) {                    use = inclset.contains(memb.toLowerCase());                } else if (exclset != null) {                    use = !exclset.contains(memb.toLowerCase());                }                if (use) {                    setmap.put(memb, item);                }                            }        }        return setmap;    }    /**     * Build map from member names to fields. This includes all non-static and     * non-transient fields of the class.     *     * @param fields     * @param prefs prefixes to be stripped in deriving names     * @param suffs suffixes to be stripped in deriving names     * @param inclset set of member names to be included (<code>null</code>     * if not specified)     * @param exclset set of member names to be excluded (<code>null</code>     * if not specified, ignored if inclset is non-<code>null</code>)     * @return map     */    private Map mapFields(IClassItem[] fields, String[] prefs, String[] suffs,        Set inclset, Set exclset) {                // check all fields for use as members        HashMap fieldmap = new HashMap();        for (int i = 0; i < fields.length; i++) {            IClassItem item = fields[i];            String name = item.getName();            String memb = MemberCustom.memberNameFromField(name, prefs, suffs);            boolean use = true;            if (inclset != null) {                use = inclset.contains(memb.toLowerCase());            } else if (exclset != null) {                use = !exclset.contains(memb.toLowerCase());            }            if (use) {                fieldmap.put(memb, item);            }        }        return fieldmap;    }    /**     * Find the most specific type for a property based on the access methods.     *     * @param gmeth read access method (<code>null</code> if not defined)     * @param smeth write access method (<code>null</code> if not defined)     * @param icl     * @return most specific type name     */    private String findPropertyType(IClassItem gmeth, IClassItem smeth,        IClassLocator icl) {        String type;        if (gmeth == null) {            if (smeth == null) {                throw new IllegalArgumentException("Internal error: no access methods known");            } else {                type = smeth.getArgumentType(0);            }        } else if (smeth == null) {            type = gmeth.getTypeName();        } else {            String gtype = gmeth.getTypeName();            String stype = smeth.getArgumentType(0);            IClass gclas = icl.getClassInfo(gtype);            if (gclas.isSuperclass(stype) || gclas.isImplements(stype)) {                type = gtype;            } else {                type = stype;            }        }        return type;    }        /**     * Apply customizations to class to fill out members.     *     * @param icl class locator     */    public void apply(IClassLocator icl) {                // initialize class information        m_classInformation = icl.getClassInfo(getName());        if (m_classInformation == null) {            throw new IllegalStateException("Internal error: unable to find class " + m_name);        }                // inherit namespace directly from package level, if not specified        String ns = getSpecifiedNamespace();        if (ns == null) {            ns = getParent().getNamespace();        }        setNamespace(ns);                // set the name(s) to be used if mapped        String cname = convertName(getSimpleName());        if (m_elementName == null) {            m_elementName = cname;        }        m_elementQName = new QName(getNamespace(), m_elementName);        if (m_typeName == null) {            m_typeName = cname;        }        m_typeQName = new QName(getNamespace(), m_typeName);                // initialize maps with existing member customizations        HashMap namemap = new HashMap();        for (Iterator iter = getChildren().iterator(); iter.hasNext();) {            MemberCustom memb = (MemberCustom)iter.next();            String name = memb.getBaseName();            if (name != null) {                namemap.put(name, memb);            }        }                // generate sets of names to be included or ignored        Set inclset = nameSet(m_includes);        Set exclset = nameSet(m_excludes);                // first find members from property access methods        Map getmap = null;        Map setmap = null;        IClassItem[] methods = m_classInformation.getMethods();        GlobalCustom global = getGlobal();        if (global.isOutput()) {                        // find properties using read access methods            getmap = mapPropertyReadMethods(methods, inclset, exclset);            if (global.isInput()) {                                // find properties using write access methods                setmap = mapPropertyWriteMethods(methods, inclset, exclset);                                // discard any read-only properties                for (Iterator iter = getmap.keySet().iterator();                    iter.hasNext();) {                    if (!setmap.containsKey(iter.next())) {                        iter.remove();                    }                }                            }        }        if (global.isInput()) {                        // find properties using write access methods            setmap = mapPropertyWriteMethods(methods, inclset, exclset);                    }                // find members from fields        Map fieldmap = mapFields(m_classInformation.getFields(),            getStripPrefixes(), getStripSuffixes(), inclset, exclset);                // get list of names selected for use by options        ArrayList names;        if (isPropertyAccess()) {            if (global.isOutput()) {                names = new ArrayList(getmap.keySet());            } else {                names = new ArrayList(setmap.keySet());            }        } else {            names = new ArrayList();            for (Iterator iter = fieldmap.keySet().iterator(); iter.hasNext();) {                String name = (String)iter.next();                IClassItem field = (IClassItem)fieldmap.get(name);                int access = field.getAccessFlags();                if (!Modifier.isStatic(access) &&                    !Modifier.isTransient(access)) {                    names.add(name);                }            }        }                // build sets of required or optional members        Set optset = null;        if (m_optionals != null) {            optset = nameSet(m_optionals);        }        Set reqset = null;        if (m_requireds != null) {            reqset = nameSet(m_requireds);        }                // process all members found in class        m_memberMap = new HashMap();        boolean auto = !getName().startsWith("java.") &&            !getName().startsWith("javax.");        for (int i = 0; i < names.size(); i++) {                        // get basic member information            String name = (String)names.get(i);            MemberCustom cust = null;            IClassItem gmeth =                (IClassItem)(getmap == null ? null: getmap.get(name));            IClassItem smeth =                (IClassItem)(setmap == null ? null: setmap.get(name));            IClassItem field =                (IClassItem)(fieldmap == null ? null: fieldmap.get(name));                        // find the optional/required setting            Boolean req = null;            if (optset != null && optset.contains(name)) {                req = Boolean.FALSE;            }            if (reqset != null && reqset.contains(name)) {                req = Boolean.TRUE;            }                        // check for existing customization            if (namemap.containsKey(name)) {                                // fill in data missing from existing member customization                cust = (MemberCustom)namemap.get(name);                if (cust instanceof MemberFieldCustom) {                    MemberFieldCustom mfcust = (MemberFieldCustom)cust;                    mfcust.completeField((IClassItem)fieldmap.get(name), req);                } else {                    String type = findPropertyType(gmeth, smeth, icl);                    MemberPropertyCustom mpcust = (MemberPropertyCustom)cust;                    mpcust.completeProperty(gmeth, smeth, type, req);                }                            } else if (auto) {                if (isPropertyAccess()) {                    if (gmeth != null || smeth != null) {                                                // check for a collection property                        MemberPropertyCustom pcust;                        String type = findPropertyType(gmeth, smeth, icl);                        IClass info = icl.getClassInfo(type);                        if (type.endsWith("[]") ||                            info.isImplements("Ljava/util/Collection;")) {                            pcust = new CollectionPropertyCustom(this, name);                        } else {                            pcust = new MemberPropertyCustom(this, name);                        }                                                // fill in the details of the property                        pcust.completeProperty(gmeth, smeth, type, req);                        cust = pcust;                                            }                } else if (field != null) {                                        // check for a collection field                    MemberFieldCustom fcust;                    String type = field.getTypeName();                    IClass info = icl.getClassInfo(type);                    if (type.endsWith("[]") ||                        info.isImplements("Ljava/util/Collection;")) {                        fcust = new CollectionFieldCustom(this, name);                    } else {                        fcust = new MemberFieldCustom(this, name);                    }                                        // fill in the details of the property                    fcust.completeField(field, req);                    cust = fcust;                                    }            }                        // add customization to map            if (cust != null) {                m_memberMap.put(name, cust);            }        }                // check for any supplied customizations that haven't been matched        for (Iterator iter = namemap.keySet().iterator(); iter.hasNext();) {            String name = (String)iter.next();            if (!m_memberMap.containsKey(name)) {                                // find the optional/required setting                Boolean req = null;                if (optset != null && optset.contains(name)) {                    req = Boolean.FALSE;                }                if (reqset != null && reqset.contains(name)) {                    req = Boolean.TRUE;                }                                // complete the customization and add to map                MemberCustom cust = (MemberCustom)namemap.get(name);                cust.complete(null, req);                m_memberMap.put(name, cust);                            }        }    }        /**     * Get customization information for a member by name. This method may only     * be called after {@link #apply(IClassLocator)}.     *     * @param name     * @return customization, or <code>null</code> if none     */    public MemberCustom getMember(String name) {        return (MemberCustom)m_memberMap.get(name);    }        /**     * Get actual class information. This method may only be called after {@link     * #apply(IClassLocator)}.     *     * @return class information     */    public IClass getClassInformation() {        return m_classInformation;    }        /**     * Get collection of members in class.     *     * @return members     */    public Collection getMembers() {        return m_memberMap.values();    }}

⌨️ 快捷键说明

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