All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.openpojo.reflection.impl.PojoMethodFactory Maven / Gradle / Ivy

Go to download

This project was born out of a need to validate all POJOs (Plain Old Java Object) are behaving correctly. This project has two main aspects to it: * Make Testing as easy as possible. * Simplifying identity management (hashCode / equals) using annotation.

There is a newer version: 0.7.3
Show newest version
/**
 * Copyright (C) 2010 Osman Shoukry
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this program.  If not, see .
 */
package com.openpojo.reflection.impl;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.openpojo.reflection.PojoMethod;
import com.openpojo.reflection.utils.AttributeHelper;

/**
 * This factory handles various method related operations on given Class or Field.
 * @author oshoukry
 */
public class PojoMethodFactory {

    /**
     * Returns all methods on a given Class.
     * Note: Constructors are treated as methods and will be returned in the list as well.
     *
     * @param clazz
     *          The class to introspect for methods / constructors.
     * @return
     *          A list of all methods and constructors in a class.
     */
    public static List getPojoMethods(final Class clazz) {
        final List pojoMethods = new LinkedList();

        for (final Constructor constructor : clazz.getDeclaredConstructors()) {
            pojoMethods.add(new PojoMethodImpl(constructor));
        }

        for (final Method method : clazz.getDeclaredMethods()) {
            pojoMethods.add(new PojoMethodImpl(method));
        }
        return Collections.unmodifiableList(pojoMethods);
    }

    /**
     * Returns a specific method given method name and parameters.
     * @param clazz
     *          The Class that holds the method.
     * @param name
     *          The name of the method to return.
     * @param parameterTypes
     *          The Parameters to match.
     * @return
     *          A PojoMethod if found, or null otherwise.
     */
    public static PojoMethod getMethod(final Class clazz, final String name, final Class... parameterTypes) {
        for (PojoMethod pojoMethod : getPojoMethods(clazz)) {
            if (pojoMethod.getName().equals(name) && Arrays.equals(pojoMethod.getParameterTypes(), parameterTypes)) {
                return pojoMethod;
            }
        }
        return null;
    }

    /**
     * Returns the Getter Method for a field.
     *
     * @param field
     *            The field to lookup the getter on.
     * @return
     *         The getter method or null if none exist.
     */
    public static PojoMethod getFieldGetter(final Field field) {
        PojoMethod pojoMethod = null;
        for (String candidateName : generateGetMethodNames(field)) {
            Class clazz = field.getDeclaringClass();
            pojoMethod = PojoMethodFactory.getMethod(clazz, candidateName);
            if (pojoMethod != null) {
                break;
            }
        }
        return pojoMethod;
    }

    /**
     * Returns a list for candidate getter names.
     *
     * @param field
     *            Field to generate the candidate getter names for.
     * @return
     *         List of candidate method names.
     */
    private static List generateGetMethodNames(final Field field) {
        List prefix = new LinkedList();
        prefix.add("get" + AttributeHelper.getAttributeName(field));
        if (field.getType() == boolean.class || field.getType() == Boolean.class) {
            prefix.add("is" + AttributeHelper.getAttributeName(field));
        }
        return prefix;
    }

    /**
     * Returns the Setter Method for a field.
     *
     * @param field
     *            The field to lookup the setter on.
     * @return
     *         The setter method or null if none exist.
     */
    public static PojoMethod getFieldSetter(final Field field) {
        PojoMethod pojoMethod = null;

        for (String candidateName : generateSetMethodNames(field)) {
            Class clazz = field.getDeclaringClass();
            pojoMethod = PojoMethodFactory.getMethod(clazz, candidateName, field.getType());
            if (pojoMethod != null) {
                break;
            }
        }
        return pojoMethod;
    }


    /**
     * Returns a list for candidate setter names.
     *
     * @param field
     *            The field to generate for.
     * @return
     */
    private static List generateSetMethodNames(final Field field) {
        List prefix = new LinkedList();
        prefix.add("set" + AttributeHelper.getAttributeName(field));
        return prefix;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy