Print proper errors with links to methods instead of throwing exceptions

This commit is contained in:
Travis Burtrum 2017-11-08 23:31:28 -05:00
parent f58e107ff5
commit a22e8d6d58
3 changed files with 51 additions and 44 deletions

View File

@ -1,6 +1,7 @@
package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.Finishable;
import com.moparisthebest.jdbc.MapperException;
import com.moparisthebest.jdbc.ResultSetMapper;
import com.moparisthebest.jdbc.TypeMappingsFactory;
import com.moparisthebest.jdbc.util.ResultSetIterable;
@ -15,6 +16,7 @@ import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.io.IOException;
import java.io.Writer;
import java.sql.ResultSet;
@ -98,48 +100,52 @@ public class CompileTimeResultSetMapper {
//final Class returnType = m.getReturnType();
final TypeMirror returnTypeMirror = eeMethod.getReturnType();
//final Class returnType = typeMirrorToClass(returnTypeMirror);
if (returnTypeMirror.getKind() == TypeKind.ARRAY && !types.isSameType(returnTypeMirror, byteArrayType) && eeMethod.getAnnotation(JdbcMapper.SingleRow.class) == null) {
final TypeMirror componentType = ((ArrayType) returnTypeMirror).getComponentType();
toArray(w, keys, componentType, maxRows, cal, cleaner, reflectionFields);
} else if (types.isAssignable(returnTypeMirror, collectionType)) {
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
toCollection(w, keys, returnTypeMirror, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
} else if (types.isAssignable(returnTypeMirror, mapType) && eeMethod.getAnnotation(JdbcMapper.SingleRow.class) == null) {
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
if (types.isAssignable(returnTypeMirror, mapCollectionType)) { // for collectionMaps
final TypeMirror collectionTypeMirror = typeArguments.get(1);
final TypeMirror componentTypeMirror = ((DeclaredType) collectionTypeMirror).getTypeArguments().get(0);
toMapCollection(w, keys,
returnTypeMirror,
typeArguments.get(0),
collectionTypeMirror,
componentTypeMirror,
maxRows, cal, cleaner, reflectionFields);
return true;
try {
if (returnTypeMirror.getKind() == TypeKind.ARRAY && !types.isSameType(returnTypeMirror, byteArrayType) && eeMethod.getAnnotation(JdbcMapper.SingleRow.class) == null) {
final TypeMirror componentType = ((ArrayType) returnTypeMirror).getComponentType();
toArray(w, keys, componentType, maxRows, cal, cleaner, reflectionFields);
} else if (types.isAssignable(returnTypeMirror, collectionType)) {
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
toCollection(w, keys, returnTypeMirror, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
} else if (types.isAssignable(returnTypeMirror, mapType) && eeMethod.getAnnotation(JdbcMapper.SingleRow.class) == null) {
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
if (types.isAssignable(returnTypeMirror, mapCollectionType)) { // for collectionMaps
final TypeMirror collectionTypeMirror = typeArguments.get(1);
final TypeMirror componentTypeMirror = ((DeclaredType) collectionTypeMirror).getTypeArguments().get(0);
toMapCollection(w, keys,
returnTypeMirror,
typeArguments.get(0),
collectionTypeMirror,
componentTypeMirror,
maxRows, cal, cleaner, reflectionFields);
return true;
}
toMap(w, keys, returnTypeMirror, typeArguments.get(0), typeArguments.get(1), maxRows, cal, cleaner, reflectionFields);
} else if (types.isAssignable(returnTypeMirror, iteratorType)) {
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
if (types.isAssignable(returnTypeMirror, resultSetIterableType)) {
toResultSetIterable(w, keys, typeArguments.get(0), cal, cleaner, closePs, reflectionFields);
return false;
} else if (types.isAssignable(returnTypeMirror, listIteratorType))
toListIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
else
toIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
}
toMap(w, keys, returnTypeMirror, typeArguments.get(0), typeArguments.get(1), maxRows, cal, cleaner, reflectionFields);
} else if (types.isAssignable(returnTypeMirror, iteratorType)) {
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
if (types.isAssignable(returnTypeMirror, resultSetIterableType)) {
toResultSetIterable(w, keys, typeArguments.get(0), cal, cleaner, closePs, reflectionFields);
//IFJAVA8_START
else if (types.isAssignable(returnTypeMirror, streamType)) {
toStream(w, keys, ((DeclaredType) returnTypeMirror).getTypeArguments().get(0), cal, cleaner, closePs, reflectionFields);
return false;
} else if (types.isAssignable(returnTypeMirror, listIteratorType))
toListIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
else
toIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
}
//IFJAVA8_START
else if (types.isAssignable(returnTypeMirror, streamType)) {
toStream(w, keys, ((DeclaredType) returnTypeMirror).getTypeArguments().get(0), cal, cleaner, closePs, reflectionFields);
return false;
}
//IFJAVA8_END
else if(types.isAssignable(returnTypeMirror, resultSetType)) {
toResultSet(w, closePs);
return false;
} else {
toObject(w, keys, returnTypeMirror, cal, cleaner, reflectionFields);
}
//IFJAVA8_END
else if (types.isAssignable(returnTypeMirror, resultSetType)) {
toResultSet(w, closePs);
return false;
} else {
toObject(w, keys, returnTypeMirror, cal, cleaner, reflectionFields);
}
} catch(MapperException e) {
JdbcMapperProcessor.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage(), eeMethod);
}
return true;
}

View File

@ -91,7 +91,7 @@ public class CompileTimeRowToObjectMapper {
// but javac 1.8 prints this with -parameters (wrongly):
// methodsAndConstructors: FieldPerson(): '', FieldPerson(long,java.util.Date,java.lang.String,java.lang.String): 'long personNo, java.util.Date firstName, java.lang.String lastName, java.lang.String arg3', FieldPerson(com.moparisthebest.jdbc.dto.Person): 'com.moparisthebest.jdbc.dto.Person person'
if(_returnTypeClass.toString().equals("com.moparisthebest.jdbc.dto.FieldPerson"))
throw new RuntimeException("methodsAndConstructors: " + methodsAndConstructors.stream().filter(e -> e.getKind() == ElementKind.CONSTRUCTOR && e.getModifiers().contains(Modifier.PUBLIC)).map(e -> e.toString() +
throw new MapperException("methodsAndConstructors: " + methodsAndConstructors.stream().filter(e -> e.getKind() == ElementKind.CONSTRUCTOR && e.getModifiers().contains(Modifier.PUBLIC)).map(e -> e.toString() +
": '" + ((ExecutableElement)e).getParameters().stream().map(param -> param.asType() + " " + param.getSimpleName().toString()).collect(java.util.stream.Collectors.joining(", ")) + "'"
).collect(java.util.stream.Collectors.joining(", ")));
*/
@ -136,7 +136,7 @@ public class CompileTimeRowToObjectMapper {
_fieldOrder = null; // didn't successfully finish
this.resultSetConstructor = resultSetConstructor;
if(!resultSetConstructor && !defaultConstructor && !paramConstructor && _columnCount > 2 && componentType == null)
throw new RuntimeException("Exception when trying to get constructor for : "+_returnTypeClass.toString() + " Must have default no-arg constructor or one that takes a single ResultSet.");
throw new MapperException("Exception when trying to get constructor for : "+_returnTypeClass.toString() + " Must have default no-arg constructor or one that takes a single ResultSet.");
}
}
@ -156,7 +156,7 @@ public class CompileTimeRowToObjectMapper {
protected void getFieldMappings() {
if(_returnTypeClass.getKind() != TypeKind.DECLARED)
throw new RuntimeException("_returnTypeClass " + _returnTypeClass + " not TypeKind.DECLARED ?? how??");
throw new MapperException("_returnTypeClass " + _returnTypeClass + " not TypeKind.DECLARED ?? how??");
final DeclaredType declaredReturnType = (DeclaredType)_returnTypeClass;

View File

@ -1,6 +1,7 @@
package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.Cleaner;
import com.moparisthebest.jdbc.MapperException;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
@ -703,7 +704,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
case DOUBLE:
return double[].class;
case ARRAY:
throw new RuntimeException("multi-dimensional arrays are not supported");
throw new MapperException("multi-dimensional arrays are not supported");
default:
return Class.forName("[L" + arrayComponentType.toString() + ";");
}