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

📄 methodextagshandler.java

📁 spring struts hibernate
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.example.antbook.xdoclet;

import java.sql.Timestamp;

import java.text.*;
import java.util.*;

import xdoclet.XDocletException;
import xdoclet.tagshandler.*;
import xdoclet.util.TypeConversionUtil;
import xjavadoc.*;

/**
 * This class is an extension to xdoclet.tagshandler.MethodTagsHandler with
 * extra features designed for appgen.
 *
 * @author hzhang(mb4henry@yahoo.com.au)
 * @author Matt Raible
 * @author David Carter
 *
 */
public class MethodExTagsHandler extends MethodTagsHandler {
    //
    private static String datePattern = "yyyy-MM-dd";
    private static String uiDatePattern = "MM/dd/yyyy";
    private static final List numericTypes = new ArrayList();
    private static final String rootClassName = getCurrentClass().getName();
    private static final String rootClassNameLower = Character.toLowerCase(rootClassName.charAt(0)) + rootClassName.substring(1);
    private String curprefix = "";

    static {
        numericTypes.add("java.lang.Integer");
        numericTypes.add("int");
        numericTypes.add("java.lang.Float");
        numericTypes.add("float");
        numericTypes.add("java.lang.Long");
        numericTypes.add("long");
        numericTypes.add("java.lang.Double");
        numericTypes.add("double");
        numericTypes.add("java.lang.Short");
        numericTypes.add("short");
        numericTypes.add("java.lang.Byte");
        numericTypes.add("byte");
    }

    public String className() {
        return rootClassName;
    }


    /**
     * Returns the transformed return type of the current method, without java.lang.
     *
     * @param attributes
     * @return                      Description of the Returned Value
     * @exception XDocletException  Description of Exception
     * @doc.tag                     type="content"
     */
    public String methodType(Properties attributes) throws XDocletException {
        String name = transformedMethodType(attributes);
        if (name.startsWith("java.lang.")) {
            name = name.substring(10);
        }
        return name;
    }

    /**
     * Iterates over all methods of current class and evaluates the body of the
     * tag for each method. <br>
     * The reason to override this method is to add nested form support when
     * iterating over all properties.
     *
     * @see xdoclet.tagshandler.MethodTagsHandler#forAllMethods(java.lang.String,
     *      java.util.Properties)
     */
    public void forAllMethods(String template, Properties attributes)
    throws XDocletException {


        if (getCurrentClass() == null) {
            throw new XDocletException("currentClass == null!!!");
        }

        forAllMembersEx(getCurrentClass(), template, attributes, FOR_METHOD, "");
    }

    /**
     * Method to print out an ActionForm, setter and parameter value
     *
     * @return @throws
     *         XDocletException
     */
    public String formSetterWithValue() throws XDocletException {
        XMethod method = super.getCurrentMethod();
        XMethod setter = method.getMutator();
        
        String value = randomValueForSetter();
        if (setter.getPropertyType().getType().isA("java.util.Date")) {
            value = "\"" + getDate(new Date(), uiDatePattern) + "\"";
        }

        return rootClassNameLower + "Form." + curprefix + setter.getName() + "(" + value + ");";
    }

    /**
     * Method to print out a class, setter and a parameter value
     *
     * @return @throws
     *         XDocletException
     */
    public String setterWithValue() throws XDocletException {
        XMethod method = super.getCurrentMethod();
        XMethod setter = method.getMutator();

        return rootClassNameLower + "." + curprefix + setter.getName() + "(" + randomValueForSetter() + ");";
    }
    
    /**
     * Method to print out a random value for use in setting WebTest parameters
     *
     * @return 
     * @throws XDocletException
     */
    public String randomValueForWebTest() throws XDocletException {
        XMethod method = super.getCurrentMethod();
        XMethod setter = method.getMutator();
        
        String value = randomValueForDbUnit();
        if (setter.getPropertyType().getType().isA("java.util.Date")) {
            value = getDate(new Date(), uiDatePattern);
        }

        return value;
    }

    /**
     * A convenient way to get information of an id field. You can get the
     * property name, property type, setter name, getter name. When using this
     * method, you can pass in a parameter call "getType". And its value can be
     * one of
     * <ul>
     * <li>propertyName</li>
     * <li>propertyType</li>
     * <li>getterName</li>
     * <li>setterName</li>
     * </ul>
     * default will return property name.
     *
     * @param attributes
     * @return required information related to id field in the model class
     * @throws XDocletException
     */
    public String idField(Properties attributes) throws XDocletException {
        XClass currentClass = getCurrentClass();
        Collection members = null;
        members = currentClass.getMethods(true);

        String result = "";

        for (Iterator j = members.iterator(); j.hasNext();) {
            XMember member = (XMember) j.next();
            setCurrentMethod((XMethod) member);

            XMethod getter = (XMethod) member;

            if (super.isGetterMethod(getter)) {
                Properties pro = new Properties();
                pro.setProperty("tagName", "hibernate.id");

                if (super.hasTag(pro, FOR_METHOD)) {
                    break;
                }

                setCurrentClass(member.getContainingClass());
            }
        }

        String type = attributes.getProperty("getType");
        XMethod getter = super.getCurrentMethod();

        if ("propertyName".equals(type)) {
            result = getter.getPropertyName();
        } else if ("getterName".equals(type)) {
            result = getter.getName();
        } else if ("setterName".equals(type)) {
            result = getter.getMutator().getName();
        } else if ("propertyType".equals(type)) {
            result = getter.getPropertyType().getType().getTransformedName();
        } else {
            result = getter.getPropertyName();
        }

        return result;
    }

    /**
     * @see xdoclet.tagshandler.MethodTagsHandler#forAllMembers
     * @param currentClass
     * @param template
     * @param attributes
     * @param forType
     * @param prefix
     * @throws XDocletException
     */
    protected void forAllMembersEx(XClass currentClass, String template,
                                   Properties attributes, int forType,
                                   String prefix) throws XDocletException {
        boolean superclasses = TypeConversionUtil.stringToBoolean(attributes.getProperty("superclasses"), false);
        boolean sort = TypeConversionUtil.stringToBoolean(attributes.getProperty("sort"), true);

        Collection members = null;

        switch (forType) {
            case FOR_METHOD:
                members = currentClass.getMethods(superclasses);
                break;

⌨️ 快捷键说明

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