mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-11-22 17:12:16 -05:00
Half measure to avoid using Class objects in CompileTimeResultSetMapper
This commit is contained in:
parent
be6689aa29
commit
a1ad929cf4
@ -3,10 +3,14 @@ package com.moparisthebest.jdbc.codegen;
|
|||||||
import com.moparisthebest.jdbc.CompilingRowToObjectMapper;
|
import com.moparisthebest.jdbc.CompilingRowToObjectMapper;
|
||||||
import com.moparisthebest.jdbc.ResultSetMapper;
|
import com.moparisthebest.jdbc.ResultSetMapper;
|
||||||
|
|
||||||
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.lang.model.type.ArrayType;
|
import javax.lang.model.type.ArrayType;
|
||||||
import javax.lang.model.type.DeclaredType;
|
import javax.lang.model.type.DeclaredType;
|
||||||
|
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.Types;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@ -19,48 +23,64 @@ import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToCl
|
|||||||
*/
|
*/
|
||||||
public class CompileTimeResultSetMapper {
|
public class CompileTimeResultSetMapper {
|
||||||
|
|
||||||
|
private final Types types;
|
||||||
|
private final TypeMirror collectionType, mapType, mapCollectionType, iteratorType, listIteratorType;
|
||||||
|
|
||||||
|
public CompileTimeResultSetMapper(final ProcessingEnvironment processingEnv) {
|
||||||
|
types = processingEnv.getTypeUtils();
|
||||||
|
final Elements elements = processingEnv.getElementUtils();
|
||||||
|
|
||||||
|
collectionType = types.getDeclaredType(elements.getTypeElement(Collection.class.getCanonicalName()), types.getWildcardType(null, null));
|
||||||
|
mapType = types.getDeclaredType(elements.getTypeElement(Map.class.getCanonicalName()), types.getWildcardType(null, null), types.getWildcardType(null, null));
|
||||||
|
mapCollectionType = types.getDeclaredType(elements.getTypeElement(Map.class.getCanonicalName()), types.getWildcardType(null, null), types.getWildcardType(collectionType, null));
|
||||||
|
|
||||||
|
iteratorType = types.getDeclaredType(elements.getTypeElement(Iterator.class.getCanonicalName()), types.getWildcardType(null, null));
|
||||||
|
listIteratorType = types.getDeclaredType(elements.getTypeElement(ListIterator.class.getCanonicalName()), types.getWildcardType(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked"})
|
@SuppressWarnings({"unchecked"})
|
||||||
public void mapToResultType(final Writer w, final String[] keys, final ExecutableElement eeMethod, final int arrayMaxLength, final Calendar cal) throws IOException, NoSuchMethodException, ClassNotFoundException {
|
public void mapToResultType(final Writer w, final String[] keys, final ExecutableElement eeMethod, final int arrayMaxLength, final Calendar cal) 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 (returnType.isArray()) {
|
if (returnTypeMirror.getKind() == TypeKind.ARRAY) {
|
||||||
toArray(w, keys, ((ArrayType) returnTypeMirror).getComponentType(), returnType.getComponentType(), arrayMaxLength, cal);
|
final TypeMirror componentType = ((ArrayType) returnTypeMirror).getComponentType();
|
||||||
} else if (Collection.class.isAssignableFrom(returnType)) {
|
toArray(w, keys, componentType, typeMirrorToClass(componentType), arrayMaxLength, cal);
|
||||||
|
} 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, returnType, typeArguments.get(0), (Class) getActualTypeArguments(typeArguments)[0], arrayMaxLength, cal);
|
toCollection(w, keys, returnTypeMirror, (Class<? extends Collection>)typeMirrorToClass(returnTypeMirror), typeArguments.get(0), (Class) getActualTypeArguments(typeArguments)[0], arrayMaxLength, cal);
|
||||||
} else if (Map.class.isAssignableFrom(returnType)) {
|
} else if (types.isAssignable(returnTypeMirror, mapType)) {
|
||||||
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
||||||
final Type[] types = getActualTypeArguments(typeArguments);
|
final Type[] typeArgs = getActualTypeArguments(typeArguments);
|
||||||
final TypeMirror collectionTypeMirror = typeArguments.get(1);
|
|
||||||
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
|
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
|
||||||
if (!((DeclaredType) collectionTypeMirror).getTypeArguments().isEmpty()) { // for collectionMaps
|
if (types.isAssignable(returnTypeMirror, mapCollectionType)) { // for collectionMaps
|
||||||
//final ParameterizedType pt = (ParameterizedType) types[1];
|
//final ParameterizedType pt = (ParameterizedType) types[1];
|
||||||
//final Class collectionType = (Class) pt.getRawType();
|
//final Class collectionType = (Class) pt.getRawType();
|
||||||
|
final TypeMirror collectionTypeMirror = typeArguments.get(1);
|
||||||
final Class collectionType = typeMirrorToClass(collectionTypeMirror);
|
final Class collectionType = typeMirrorToClass(collectionTypeMirror);
|
||||||
if (Collection.class.isAssignableFrom(collectionType)) {
|
if (Collection.class.isAssignableFrom(collectionType)) {
|
||||||
final TypeMirror componentTypeMirror = ((DeclaredType) collectionTypeMirror).getTypeArguments().get(0);
|
final TypeMirror componentTypeMirror = ((DeclaredType) collectionTypeMirror).getTypeArguments().get(0);
|
||||||
//final Class componentType = (Class) pt.getActualTypeArguments()[0];
|
//final Class componentType = (Class) pt.getActualTypeArguments()[0];
|
||||||
final Class componentType = typeMirrorToClass(componentTypeMirror);
|
final Class componentType = typeMirrorToClass(componentTypeMirror);
|
||||||
toMapCollection(w, keys,
|
toMapCollection(w, keys,
|
||||||
returnTypeMirror, returnType,
|
returnTypeMirror, (Class<? extends Map>)typeMirrorToClass(returnTypeMirror),
|
||||||
typeArguments.get(0), (Class) types[0],
|
typeArguments.get(0), (Class) typeArgs[0],
|
||||||
collectionTypeMirror, collectionType,
|
collectionTypeMirror, collectionType,
|
||||||
componentTypeMirror, componentType,
|
componentTypeMirror, componentType,
|
||||||
arrayMaxLength, cal);
|
arrayMaxLength, cal);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
toMap(w, keys, returnTypeMirror, returnType, typeArguments.get(0), (Class) types[0], typeArguments.get(1), (Class) types[1], arrayMaxLength, cal);
|
toMap(w, keys, returnTypeMirror, (Class<? extends Map>)typeMirrorToClass(returnTypeMirror), typeArguments.get(0), (Class) typeArgs[0], typeArguments.get(1), (Class) typeArgs[1], arrayMaxLength, cal);
|
||||||
} else if (Iterator.class.isAssignableFrom(returnType)) {
|
} else if (types.isAssignable(returnTypeMirror, iteratorType)) {
|
||||||
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
||||||
if (ListIterator.class.isAssignableFrom(returnType))
|
if (types.isAssignable(returnTypeMirror, listIteratorType))
|
||||||
toListIterator(w, keys, typeArguments.get(0), (Class) getActualTypeArguments(typeArguments)[0], arrayMaxLength, cal);
|
toListIterator(w, keys, typeArguments.get(0), (Class) getActualTypeArguments(typeArguments)[0], arrayMaxLength, cal);
|
||||||
else
|
else
|
||||||
toIterator(w, keys, typeArguments.get(0), (Class) getActualTypeArguments(typeArguments)[0], arrayMaxLength, cal);
|
toIterator(w, keys, typeArguments.get(0), (Class) getActualTypeArguments(typeArguments)[0], arrayMaxLength, cal);
|
||||||
} else {
|
} else {
|
||||||
toObject(w, keys, returnTypeMirror, returnType, cal);
|
toObject(w, keys, returnTypeMirror, typeMirrorToClass(returnTypeMirror), cal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,13 +26,13 @@ import static com.moparisthebest.jdbc.TryClose.tryClose;
|
|||||||
public class JdbcMapperProcessor extends AbstractProcessor {
|
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 static final CompileTimeResultSetMapper rsm = new CompileTimeResultSetMapper();
|
|
||||||
|
|
||||||
private Types types;
|
private Types types;
|
||||||
private TypeMirror sqlExceptionType, stringType, numberType, utilDateType, readerType, clobType,
|
private TypeMirror sqlExceptionType, stringType, numberType, utilDateType, readerType, clobType,
|
||||||
byteArrayType, inputStreamType, fileType, blobType, sqlArrayType, collectionType;
|
byteArrayType, inputStreamType, fileType, blobType, sqlArrayType, collectionType;
|
||||||
private JdbcMapper.DatabaseType defaultDatabaseType;
|
private JdbcMapper.DatabaseType defaultDatabaseType;
|
||||||
private String defaultArrayNumberTypeName, defaultArrayStringTypeName;
|
private String defaultArrayNumberTypeName, defaultArrayStringTypeName;
|
||||||
|
private CompileTimeResultSetMapper rsm;
|
||||||
|
|
||||||
public JdbcMapperProcessor() {
|
public JdbcMapperProcessor() {
|
||||||
//out.println("JdbcMapperProcessor running!");
|
//out.println("JdbcMapperProcessor running!");
|
||||||
@ -67,6 +67,8 @@ public class JdbcMapperProcessor extends AbstractProcessor {
|
|||||||
defaultArrayStringTypeName = processingEnv.getOptions().get("JdbcMapper.arrayStringTypeName");
|
defaultArrayStringTypeName = processingEnv.getOptions().get("JdbcMapper.arrayStringTypeName");
|
||||||
if (defaultArrayStringTypeName == null || defaultArrayStringTypeName.isEmpty())
|
if (defaultArrayStringTypeName == null || defaultArrayStringTypeName.isEmpty())
|
||||||
defaultArrayStringTypeName = defaultDatabaseType.arrayStringTypeName;
|
defaultArrayStringTypeName = defaultDatabaseType.arrayStringTypeName;
|
||||||
|
|
||||||
|
rsm = new CompileTimeResultSetMapper(processingEnv);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user