Android Open Source - Android-Data-Binding Base P O J O






From Project

Back to project page Android-Data-Binding.

License

The source code is released under:

MIT License

If you think the Android project Android-Data-Binding 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 com.furkanbayraktar.databinding.model;
/*from  w w w.ja v a2s.co  m*/
import com.furkanbayraktar.databinding.exception.POJOParseException;
import com.furkanbayraktar.databinding.logging.Logger;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * Created by Furkan Bayraktar.
 * Created at 8/15/14.
 *
 */
public class BasePOJO implements Serializable {

    public Object get(String fieldName){
        try {
            Logger.info("Get field: " + fieldName);

            Method method;
            Object value = null;

            String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

            try {
                method = ((Object) this).getClass().getDeclaredMethod(methodName);
                method.setAccessible(true);
                value = method.invoke(this);
            } catch (Exception e) {
                Logger.info("Method not found with name: " + methodName);
            }

            if (value == null) {
                methodName = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

                try {
                    method = ((Object) this).getClass().getDeclaredMethod(methodName);
                    method.setAccessible(true);
                    value = method.invoke(this);
                } catch (Exception e) {
                    Logger.info("Method not found with name: " + methodName);
                }

                if (value == null) {
                    Field field;

                    try {
                        field = ((Object) this).getClass().getDeclaredField(fieldName);
                        field.setAccessible(true);
                        value = field.get(this);
                    } catch (NoSuchFieldException e) {
                        POJOParseException ex = new POJOParseException(e);
                        Logger.warn("Field not found.", ex);
                    } catch (IllegalAccessException e) {
                        POJOParseException ex = new POJOParseException("Field access not authorized.", e);
                        Logger.warn("Field access error.", ex);
                    }
                }
            }
            Logger.info("Field value: " + value);

            return value;
        } catch (Exception ex){
            Logger.error("Unexpected error.", ex);
        }
        return null;
    }

    public void set(String fieldName, Object value){
        try {
            Logger.info("Set field: " + fieldName + " to value: " + value);

            Method method;

            String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

            try {
                method = ((Object) this).getClass().getDeclaredMethod(methodName, Object.class);
                method.setAccessible(true);
                method.invoke(this, value);

                Logger.info("Field value set to: " + value);
            } catch (Exception e) {
                Logger.info("Method not found with name: " + methodName);

                Field field;

                try {
                    field = ((Object) this).getClass().getDeclaredField(fieldName);
                    field.setAccessible(true);
                    field.set(this, value);

                    Logger.info("Field value set to: " + value);
                } catch (NoSuchFieldException ee) {
                    POJOParseException ex = new POJOParseException(ee);
                    Logger.warn("Field not found.", ex);
                } catch (IllegalAccessException ee) {
                    POJOParseException ex = new POJOParseException("Field access not authorized.", ee);
                    Logger.warn("Field access error.", ex);
                }
            }

        } catch (Exception ex){
            Logger.error("Unexpected error.", ex);
        }
    }
}




Java Source Code List

com.furkanbayraktar.databinding.ViewBinder.java
com.furkanbayraktar.databinding.annotations.BindedFunction.java
com.furkanbayraktar.databinding.common.adapter.BoundListAdapter.java
com.furkanbayraktar.databinding.common.external.Expression.java
com.furkanbayraktar.databinding.common.parser.ExpressionEvaluator.java
com.furkanbayraktar.databinding.common.parser.Parser.java
com.furkanbayraktar.databinding.common.resolver.AssignmentResolver.java
com.furkanbayraktar.databinding.common.resolver.FunctionResolver.java
com.furkanbayraktar.databinding.common.resolver.PathResolver.java
com.furkanbayraktar.databinding.common.resolver.ViewResolver.java
com.furkanbayraktar.databinding.common.resolver.VisibilityResolver.java
com.furkanbayraktar.databinding.exception.FunctionException.java
com.furkanbayraktar.databinding.exception.POJOParseException.java
com.furkanbayraktar.databinding.exception.ResolverException.java
com.furkanbayraktar.databinding.exception.UnsupportedObjectException.java
com.furkanbayraktar.databinding.exception.UnsupportedSyntaxError.java
com.furkanbayraktar.databinding.exception.UnsupportedViewException.java
com.furkanbayraktar.databinding.logging.Logger.java
com.furkanbayraktar.databinding.model.BasePOJO.java
com.furkanbayraktar.databindingprivate.app.ListActivity.java
com.furkanbayraktar.databindingprivate.app.MainActivity.java
com.furkanbayraktar.databindingprivate.app.model.news.Content.java
com.furkanbayraktar.databindingprivate.app.model.news.Entry.java
com.furkanbayraktar.databindingprivate.app.model.news.Feed.java
com.furkanbayraktar.databindingprivate.app.model.news.MediaGroup.java
com.furkanbayraktar.databindingprivate.app.model.news.ResponseData.java
com.furkanbayraktar.databindingprivate.app.model.news.ResponseObject.java
com.furkanbayraktar.databindingprivate.app.model.sample.SampleObject.java
com.furkanbayraktar.databindingprivate.app.network.GsonRequest.java