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.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static LruCache cache;

    public static String getValueByClassAnntation(Class clazz, String annClassName, String key) {
        Annotation[] annotations = clazz.getDeclaredAnnotations();
        return getValueInAnntationList(annotations, annClassName, key);
    }

    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;
    }
}