mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-11-28 03:42:23 -05:00
Allow JdbcMapper to use reflection for non-public fields optionally
This commit is contained in:
parent
115a62293e
commit
46686e5b05
@ -36,6 +36,11 @@ public interface JdbcMapper extends Closeable {
|
|||||||
*/
|
*/
|
||||||
Class<? extends SQLParser> sqlParser() default SQLParser.class;
|
Class<? extends SQLParser> sqlParser() default SQLParser.class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This defaults to false
|
||||||
|
*/
|
||||||
|
OptionalBool allowReflection() default OptionalBool.DEFAULT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This defaults to SimpleSQLParser, PrestoSQLParser is another option for Java 8, or implement your own
|
* This defaults to SimpleSQLParser, PrestoSQLParser is another option for Java 8, or implement your own
|
||||||
* @return
|
* @return
|
||||||
@ -58,6 +63,11 @@ public interface JdbcMapper extends Closeable {
|
|||||||
*/
|
*/
|
||||||
OptionalBool cachePreparedStatement() default OptionalBool.DEFAULT;
|
OptionalBool cachePreparedStatement() default OptionalBool.DEFAULT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This defaults to the value of the class-level @JdbcMapper.Mapper.allowReflection annotation, but can be configured on a per-method level here
|
||||||
|
*/
|
||||||
|
OptionalBool allowReflection() default OptionalBool.DEFAULT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maximum rows returned in collections/maps/arrays/etc, < 1 mean no limit
|
* Maximum rows returned in collections/maps/arrays/etc, < 1 mean no limit
|
||||||
*
|
*
|
||||||
|
@ -90,17 +90,17 @@ public class CompileTimeResultSetMapper {
|
|||||||
*
|
*
|
||||||
* @return true if calling code should close rs (ResultSet) and ps (PreparedStatement) if closePs is false, false otherwise
|
* @return true if calling code should close rs (ResultSet) and ps (PreparedStatement) if closePs is false, false otherwise
|
||||||
*/
|
*/
|
||||||
public boolean mapToResultType(final Writer w, final String[] keys, final ExecutableElement eeMethod, final MaxRows maxRows, final String cal, final String cleaner, final boolean closePs) throws IOException, NoSuchMethodException, ClassNotFoundException {
|
public boolean mapToResultType(final Writer w, final String[] keys, final ExecutableElement eeMethod, final MaxRows maxRows, final String cal, final String cleaner, final boolean closePs, final ReflectionFields reflectionFields) throws IOException, NoSuchMethodException, ClassNotFoundException {
|
||||||
//final Method m = fromExecutableElement(eeMethod);
|
//final Method m = fromExecutableElement(eeMethod);
|
||||||
//final Class returnType = m.getReturnType();
|
//final Class returnType = m.getReturnType();
|
||||||
final TypeMirror returnTypeMirror = eeMethod.getReturnType();
|
final TypeMirror returnTypeMirror = eeMethod.getReturnType();
|
||||||
//final Class returnType = typeMirrorToClass(returnTypeMirror);
|
//final Class returnType = typeMirrorToClass(returnTypeMirror);
|
||||||
if (returnTypeMirror.getKind() == TypeKind.ARRAY) {
|
if (returnTypeMirror.getKind() == TypeKind.ARRAY) {
|
||||||
final TypeMirror componentType = ((ArrayType) returnTypeMirror).getComponentType();
|
final TypeMirror componentType = ((ArrayType) returnTypeMirror).getComponentType();
|
||||||
toArray(w, keys, componentType, maxRows, cal, cleaner);
|
toArray(w, keys, componentType, maxRows, cal, cleaner, reflectionFields);
|
||||||
} else if (types.isAssignable(returnTypeMirror, collectionType)) {
|
} else if (types.isAssignable(returnTypeMirror, collectionType)) {
|
||||||
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
||||||
toCollection(w, keys, returnTypeMirror, typeArguments.get(0), maxRows, cal, cleaner);
|
toCollection(w, keys, returnTypeMirror, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
|
||||||
} else if (types.isAssignable(returnTypeMirror, mapType)) {
|
} else if (types.isAssignable(returnTypeMirror, mapType)) {
|
||||||
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
||||||
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
|
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
|
||||||
@ -112,57 +112,57 @@ public class CompileTimeResultSetMapper {
|
|||||||
typeArguments.get(0),
|
typeArguments.get(0),
|
||||||
collectionTypeMirror,
|
collectionTypeMirror,
|
||||||
componentTypeMirror,
|
componentTypeMirror,
|
||||||
maxRows, cal, cleaner);
|
maxRows, cal, cleaner, reflectionFields);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
toMap(w, keys, returnTypeMirror, typeArguments.get(0), typeArguments.get(1), maxRows, cal, cleaner);
|
toMap(w, keys, returnTypeMirror, typeArguments.get(0), typeArguments.get(1), maxRows, cal, cleaner, reflectionFields);
|
||||||
} else if (types.isAssignable(returnTypeMirror, iteratorType)) {
|
} else if (types.isAssignable(returnTypeMirror, iteratorType)) {
|
||||||
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
||||||
if (types.isAssignable(returnTypeMirror, resultSetIterableType)) {
|
if (types.isAssignable(returnTypeMirror, resultSetIterableType)) {
|
||||||
toResultSetIterable(w, keys, typeArguments.get(0), cal, cleaner, closePs);
|
toResultSetIterable(w, keys, typeArguments.get(0), cal, cleaner, closePs, reflectionFields);
|
||||||
return false;
|
return false;
|
||||||
} else if (types.isAssignable(returnTypeMirror, listIteratorType))
|
} else if (types.isAssignable(returnTypeMirror, listIteratorType))
|
||||||
toListIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner);
|
toListIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
|
||||||
else
|
else
|
||||||
toIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner);
|
toIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
|
||||||
}
|
}
|
||||||
//IFJAVA8_START
|
//IFJAVA8_START
|
||||||
else if (types.isAssignable(returnTypeMirror, streamType)) {
|
else if (types.isAssignable(returnTypeMirror, streamType)) {
|
||||||
toStream(w, keys, ((DeclaredType) returnTypeMirror).getTypeArguments().get(0), cal, cleaner, closePs);
|
toStream(w, keys, ((DeclaredType) returnTypeMirror).getTypeArguments().get(0), cal, cleaner, closePs, reflectionFields);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//IFJAVA8_END
|
//IFJAVA8_END
|
||||||
else {
|
else {
|
||||||
toObject(w, keys, returnTypeMirror, cal, cleaner);
|
toObject(w, keys, returnTypeMirror, cal, cleaner, reflectionFields);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompileTimeRowToObjectMapper getRowMapper(final String[] keys, TypeMirror returnTypeClass, String cal, TypeMirror mapValType, TypeMirror mapKeyType) {
|
public CompileTimeRowToObjectMapper getRowMapper(final String[] keys, TypeMirror returnTypeClass, String cal, TypeMirror mapValType, TypeMirror mapKeyType, final ReflectionFields reflectionFields) {
|
||||||
return getRowMapper(keys, returnTypeClass, "rs", cal, mapValType, mapKeyType);
|
return getRowMapper(keys, returnTypeClass, "rs", cal, mapValType, mapKeyType, reflectionFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompileTimeRowToObjectMapper getRowMapper(final String[] keys, TypeMirror returnTypeClass, final String resultSetName, String cal, TypeMirror mapValType, TypeMirror mapKeyType) {
|
public CompileTimeRowToObjectMapper getRowMapper(final String[] keys, TypeMirror returnTypeClass, final String resultSetName, String cal, TypeMirror mapValType, TypeMirror mapKeyType, final ReflectionFields reflectionFields) {
|
||||||
return new CompileTimeRowToObjectMapper(this, keys, returnTypeClass, resultSetName, cal, mapValType, mapKeyType);
|
return new CompileTimeRowToObjectMapper(this, keys, returnTypeClass, resultSetName, cal, mapValType, mapKeyType, reflectionFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal) throws IOException, ClassNotFoundException {
|
public void writeObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
writeObject(w, keys, returnTypeMirror, "rs", cal);
|
writeObject(w, keys, returnTypeMirror, "rs", cal, reflectionFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String resultSetName, final String cal) throws IOException, ClassNotFoundException {
|
public void writeObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String resultSetName, final String cal, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
getRowMapper(keys, returnTypeMirror, resultSetName, cal, null, null).gen(w, returnTypeMirror.toString());
|
getRowMapper(keys, returnTypeMirror, resultSetName, cal, null, null, reflectionFields).gen(w, returnTypeMirror.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
public void toObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
w.write("\t\t\tif(rs.next()) {\n");
|
w.write("\t\t\tif(rs.next()) {\n");
|
||||||
writeObject(w, keys, returnTypeMirror, cal);
|
writeObject(w, keys, returnTypeMirror, cal, reflectionFields);
|
||||||
w.write("\t\t\t\treturn ");
|
w.write("\t\t\t\treturn ");
|
||||||
// this does not clean null on purpose, neither does CleaningResultSetMapper
|
// this does not clean null on purpose, neither does CleaningResultSetMapper
|
||||||
clean(w, cleaner).write(";\n\t\t\t} else {\n\t\t\t\treturn null;\n\t\t\t}\n");
|
clean(w, cleaner).write(";\n\t\t\t} else {\n\t\t\t\treturn null;\n\t\t\t}\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void toResultSetIterable(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner, final boolean closePs) throws IOException, ClassNotFoundException {
|
private void toResultSetIterable(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner, final boolean closePs, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
w.write("\t\t\treturn com.moparisthebest.jdbc.util.ResultSetIterable.getResultSetIterable(rs,\n\t\t\t\t\trs.next() ? ");
|
w.write("\t\t\treturn com.moparisthebest.jdbc.util.ResultSetIterable.getResultSetIterable(rs,\n\t\t\t\t\trs.next() ? ");
|
||||||
|
|
||||||
if(java8) {
|
if(java8) {
|
||||||
@ -175,7 +175,7 @@ public class CompileTimeResultSetMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// com.moparisthebest.jdbc.util.ResultSetToObject implementation
|
// com.moparisthebest.jdbc.util.ResultSetToObject implementation
|
||||||
writeObject(w, keys, returnTypeMirror, "_rs", cal == null ? null : "_cal");
|
writeObject(w, keys, returnTypeMirror, "_rs", cal == null ? null : "_cal", reflectionFields);
|
||||||
w.write("\t\t\t\t\t\treturn ");
|
w.write("\t\t\t\t\t\treturn ");
|
||||||
clean(w, cleaner).write(";\n");
|
clean(w, cleaner).write(";\n");
|
||||||
// end ResultSetToObject implementation
|
// end ResultSetToObject implementation
|
||||||
@ -192,7 +192,7 @@ public class CompileTimeResultSetMapper {
|
|||||||
//IFJAVA8_START
|
//IFJAVA8_START
|
||||||
|
|
||||||
// being in this method implies java8 is true already, how else could you be compiling code using Stream? so this won't have the checks for lambdas toResultSetIterable does...
|
// being in this method implies java8 is true already, how else could you be compiling code using Stream? so this won't have the checks for lambdas toResultSetIterable does...
|
||||||
private void toStream(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner, final boolean closePs) throws IOException, ClassNotFoundException {
|
private void toStream(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner, final boolean closePs, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
|
|
||||||
if(closePs)
|
if(closePs)
|
||||||
w.write("\t\t\tfinal PreparedStatement finalPs = ps;\n");
|
w.write("\t\t\tfinal PreparedStatement finalPs = ps;\n");
|
||||||
@ -202,7 +202,7 @@ public class CompileTimeResultSetMapper {
|
|||||||
w.append("(_rs, _cal) -> {\n");
|
w.append("(_rs, _cal) -> {\n");
|
||||||
|
|
||||||
// com.moparisthebest.jdbc.util.ResultSetToObject implementation
|
// com.moparisthebest.jdbc.util.ResultSetToObject implementation
|
||||||
writeObject(w, keys, returnTypeMirror, "_rs", cal == null ? null : "_cal");
|
writeObject(w, keys, returnTypeMirror, "_rs", cal == null ? null : "_cal", reflectionFields);
|
||||||
w.write("\t\t\t\t\t\treturn ");
|
w.write("\t\t\t\t\t\treturn ");
|
||||||
clean(w, cleaner).write(";\n");
|
clean(w, cleaner).write(";\n");
|
||||||
// end ResultSetToObject implementation
|
// end ResultSetToObject implementation
|
||||||
@ -215,46 +215,46 @@ public class CompileTimeResultSetMapper {
|
|||||||
|
|
||||||
//IFJAVA8_END
|
//IFJAVA8_END
|
||||||
|
|
||||||
public void writeCollection(final Writer w, final String[] keys, final String returnTypeString, final String concreteTypeString, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
public void writeCollection(final Writer w, final String[] keys, final String returnTypeString, final String concreteTypeString, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
maxRowInit(w, maxRows).write("\t\t\tfinal ");
|
maxRowInit(w, maxRows).write("\t\t\tfinal ");
|
||||||
w.write(returnTypeString);
|
w.write(returnTypeString);
|
||||||
w.write(" _colret = new ");
|
w.write(" _colret = new ");
|
||||||
w.write(concreteTypeString);
|
w.write(concreteTypeString);
|
||||||
w.write(returnTypeString.substring(returnTypeString.indexOf('<')));
|
w.write(returnTypeString.substring(returnTypeString.indexOf('<')));
|
||||||
w.write("();\n\t\t\twhile(rs.next()) {\n");
|
w.write("();\n\t\t\twhile(rs.next()) {\n");
|
||||||
writeObject(w, keys, componentTypeMirror, cal);
|
writeObject(w, keys, componentTypeMirror, cal, reflectionFields);
|
||||||
w.write("\t\t\t\t_colret.add(");
|
w.write("\t\t\t\t_colret.add(");
|
||||||
clean(w, cleaner).write(");\n");
|
clean(w, cleaner).write(");\n");
|
||||||
maxRowBreak(w, maxRows).write("\t\t\t}\n");
|
maxRowBreak(w, maxRows).write("\t\t\t}\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toCollection(final Writer w, final String[] keys, final TypeMirror collectionTypeMirror, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
public void toCollection(final Writer w, final String[] keys, final TypeMirror collectionTypeMirror, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
final String collectionType = getConcreteClassCanonicalName(collectionTypeMirror, ArrayList.class);
|
final String collectionType = getConcreteClassCanonicalName(collectionTypeMirror, ArrayList.class);
|
||||||
writeCollection(w, keys, collectionTypeMirror.toString(), collectionType, componentTypeMirror, maxRows, cal, cleaner);
|
writeCollection(w, keys, collectionTypeMirror.toString(), collectionType, componentTypeMirror, maxRows, cal, cleaner, reflectionFields);
|
||||||
w.write("\t\t\treturn _colret;\n");
|
w.write("\t\t\treturn _colret;\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toArray(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
public void toArray(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
final String returnTypeString = componentTypeMirror.toString();
|
final String returnTypeString = componentTypeMirror.toString();
|
||||||
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, maxRows, cal, cleaner);
|
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, maxRows, cal, cleaner, reflectionFields);
|
||||||
w.write("\t\t\treturn _colret.toArray(new ");
|
w.write("\t\t\treturn _colret.toArray(new ");
|
||||||
w.write(returnTypeString);
|
w.write(returnTypeString);
|
||||||
w.write("[_colret.size()]);\n");
|
w.write("[_colret.size()]);\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toListIterator(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
public void toListIterator(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
final String returnTypeString = componentTypeMirror.toString();
|
final String returnTypeString = componentTypeMirror.toString();
|
||||||
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, maxRows, cal, cleaner);
|
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, maxRows, cal, cleaner, reflectionFields);
|
||||||
w.write("\t\t\treturn _colret.listIterator();\n");
|
w.write("\t\t\treturn _colret.listIterator();\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toIterator(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
public void toIterator(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
final String returnTypeString = componentTypeMirror.toString();
|
final String returnTypeString = componentTypeMirror.toString();
|
||||||
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, maxRows, cal, cleaner);
|
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, maxRows, cal, cleaner, reflectionFields);
|
||||||
w.write("\t\t\treturn _colret.iterator();\n");
|
w.write("\t\t\treturn _colret.iterator();\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toMap(final Writer w, final String[] keys, final TypeMirror mapTypeMirror, final TypeMirror mapKeyTypeMirror, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
public void toMap(final Writer w, final String[] keys, final TypeMirror mapTypeMirror, final TypeMirror mapKeyTypeMirror, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
final String mapType = getConcreteClassCanonicalName(mapTypeMirror, HashMap.class);
|
final String mapType = getConcreteClassCanonicalName(mapTypeMirror, HashMap.class);
|
||||||
final String returnTypeString = mapTypeMirror.toString();
|
final String returnTypeString = mapTypeMirror.toString();
|
||||||
maxRowInit(w, maxRows).write("\t\t\tfinal ");
|
maxRowInit(w, maxRows).write("\t\t\tfinal ");
|
||||||
@ -265,7 +265,7 @@ public class CompileTimeResultSetMapper {
|
|||||||
w.write("();\n\t\t\twhile(rs.next()) {\n");
|
w.write("();\n\t\t\twhile(rs.next()) {\n");
|
||||||
|
|
||||||
//writeObject(w, keys, componentTypeMirror, componentType, cal);
|
//writeObject(w, keys, componentTypeMirror, componentType, cal);
|
||||||
final CompileTimeRowToObjectMapper rm = getRowMapper(keys, componentTypeMirror, cal, null, mapKeyTypeMirror);
|
final CompileTimeRowToObjectMapper rm = getRowMapper(keys, componentTypeMirror, cal, null, mapKeyTypeMirror, reflectionFields);
|
||||||
rm.gen(w, componentTypeMirror.toString());
|
rm.gen(w, componentTypeMirror.toString());
|
||||||
w.write("\t\t\t\t_colret.put(");
|
w.write("\t\t\t\t_colret.put(");
|
||||||
rm.extractColumnValueString(w, 1, mapKeyTypeMirror);
|
rm.extractColumnValueString(w, 1, mapKeyTypeMirror);
|
||||||
@ -280,7 +280,7 @@ public class CompileTimeResultSetMapper {
|
|||||||
final TypeMirror mapKeyTypeMirror,
|
final TypeMirror mapKeyTypeMirror,
|
||||||
final TypeMirror collectionTypeMirror,
|
final TypeMirror collectionTypeMirror,
|
||||||
final TypeMirror componentTypeMirror,
|
final TypeMirror componentTypeMirror,
|
||||||
MaxRows maxRows, String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
MaxRows maxRows, String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException {
|
||||||
final String mapType = getConcreteClassCanonicalName(mapTypeMirror, HashMap.class);
|
final String mapType = getConcreteClassCanonicalName(mapTypeMirror, HashMap.class);
|
||||||
final String collectionType = getConcreteClassCanonicalName(collectionTypeMirror, ArrayList.class);
|
final String collectionType = getConcreteClassCanonicalName(collectionTypeMirror, ArrayList.class);
|
||||||
final String returnTypeString = mapTypeMirror.toString();
|
final String returnTypeString = mapTypeMirror.toString();
|
||||||
@ -294,7 +294,7 @@ public class CompileTimeResultSetMapper {
|
|||||||
w.write("();\n\t\t\twhile(rs.next()) {\n");
|
w.write("();\n\t\t\twhile(rs.next()) {\n");
|
||||||
|
|
||||||
//writeObject(w, keys, componentTypeMirror, componentType, cal);
|
//writeObject(w, keys, componentTypeMirror, componentType, cal);
|
||||||
final CompileTimeRowToObjectMapper rm = getRowMapper(keys, componentTypeMirror, cal, null, mapKeyTypeMirror);
|
final CompileTimeRowToObjectMapper rm = getRowMapper(keys, componentTypeMirror, cal, null, mapKeyTypeMirror, reflectionFields);
|
||||||
rm.gen(w, componentTypeMirror.toString());
|
rm.gen(w, componentTypeMirror.toString());
|
||||||
|
|
||||||
w.write("\t\t\t\tfinal ");
|
w.write("\t\t\t\tfinal ");
|
||||||
|
@ -51,9 +51,12 @@ public class CompileTimeRowToObjectMapper {
|
|||||||
protected Element[] _fields = null;
|
protected Element[] _fields = null;
|
||||||
protected int[] _fieldTypes;
|
protected int[] _fieldTypes;
|
||||||
|
|
||||||
public CompileTimeRowToObjectMapper(final CompileTimeResultSetMapper rsm, final String[] keys, final TypeMirror returnTypeClass, final String resultSetName, final String calendarName, final TypeMirror mapValType, final TypeMirror mapKeyType) {
|
protected final ReflectionFields reflectionFields;
|
||||||
|
|
||||||
|
public CompileTimeRowToObjectMapper(final CompileTimeResultSetMapper rsm, final String[] keys, final TypeMirror returnTypeClass, final String resultSetName, final String calendarName, final TypeMirror mapValType, final TypeMirror mapKeyType, final ReflectionFields reflectionFields) {
|
||||||
this.rsm = rsm;
|
this.rsm = rsm;
|
||||||
this.keys = keys;
|
this.keys = keys;
|
||||||
|
this.reflectionFields = reflectionFields;
|
||||||
|
|
||||||
_calendarName = calendarName;
|
_calendarName = calendarName;
|
||||||
_resultSetName = resultSetName;
|
_resultSetName = resultSetName;
|
||||||
@ -176,7 +179,7 @@ public class CompileTimeRowToObjectMapper {
|
|||||||
final VariableElement f = (VariableElement)e;
|
final VariableElement f = (VariableElement)e;
|
||||||
//System.out.println("f.fieldName: "+f.getSimpleName());
|
//System.out.println("f.fieldName: "+f.getSimpleName());
|
||||||
if (f.getModifiers().contains(Modifier.STATIC)) continue;
|
if (f.getModifiers().contains(Modifier.STATIC)) continue;
|
||||||
if (!f.getModifiers().contains(Modifier.PUBLIC)) continue;
|
//if (reflectionFields == null && !f.getModifiers().contains(Modifier.PUBLIC)) continue;
|
||||||
String fieldName = f.getSimpleName().toString().toUpperCase();
|
String fieldName = f.getSimpleName().toString().toUpperCase();
|
||||||
//System.out.println("fieldName: "+fieldName);
|
//System.out.println("fieldName: "+fieldName);
|
||||||
if (!mapFields.containsKey(fieldName)) {
|
if (!mapFields.containsKey(fieldName)) {
|
||||||
@ -355,9 +358,16 @@ public class CompileTimeRowToObjectMapper {
|
|||||||
}
|
}
|
||||||
if (isField) {
|
if (isField) {
|
||||||
// if f not accessible (but super.getFieldMappings() sets it), throw exception during compilation is fine
|
// if f not accessible (but super.getFieldMappings() sets it), throw exception during compilation is fine
|
||||||
java.append("ret.").append(f.getSimpleName().toString()).append(" = ");
|
final Set<Modifier> mods = reflectionFields == null ? null : f.getModifiers();
|
||||||
extractColumnValueString(java, i, _fieldTypes[i], enumName);
|
if(mods != null && (mods.contains(Modifier.PRIVATE) || mods.contains(Modifier.PROTECTED) || mods.contains(Modifier.FINAL))) {
|
||||||
java.append(";\n");
|
java.append("com.moparisthebest.jdbc.util.ReflectionUtil.setValue(_fields[").append(String.valueOf((reflectionFields.addGetIndex((VariableElement)f)))).append("], ret, ");
|
||||||
|
extractColumnValueString(java, i, _fieldTypes[i], enumName);
|
||||||
|
java.append(");\n");
|
||||||
|
} else {
|
||||||
|
java.append("ret.").append(f.getSimpleName().toString()).append(" = ");
|
||||||
|
extractColumnValueString(java, i, _fieldTypes[i], enumName);
|
||||||
|
java.append(";\n");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
java.append("ret.").append(f.getSimpleName().toString()).append("(");
|
java.append("ret.").append(f.getSimpleName().toString()).append("(");
|
||||||
extractColumnValueString(java, i, _fieldTypes[i], enumName);
|
extractColumnValueString(java, i, _fieldTypes[i], enumName);
|
||||||
|
@ -200,6 +200,9 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
|||||||
lookupCloseMethod = false;
|
lookupCloseMethod = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final boolean defaultAllowReflection = mapper.allowReflection().combine(false);
|
||||||
|
final ReflectionFields reflectionFields = new ReflectionFields();
|
||||||
|
|
||||||
for (final Element methodElement : genClass.getEnclosedElements()) {
|
for (final Element methodElement : genClass.getEnclosedElements()) {
|
||||||
// can only implement abstract methods
|
// can only implement abstract methods
|
||||||
if (methodElement.getKind() != ElementKind.METHOD || !methodElement.getModifiers().contains(Modifier.ABSTRACT))
|
if (methodElement.getKind() != ElementKind.METHOD || !methodElement.getModifiers().contains(Modifier.ABSTRACT))
|
||||||
@ -215,6 +218,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
|||||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.SQL with non-empty query is required on abstract or interface methods", methodElement);
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.SQL with non-empty query is required on abstract or interface methods", methodElement);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
final boolean allowReflection = sql.allowReflection().combine(defaultAllowReflection);
|
||||||
w.write("\n\t@Override\n\tpublic ");
|
w.write("\n\t@Override\n\tpublic ");
|
||||||
final String returnType = eeMethod.getReturnType().toString();
|
final String returnType = eeMethod.getReturnType().toString();
|
||||||
w.write(returnType);
|
w.write(returnType);
|
||||||
@ -372,7 +376,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
|||||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.SQL sql parsed a wildcard column name which is not supported", methodElement);
|
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.SQL sql parsed a wildcard column name which is not supported", methodElement);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
closeRs = rsm.mapToResultType(w, keys, eeMethod, maxRows, calendarName, cleanerName, !cachePreparedStatements);
|
closeRs = rsm.mapToResultType(w, keys, eeMethod, maxRows, calendarName, cleanerName, !cachePreparedStatements, allowReflection ? reflectionFields : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// close things
|
// close things
|
||||||
@ -412,6 +416,16 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!reflectionFields.isEmpty()) {
|
||||||
|
w.append("\n\tprivate static final java.lang.reflect.Field[] _fields = new java.lang.reflect.Field[] {\n");
|
||||||
|
for(final VariableElement ve : reflectionFields) {
|
||||||
|
w.append("\t\tcom.moparisthebest.jdbc.util.ReflectionUtil.getAccessibleField(")
|
||||||
|
.append(ve.getEnclosingElement().asType().toString()).append(".class, \"")
|
||||||
|
.append(ve.getSimpleName().toString()).append("\"),\n");
|
||||||
|
}
|
||||||
|
w.append("\t};\n");
|
||||||
|
}
|
||||||
|
|
||||||
if (cachedPreparedStatements > 0) {
|
if (cachedPreparedStatements > 0) {
|
||||||
w.write("\n\tprivate final PreparedStatement[] psCache = new PreparedStatement[");
|
w.write("\n\tprivate final PreparedStatement[] psCache = new PreparedStatement[");
|
||||||
w.write(Integer.toString(cachedPreparedStatements));
|
w.write(Integer.toString(cachedPreparedStatements));
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.moparisthebest.jdbc.codegen;
|
||||||
|
|
||||||
|
import javax.lang.model.element.VariableElement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by mopar on 6/19/17.
|
||||||
|
*/
|
||||||
|
public class ReflectionFields implements Iterable<VariableElement> {
|
||||||
|
private final List<VariableElement> fields = new ArrayList<VariableElement>();
|
||||||
|
|
||||||
|
public int addGetIndex(final VariableElement ve) {
|
||||||
|
final int ret = fields.indexOf(ve);
|
||||||
|
if(ret != -1)
|
||||||
|
return ret;
|
||||||
|
fields.add(ve);
|
||||||
|
return fields.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return fields.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<VariableElement> iterator() {
|
||||||
|
return fields.iterator();
|
||||||
|
}
|
||||||
|
}
|
@ -189,4 +189,9 @@ public class JdbcMapperTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//IFJAVA8_END
|
//IFJAVA8_END
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPerson() throws SQLException {
|
||||||
|
assertEquals(fieldPerson1, dao.getPerson(fieldPerson1.getPersonNo()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ import java.time.*;
|
|||||||
// databaseType = JdbcMapper.DatabaseType.ORACLE
|
// databaseType = JdbcMapper.DatabaseType.ORACLE
|
||||||
cachePreparedStatements = JdbcMapper.OptionalBool.FALSE
|
cachePreparedStatements = JdbcMapper.OptionalBool.FALSE
|
||||||
// , sqlParser = SimpleSQLParser.class
|
// , sqlParser = SimpleSQLParser.class
|
||||||
|
, allowReflection = JdbcMapper.OptionalBool.TRUE
|
||||||
)
|
)
|
||||||
public interface PersonDAO extends Closeable {
|
public interface PersonDAO extends Closeable {
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ public interface PersonDAO extends Closeable {
|
|||||||
@JdbcMapper.SQL("SELECT first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
|
@JdbcMapper.SQL("SELECT first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
|
||||||
FieldPerson getPerson(long personNo, Calendar cal) throws SQLException;
|
FieldPerson getPerson(long personNo, Calendar cal) throws SQLException;
|
||||||
|
|
||||||
@JdbcMapper.SQL("SELECT first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
|
@JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
|
||||||
FieldPerson getPerson(long personNo) throws SQLException;
|
FieldPerson getPerson(long personNo) throws SQLException;
|
||||||
|
|
||||||
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}")
|
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}")
|
||||||
|
@ -25,6 +25,7 @@ import java.time.*;
|
|||||||
// databaseType = JdbcMapper.DatabaseType.ORACLE
|
// databaseType = JdbcMapper.DatabaseType.ORACLE
|
||||||
cachePreparedStatements = JdbcMapper.OptionalBool.FALSE
|
cachePreparedStatements = JdbcMapper.OptionalBool.FALSE
|
||||||
, sqlParser = PrestoSQLParser.class
|
, sqlParser = PrestoSQLParser.class
|
||||||
|
, allowReflection = JdbcMapper.OptionalBool.TRUE
|
||||||
)
|
)
|
||||||
public interface PrestoPersonDAO extends PersonDAO {
|
public interface PrestoPersonDAO extends PersonDAO {
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ public interface PrestoPersonDAO extends PersonDAO {
|
|||||||
@JdbcMapper.SQL("SELECT first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
|
@JdbcMapper.SQL("SELECT first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
|
||||||
FieldPerson getPerson(long personNo, Calendar cal) throws SQLException;
|
FieldPerson getPerson(long personNo, Calendar cal) throws SQLException;
|
||||||
|
|
||||||
@JdbcMapper.SQL("SELECT first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
|
@JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
|
||||||
FieldPerson getPerson(long personNo) throws SQLException;
|
FieldPerson getPerson(long personNo) throws SQLException;
|
||||||
|
|
||||||
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}")
|
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}")
|
||||||
|
@ -11,17 +11,17 @@ public class CleaningCompilingResultSetMapper<E> extends CompilingResultSetMappe
|
|||||||
|
|
||||||
private final Cleaner<E> cleaner;
|
private final Cleaner<E> cleaner;
|
||||||
|
|
||||||
public CleaningCompilingResultSetMapper(final Calendar cal, final int arrayMaxLength, final CompilingRowToObjectMapper.Cache cache, final Cleaner<E> cleaner) {
|
public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final Calendar cal, final int arrayMaxLength, final CompilingRowToObjectMapper.Cache cache) {
|
||||||
super(cal, arrayMaxLength, cache);
|
super(cal, arrayMaxLength, cache);
|
||||||
this.cleaner = cleaner;
|
this.cleaner = cleaner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CleaningCompilingResultSetMapper(final CompilingRowToObjectMapper.Cache cache, final Cleaner<E> cleaner) {
|
public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final CompilingRowToObjectMapper.Cache cache) {
|
||||||
super(cache);
|
super(cache);
|
||||||
this.cleaner = cleaner;
|
this.cleaner = cleaner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CleaningCompilingResultSetMapper(final int arrayMaxLength, final CompilingRowToObjectMapper.Cache cache, final Cleaner<E> cleaner) {
|
public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final int arrayMaxLength, final CompilingRowToObjectMapper.Cache cache) {
|
||||||
super(arrayMaxLength, cache);
|
super(arrayMaxLength, cache);
|
||||||
this.cleaner = cleaner;
|
this.cleaner = cleaner;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.moparisthebest.jdbc;
|
package com.moparisthebest.jdbc;
|
||||||
|
|
||||||
import com.moparisthebest.classgen.Compiler;
|
import com.moparisthebest.classgen.Compiler;
|
||||||
import com.sun.org.apache.xpath.internal.operations.Mod;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.AccessibleObject;
|
import java.lang.reflect.AccessibleObject;
|
||||||
|
@ -65,7 +65,7 @@ public class CleaningQueryMapperTest {
|
|||||||
return Arrays.asList(new Object[][] {
|
return Arrays.asList(new Object[][] {
|
||||||
{ new CleaningResultSetMapper<FieldPerson>(personCleaner) },
|
{ new CleaningResultSetMapper<FieldPerson>(personCleaner) },
|
||||||
{ new CleaningCachingResultSetMapper<FieldPerson>(personCleaner) },
|
{ new CleaningCachingResultSetMapper<FieldPerson>(personCleaner) },
|
||||||
{ new CleaningCompilingResultSetMapper<FieldPerson>(personCleaner) },
|
{ new CleaningCompilingResultSetMapper<FieldPerson>(personCleaner, new CompilingRowToObjectMapper.Cache(true)) },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,8 +7,8 @@ import java.util.Date;
|
|||||||
*/
|
*/
|
||||||
public class FieldPerson implements Person {
|
public class FieldPerson implements Person {
|
||||||
|
|
||||||
public long personNo;
|
protected long personNo;
|
||||||
public Date birthDate;
|
protected Date birthDate;
|
||||||
public String firstName;
|
public String firstName;
|
||||||
public String lastName;
|
public String lastName;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user