All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.droitateddb.DatabaseValidatorAnnotationHandler Maven / Gradle / Ivy

Go to download

droitatedDB is a lightweight framework, which frees you from the burden of dealing with Androids SQLite database directly if you don't want to and let's you access it directly if you have to.

There is a newer version: 0.1.10
Show newest version
package org.droitateddb;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Handler that redirects the calls to the annotation methods and provides the correct parameter data.
 *
 * @author Falk Appel
 * @author Alexander Frank
 */
class DatabaseValidatorAnnotationHandler implements InvocationHandler {
    private final Object[] params;

    public DatabaseValidatorAnnotationHandler(Object[] params) {
        this.params = params;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        for (int i = 0; i < params.length; i = i + 2) {
            if (params[i].equals(method.getName())) {
                Object param = params[i + 1];
                if (Number.class.isAssignableFrom(param.getClass())) {
                    Class returnType = method.getReturnType();
                    return handleNumber((Number) param, returnType);
                } else {
                    return param;
                }
            }
        }
        return null;
    }

    private Object handleNumber(Number number, Class returnType) {
        if (returnType.equals(long.class)) {
            return number.longValue();
        } else if (returnType.equals(int.class)) {
            return number.intValue();
        } else if (returnType.equals(byte.class)) {
            return number.byteValue();
        } else if (returnType.equals(double.class)) {
            return number.doubleValue();
        } else if (returnType.equals(float.class)) {
            return number.floatValue();
        } else if (returnType.equals(short.class)) {
            return number.shortValue();
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy