From eb0f41b3dff76bc1b83fa7c7095f16b5c6ebde0e Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sat, 18 Jul 2020 11:50:35 +0200 Subject: [PATCH] [pushbullet] Workaround for thing actions bug (#8143) Related to #8116 Signed-off-by: Laurent Garnier Signed-off-by: CSchlipp --- .../internal/action/IPushbulletActions.java | 29 +++++++++++++ .../internal/action/PushbulletActions.java | 41 ++++++++++++++----- 2 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 bundles/org.openhab.binding.pushbullet/src/main/java/org/openhab/binding/pushbullet/internal/action/IPushbulletActions.java diff --git a/bundles/org.openhab.binding.pushbullet/src/main/java/org/openhab/binding/pushbullet/internal/action/IPushbulletActions.java b/bundles/org.openhab.binding.pushbullet/src/main/java/org/openhab/binding/pushbullet/internal/action/IPushbulletActions.java new file mode 100644 index 0000000000000..46cbbc462a867 --- /dev/null +++ b/bundles/org.openhab.binding.pushbullet/src/main/java/org/openhab/binding/pushbullet/internal/action/IPushbulletActions.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.pushbullet.internal.action; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + +/** + * The {@link IPushbulletActions} interface defines rule actions for sending notifications + * + * @author Laurent Garnier - Initial contribution + */ +@NonNullByDefault +public interface IPushbulletActions { + + public Boolean sendPushbulletNote(@Nullable String recipient, @Nullable String title, @Nullable String message); + + public Boolean sendPushbulletNote(@Nullable String recipient, @Nullable String message); +} diff --git a/bundles/org.openhab.binding.pushbullet/src/main/java/org/openhab/binding/pushbullet/internal/action/PushbulletActions.java b/bundles/org.openhab.binding.pushbullet/src/main/java/org/openhab/binding/pushbullet/internal/action/PushbulletActions.java index fe64af81ec745..348c2dccbb20a 100644 --- a/bundles/org.openhab.binding.pushbullet/src/main/java/org/openhab/binding/pushbullet/internal/action/PushbulletActions.java +++ b/bundles/org.openhab.binding.pushbullet/src/main/java/org/openhab/binding/pushbullet/internal/action/PushbulletActions.java @@ -12,6 +12,9 @@ */ package org.openhab.binding.pushbullet.internal.action; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.thing.binding.ThingActions; @@ -26,12 +29,17 @@ /** * The {@link PushbulletActions} class defines rule actions for sending notifications + *

+ * Note:The static method invokeMethodOf handles the case where + * the test actions instanceof PushbulletActions fails. This test can fail + * due to an issue in openHAB core v2.5.0 where the {@link PushbulletActions} class + * can be loaded by a different classloader than the actions instance. * * @author Hakan Tandogan - Initial contribution */ @ThingActionsScope(name = "pushbullet") @NonNullByDefault -public class PushbulletActions implements ThingActions { +public class PushbulletActions implements ThingActions, IPushbulletActions { private final Logger logger = LoggerFactory.getLogger(PushbulletActions.class); @@ -47,6 +55,7 @@ public void setThingHandler(@Nullable ThingHandler handler) { return this.handler; } + @Override @RuleAction(label = "@text/actionSendPushbulletNoteLabel", description = "@text/actionSendPushbulletNoteDesc") public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPushbulletNote( @ActionInput(name = "recipient", label = "@text/actionSendPushbulletNoteInputRecipientLabel", description = "@text/actionSendPushbulletNoteInputRecipientDesc") @Nullable String recipient, @@ -67,13 +76,10 @@ public void setThingHandler(@Nullable ThingHandler handler) { public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String title, @Nullable String message) { - if (actions instanceof PushbulletActions) { - return ((PushbulletActions) actions).sendPushbulletNote(recipient, title, message); - } else { - throw new IllegalArgumentException("Instance is not a PushbulletActions class ( " + actions + " )"); - } + return invokeMethodOf(actions).sendPushbulletNote(recipient, title, message); } + @Override @RuleAction(label = "@text/actionSendPushbulletNoteLabel", description = "@text/actionSendPushbulletNoteDesc") public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPushbulletNote( @ActionInput(name = "recipient", label = "@text/actionSendPushbulletNoteInputRecipientLabel", description = "@text/actionSendPushbulletNoteInputRecipientDesc") @Nullable String recipient, @@ -93,10 +99,25 @@ public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullab public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String message) { - if (actions instanceof PushbulletActions) { - return ((PushbulletActions) actions).sendPushbulletNote(recipient, message); - } else { - throw new IllegalArgumentException("Instance is not a PushbulletActions class ( " + actions + " )"); + return invokeMethodOf(actions).sendPushbulletNote(recipient, message); + } + + private static IPushbulletActions invokeMethodOf(@Nullable ThingActions actions) { + if (actions == null) { + throw new IllegalArgumentException("actions cannot be null"); + } + if (actions.getClass().getName().equals(PushbulletActions.class.getName())) { + if (actions instanceof IPushbulletActions) { + return (IPushbulletActions) actions; + } else { + return (IPushbulletActions) Proxy.newProxyInstance(IPushbulletActions.class.getClassLoader(), + new Class[] { IPushbulletActions.class }, (Object proxy, Method method, Object[] args) -> { + Method m = actions.getClass().getDeclaredMethod(method.getName(), + method.getParameterTypes()); + return m.invoke(actions, args); + }); + } } + throw new IllegalArgumentException("Actions is not an instance of PushbulletActions"); } }