Look at all implemented classes for methods+fields in JdbcMapperProcessor
This commit is contained in:
parent
b2c5235a81
commit
f58e107ff5
|
@ -140,6 +140,14 @@ public class CompileTimeRowToObjectMapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<DeclaredType> getAllImplementedTypes(final DeclaredType type, final List<DeclaredType> ret) {
|
||||||
|
ret.add(type);
|
||||||
|
for(final TypeMirror tm : JdbcMapperProcessor.getTypes().directSupertypes(type))
|
||||||
|
if(tm.getKind() == TypeKind.DECLARED && !tm.toString().equals("java.lang.Object"))
|
||||||
|
getAllImplementedTypes((DeclaredType) tm, ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the structures necessary to do the mapping
|
* Build the structures necessary to do the mapping
|
||||||
*
|
*
|
||||||
|
@ -172,49 +180,52 @@ public class CompileTimeRowToObjectMapper {
|
||||||
mapFields.put(keys[i], null);
|
mapFields.put(keys[i], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public methods todo: does this do super methods too?
|
final List<DeclaredType> allTypes = getAllImplementedTypes(declaredReturnType, new ArrayList<>());
|
||||||
for (Element e : ((TypeElement)declaredReturnType.asElement()).getEnclosedElements()) {
|
|
||||||
if(e.getKind() != ElementKind.METHOD)
|
// public methods
|
||||||
continue;
|
// have to loop to get super methods too
|
||||||
final ExecutableElement m = (ExecutableElement)e;
|
for (final DeclaredType clazz : allTypes) {
|
||||||
//System.out.printf("method: '%s', isSetterMethod: '%s'\n", m, isSetterMethod(m));
|
for (Element e : ((TypeElement) clazz.asElement()).getEnclosedElements()) {
|
||||||
if (isSetterMethod(m)) {
|
if (e.getKind() != ElementKind.METHOD)
|
||||||
String fieldName = m.getSimpleName().toString().substring(3).toUpperCase();
|
continue;
|
||||||
//System.out.println("METHOD-fieldName1: "+fieldName);
|
final ExecutableElement m = (ExecutableElement) e;
|
||||||
if (!mapFields.containsKey(fieldName)) {
|
//System.out.printf("method: '%s', isSetterMethod: '%s'\n", m, isSetterMethod(m));
|
||||||
fieldName = strippedKeys.get(fieldName);
|
if (isSetterMethod(m)) {
|
||||||
if (fieldName == null)
|
String fieldName = m.getSimpleName().toString().substring(3).toUpperCase();
|
||||||
continue;
|
//System.out.println("METHOD-fieldName1: "+fieldName);
|
||||||
//System.out.println("METHOD-fieldName2: "+fieldName);
|
if (!mapFields.containsKey(fieldName)) {
|
||||||
}
|
fieldName = strippedKeys.get(fieldName);
|
||||||
final Element field = mapFields.get(fieldName);
|
if (fieldName == null)
|
||||||
// check for overloads
|
continue;
|
||||||
if (field == null) {
|
//System.out.println("METHOD-fieldName2: "+fieldName);
|
||||||
mapFields.put(fieldName, m);
|
}
|
||||||
} else {
|
final Element field = mapFields.get(fieldName);
|
||||||
// todo: does this work?
|
// check for overloads
|
||||||
// fix for 'overloaded' methods when it comes to stripped keys, we want the exact match
|
if (field == null) {
|
||||||
final String thisName = m.getSimpleName().toString().substring(3).toUpperCase();
|
mapFields.put(fieldName, m);
|
||||||
final String previousName = field.getSimpleName().toString().substring(3).toUpperCase();
|
} else {
|
||||||
//System.out.printf("thisName: '%s', previousName: '%s', mapFields.containsKey(thisName): %b, strippedKeys.containsKey(previousName): %b\n", thisName, previousName, mapFields.containsKey(thisName), strippedKeys.containsKey(previousName));
|
// todo: does this work?
|
||||||
if(mapFields.containsKey(thisName) && strippedKeys.containsKey(previousName)) {
|
// fix for 'overloaded' methods when it comes to stripped keys, we want the exact match
|
||||||
mapFields.put(fieldName, m);
|
final String thisName = m.getSimpleName().toString().substring(3).toUpperCase();
|
||||||
} else if (!mapFields.containsKey(previousName) || !strippedKeys.containsKey(thisName)) {
|
final String previousName = field.getSimpleName().toString().substring(3).toUpperCase();
|
||||||
throw new MapperException("Unable to choose between overloaded methods '" + m.getSimpleName().toString()
|
//System.out.printf("thisName: '%s', previousName: '%s', mapFields.containsKey(thisName): %b, strippedKeys.containsKey(previousName): %b\n", thisName, previousName, mapFields.containsKey(thisName), strippedKeys.containsKey(previousName));
|
||||||
+ "' and '" + field.getSimpleName().toString() + "' for field '" + fieldName + "' on the '" + _returnTypeClass.toString() + "' class. Mapping is done using "
|
if (mapFields.containsKey(thisName) && strippedKeys.containsKey(previousName)) {
|
||||||
+ "a case insensitive comparison of SQL ResultSet columns to field "
|
mapFields.put(fieldName, m);
|
||||||
+ "names and public setter methods on the return class. Columns are also "
|
} else if (!mapFields.containsKey(previousName) || !strippedKeys.containsKey(thisName)) {
|
||||||
+ "stripped of '_' and compared if no match is found with them.");
|
throw new MapperException("Unable to choose between overloaded methods '" + m.getSimpleName().toString()
|
||||||
|
+ "' and '" + field.getSimpleName().toString() + "' for field '" + fieldName + "' on the '" + _returnTypeClass.toString() + "' class. Mapping is done using "
|
||||||
|
+ "a case insensitive comparison of SQL ResultSet columns to field "
|
||||||
|
+ "names and public setter methods on the return class. Columns are also "
|
||||||
|
+ "stripped of '_' and compared if no match is found with them.");
|
||||||
|
}
|
||||||
|
// then the 'overloaded' method is already correct
|
||||||
}
|
}
|
||||||
// then the 'overloaded' method is already correct
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fix for 8813: include inherited and non-public fields
|
// fix for 8813: include inherited and non-public fields
|
||||||
for (DeclaredType clazz = declaredReturnType; clazz != null && !clazz.toString().equals("java.lang.Object");
|
for (final DeclaredType clazz : allTypes) {
|
||||||
clazz = clazz.getEnclosingType().getKind() == TypeKind.DECLARED ? (DeclaredType)clazz.getEnclosingType() : null
|
|
||||||
) {
|
|
||||||
//System.out.println("fields in class: "+Arrays.toString(classFields));
|
//System.out.println("fields in class: "+Arrays.toString(classFields));
|
||||||
for (Element e : ((TypeElement)clazz.asElement()).getEnclosedElements()) {
|
for (Element e : ((TypeElement)clazz.asElement()).getEnclosedElements()) {
|
||||||
if(e.getKind() != ElementKind.FIELD)
|
if(e.getKind() != ElementKind.FIELD)
|
||||||
|
|
|
@ -32,7 +32,17 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
private static final Pattern paramPattern = Pattern.compile("\\{(([^\\s]+)\\s+(([Nn][Oo][Tt]\\s+)?[Ii][Nn]\\s+))?([^}]+)\\}");
|
private static final Pattern paramPattern = Pattern.compile("\\{(([^\\s]+)\\s+(([Nn][Oo][Tt]\\s+)?[Ii][Nn]\\s+))?([^}]+)\\}");
|
||||||
|
|
||||||
private Types types;
|
private static Types types;
|
||||||
|
private static Messager messager;
|
||||||
|
|
||||||
|
public static Types getTypes() {
|
||||||
|
return types;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Messager getMessager() {
|
||||||
|
return messager;
|
||||||
|
}
|
||||||
|
|
||||||
private TypeMirror sqlExceptionType, stringType, numberType, utilDateType, readerType, clobType,
|
private TypeMirror sqlExceptionType, stringType, numberType, utilDateType, readerType, clobType,
|
||||||
byteArrayType, inputStreamType, fileType, blobType, sqlArrayType, collectionType, calendarType, cleanerType;
|
byteArrayType, inputStreamType, fileType, blobType, sqlArrayType, collectionType, calendarType, cleanerType;
|
||||||
//IFJAVA8_START
|
//IFJAVA8_START
|
||||||
|
@ -51,7 +61,8 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
||||||
@Override
|
@Override
|
||||||
public synchronized void init(final ProcessingEnvironment processingEnv) {
|
public synchronized void init(final ProcessingEnvironment processingEnv) {
|
||||||
super.init(processingEnv);
|
super.init(processingEnv);
|
||||||
this.types = processingEnv.getTypeUtils();
|
types = processingEnv.getTypeUtils();
|
||||||
|
messager = processingEnv.getMessager();
|
||||||
final Elements elements = processingEnv.getElementUtils();
|
final Elements elements = processingEnv.getElementUtils();
|
||||||
sqlExceptionType = elements.getTypeElement(SQLException.class.getCanonicalName()).asType();
|
sqlExceptionType = elements.getTypeElement(SQLException.class.getCanonicalName()).asType();
|
||||||
stringType = elements.getTypeElement(String.class.getCanonicalName()).asType();
|
stringType = elements.getTypeElement(String.class.getCanonicalName()).asType();
|
||||||
|
|
Loading…
Reference in New Issue