Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.support.v4.util.LruCache;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static LruCache cache;

    public static Field findFieldWithAnntationAndValue(Field[] fields, String annClassName, String key,
            String value) {
        if (fields == null) {
            return null;
        }
        for (Field field : fields) {
            Annotation[] annotations = field.getDeclaredAnnotations();
            String ret = getValueInAnntationList(annotations, annClassName, key);
            if (ret.equals(value)) {
                return field;
            }
        }
        return null;
    }

    public static String getValueInAnntationList(Annotation[] annotations, String annClassName, String key) {
        if (annotations != null) {
            for (Annotation annotation : annotations) {
                String annName = annotation.toString();
                if (annName.indexOf(annClassName) != 0) {
                    return getVlaueFromAnnotation(annotation, key);
                }
            }
        }
        return "";
    }

    public static String getVlaueFromAnnotation(Annotation annotation, String annName) {
        if (cache == null) {
            cache = new LruCache(500);
        }
        String annotationString = annotation.toString();
        String cacheKey = "getVlaueByColumnAnnotation:" + annotationString.hashCode() + "," + annName;
        String ret = (String) cache.get(cacheKey);
        if (ret == null || "".equals(ret)) {
            String pattern = annName + "=(.*?),";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(annotation.toString());
            if (m.find()) {
                ret = m.group();
                ret = ret.substring(annName.length() + 1, ret.length() - 1);
            }
            cache.put(cacheKey, ret);
        }
        return ret;
    }
}