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 Class<?> sCurrentClass;

    public final static int getIntValue(Object owner, String name, int defvalue) {
        int result = defvalue;
        setClass(owner.getClass().getName());
        Field field = getField(name);

        if (field != null) {
            try {
                result = field.getInt(owner);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public final static boolean setClass(String name) {
        Class<?> tmpClass = null;
        try {
            tmpClass = Class.forName(name);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (ExceptionInInitializerError e) {
            e.printStackTrace();
        } catch (LinkageError e) {
            e.printStackTrace();
        }

        sCurrentClass = tmpClass;
        return tmpClass != null;
    }

    private final static Field getField(String name) {
        Field field = null;
        try {
            field = sCurrentClass.getDeclaredField(name);
            field.setAccessible(true);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            //         e.printStackTrace();
        }

        return field;
    }
}