Android Open Source - android-view-selector View Attributes






From Project

Back to project page android-view-selector.

License

The source code is released under:

Copyright (c) 2013 Nik Haldimann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Soft...

If you think the Android project android-view-selector 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.nikhaldimann.viewselector.attributes;
/*from  w  w  w  .j  a  va  2 s .  c om*/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.view.View;

/**
 * Utils for accessing view attributes using reflection.
 */
public class ViewAttributes {

    private ViewAttributes() { }

    /**
     * @return the full getter method name for a plain attribute, following conventions
     *     used in the Android code. Prepends "get" to the attribute name and formats
     *     it in camel-case. Recognizes boolean attributes of the format {@code isFoo} or
     *     {@code hasFoo} and returns them unchanged.
     */
    public static String getGetterMethodName(String attributeName) {
        if (isBooleanAttribute(attributeName, "is") || isBooleanAttribute(attributeName, "has")) {
            return attributeName;
        }
        return "get" + attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1);
    }

    private static boolean isBooleanAttribute(String attributeName, String prefix) {
        return attributeName.startsWith(prefix)
                && attributeName.length() > prefix.length()
                && Character.isUpperCase(attributeName.charAt(prefix.length()));
    }

    /**
     * Calls the given method by name on the given view, assuming that it's a getter,
     * i.e., it doesn't have arguments.
     * @return the result of the method call
     * @throws AttributeAccessException when the method doesn't exist or can't be
     *     called for various reasons
     */
    public static Object callGetter(View view, String methodName) {
        try {
            Method method = view.getClass().getMethod(methodName);
            return method.invoke(view);
        } catch (SecurityException ex) {
            throw new AttributeAccessException(ex);
        } catch (NoSuchMethodException ex) {
            throw new AttributeAccessException("No such attribute", ex);
        } catch (IllegalAccessException ex) {
            throw new AttributeAccessException(ex);
        } catch (InvocationTargetException ex) {
            throw new AttributeAccessException(ex);
        }
    }

    /**
     * Calls the given method by name on the given view, assuming that it's a getter,
     * i.e., it doesn't have arguments.
     *
     * This method normalizes all instances of CharSequences to be instances of String.
     * This is useful because Android is using some CharSequence representations
     * internally that don't compare well with strings (in particular as returned
     * from TextView.getText()).
     *
     * @return the result of the method call
     * @throws AttributeAccessException when the method doesn't exist or can't be
     *     called for various reasons
     */
    public static Object callGetterNormalizingStrings(View view, String methodName) {
        Object value = callGetter(view, methodName);
        if (value instanceof CharSequence) {
            value = value.toString();
        }
        return value;
    }

}




Java Source Code List

com.nikhaldimann.viewselector.Attributes.java
com.nikhaldimann.viewselector.InvalidSelectorException.java
com.nikhaldimann.viewselector.ViewSelectionAssert.java
com.nikhaldimann.viewselector.ViewSelectionAttributeAssert.java
com.nikhaldimann.viewselector.ViewSelectorAssertions.java
com.nikhaldimann.viewselector.ViewSelector.java
com.nikhaldimann.viewselector.android.AllTests.java
com.nikhaldimann.viewselector.android.ViewSelectionAssertTest.java
com.nikhaldimann.viewselector.android.ViewSelectorAssertionsTest.java
com.nikhaldimann.viewselector.android.ViewSelectorTest.java
com.nikhaldimann.viewselector.android.activities.HelloWorldExampleActivityTest.java
com.nikhaldimann.viewselector.android.activities.HelloWorldExampleFragmentActivityTest.java
com.nikhaldimann.viewselector.android.activities.ListViewExampleActivityTest.java
com.nikhaldimann.viewselector.android.activities.SimpleFragmentTest.java
com.nikhaldimann.viewselector.android.attributes.ViewAttributesTest.java
com.nikhaldimann.viewselector.android.checker.AttributeSpecifierCheckerTest.java
com.nikhaldimann.viewselector.android.checker.ClassCheckerTest.java
com.nikhaldimann.viewselector.android.testutil.AndroidTestViewFactory.java
com.nikhaldimann.viewselector.attributes.AttributeAccessException.java
com.nikhaldimann.viewselector.attributes.ViewAttributes.java
com.nikhaldimann.viewselector.attributes.ViewSelectionAttribute.java
com.nikhaldimann.viewselector.checker.AttributeSpecifierChecker.java
com.nikhaldimann.viewselector.checker.ClassChecker.java
com.nikhaldimann.viewselector.checker.MatchPredicate.java
com.nikhaldimann.viewselector.checker.MatchPredicates.java
com.nikhaldimann.viewselector.checker.ViewTraversalChecker.java
com.nikhaldimann.viewselector.robolectric.RobolectricTestHelloWorldActivity.java
com.nikhaldimann.viewselector.robolectric.RobolectricTestHelloWorldFragmentActivity.java
com.nikhaldimann.viewselector.selection.ViewSelection.java