Java Number Parse isParsable(Object parser, String str)

Here you can find the source of isParsable(Object parser, String str)

Description

is Parsable

License

Open Source License

Parameter

Parameter Description
parser The Class type or instantiated Object to find a parse method in.
str The String you want to parse

Exception

Parameter Description

Return

true if a parse method was found and completed without exception

Declaration

public static boolean isParsable(Object parser, String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.*;

public class Main {
    /**/*  w w  w .jav a2s.  co m*/
     * @author Jamie Bell
     * 
     * METHOD: isParsable<p><p>
     * 
     * This method will look through the methods of the specified <code>from</code> parameter
     * looking for a public method name starting with "parse" which has only one String
     * parameter.<p>
     * 
     * The <code>parser</code> parameter can be a class or an instantiated object, eg:
     * <code>Integer.class</code> or <code>new Integer(1)</code>. If you use a
     * <code>Class</code> type then only static methods are considered.<p>
     * 
     * When looping through potential methods, it first looks at the <code>Class</code> associated
     * with the <code>parser</code> parameter, then looks through the methods of the parent's class
     * followed by subsequent ancestors, using the first method that matches the criteria specified
     * above.<p>
     * 
     * This method will hide any normal parse exceptions, but throws any exceptions due to
     * programmatic errors, eg: NullPointerExceptions, etc. If you specify a <code>parser</code>
     * parameter which has no matching parse methods, a NoSuchMethodException will be thrown
     * embedded within a RuntimeException.<p><p>
     * 
     * Example:<br>
     * <code>isParsable(Boolean.class, "true");<br>
     * isParsable(Integer.class, "11");<br>
     * isParsable(Double.class, "11.11");<br>
     * Object dateFormater = new java.text.SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");<br>
     * isParsable(dateFormater, "2001.07.04 AD at 12:08:56 PDT");<br></code>
     * <p>
     * 
     * @param parser    The Class type or instantiated Object to find a parse method in.
     * @param str   The String you want to parse
     * 
     * @return true if a parse method was found and completed without exception
     * @throws java.lang.NoSuchMethodException If no such method is accessible 
     */
    public static boolean isParsable(Object parser, String str) {
        Class theClass = (parser instanceof Class ? (Class) parser : parser.getClass());
        boolean staticOnly = (parser == theClass), foundAtLeastOne = false;
        Method[] methods = theClass.getMethods();

        // Loop over methods
        for (int index = 0; index < methods.length; index++) {
            Method method = methods[index];

            // If method starts with parse, is public and has one String parameter.
            // If the parser parameter was a Class, then also ensure the method is static. 
            if (method.getName().startsWith("parse") && (!staticOnly || Modifier.isStatic(method.getModifiers()))
                    && Modifier.isPublic(method.getModifiers()) && method.getGenericParameterTypes().length == 1
                    && method.getGenericParameterTypes()[0] == String.class) {
                try {
                    foundAtLeastOne = true;
                    method.invoke(parser, str);
                    return true; // Successfully parsed without exception
                } catch (Exception exception) {
                    // If invoke problem, try a different method
                    /*if(!(exception instanceof IllegalArgumentException) &&
                       !(exception instanceof IllegalAccessException) &&
                       !(exception instanceof InvocationTargetException))
                       continue; // Look for other parse methods*/

                    // Parse method refuses to parse, look for another different method
                    continue; // Look for other parse methods
                }
            }
        }

        // No more accessible parse method could be found.
        if (foundAtLeastOne)
            return false;
        else
            throw new RuntimeException(new NoSuchMethodException());
    }
}

Related

  1. isNumeric(final String str)
  2. isNumeric(String str)
  3. isNumeric(String str)
  4. isNumeric(String str)
  5. isNumeric(String value, Locale locale)
  6. toNumber(char letter)
  7. toNumber(CharSequence jsonText)
  8. toNumber(final Object obj)
  9. toNumber(Object object, Number defaultValue)