Android Open Source - Paginize Annotation Utils






From Project

Back to project page Paginize.

License

The source code is released under:

Copyright (c) 2014 neevek <i at neevek.net> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal ...

If you think the Android project Paginize 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 net.neevek.android.lib.paginize.util;
/*from w w  w  . ja  v  a2s .com*/
import android.text.TextWatcher;
import android.view.View;
import net.neevek.android.lib.paginize.annotation.DecoratePageConstructor;
import net.neevek.android.lib.paginize.annotation.InjectView;
import net.neevek.android.lib.paginize.annotation.SetListeners;
import net.neevek.android.lib.paginize.exception.NotImplementedInterfaceException;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by neevek on 1/1/14.
 */
public class AnnotationUtils {
    private static Map<Class, String> sSetListenerMethodMap = new HashMap<Class, String>();
    static {
        sSetListenerMethodMap.put(TextWatcher.class, "addTextChangedListener");
    }

    private static void setListenersForView(View view, Class[] listenerTypes, Object listener) throws InvocationTargetException
        , IllegalAccessException, NoSuchMethodException, InstantiationException {

        for (int j = 0; j < listenerTypes.length; ++j) {
            Class listenerClass = listenerTypes[j];

            if (!listenerClass.isAssignableFrom(listener.getClass())) {
                throw new NotImplementedInterfaceException(listener.getClass().getName() + " does not implement " + listenerClass.getName());
            }

            String methodName = sSetListenerMethodMap.get(listenerClass);
            if (methodName == null) {
                methodName = listenerClass.getSimpleName();

                // for interfaces from android.support.v4.**, Class.getSimpleName() may return names that contain the dollar sign
                // I have no idea whether this is a bug, the following workaround fixes the problem
                int index = methodName.lastIndexOf('$');
                if (index != -1) {
                    methodName = methodName.substring(index + 1);
                }
                methodName = "set" + methodName;

                sSetListenerMethodMap.put(listenerClass, methodName);
            }

            try {
                Method method = view.getClass().getMethod(methodName, listenerClass);
                method.invoke(view, listener);
            } catch (NoSuchMethodException e) {
                throw new NoSuchMethodException("No such method: " + listenerClass.getSimpleName() + "." + methodName
                        + ", you have to manually add the set-listener method to sSetListenerMethodMap.");
            }
        }
    }

    public static void handleAnnotatedPageConstructors(Class clazz, Object object, ViewFinder viewFinder) throws InvocationTargetException
            , IllegalAccessException, NoSuchMethodException, InstantiationException {

        Constructor[] constructors = clazz.getConstructors();
        for (int i = 0; i < constructors.length; ++i) {

            Constructor constructor = constructors[i];
            Annotation[] annotations = constructor.getAnnotations();
            for (int j = 0; j < annotations.length; ++j) {

                Annotation anno = annotations[j];
                if (!(anno instanceof DecoratePageConstructor)) {
                    continue;
                }

                DecoratePageConstructor annoContainer = (DecoratePageConstructor)anno;
                if (annoContainer.viewListeners().length == 0) {
                    continue;
                }

                Annotation[] listenerAnno = annoContainer.viewListeners();
                for (int k = 0; k < listenerAnno.length; ++k) {
                    SetListeners setListenersAnno = (SetListeners) listenerAnno[k];
                    View view = viewFinder.findViewById(setListenersAnno.view());
                    if (view == null) {
                        throw new IllegalArgumentException("The view specified in @SetListeners is not found.");
                    }

                    Object targetListener = object;
                    if (setListenersAnno.listener() != null && setListenersAnno.listener() != void.class) {
                        Class targetListenerClass = setListenersAnno.listener();
                        try {
                            boolean isStatic = Modifier.isStatic(targetListenerClass.getModifiers());
                            if (isStatic) {
                                Constructor ctor = targetListenerClass.getDeclaredConstructor();
                                ctor.setAccessible(true);
                                targetListener = ctor.newInstance();

                            } else {
                                Constructor ctor = targetListenerClass.getDeclaredConstructor(clazz);
                                ctor.setAccessible(true);
                                targetListener = ctor.newInstance(object);
                            }
                        } catch (NoSuchMethodException e) {
                            throw new IllegalArgumentException("The 'listener' field in @SetListeners must contain a default constructor without arguments.");
                        }
                    }

                    AnnotationUtils.setListenersForView(view, setListenersAnno.listenerTypes(), targetListener);
                }

            }
        }
    }

    public static void initAnnotatedFields(Class clazz, Object object, ViewFinder viewFinder) throws InvocationTargetException
            , IllegalAccessException, NoSuchMethodException, InstantiationException {
        Field fields[] = clazz.getDeclaredFields();

        for (int i = 0; i < fields.length; ++i) {
            Field field = fields[i];
            Annotation[] annotations = field.getAnnotations();

            if (annotations == null || annotations.length == 0) {
                continue;
            }

            for (int j = 0; j < annotations.length; ++j) {
                Annotation anno = annotations[j];

                if (InjectView.class.isAssignableFrom(anno.getClass())) {
                    InjectView annotation = (InjectView)anno;
                    View view = viewFinder.findViewById(annotation.value());
                    field.setAccessible(true);
                    field.set(object, view);

                    if (annotation.listenerTypes().length > 0) {
                        AnnotationUtils.setListenersForView(view, annotation.listenerTypes(), object);
                    }

                }
            }
        }
    }
}




Java Source Code List

net.neevek.android.demo.paginize.activities.MainActivity.java
net.neevek.android.demo.paginize.pages.general.FrameInnerPage.java
net.neevek.android.demo.paginize.pages.general.FramePage.java
net.neevek.android.demo.paginize.pages.main.MainPage.java
net.neevek.android.demo.paginize.pages.main.TabPage1.java
net.neevek.android.demo.paginize.pages.main.TabPage2.java
net.neevek.android.demo.paginize.pages.other.AlertPage.java
net.neevek.android.demo.paginize.pages.other.ListItemPage.java
net.neevek.android.demo.paginize.pages.other.ListPage.java
net.neevek.android.demo.paginize.pages.other.TestPage.java
net.neevek.android.demo.paginize.pages.viewpager.MyViewPagerPage.java
net.neevek.android.demo.paginize.pages.viewpager.ViewPageSubPage1.java
net.neevek.android.demo.paginize.pages.viewpager.ViewPageSubPage2.java
net.neevek.android.lib.paginize.InnerPageContainer.java
net.neevek.android.lib.paginize.InnerPageManager.java
net.neevek.android.lib.paginize.InnerPage.java
net.neevek.android.lib.paginize.PageActivity.java
net.neevek.android.lib.paginize.PageDataCallback.java
net.neevek.android.lib.paginize.PageManager.java
net.neevek.android.lib.paginize.PagePagerAdapter.java
net.neevek.android.lib.paginize.Page.java
net.neevek.android.lib.paginize.ViewPagerInnerPage.java
net.neevek.android.lib.paginize.ViewPagerPage.java
net.neevek.android.lib.paginize.ViewWrapper.java
net.neevek.android.lib.paginize.anim.PageAnimator.java
net.neevek.android.lib.paginize.anim.SlidePageAnimator.java
net.neevek.android.lib.paginize.anim.ZoomPageAnimator.java
net.neevek.android.lib.paginize.annotation.DecoratePageConstructor.java
net.neevek.android.lib.paginize.annotation.InheritPageLayout.java
net.neevek.android.lib.paginize.annotation.InjectPageAnimator.java
net.neevek.android.lib.paginize.annotation.InjectView.java
net.neevek.android.lib.paginize.annotation.InnerPageContainerLayoutResId.java
net.neevek.android.lib.paginize.annotation.PageLayout.java
net.neevek.android.lib.paginize.annotation.SetListeners.java
net.neevek.android.lib.paginize.annotation.ViewPagerResId.java
net.neevek.android.lib.paginize.exception.InjectFailedException.java
net.neevek.android.lib.paginize.exception.NotImplementedInterfaceException.java
net.neevek.android.lib.paginize.util.AnnotationUtils.java
net.neevek.android.lib.paginize.util.ViewFinder.java