Android Open Source - AGOGCyberStat Reflect Utils






From Project

Back to project page AGOGCyberStat.

License

The source code is released under:

MIT License

If you think the Android project AGOGCyberStat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.allenz.androidapplog;
//from   w w  w .jav a  2  s.c  om
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ReflectUtils {

    private ReflectUtils(){
        throw new UnsupportedOperationException();
    }

    static CallerResolver callerResolver = new CallerResolver();

    public static int intReflectStaticFieldValue(final String className,
                                                 final String fieldName, final int defaultValue) {
        try {
            final Class<?> clazz = Class.forName(className);
            final Field field = clazz.getDeclaredField(fieldName);
            if (Modifier.isStatic(field.getModifiers())
                &&
                field.getType().getName().equals("int")) {
                field.setAccessible(true);
                return field.getInt(null);
            }
        } catch (final Exception e) {
        }
        return defaultValue;
    }

    public static boolean booleanReflectStaticFieldValue(
                                                         final String className, final String fieldName,
                                                         final boolean defaultValue) {
        try {
            final Class<?> clazz = Class.forName(className);
            final Field field = clazz.getDeclaredField(fieldName);
            if (Modifier.isStatic(field.getModifiers())
                &&
                field.getType().getName().equals("boolean")) {
                field.setAccessible(true);
                return field.getBoolean(null);
            }
        } catch (final Exception e) {
        }
        return defaultValue;
    }

    static String getCallerClassName(final String classBeingCalled) {
        final Class<?> caller = callerResolver.getCaller(classBeingCalled);
        if (caller == null) {
            final StackTraceElement callerStackTrace = getCallerStackTrace(
                classBeingCalled);
            return callerStackTrace == null? null: callerStackTrace
                .getClassName();
        } else {
            return caller.getName();
        }
    }

    static StackTraceElement getCallerStackTrace(
                                                 final String classBeingCalled) {
        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        if (stackTrace == null ||
            stackTrace.length <= 0) {
            return null;
        }

        for (int i = 1; i < stackTrace.length; i++) {
            final StackTraceElement stackTraceElement = stackTrace[i];
            if (stackTraceElement.getClassName().equals(classBeingCalled)) {
                return stackTrace[i + 1];
            }
        }
        return null;
    }

    static final class CallerResolver extends SecurityManager {

        @SuppressWarnings("rawtypes")
        public Class<?> getCaller(final String classBeingCalled) {
            final Class[] classContext = getClassContext();
            if (classContext == null ||
                classContext.length <= 0) {
                return null;
            }
            for (int i = 1; i < classContext.length; i++) {
                final Class clazz = classContext[i];
                if (clazz.getName().equals(classBeingCalled)) {
                    return classContext[i + 1];
                }
            }
            return null;
        }

        @SuppressWarnings("rawtypes")
        public Class[] getClassStacks() {
            return getClassContext();
        }
    }

}




Java Source Code List

com.agog.cyberstat.BrR.java
com.agog.cyberstat.JSONSettings.java
com.agog.cyberstat.MainActivity.java
com.agog.cyberstat.MotisonNetTask.java
com.agog.cyberstat.MotisonXML.java
com.agog.cyberstat.MyPrefs.java
com.agog.cyberstat.NetR.java
com.agog.cyberstat.Trigger.java
me.allenz.androidapplog.AbstractAppender.java
me.allenz.androidapplog.AbstractLogger.java
me.allenz.androidapplog.AppenderSupportLogger.java
me.allenz.androidapplog.Appender.java
me.allenz.androidapplog.AsyncAppender.java
me.allenz.androidapplog.Configure.java
me.allenz.androidapplog.InternalLogger.java
me.allenz.androidapplog.LogCatAppender.java
me.allenz.androidapplog.LogEvent.java
me.allenz.androidapplog.LogLevel.java
me.allenz.androidapplog.LogTextView.java
me.allenz.androidapplog.LoggerConfig.java
me.allenz.androidapplog.LoggerFactory.java
me.allenz.androidapplog.Logger.java
me.allenz.androidapplog.PropertiesParser.java
me.allenz.androidapplog.ReflectUtils.java
me.allenz.androidapplog.Repository.java
me.allenz.androidapplog.RollingFileAppender.java
me.allenz.androidapplog.TextViewAppender.java