Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Field> getAllFields(Class<?> type) {
        List<Field> fields = new ArrayList<Field>();

        doGetAllField(type, fields, false);

        return fields;
    }

    public static List<Field> getAllFields(Class<?> type, boolean ignoreSuperClasses) {
        List<Field> fields = new ArrayList<Field>();

        doGetAllField(type, fields, ignoreSuperClasses);

        return fields;
    }

    public static void doGetAllField(Class<?> type, List<Field> fields, boolean ignoreSuperClasses) {
        for (Field newField : type.getDeclaredFields()) {
            boolean found = false;
            for (Field oldField : fields) {
                if (oldField.getName().equals(newField.getName())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                fields.add(newField);
            }
        }

        if (type.getSuperclass() != null && !ignoreSuperClasses) {
            doGetAllField(type.getSuperclass(), fields, ignoreSuperClasses);
        }
    }
}