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

View File

@ -91,7 +91,7 @@ public class CompileTimeRowToObjectMapper {
// but javac 1.8 prints this with -parameters (wrongly): // 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' // 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")) 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(", ")) + "'" ": '" + ((ExecutableElement)e).getParameters().stream().map(param -> param.asType() + " " + param.getSimpleName().toString()).collect(java.util.stream.Collectors.joining(", ")) + "'"
).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 _fieldOrder = null; // didn't successfully finish
this.resultSetConstructor = resultSetConstructor; this.resultSetConstructor = resultSetConstructor;
if(!resultSetConstructor && !defaultConstructor && !paramConstructor && _columnCount > 2 && componentType == null) 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() { protected void getFieldMappings() {
if(_returnTypeClass.getKind() != TypeKind.DECLARED) 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; final DeclaredType declaredReturnType = (DeclaredType)_returnTypeClass;

View File

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