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

com.flextrade.jfixture.utility.PropertyUtil Maven / Gradle / Ivy

Go to download

JFixture is an open source library based on the popular .NET library, AutoFixture

There is a newer version: 2.7.2
Show newest version
package com.flextrade.jfixture.utility;

import java.lang.reflect.Method;

public final class PropertyUtil {
    public static Boolean isMethodASetterProperty(Method method, Class clazz) {
        String name = method.getName();
        if (!name.startsWith("set")) return false;

        Class[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length != 1) return false;

        String propertyName = method.getName().substring(3, method.getName().length());
        Method[] methodsInType = clazz.getMethods();
        for (Method m : methodsInType) {
            if (methodIsStandardSetter(propertyName, m) ||
                    methodIsBooleanSetter(propertyName, m)) {
                return true;
            }
        }

        return false;
    }

    public static String getMemberNameFromMethod(Method method) {
        String name = method.getName();
        if(name.startsWith("get") || name.startsWith("set")) {
            return name.substring(3, name.length());
        }

        if(name.startsWith("is") && Character.isUpperCase(name.charAt(2))) {
            return name.substring(2, name.length());
        }

        return name;
    }

    private static boolean methodIsStandardSetter(String propertyName, Method m) {
        return m.getName().equals("get" + propertyName);
    }

    private static boolean methodIsBooleanSetter(String propertyName, Method m) {
        return m.getName().equals("is" + propertyName) &&
                (m.getGenericReturnType().equals(Boolean.class) ||
                        m.getGenericReturnType().equals(boolean.class));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy