Search in sources :

Example 1 with Setter

use of org.sql2o.reflection.Setter in project sql2o by aaberg.

the class AbstractFieldSetterFactoryTest method testAllTypes.

public void testAllTypes() throws IllegalAccessException {
    POJO1 pojo1 = new POJO1();
    pojo1._boolean = true;
    pojo1._byte = 17;
    pojo1._short = 87;
    pojo1._int = Integer.MIN_VALUE;
    pojo1._long = 1337;
    pojo1._char = 'a';
    pojo1._double = Math.PI;
    pojo1._float = (float) Math.log(93);
    pojo1._obj = pojo1;
    POJO1 pojo2 = new POJO1();
    assertFalse(pojo1.equals(pojo2));
    Field[] fields = pojo1.getClass().getDeclaredFields();
    for (Field field : fields) {
        Setter setter = fsf.newSetter(field);
        assertSame(field.getType(), setter.getType());
        Object val1 = field.get(pojo1);
        Object val2 = field.get(pojo2);
        assertFalse(val1.equals(val2));
        setter.setProperty(pojo2, val1);
        Object val3 = field.get(pojo2);
        assertEquals(val1, val3);
    }
    assertEquals(pojo1, pojo2);
    // primitive fields will not be affected
    for (Field field : fields) {
        Setter setter = fsf.newSetter(field);
        Object val1 = field.get(pojo1);
        assertNotNull(val1);
        setter.setProperty(pojo1, null);
        Object val2 = field.get(pojo1);
        if (!setter.getType().isPrimitive()) {
            assertNull(val2);
            continue;
        }
        assertNotNull(val2);
        // not affected
        assertEquals(val1, val2);
    }
    pojo2._obj = null;
    assertEquals(pojo2, pojo1);
}
Also used : Field(java.lang.reflect.Field) Setter(org.sql2o.reflection.Setter)

Example 2 with Setter

use of org.sql2o.reflection.Setter in project sql2o by aaberg.

the class AbstractMethodSetterFactoryTest method testAllTypes.

public void testAllTypes() throws IllegalAccessException, NoSuchFieldException {
    POJO1 pojo1 = new POJO1();
    pojo1._boolean = true;
    pojo1._byte = 17;
    pojo1._short = 87;
    pojo1._int = Integer.MIN_VALUE;
    pojo1._long = 1337;
    pojo1._char = 'a';
    pojo1._double = Math.PI;
    pojo1._float = (float) Math.log(93);
    pojo1._obj = pojo1;
    POJO1 pojo2 = new POJO1();
    assertFalse(pojo1.equals(pojo2));
    Method[] methods = pojo1.getClass().getDeclaredMethods();
    for (Method method : methods) {
        if (!method.getName().startsWith("set_"))
            continue;
        Field field = pojo1.getClass().getDeclaredField(method.getName().substring(3));
        Setter setter = msf.newSetter(method);
        assertSame(field.getType(), setter.getType());
        Object val1 = field.get(pojo1);
        Object val2 = field.get(pojo2);
        assertFalse(val1.equals(val2));
        setter.setProperty(pojo2, val1);
        Object val3 = field.get(pojo2);
        assertEquals(val1, val3);
    }
    assertEquals(pojo1, pojo2);
    // primitive fields will not be affected
    for (Method method : methods) {
        if (!method.getName().startsWith("set_"))
            continue;
        Field field = pojo1.getClass().getDeclaredField(method.getName().substring(3));
        Setter setter = msf.newSetter(method);
        Object val1 = field.get(pojo1);
        assertNotNull(val1);
        setter.setProperty(pojo1, null);
        Object val2 = field.get(pojo1);
        if (!setter.getType().isPrimitive()) {
            assertNull(val2);
            continue;
        }
        assertNotNull(val2);
        // not affected
        assertEquals(val1, val2);
    }
    pojo2._obj = null;
    assertEquals(pojo2, pojo1);
}
Also used : Field(java.lang.reflect.Field) Setter(org.sql2o.reflection.Setter) Method(java.lang.reflect.Method)

Example 3 with Setter

use of org.sql2o.reflection.Setter in project sql2o by aaberg.

the class DefaultResultSetHandlerFactory method getSetter.

@SuppressWarnings("unchecked")
private static Setter getSetter(final Quirks quirks, final String propertyPath, final PojoMetadata metadata) {
    int index = propertyPath.indexOf('.');
    if (index <= 0) {
        // Simple path - fast way
        final Setter setter = metadata.getPropertySetterIfExists(propertyPath);
        // behavior change: do not throw if POJO contains less properties
        if (setter == null)
            return null;
        final Converter converter = quirks.converterOf(setter.getType());
        // setter without converter
        if (converter == null)
            return setter;
        return new Setter() {

            public void setProperty(Object obj, Object value) {
                try {
                    setter.setProperty(obj, converter.convert(value));
                } catch (ConverterException e) {
                    throw new Sql2oException("Error trying to convert column " + propertyPath + " to type " + setter.getType(), e);
                }
            }

            public Class getType() {
                return setter.getType();
            }
        };
    }
    // TODO: rewrite, get rid of POJO class
    return new Setter() {

        public void setProperty(Object obj, Object value) {
            Pojo pojo = new Pojo(metadata, metadata.isCaseSensitive(), obj);
            pojo.setProperty(propertyPath, value, quirks);
        }

        public Class getType() {
            // doesn't used anyway
            return Object.class;
        }
    };
}
Also used : ConverterException(org.sql2o.converters.ConverterException) Pojo(org.sql2o.reflection.Pojo) Setter(org.sql2o.reflection.Setter) Converter(org.sql2o.converters.Converter)

Example 4 with Setter

use of org.sql2o.reflection.Setter in project sql2o by aaberg.

the class DefaultResultSetHandlerFactory method newResultSetHandler0.

@SuppressWarnings("unchecked")
private ResultSetHandler<T> newResultSetHandler0(final ResultSetMetaData meta) throws SQLException {
    final Getter[] getters;
    final Setter[] setters;
    final Converter converter;
    final boolean useExecuteScalar;
    // TODO: it's possible to cache converter/setters/getters
    // cache key is ResultSetMetadata + Bean type
    converter = quirks.converterOf(metadata.getType());
    final int columnCount = meta.getColumnCount();
    // getters[0] is always null
    getters = new Getter[columnCount + 1];
    for (int i = 1; i <= columnCount; i++) {
        String colName = quirks.getColumnName(meta, i);
        // behavior change: do not throw if POJO contains less properties
        getters[i] = getGetter(quirks, colName, metadata);
        // and the getter doesn't exist, throw exception.
        if (this.metadata.throwOnMappingFailure && getters[i] == null && columnCount > 1) {
            throw new Sql2oException("Could not map " + colName + " to any property.");
        }
    }
    // setters[0] is always null
    setters = new Setter[columnCount + 1];
    for (int i = 1; i <= columnCount; i++) {
        String colName = quirks.getColumnName(meta, i);
        setters[i] = getSetter(quirks, colName, metadata);
        // and the setter doesn't exist, throw exception.
        if (this.metadata.throwOnMappingFailure && setters[i] == null && columnCount > 1) {
            throw new Sql2oException("Could not map " + colName + " to any property.");
        }
    }
    /**
     * Fallback to executeScalar if converter exists,
     * we're selecting 1 column, and no property setter exists for the column.
     */
    useExecuteScalar = converter != null && columnCount == 1 && setters[1] == null;
    return new ResultSetHandler<T>() {

        @SuppressWarnings("unchecked")
        public T handle(ResultSet resultSet) throws SQLException {
            if (useExecuteScalar) {
                try {
                    return (T) converter.convert(quirks.getRSVal(resultSet, 1));
                } catch (ConverterException e) {
                    throw new Sql2oException("Error occurred while converting value from database to type " + metadata.getType(), e);
                }
            }
            // otherwise we want executeAndFetch with object mapping
            Object pojo = metadata.getObjectConstructor().newInstance();
            for (int colIdx = 1; colIdx <= columnCount; colIdx++) {
                Setter setter = setters[colIdx];
                if (setter == null)
                    continue;
                setter.setProperty(pojo, quirks.getRSVal(resultSet, colIdx));
            }
            return (T) pojo;
        }
    };
}
Also used : ConverterException(org.sql2o.converters.ConverterException) Getter(org.sql2o.reflection.Getter) Setter(org.sql2o.reflection.Setter) ResultSet(java.sql.ResultSet) Converter(org.sql2o.converters.Converter)

Aggregations

Setter (org.sql2o.reflection.Setter)4 Field (java.lang.reflect.Field)2 Converter (org.sql2o.converters.Converter)2 ConverterException (org.sql2o.converters.ConverterException)2 Method (java.lang.reflect.Method)1 ResultSet (java.sql.ResultSet)1 Getter (org.sql2o.reflection.Getter)1 Pojo (org.sql2o.reflection.Pojo)1