Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events in COM are just method calls - deprecate ComEventCallback in favor of ComMethod #1032

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Release 5.1.0 (Next release)
Features
--------
* [#1029](https://github.com/java-native-access/jna/issues/1029): Add `statvfs` to `c.s.j.platform.linux.LibC` - [@dbwiddis](https://github.com/dbwiddis).
* [#1032](https://github.com/java-native-access/jna/pull/1032): Deprecate `c.s.j.platform.win32.COM.util.annotation.ComEventCallback` in favour of `c.s.j.platform.win32.COM.util.annotation.ComMethod` - [@matthiasblaesing](https://github.com/matthiasblaesing).

Bug Fixes
---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.sun.jna.platform.win32.COM.Unknown;
import com.sun.jna.platform.win32.COM.util.annotation.ComEventCallback;
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;

Expand Down Expand Up @@ -100,11 +101,23 @@ Map<DISPID, Method> createDispIdMap(Class<?> comEventCallbackInterface) {
Map<DISPID, Method> map = new HashMap<DISPID, Method>();

for (Method meth : comEventCallbackInterface.getMethods()) {
ComEventCallback annotation = meth.getAnnotation(ComEventCallback.class);
if (null != annotation) {
int dispId = annotation.dispid();
ComEventCallback callbackAnnotation = meth.getAnnotation(ComEventCallback.class);
ComMethod methodAnnotation = meth.getAnnotation(ComMethod.class);
if (methodAnnotation != null) {
int dispId = methodAnnotation.dispId();
if (-1 == dispId) {
dispId = this.fetchDispIdFromName(annotation);
dispId = this.fetchDispIdFromName(callbackAnnotation);
}
if(dispId == -1) {
CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent(
"DISPID for " + meth.getName() + " not found",
null);
}
map.put(new DISPID(dispId), meth);
} else if (null != callbackAnnotation) {
int dispId = callbackAnnotation.dispid();
if (-1 == dispId) {
dispId = this.fetchDispIdFromName(callbackAnnotation);
}
if(dispId == -1) {
CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @deprecated Use {@link ComMethod} as a replacement
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
@Inherited
@Deprecated
public @interface ComEventCallback {
int dispid() default -1; // default to dispid unknown
String name() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void testShowProperties() {
assertTrue(year > 1970);
assertTrue(month >= 1 && month <= 12);
assertTrue(day >= 1 && day <= 31);
assertTrue(hour >= 0 && day <= 23);
assertTrue(hour >= 0 && hour <= 23);
assertTrue(minute >= 0 && minute <= 59);
assertTrue(second >= 0 && second <= 59);
OleAuto.INSTANCE.VariantClear(pVal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.sun.jna.Pointer;
import static com.sun.jna.platform.win32.AbstractWin32TestSupport.checkCOMRegistered;
import com.sun.jna.platform.win32.COM.COMUtils;
import com.sun.jna.platform.win32.COM.util.annotation.ComEventCallback;
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.platform.win32.COM.util.annotation.ComObject;
import com.sun.jna.platform.win32.Ole32;
import com.sun.jna.platform.win32.Variant;
Expand Down Expand Up @@ -241,14 +241,14 @@ interface ApplicationEvents4ListenerMatching {
* <p>
* id(0x3)</p>
*/
@ComEventCallback(dispid = 0x3)
@ComMethod(dispId = 0x3)
void DocumentChange();

/**
* <p>
* id(0x6)</p>
*/
@ComEventCallback(dispid = 0x6)
@ComMethod(dispId = 0x6)
void DocumentBeforeClose(IDispatch Doc, Variant.VARIANT Cancel);
}

Expand All @@ -259,14 +259,14 @@ interface ApplicationEvents4ListenerLessArguments {
* <p>
* id(0x3)</p>
*/
@ComEventCallback(dispid = 0x3)
@ComMethod(dispId = 0x3)
void DocumentChange();

/**
* <p>
* id(0x6)</p>
*/
@ComEventCallback(dispid = 0x6)
@ComMethod(dispId = 0x6)
void DocumentBeforeClose();
}

Expand All @@ -277,14 +277,14 @@ interface ApplicationEvents4ListenerMoreArguments {
* <p>
* id(0x3)</p>
*/
@ComEventCallback(dispid = 0x3)
@ComMethod(dispId = 0x3)
void DocumentChange();

/**
* <p>
* id(0x6)</p>
*/
@ComEventCallback(dispid = 0x6)
@ComMethod(dispId = 0x6)
void DocumentBeforeClose(IDispatch Doc, Variant.VARIANT Cancel, Boolean fakeArgumentObject, int fakeArgumentInt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.Before;
import org.junit.Test;

import com.sun.jna.platform.win32.COM.util.annotation.ComEventCallback;
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.platform.win32.COM.util.annotation.ComObject;
Expand Down Expand Up @@ -116,13 +115,13 @@ interface ComIWebBrowser2 extends IUnknown, IConnectionPoint {
interface DWebBrowserEvents2 {
public static final String IID = "{34A715A0-6587-11D0-924A-0020AFC7AC4D}";

@ComEventCallback(dispid=0x000000fd)
@ComMethod(dispId = 0x000000fd)
void OnQuit();

@ComEventCallback(dispid=0x000000fc)
@ComMethod(dispId = 0x000000fc)
void NavigateComplete2(IUnknown source, Object url);

@ComEventCallback(dispid=0x000000fa)
@ComMethod(dispId = 0x000000fa)
void BeforeNavigate2(IUnknown pDisp,
String URL,
long Flags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,70 @@ public void OnQuit() {
}
}

@ComInterface(iid=DWebBrowserEvents2.IID)
interface DWebBrowserEvents2Method {
public static final String IID = "{34A715A0-6587-11D0-924A-0020AFC7AC4D}";

@ComMethod(dispId=0x000000fd)
void OnQuit();

@ComMethod(dispId=0x000000fc)
void NavigateComplete2(IUnknown source, Object url);

@ComMethod(dispId=0x000000fa)
void BeforeNavigate2(IUnknown pDisp,
String URL,
long Flags,
String TargetFrameName,
VARIANT.ByReference PostData,
VARIANT.ByReference Headers,
OaIdl.VARIANT_BOOLByReference Cancel);
}

class DWebBrowserEvents2_ListenerMethod extends AbstractComEventCallbackListener implements DWebBrowserEvents2Method {

@Override
public void errorReceivingCallbackEvent(String message, Exception exception) {
// System.err.println(message);
// if(exception != null) {
// System.err.println(exception.getMessage());
// exception.printStackTrace(System.err);
// }
}

volatile boolean blockNavigate = false;

public void BeforeNavigate2(
IUnknown pDisp,
String URL,
long Flags,
String TargetFrameName,
VARIANT.ByReference PostData,
VARIANT.ByReference Headers,
OaIdl.VARIANT_BOOLByReference Cancel) {
// The utilizing unittest is adviseBeforeNavigate
if(blockNavigate){
Cancel.setValue(Variant.VARIANT_TRUE);
}
}

volatile boolean navigateComplete2Called = false;
volatile String navigateComplete2URL = null;
@Override
public void NavigateComplete2( IUnknown source, Object url) {
navigateComplete2Called = true;
if(url != null) {
navigateComplete2URL = url.toString();
}
}

volatile Boolean Quit_called = null;
@Override
public void OnQuit() {
Quit_called = true;
}
}

@Test
public void advise_Quit() throws InterruptedException {
ComInternetExplorer ieApp = factory.createObject(ComInternetExplorer.class);
Expand All @@ -193,6 +257,23 @@ public void advise_Quit() throws InterruptedException {
Assert.assertTrue(listener.Quit_called);
}

@Test
public void advise_Quit_Method() throws InterruptedException {
ComInternetExplorer ieApp = factory.createObject(ComInternetExplorer.class);
ComIWebBrowser2 iWebBrowser2 = ieApp.queryInterface(ComIWebBrowser2.class);
iWebBrowser2.setVisible(true);
DWebBrowserEvents2_ListenerMethod listener = new DWebBrowserEvents2_ListenerMethod();
iWebBrowser2.advise(DWebBrowserEvents2Method.class, listener);

iWebBrowser2.Quit();

//Wait for event to happen
Thread.sleep(200);

Assert.assertNotNull(listener.Quit_called);
Assert.assertTrue(listener.Quit_called);
}

@Test
public void unadvise_Quit() throws InterruptedException {
ComInternetExplorer ieApp = factory.createObject(ComInternetExplorer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.sun.jna.platform.win32.AbstractWin32TestSupport;
import static com.sun.jna.platform.win32.AbstractWin32TestSupport.checkCOMRegistered;
import com.sun.jna.platform.win32.COM.COMUtils;
import com.sun.jna.platform.win32.COM.util.annotation.ComEventCallback;
import com.sun.jna.platform.win32.COM.util.annotation.ComInterface;
import com.sun.jna.platform.win32.COM.util.annotation.ComMethod;
import com.sun.jna.platform.win32.COM.util.annotation.ComObject;
Expand Down Expand Up @@ -362,35 +361,35 @@ interface ComIWebBrowser2EventTest extends IUnknown, IConnectionPoint {
@ComInterface(iid = "{34A715A0-6587-11D0-924A-0020AFC7AC4D}")
interface DWebBrowserEvents2EventTestUtilIUnknown {

@ComEventCallback(dispid = 0x000000fc)
@ComMethod(dispId = 0x000000fc)
void NavigateComplete2UtilIUnknown(IUnknown source, Object url);
}

@ComInterface(iid = "{34A715A0-6587-11D0-924A-0020AFC7AC4D}")
interface DWebBrowserEvents2EventTestUtilIDispatch {

@ComEventCallback(dispid = 0x000000fc)
@ComMethod(dispId = 0x000000fc)
void NavigateComplete2UtilIDispatch(IDispatch source, Object url);
}

@ComInterface(iid = "{34A715A0-6587-11D0-924A-0020AFC7AC4D}")
interface DWebBrowserEvents2EventTestIUnknown {

@ComEventCallback(dispid = 0x000000fc)
@ComMethod(dispId = 0x000000fc)
void NavigateComplete2IUnknown(com.sun.jna.platform.win32.COM.IUnknown source, Object url);
}

@ComInterface(iid = "{34A715A0-6587-11D0-924A-0020AFC7AC4D}")
interface DWebBrowserEvents2EventTestIDispatch {

@ComEventCallback(dispid = 0x000000fc)
@ComMethod(dispId = 0x000000fc)
void NavigateComplete2IDispatch(com.sun.jna.platform.win32.COM.IDispatch source, Object url);
}

@ComInterface(iid = "{34A715A0-6587-11D0-924A-0020AFC7AC4D}")
interface DWebBrowserEvents2EventTestSubclass {

@ComEventCallback(dispid = 0x000000fc)
@ComMethod(dispId = 0x000000fc)
void NavigateComplete2Subclass(ComIWebBrowser2EventTest source, Object url);
}

Expand Down