/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.framework.extra;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.jfox.framework.util.Methods;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ManagementInfo {

    /**
     * manangementOperationInfo hash => ManagementOperationInfo object
     */
    private Map mangementOperationInfos = new HashMap();

    public ManagementOperationInfo getManagementOperationInfo(String hash) {
        return (ManagementOperationInfo) mangementOperationInfos.get(hash);
    }

    public ManagementOperationInfo[] getAllManagementOperationInfos() {
        return (ManagementOperationInfo[]) mangementOperationInfos.values().toArray(new ManagementOperationInfo[mangementOperationInfos.size()]);
    }

    public void addOperationInfo(ManagementOperationInfo operInfo) {
        mangementOperationInfos.put(operInfo.getMethodHashCode(), operInfo);
    }

    public static ManagementInfo exposePublic(Class clz) {
        ManagementInfo managementInfo = new ManagementInfo();
        Method[] methods = clz.getMethods();
        for(int i = 0; i < methods.length; i++) {
            if(!Methods.isAbstract(methods[i])
                    && !Methods.isPrivate(methods[i])
                    && !Methods.isStatic(methods[i])
                    && !Methods.isDeclaredBy(methods[i], Object.class)
                    && !Methods.isDeclaredBy(methods[i], Managable.class)
            )
                managementInfo.addOperationInfo(new ManagementOperationInfo(methods[i]));
        }
        return managementInfo;
    }

    public static ManagementInfo exposeGet(Class clz) {
        ManagementInfo managementInfo = new ManagementInfo();
        Method[] methods = clz.getMethods();
        for(int i = 0; i < methods.length; i++) {
            if(!Methods.isAbstract(methods[i])
                    && !Methods.isPrivate(methods[i])
                    && !Methods.isStatic(methods[i])
                    && !Methods.isDeclaredBy(methods[i], Object.class)
                    && !Methods.isDeclaredBy(methods[i], Managable.class)
                    && Methods.isGetMethod(methods[i])) {
                managementInfo.addOperationInfo(new ManagementOperationInfo(methods[i]));
            }
        }
        return managementInfo;
    }

    public static ManagementInfo exposeSet(Class clz) {
        ManagementInfo managementInfo = new ManagementInfo();
        Method[] methods = clz.getMethods();
        for(int i = 0; i < methods.length; i++) {
            if(!Methods.isAbstract(methods[i])
                    && !Methods.isPrivate(methods[i])
                    && !Methods.isStatic(methods[i])
                    && !Methods.isDeclaredBy(methods[i], Object.class)
                    && !Methods.isDeclaredBy(methods[i], Managable.class)
                    && Methods.isSetMethod(methods[i])) {
                managementInfo.addOperationInfo(new ManagementOperationInfo(methods[i]));
            }
        }
        return managementInfo;
    }

    public static ManagementInfo exposeDeclaring(Class clz) {
        ManagementInfo managementInfo = new ManagementInfo();
        Method[] methods = clz.getDeclaredMethods();
        for(int i = 0; i < methods.length; i++) {
            if(!Methods.isAbstract(methods[i])
                    && !Methods.isPrivate(methods[i])
                    && !Methods.isStatic(methods[i])
                    && !Methods.isDeclaredBy(methods[i], Object.class)
                    && !Methods.isDeclaredBy(methods[i], Managable.class)
            )
                managementInfo.addOperationInfo(new ManagementOperationInfo(methods[i]));
        }
        return managementInfo;

    }

    public static void main(String[] args) {

    }
}

