Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Field;

public class Main {

    public static void copyProperties(Object desc, Object obj) {
        Class<?> descClass = desc.getClass();
        Class<?> objClass = obj.getClass();
        Field[] fields = objClass.getDeclaredFields();
        try {
            for (int i = 0; i < fields.length; i++) {
                String name = fields[i].getName();
                String getMethodName = "get" + toFirstLetterUpperCase(name);
                String setMethodName = "set" + toFirstLetterUpperCase(name);
                try {
                    Object value = objClass.getMethod(getMethodName).invoke(obj);
                    descClass.getMethod(setMethodName, value.getClass()).invoke(desc, value);
                } catch (Exception e) {
                }
            }
        } catch (Exception e) {

        }
    }

    public static String toFirstLetterUpperCase(String str) {
        if (str == null || str.length() < 2) {
            return str;
        }
        String firstLetter = str.substring(0, 1).toUpperCase();
        return firstLetter + str.substring(1, str.length());
    }
}