Search in sources :

Example 1 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class MethodAccessorsGenerator method newSetter.

public Setter newSetter(final Method method) {
    final Class type = method.getParameterTypes()[0];
    final MethodAccessor methodAccessor = newMethodAccessor(method);
    return new Setter() {

        public void setProperty(Object obj, Object value) {
            if (value == null && type.isPrimitive())
                return;
            try {
                methodAccessor.invoke(obj, new Object[] { value });
            } catch (InvocationTargetException e) {
                throw new Sql2oException("error while calling setter method with name " + method.getName() + " on class " + obj.getClass().toString(), e);
            }
        }

        public Class getType() {
            return type;
        }
    };
}
Also used : Sql2oException(org.sql2o.Sql2oException) MethodAccessor(sun.reflect.MethodAccessor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class MethodAccessorsGenerator method newConstructor.

@Override
public ObjectConstructor newConstructor(final Class<?> cls) {
    for (Class<?> cls0 = cls; cls != Object.class; cls0 = cls0.getSuperclass()) {
        try {
            Constructor<?> ctor = cls0.getDeclaredConstructor();
            final ConstructorAccessor constructorAccessor = (cls0 == cls) ? newConstructorAccessor(ctor) : newConstructorAccessor(ctor, cls);
            return new ObjectConstructor() {

                @Override
                public Object newInstance() {
                    try {
                        return constructorAccessor.newInstance(null);
                    } catch (InstantiationException | InvocationTargetException e) {
                        throw new Sql2oException("Could not create a new instance of class " + cls, e);
                    }
                }
            };
        } catch (NoSuchMethodException e) {
        // ignore
        }
    }
    return UnsafeFieldSetterFactory.getConstructor(cls);
}
Also used : Sql2oException(org.sql2o.Sql2oException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConstructorAccessor(sun.reflect.ConstructorAccessor)

Example 3 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class Pojo method setProperty.

@SuppressWarnings("unchecked")
public void setProperty(String propertyPath, Object value, Quirks quirks) {
    // String.split uses RegularExpression
    // this is overkill for every column for every row
    int index = propertyPath.indexOf('.');
    Setter setter;
    if (index > 0) {
        final String substring = propertyPath.substring(0, index);
        setter = metadata.getPropertySetter(substring);
        String newPath = propertyPath.substring(index + 1);
        Object subValue = this.metadata.getValueOfProperty(substring, this.object);
        if (subValue == null) {
            try {
                subValue = setter.getType().newInstance();
            } catch (InstantiationException e) {
                throw new Sql2oException("Could not instantiate a new instance of class " + setter.getType().toString(), e);
            } catch (IllegalAccessException e) {
                throw new Sql2oException("Could not instantiate a new instance of class " + setter.getType().toString(), e);
            }
            setter.setProperty(this.object, subValue);
        }
        PojoMetadata subMetadata = new PojoMetadata(setter.getType(), this.caseSensitive, this.metadata.isAutoDeriveColumnNames(), this.metadata.getColumnMappings(), this.metadata.throwOnMappingFailure);
        Pojo subPojo = new Pojo(subMetadata, this.caseSensitive, subValue);
        subPojo.setProperty(newPath, value, quirks);
    } else {
        setter = metadata.getPropertySetter(propertyPath);
        Converter converter;
        try {
            converter = throwIfNull(setter.getType(), quirks.converterOf(setter.getType()));
        } catch (ConverterException e) {
            throw new Sql2oException("Cannot convert column " + propertyPath + " to type " + setter.getType(), e);
        }
        try {
            setter.setProperty(this.object, converter.convert(value));
        } catch (ConverterException e) {
            throw new Sql2oException("Error trying to convert column " + propertyPath + " to type " + setter.getType(), e);
        }
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) ConverterException(org.sql2o.converters.ConverterException) Converter(org.sql2o.converters.Converter)

Example 4 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class ReflectionObjectConstructorFactory method newConstructor.

public ObjectConstructor newConstructor(final Class<?> clazz) {
    try {
        final Constructor<?> ctor = clazz.getDeclaredConstructor();
        ctor.setAccessible(true);
        return new ObjectConstructor() {

            public Object newInstance() {
                try {
                    return ctor.newInstance((Object[]) null);
                } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                    throw new Sql2oException("Could not create a new instance of class " + clazz, e);
                }
            }
        };
    } catch (Throwable e) {
        throw new Sql2oException("Could not find parameter-less constructor of class " + clazz, e);
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class IssuesTest method testIssue149NullPointerWhenUsingWrongParameterName.

@Test
public void testIssue149NullPointerWhenUsingWrongParameterName() {
    try (Connection connection = sql2o.open()) {
        connection.createQuery("create table issue149 (id integer primary key, val varchar(20))").executeUpdate();
        connection.createQuery("insert into issue149(id, val) values (:id, :val)").addParameter("id", 1).addParameter("asdsa", // spell-error in parameter name
        "something").executeUpdate();
        fail("Expected exception!!");
    } catch (Sql2oException ex) {
    // awesome!
    } catch (Throwable t) {
        fail("A " + t.getClass().getName() + " was thrown, but An " + Sql2oException.class.getName() + " was expected");
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) Connection(org.sql2o.Connection) Test(org.junit.Test)

Aggregations

Sql2oException (org.sql2o.Sql2oException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Test (org.junit.Test)3 Connection (org.sql2o.Connection)2 Converter (org.sql2o.converters.Converter)2 ConverterException (org.sql2o.converters.ConverterException)2 MethodAccessor (sun.reflect.MethodAccessor)2 Row (org.sql2o.data.Row)1 Table (org.sql2o.data.Table)1 Issue1Pojo (org.sql2o.issues.pojos.Issue1Pojo)1 ConstructorAccessor (sun.reflect.ConstructorAccessor)1