From a22e8d6d580ffe4a66f0a159a2487421a0d76ce2 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Wed, 8 Nov 2017 23:31:28 -0500 Subject: [PATCH] Print proper errors with links to methods instead of throwing exceptions --- .../codegen/CompileTimeResultSetMapper.java | 86 ++++++++++--------- .../codegen/CompileTimeRowToObjectMapper.java | 6 +- .../jdbc/codegen/JdbcMapperProcessor.java | 3 +- 3 files changed, 51 insertions(+), 44 deletions(-) diff --git a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java index 273d6e5..7c9b5e6 100644 --- a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java +++ b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java @@ -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 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 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 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 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 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 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; } diff --git a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeRowToObjectMapper.java b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeRowToObjectMapper.java index f62b632..e1d1f74 100644 --- a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeRowToObjectMapper.java +++ b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeRowToObjectMapper.java @@ -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; diff --git a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java index 740e8c6..d2012e6 100644 --- a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java +++ b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java @@ -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() + ";"); }