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 {
    /**
     * Gets the specified field of the given class, or null if it does not exist.
     */
    public static Field getField(final String className, final String fieldName) {
        return getField(loadClass(className), fieldName);
    }

    /**
     * Gets the specified field of the given class, or null if it does not exist.
     */
    public static Field getField(final Class<?> c, final String fieldName) {
        if (c == null)
            return null;
        try {
            return c.getDeclaredField(fieldName);
        } catch (final NoSuchFieldException e) {
            return null;
        }
    }

    /** Loads the class with the given name, or null if it cannot be loaded. */
    public static Class<?> loadClass(final String className) {
        return loadClass(className, null);
    }

    /** Loads the class with the given name, or null if it cannot be loaded. */
    public static Class<?> loadClass(final String className, final ClassLoader classLoader) {
        try {
            if (classLoader == null)
                return Class.forName(className);
            return classLoader.loadClass(className);
        } catch (final ClassNotFoundException e) {
            return null;
        }
    }
}