Fixed issues with getting generic parameter names to support lambda

expressions
This commit is contained in:
nathan 2018-01-22 15:26:15 +01:00
parent ffc2375fe8
commit f8de99df31
2 changed files with 18 additions and 24 deletions

View File

@ -32,6 +32,7 @@ import dorkbox.network.connection.listenerManagement.OnIdleManager;
import dorkbox.network.connection.listenerManagement.OnMessageReceivedManager; import dorkbox.network.connection.listenerManagement.OnMessageReceivedManager;
import dorkbox.util.ClassHelper; import dorkbox.util.ClassHelper;
import dorkbox.util.Property; import dorkbox.util.Property;
import dorkbox.util.TypeResolver;
import dorkbox.util.collections.ConcurrentEntry; import dorkbox.util.collections.ConcurrentEntry;
// .equals() compares the identity on purpose,this because we cannot create two separate objects that are somehow equal to each other. // .equals() compares the identity on purpose,this because we cannot create two separate objects that are somehow equal to each other.
@ -124,23 +125,12 @@ class ConnectionManager<C extends Connection> implements Listeners, ISessionMana
throw new IllegalArgumentException("listener cannot be null."); throw new IllegalArgumentException("listener cannot be null.");
} }
// find the class that uses Listener.class. // this is the connection generic parameter for the listener, works for lambda expressions as well
Class<?> clazz = listener.getClass(); Class<?> genericClass = ClassHelper.getGenericParameterAsClassForSuperClass(Listener.class, listener.getClass(), 0);
// Class<?>[] interfaces = clazz.getInterfaces();
// for (Class<?> anInterface : interfaces) {
// }
//
// while (!(clazz.getSuperclass() != Object.class)) {
// clazz = clazz.getSuperclass();
// }
// this is the connection generic parameter for the listener
Class<?> genericClass = ClassHelper.getGenericParameterAsClassForSuperClass(clazz, 0);
// if we are null, it means that we have no generics specified for our listener! // if we are null, it means that we have no generics specified for our listener!
//noinspection IfStatementWithIdenticalBranches //noinspection IfStatementWithIdenticalBranches
if (genericClass == this.baseClass || genericClass == null) { if (genericClass == this.baseClass || genericClass == TypeResolver.Unknown.class || genericClass == null) {
// we are the base class, so we are fine. // we are the base class, so we are fine.
addListener0(listener); addListener0(listener);
return this; return this;

View File

@ -15,9 +15,19 @@
*/ */
package dorkbox.network.connection.listenerManagement; package dorkbox.network.connection.listenerManagement;
import static dorkbox.util.collections.ConcurrentIterator.headREF;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.slf4j.Logger;
import com.esotericsoftware.kryo.util.IdentityMap; import com.esotericsoftware.kryo.util.IdentityMap;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionManager; import dorkbox.network.connection.ConnectionManager;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.Listener.OnError; import dorkbox.network.connection.Listener.OnError;
import dorkbox.network.connection.Listener.OnMessageReceived; import dorkbox.network.connection.Listener.OnMessageReceived;
import dorkbox.network.connection.Listener.SelfDefinedType; import dorkbox.network.connection.Listener.SelfDefinedType;
@ -25,13 +35,6 @@ import dorkbox.network.rmi.RmiMessages;
import dorkbox.util.ClassHelper; import dorkbox.util.ClassHelper;
import dorkbox.util.collections.ConcurrentEntry; import dorkbox.util.collections.ConcurrentEntry;
import dorkbox.util.collections.ConcurrentIterator; import dorkbox.util.collections.ConcurrentIterator;
import org.slf4j.Logger;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import static dorkbox.util.collections.ConcurrentIterator.headREF;
/** /**
* Called when the remote end has been connected. This will be invoked before any objects are received by the network. * Called when the remote end has been connected. This will be invoked before any objects are received by the network.
@ -297,14 +300,15 @@ class OnMessageReceivedManager<C extends Connection> {
* This works for compile time code. The generic type parameter #2 (index 1) is pulled from type arguments. * This works for compile time code. The generic type parameter #2 (index 1) is pulled from type arguments.
* generic parameters cannot be primitive types * generic parameters cannot be primitive types
*/ */
private static Class<?> identifyType(final Object listener) { private static
Class<?> identifyType(final OnMessageReceived listener) {
final Class<?> clazz = listener.getClass(); final Class<?> clazz = listener.getClass();
Class<?> objectType = ClassHelper.getGenericParameterAsClassForSuperClass(clazz, 1); Class<?> objectType = ClassHelper.getGenericParameterAsClassForSuperClass(Listener.OnMessageReceived.class, clazz, 1);
if (objectType != null) { if (objectType != null) {
// SOMETIMES generics get confused on which parameter we actually mean (when sub-classing) // SOMETIMES generics get confused on which parameter we actually mean (when sub-classing)
if (objectType != Object.class && ClassHelper.hasInterface(Connection.class, objectType)) { if (objectType != Object.class && ClassHelper.hasInterface(Connection.class, objectType)) {
Class<?> objectType2 = ClassHelper.getGenericParameterAsClassForSuperClass(clazz, 2); Class<?> objectType2 = ClassHelper.getGenericParameterAsClassForSuperClass(Listener.OnMessageReceived.class, clazz, 2);
if (objectType2 != null) { if (objectType2 != null) {
objectType = objectType2; objectType = objectType2;
} }