From 3c34ae001e2ba7dd22db01552f53c9757249d01b Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 21 Jun 2023 14:49:39 +0200 Subject: [PATCH] Move BindingReflections.isNotificationCallback() This method has only one user, mdsal-binding-dom-adapter. Move it there. JIRA: MDSAL-781 Change-Id: I128d588d58a75e00b63ebdbba6f503dd261646c7 Signed-off-by: Robert Varga --- ...BindingDOMNotificationListenerAdapter.java | 2 +- .../invoke/NotificationListenerInvoker.java | 31 ++++++++++++------ .../spec/reflect/BindingReflections.java | 32 ------------------- .../spec/reflect/BindingReflectionsTest.java | 1 - 4 files changed, 23 insertions(+), 43 deletions(-) diff --git a/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/BindingDOMNotificationListenerAdapter.java b/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/BindingDOMNotificationListenerAdapter.java index 28e5c631d3..b97853e7d7 100644 --- a/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/BindingDOMNotificationListenerAdapter.java +++ b/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/BindingDOMNotificationListenerAdapter.java @@ -66,7 +66,7 @@ final class BindingDOMNotificationListenerAdapter extends AbstractDOMNotificatio // it from NotificationListenerInvoker final Set ret = new HashSet<>(); for (final Method method : type.getMethods()) { - if (BindingReflections.isNotificationCallback(method)) { + if (NotificationListenerInvoker.isNotificationCallback(method)) { final Class notification = method.getParameterTypes()[0]; ret.add(Absolute.of(BindingReflections.findQName(notification))); } diff --git a/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/NotificationListenerInvoker.java b/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/NotificationListenerInvoker.java index 471d6d1378..b177cebcb2 100644 --- a/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/NotificationListenerInvoker.java +++ b/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/NotificationListenerInvoker.java @@ -18,13 +18,13 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodHandles.Lookup; import java.lang.invoke.MethodType; import java.lang.reflect.Method; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections; import org.opendaylight.yangtools.yang.binding.DataContainer; +import org.opendaylight.yangtools.yang.binding.Notification; import org.opendaylight.yangtools.yang.binding.NotificationListener; import org.opendaylight.yangtools.yang.common.QName; @@ -36,9 +36,6 @@ import org.opendaylight.yangtools.yang.common.QName; * via {@link #invokeNotification(NotificationListener, QName, DataContainer)} method. */ public final class NotificationListenerInvoker { - - private static final Lookup LOOKUP = MethodHandles.publicLookup(); - private static final LoadingCache, NotificationListenerInvoker> INVOKERS = CacheBuilder.newBuilder().weakKeys() .build(new CacheLoader, NotificationListenerInvoker>() { @@ -51,14 +48,13 @@ public final class NotificationListenerInvoker { final Class key) { final Builder ret = ImmutableMap.builder(); for (final Method method : key.getMethods()) { - if (BindingReflections.isNotificationCallback(method)) { - + if (isNotificationCallback(method)) { final Class notification = method.getParameterTypes()[0]; final QName name = BindingReflections.findQName(notification); MethodHandle handle; try { - handle = LOOKUP.unreflect(method).asType(MethodType.methodType(void.class, - NotificationListener.class, DataContainer.class)); + handle = MethodHandles.publicLookup().unreflect(method).asType( + MethodType.methodType(void.class, NotificationListener.class, DataContainer.class)); ret.put(name, handle); } catch (final IllegalAccessException e) { throw new IllegalStateException("Can not access public method.", e); @@ -73,7 +69,7 @@ public final class NotificationListenerInvoker { private final ImmutableMap methodInvokers; NotificationListenerInvoker(final ImmutableMap map) { - this.methodInvokers = map; + methodInvokers = map; } /** @@ -110,4 +106,21 @@ public final class NotificationListenerInvoker { throw new IllegalStateException(e); } } + + /** + * Checks if supplied method is callback for notifications. + * + * @param method method to check + * @return true if method is notification callback. + */ + public static boolean isNotificationCallback(final Method method) { + if (method.getName().startsWith("on") && method.getParameterCount() == 1) { + Class potentialNotification = method.getParameterTypes()[0]; + if (Notification.class.isAssignableFrom(potentialNotification) + && method.getName().equals("on" + potentialNotification.getSimpleName())) { + return true; + } + } + return false; + } } diff --git a/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java b/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java index be9704c2de..33d4265841 100644 --- a/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java +++ b/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java @@ -10,13 +10,11 @@ package org.opendaylight.mdsal.binding.spec.reflect; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; -import com.google.common.annotations.VisibleForTesting; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.Optional; import java.util.concurrent.TimeUnit; import org.eclipse.jdt.annotation.NonNull; @@ -123,36 +121,6 @@ public final class BindingReflections { return cls.getName().startsWith(Naming.PACKAGE_PREFIX); } - /** - * Checks if supplied method is callback for notifications. - * - * @param method method to check - * @return true if method is notification callback. - */ - public static boolean isNotificationCallback(final Method method) { - checkArgument(method != null); - if (method.getName().startsWith("on") && method.getParameterCount() == 1) { - Class potentialNotification = method.getParameterTypes()[0]; - if (isNotification(potentialNotification) - && method.getName().equals("on" + potentialNotification.getSimpleName())) { - return true; - } - } - return false; - } - - /** - * Checks is supplied class is a {@link Notification}. - * - * @param potentialNotification class to examine - * @return True if the class represents a Notification. - */ - @VisibleForTesting - static boolean isNotification(final Class potentialNotification) { - checkArgument(potentialNotification != null, "potentialNotification must not be null."); - return Notification.class.isAssignableFrom(potentialNotification); - } - /** * Checks if supplied class represents RPC Input / RPC Output. * diff --git a/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflectionsTest.java b/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflectionsTest.java index 83b1961555..472f174bfe 100644 --- a/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflectionsTest.java +++ b/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflectionsTest.java @@ -26,7 +26,6 @@ public class BindingReflectionsTest { public void testBindingWithDummyObject() throws Exception { assertFalse("Should not be RpcType", BindingReflections.isRpcType(DataObject.class)); assertTrue("Should be BindingClass", BindingReflections.isBindingClass(DataObject.class)); - assertFalse("Should not be Notification", BindingReflections.isNotification(DataObject.class)); assertEquals(QName.create("test", "test"), BindingReflections.getQName(TestIdentity.VALUE)); } -- 2.36.6