Setter methods now just have to start with 'set' not 'set[A-Z_]'

This commit is contained in:
Travis Burtrum 2018-01-15 12:54:59 -05:00
parent 2580de8f42
commit 6ba284e8c2
6 changed files with 50 additions and 23 deletions

View File

@ -12,10 +12,8 @@ import javax.lang.model.type.TypeMirror;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import static com.moparisthebest.jdbc.CompilingRowToObjectMapper.escapeMapKeyString; import static com.moparisthebest.jdbc.CompilingRowToObjectMapper.escapeMapKeyString;
import static com.moparisthebest.jdbc.RowToObjectMapper._setterRegex;
import static com.moparisthebest.jdbc.codegen.CompileTimeResultSetMapper.getConcreteClassCanonicalName; import static com.moparisthebest.jdbc.codegen.CompileTimeResultSetMapper.getConcreteClassCanonicalName;
import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToClass; import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToClass;
@ -292,8 +290,7 @@ public class CompileTimeRowToObjectMapper {
* @return True if the method is a setter method. * @return True if the method is a setter method.
*/ */
protected boolean isSetterMethod(final ExecutableElement method) { protected boolean isSetterMethod(final ExecutableElement method) {
Matcher matcher = _setterRegex.matcher(method.getSimpleName()); if (method.getSimpleName().toString().startsWith("set")) {
if (matcher.matches()) {
final Set<Modifier> modifiers = method.getModifiers(); final Set<Modifier> modifiers = method.getModifiers();
if (modifiers.contains(Modifier.STATIC)) return false; if (modifiers.contains(Modifier.STATIC)) return false;
@ -393,11 +390,7 @@ public class CompileTimeRowToObjectMapper {
return _returnTypeClass.cast(val); return _returnTypeClass.cast(val);
} }
*/ */
// we could actually pull from first row like above and test it first and fail now, but maybe just failing during compilation is enough? // todo: we could actually pull from first row like above and test it first, but for now we will fall-through to field mappings...
java.append("final ").append(tType).append(" ret = (").append(tType).append(") ");
extractColumnValueString(java, 1, typeId, _returnTypeClass.toString());
java.append(";\n");
return;
} }
} catch (Exception e) { } catch (Exception e) {
throw new MapperException(e.getMessage(), e); throw new MapperException(e.getMessage(), e);

View File

@ -1,9 +1,6 @@
package com.moparisthebest.jdbc.codegen; package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.dto.EnumPerson; import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.dto.FieldPerson;
import com.moparisthebest.jdbc.dto.FirstName;
import com.moparisthebest.jdbc.dto.Val;
import com.moparisthebest.jdbc.util.ResultSetIterable; import com.moparisthebest.jdbc.util.ResultSetIterable;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -121,6 +118,13 @@ public class JdbcMapperTest {
assertEquals(null, dao.getEnumNull()); assertEquals(null, dao.getEnumNull());
} }
@Test
public void testCaseInsensitiveMethods() throws SQLException {
final CaseSensitivePerson expected = new CaseSensitivePerson();
expected.setmPersonFirstName(fieldPerson1.getFirstName());
assertEquals(expected, dao.getCaseSensitivePerson(fieldPerson1.getPersonNo()));
}
//IFJAVA8_START //IFJAVA8_START
@Test @Test

View File

@ -1,10 +1,7 @@
package com.moparisthebest.jdbc.codegen; package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.Cleaner; import com.moparisthebest.jdbc.Cleaner;
import com.moparisthebest.jdbc.dto.EnumPerson; import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.dto.FieldPerson;
import com.moparisthebest.jdbc.dto.FirstName;
import com.moparisthebest.jdbc.dto.Person;
import com.moparisthebest.jdbc.util.ResultSetIterable; import com.moparisthebest.jdbc.util.ResultSetIterable;
import java.io.Closeable; import java.io.Closeable;
@ -208,6 +205,9 @@ public interface PersonDAO extends JdbcMapper {
@JdbcMapper.SQL("SELECT str_val FROM val WHERE val_no = 4") @JdbcMapper.SQL("SELECT str_val FROM val WHERE val_no = 4")
FirstName getEnumNull() throws SQLException; FirstName getEnumNull() throws SQLException;
@JdbcMapper.SQL("SELECT first_name AS M_PERSON_FIRST_NAME FROM person WHERE person_no = {personNo}")
CaseSensitivePerson getCaseSensitivePerson(long personNo);
//IFJAVA8_START //IFJAVA8_START
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}") @JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")

View File

@ -30,8 +30,6 @@ import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//IFJAVA8_START //IFJAVA8_START
import java.time.*; import java.time.*;
//IFJAVA8_END //IFJAVA8_END
@ -58,9 +56,7 @@ import static com.moparisthebest.jdbc.UpdateableDTO.NO;
*/ */
public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> { public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> {
private static final String SETTER_NAME_REGEX = "^(set)([A-Z_]\\w*+)";
protected static final TypeMappingsFactory _tmf = TypeMappingsFactory.getInstance(); protected static final TypeMappingsFactory _tmf = TypeMappingsFactory.getInstance();
public static final Pattern _setterRegex = Pattern.compile(SETTER_NAME_REGEX);
public static final int TYPE_BOOLEAN = _tmf.getTypeId(Boolean.TYPE);//TypeMappingsFactory.TYPE_BOOLEAN; // not public? public static final int TYPE_BOOLEAN = _tmf.getTypeId(Boolean.TYPE);//TypeMappingsFactory.TYPE_BOOLEAN; // not public?
public static final int TYPE_BOOLEAN_OBJ = _tmf.getTypeId(Boolean.class);//TypeMappingsFactory.TYPE_BOOLEAN_OBJ; // not public? public static final int TYPE_BOOLEAN_OBJ = _tmf.getTypeId(Boolean.class);//TypeMappingsFactory.TYPE_BOOLEAN_OBJ; // not public?
@ -556,8 +552,7 @@ public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> {
* @return True if the method is a setter method. * @return True if the method is a setter method.
*/ */
protected boolean isSetterMethod(Method method) { protected boolean isSetterMethod(Method method) {
Matcher matcher = _setterRegex.matcher(method.getName()); if (method.getName().startsWith("set")) {
if (matcher.matches()) {
if (Modifier.isStatic(method.getModifiers())) return false; if (Modifier.isStatic(method.getModifiers())) return false;
if (!Modifier.isPublic(method.getModifiers())) return false; if (!Modifier.isPublic(method.getModifiers())) return false;

View File

@ -477,6 +477,13 @@ public class QueryMapperTest {
assertEquals(null, qm.toObject("SELECT str_val FROM val WHERE val_no = 4", FirstName.class)); assertEquals(null, qm.toObject("SELECT str_val FROM val WHERE val_no = 4", FirstName.class));
} }
@Test
public void testCaseInsensitiveMethods() throws SQLException {
final CaseSensitivePerson expected = new CaseSensitivePerson();
expected.setmPersonFirstName(fieldPerson1.getFirstName());
assertEquals(expected, qm.toObject("SELECT first_name AS M_PERSON_FIRST_NAME FROM person WHERE person_no = ?", CaseSensitivePerson.class, fieldPerson1.getPersonNo()));
}
//IFJAVA8_START //IFJAVA8_START
@Test @Test

View File

@ -0,0 +1,28 @@
package com.moparisthebest.jdbc.dto;
public class CaseSensitivePerson {
private String mPersonFirstName;
public String getmPersonFirstName() {
return mPersonFirstName;
}
public void setmPersonFirstName(final String mPersonFirstName) {
this.mPersonFirstName = mPersonFirstName;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final CaseSensitivePerson that = (CaseSensitivePerson) o;
return mPersonFirstName != null ? mPersonFirstName.equals(that.mPersonFirstName) : that.mPersonFirstName == null;
}
@Override
public int hashCode() {
return mPersonFirstName != null ? mPersonFirstName.hashCode() : 0;
}
}