Skip to content

Commit

Permalink
[#6787] Inefficient ProxyMapper allocates a new MutablePOJOMapper del…
Browse files Browse the repository at this point in the history
…egate every time
  • Loading branch information
lukaseder committed Nov 7, 2017
1 parent 6cf7f41 commit 692d833
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions jOOQ/src/main/java/org/jooq/impl/DefaultRecordMapper.java
Expand Up @@ -66,6 +66,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.stream.Stream;

import javax.persistence.Column;
Expand Down Expand Up @@ -315,7 +316,7 @@ private final void init(E instance) {

// [#1340] Allow for using non-public default constructors
try {
delegate = new MutablePOJOMapper(type.getDeclaredConstructor(), instance);
delegate = new MutablePOJOMapper(new ConstructorCall<E>(accessible(type.getDeclaredConstructor())), instance);
return;
}
catch (NoSuchMethodException ignore) {}
Expand Down Expand Up @@ -428,11 +429,21 @@ public final E map(R record) {
*/
private class ProxyMapper implements RecordMapper<R, E> {

Constructor<Lookup> constructor;
private Constructor<Lookup> constructor;
private final MutablePOJOMapper pojomapper;

ProxyMapper() {
this.pojomapper = new MutablePOJOMapper(new Callable<E>() {
@Override
public E call() throws Exception {
return proxy();
}
}, null);
}

@Override
public final E map(R record) {
return new MutablePOJOMapper(null, proxy()).map(record);
return pojomapper.map(record);
}

private E proxy() {
Expand Down Expand Up @@ -533,6 +544,19 @@ public Object map(R record) {
}
}

private static final class ConstructorCall<E> implements Callable<E> {
private final Constructor<? extends E> constructor;

ConstructorCall(Constructor<? extends E> constructor) {
this.constructor = constructor;
}

@Override
public E call() throws Exception {
return constructor.newInstance();
}
}

/**
* Convert a record into a mutable POJO type
* <p>
Expand All @@ -541,15 +565,15 @@ public Object map(R record) {
*/
private class MutablePOJOMapper implements RecordMapper<R, E> {

private final Constructor<? extends E> constructor;
private final Callable<E> constructor;
private final boolean useAnnotations;
private final List<java.lang.reflect.Field>[] members;
private final List<java.lang.reflect.Method>[] methods;
private final Map<String, List<RecordMapper<R, Object>>> nested;
private final E instance;

MutablePOJOMapper(Constructor<? extends E> constructor, E instance) {
this.constructor = accessible(constructor);
MutablePOJOMapper(Callable<E> constructor, E instance) {
this.constructor = constructor;
this.useAnnotations = hasColumnAnnotations(configuration, type);
this.members = new List[fields.length];
this.methods = new List[fields.length];
Expand Down Expand Up @@ -629,7 +653,7 @@ private class MutablePOJOMapper implements RecordMapper<R, E> {
@Override
public final E map(R record) {
try {
E result = instance != null ? instance : constructor.newInstance();
E result = instance != null ? instance : constructor.call();

for (int i = 0; i < fields.length; i++) {
for (java.lang.reflect.Field member : members[i]) {
Expand Down

0 comments on commit 692d833

Please sign in to comment.