11package org .tensorics .core .tensorbacked ;
22
3- import org .tensorics .core .tensor .Tensor ;
4- import org .tensorics .core .util .JavaVersions ;
3+ import static java .util .Objects .requireNonNull ;
54
65import java .lang .invoke .MethodHandle ;
76import java .lang .invoke .MethodHandles ;
87import java .lang .invoke .MethodType ;
9- import java .lang .reflect .*;
8+ import java .lang .reflect .Constructor ;
9+ import java .lang .reflect .InvocationHandler ;
10+ import java .lang .reflect .InvocationTargetException ;
11+ import java .lang .reflect .Method ;
12+ import java .lang .reflect .Proxy ;
13+ import java .util .Objects ;
1014
11- import static java .util .Objects .requireNonNull ;
15+ import org .tensorics .core .tensor .Tensor ;
16+ import org .tensorics .core .util .JavaVersions ;
1217
1318public final class ProxiedInterfaceTensorbackeds {
1419
@@ -21,7 +26,7 @@ public static <V, T extends Tensorbacked<V>> T create(Class<T> tensorbackedType,
2126 throw new IllegalArgumentException ("The given tensorbacked type '" + tensorbackedType
2227 + "' is not an interface! Therefore, it cannot be instantiated by proxying!" );
2328 }
24- return (T ) Proxy .newProxyInstance (Tensorbacked .class .getClassLoader (), new Class <?>[]{ tensorbackedType },
29+ return (T ) Proxy .newProxyInstance (Tensorbacked .class .getClassLoader (), new Class <?>[] { tensorbackedType },
2530 new DelegatingInvocationHandler <>(tensor , tensorbackedType ));
2631 }
2732
@@ -47,16 +52,50 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
4752 return delegate ;
4853 }
4954
55+ if ("toString" .equals (method .getName ()) && (args == null )) {
56+ return toString ();
57+ }
58+ if ("hashCode" .equals (method .getName ()) && (args == null )) {
59+ return hashCode ();
60+ }
61+ if ("equals" .equals (method .getName ()) && (args .length == 1 )) {
62+ return equals (args [0 ]);
63+ }
64+
5065 throw new UnsupportedOperationException ("The method " + method + " is not supported by the proxy!" );
5166 }
67+
68+ @ Override
69+ public int hashCode () {
70+ return Objects .hash (delegate , intfc );
71+ }
72+
73+ @ Override
74+ public boolean equals (Object obj ) {
75+ if (this == obj ) {
76+ return true ;
77+ }
78+ if (obj == null ) {
79+ return false ;
80+ }
81+ if (getClass () != obj .getClass ()) {
82+ return false ;
83+ }
84+ DelegatingInvocationHandler other = (DelegatingInvocationHandler ) obj ;
85+ return Objects .equals (delegate , other .delegate ) && Objects .equals (intfc , other .intfc );
86+ }
87+
88+ @ Override
89+ public String toString () {
90+ return "Tensorbacked [intfc=" + intfc + ", delegate=" + delegate + "]" ;
91+ }
5292 }
5393
5494 @ FunctionalInterface
5595 private interface MethodCallHandler {
5696 Object invoke (Object proxy , Object [] args ) throws Throwable ;
5797 }
5898
59-
6099 private static MethodCallHandler callHandlerFor (Class <?> intfc , Method method ) {
61100 MethodHandle handle = getMethodHandle (intfc , method );
62101 return (proxy , args ) -> handle .bindTo (proxy ).invokeWithArguments (args );
@@ -65,16 +104,15 @@ private static MethodCallHandler callHandlerFor(Class<?> intfc, Method method) {
65104 private static MethodHandle getMethodHandle (Class <?> intfc , Method method ) {
66105 Class <?> declaringClass = method .getDeclaringClass ();
67106
68- /*
69- XXX: This is not too nice: For some reason, the original code (java) 8 did not run in java 11 anymore.
70- And vice versa ... what the exact border is where it stopped working was not full checked.
71- So potentially more research would be needed here.
72- */
107+ /*
108+ * XXX: This is not too nice: For some reason, the original code (java) 8 did not run in java 11 anymore.
109+ * And vice versa ... what the exact border is where it stopped working was not full checked.
110+ * So potentially more research would be needed here.
111+ */
73112 if (JavaVersions .isAtLeastJava11 ()) {
74113 try {
75- return MethodHandles .lookup ()
76- .findSpecial (intfc , method .getName (),
77- MethodType .methodType (method .getReturnType (), method .getParameterTypes ()), intfc );
114+ return MethodHandles .lookup ().findSpecial (intfc , method .getName (),
115+ MethodType .methodType (method .getReturnType (), method .getParameterTypes ()), intfc );
78116 } catch (NoSuchMethodException | IllegalAccessException e ) {
79117 throw new RuntimeException (e );
80118 }
0 commit comments