Android Open Source - jadab Property Extractor






From Project

Back to project page jadab.

License

The source code is released under:

Apache License

If you think the Android project jadab 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.github.binding;
// ww  w .j a  v  a2  s.c o  m
import java.lang.reflect.Field;

public class PropertyExtractor {

    public class CouldNotGetFieldException extends RuntimeException {
        private static final long serialVersionUID = 1L;

        public CouldNotGetFieldException(String message) {
            super(message);
        }
    }
    
    /**
     * Get a property member of an object.
     * 
     * This method finds the property {@code object.name}, if such a property
     * exists. Otherwise, a RuntimeException is thrown, hence the "unsafe".
     */
    public Property<?> unsafeGetNamedProperty(Object object, String name) {
        Object maybeProperty = getFieldValue(object, name);
        if (maybeProperty instanceof Property<?>) {
            return (Property<?>)maybeProperty;
        } else {
            throw new CouldNotGetFieldException("" + object + " has field '" + name +
                    "', but it is not a Property");
        }
    }
    
    private Object getFieldValue(Object object, String name)
            throws CouldNotGetFieldException {
        try {
            Class<?> clazz = object.getClass();
            Field field = clazz.getField(name);
            return field.get(object);
        } catch (SecurityException e) {
            throw new CouldNotGetFieldException("No access: " + name);
        } catch (NoSuchFieldException e) {
            throw new CouldNotGetFieldException("No field named " + name);
        } catch (IllegalArgumentException e) {
            throw new CouldNotGetFieldException("Illegal argument: " + e.getMessage());
        } catch (IllegalAccessException e) {
            throw new CouldNotGetFieldException("Illegal access: " + e.getMessage());
        }
    }
}




Java Source Code List

com.github.binding.AbstractBinding.java
com.github.binding.BindingBuilder.java
com.github.binding.PropertyExtractor.java
com.github.binding.Property.java
com.github.binding.android.AndroidPrefix.java
com.github.binding.android.BindingActivity.java
com.github.binding.android.BindingFactory.java
com.github.binding.android.BindingName.java
com.github.binding.android.Constants.java
com.github.binding.android.propertyprovider.AbstractPropertyProvider.java
com.github.binding.android.propertyprovider.PropertyProviderFactory.java
com.github.binding.android.propertyprovider.PropertyProviderStack.java
com.github.binding.android.propertyprovider.PropertyProvider.java
com.github.binding.android.propertyprovider.TextViewPropertyProvider.java
com.github.binding.android.propertyprovider.ViewPropertyProvider.java
com.github.binding.android.propertyprovider.listener.OnClickListenerProperty.java
com.github.binding.android.propertyprovider.listener.TextWatcherProperty.java
com.github.event.PropertyChangeListener.java
com.github.jadab.example.CalculatorActivity.java
com.github.jadab.example.logic.DbaCalculatorLogic.java
com.github.util.ObjectUtil.java