Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;

public class Main {
    @SuppressWarnings({ "rawtypes" })
    public static Method hasProperty(Object o, String propertyName) {

        Class c = o.getClass();
        Method method = methodOrNull(c, propertyName);
        if (method != null) {
            return method;
        }
        method = methodOrNull(c, "get" + captitalize(propertyName));
        if (method != null) {
            return method;
        }
        method = methodOrNull(c, "is" + captitalize(propertyName));
        return method;

        /*
         * for (Method m : methods) { String methodName = m.getName(); if
         * (methodName.equals(propertyName) ||
         * methodName.equals("is"+captitalize(propertyName)) ||
         * methodName.equals("get"+captitalize(propertyName))) { return m; } }
         */

    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static Method methodOrNull(Class c, String methodName) {
        try {
            return c.getMethod(methodName);
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

    private static String captitalize(String propertyName) {
        return propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    }
}