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

com.github.jinahya.sql.database.metadata.bind.Reflections Maven / Gradle / Ivy

/*
 * Copyright 2015 Jin Kwon <jinahya_at_gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.github.jinahya.sql.database.metadata.bind;


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import static java.util.logging.Logger.getLogger;


/**
 *
 * @author Jin Kwon <jinahya_at_gmail.com>
 */
class Reflections {


    private static final Logger logger = getLogger(Metadata.class.getName());


    private static final Map, Class> WRAPPERS;


    static {
        final Map, Class> map = new HashMap, Class>(9);
        map.put(boolean.class, Boolean.class);
        map.put(byte.class, Byte.class);
        map.put(char.class, Character.class);
        map.put(double.class, Double.class);
        map.put(float.class, Float.class);
        map.put(int.class, Integer.class);
        map.put(long.class, Long.class);
        map.put(short.class, Short.class);
        map.put(void.class, Void.class);
        WRAPPERS = Collections.unmodifiableMap(map);
    }


    static Class wrapper(final Class primitive) {

        if (primitive == null) {
            throw new NullPointerException("null primitive");
        }

        if (!primitive.isPrimitive()) {
            throw new IllegalArgumentException("not primitive: " + primitive);
        }

        return WRAPPERS.get(primitive);
    }


    private static String capitalize(final String name) {

        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }


    private static String getterName(final String fieldName) {

        return "get" + capitalize(fieldName);
    }


    private static String getterName(final Field field) {

        return getterName(field.getName());
    }


    private static String setterName(final String fieldName) {

        return "set" + capitalize(fieldName);
    }


    private static String setterName(final Field field) {

        return setterName(field.getName());
    }


    Object fieldValue(final Field field, final Object obj)
        throws IllegalAccessException {

        if (!field.isAccessible()) {
            field.setAccessible(true);
        }

        return field.get(obj);
    }


    Object property(final Class klass, final String name, final Object obj)
        throws ReflectiveOperationException {

        try {
            final Method getter = klass.getDeclaredMethod(getterName(name));
            if (!getter.isAccessible()) {
                getter.setAccessible(true);
            }
            return getter.invoke(obj);
        } catch (final NoSuchMethodException nsme) {
        }

        return fieldValue(klass.getDeclaredField(name), obj);
    }


    private Reflections() {

        super();
    }


}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy