Java Reflection Primitive matchPrimitive(Class paramType, Object arg)

Here you can find the source of matchPrimitive(Class paramType, Object arg)

Description

Attempts to match the argument value against the passed primitive type.

License

Apache License

Declaration

private static int matchPrimitive(Class<?> paramType, Object arg) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static Map<Class<?>, Class<?>> primitiveLookup = new HashMap<Class<?>, Class<?>>();
    private static Map<Class<?>, Integer> integralDistance = new HashMap<Class<?>, Integer>();
    private static Map<Class<?>, Integer> floatDistance = new HashMap<Class<?>, Integer>();

    /**//from www  .j  a  v a  2s .c om
     *  Attempts to match the argument value against the passed primitive type.
     *  Returned distance is:
     *  <ul>
     *  <li> -1 if the argument is not a numeric or incompatible with the
     *       parameter type
     *  <li> 1 if it's a numeric that exactly matches the  parameter type
     *  <li> 2-4 if it's ac numeric and the parameter type is a larger
     *       numeric of the same family (integer or floating point). See
     *       the code for details.
     *  <li> 5 if it's an integral numeric and the parameter type is a
     *       <code>double</code>.
     *  </ul>
     */
    private static int matchPrimitive(Class<?> paramType, Object arg) {
        Class<?> argType = getPrimitiveType(arg);
        if (argType == null)
            return -1;

        if (paramType == argType)
            return 1;

        if (integralDistance.containsKey(argType) && integralDistance.containsKey(paramType)) {
            int argDepth = integralDistance.get(argType).intValue();
            int paramDepth = integralDistance.get(paramType).intValue();
            if (argDepth < paramDepth)
                return 1 + (paramDepth - argDepth);
        } else if (floatDistance.containsKey(argType) && floatDistance.containsKey(paramType)) {
            int argDepth = floatDistance.get(argType).intValue();
            int paramDepth = floatDistance.get(paramType).intValue();
            if (argDepth < paramDepth)
                return 1 + (paramDepth - argDepth);
        } else if (integralDistance.containsKey(argType) && (paramType == Double.TYPE)) {
            return 5;
        }

        return -1;
    }

    /**
     *  Extracts the <code>TYPE</code> field value from a primitive wrapper type.
     *  Returns <code>null</code> if the passed value is not a wrapper type.
     *  <p>
     *  This method is useful for reflective parameter matching.
     *
     *  @since 1.0.9
     */
    public static Class<?> getPrimitiveType(Object val) {
        if (val == null)
            return null;

        return primitiveLookup.get(val.getClass());
    }
}

Related

  1. isWrapper(Class clazz)
  2. isWrapper(Class dataType)
  3. isWrapperAndPrimitivePair(Class c1, Class c2)
  4. isWrapperClass(Class clazz)
  5. isWrapperType(Class type)
  6. primitiveArraysForName(String className)
  7. primitiveToWrapper(final Class cls)
  8. primitiveToWrapper(final Class cls)
  9. resolveClassPrimitiveAware(String className)