Skip to content

Commit

Permalink
Merge pull request #903 from matthiasblaesing/extend_comexception
Browse files Browse the repository at this point in the history
Update COMException to contain HRESULT and introduce COMInvokeException
  • Loading branch information
matthiasblaesing authored Jan 13, 2018
2 parents 0d91fb6 + bf680a5 commit 1ad4606
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 153 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Release 5.0.0 (Next release)

Features
--------
* [#903](https://github.com/java-native-access/jna/pull/903): Carry `HRESULT` in `c.s.j.p.win32.COM.COMException`, introduce `c.s.j.p.win32.COM.COMInvokeException` as subclass of `COMException` for exception as the result of a `IDispatch#Invoke`. The `EXECPINFO` is unwrapped into fields in the `COMInvokeException` and correctly freed. - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#822](https://github.com/java-native-access/jna/issues/822): `Native#loadLibrary` requires that the interface class passed in is an instance of Library. The runtime check can be enhanced by using a constraint generic. This breaks binary compatibility (see notes below) - [@d-noll](https://github.com/d-noll).
* [#889](https://github.com/java-native-access/jna/issues/889): The `Structure#newInstance` receive the target type as a parameter. This adds a limited generic type, so that the return type ist the target type and not a generic structure, removing the necessity to do an explizit cast - [@matthiasblaesing](https://github.com/matthiasblaesing).

Expand Down Expand Up @@ -69,6 +70,9 @@ Breaking Changes
* `com.sun.jna.platform.win32.Kernel32Util.formatMessageFromHR(HRESULT)`
was replaced by
`com.sun.jna.platform.win32.Kernel32Util.formatMessage(HRESULT)`
* `com.sun.jna.platform.win32.COM.COMException` was structurally modified. The
`pExcepInfo` and `puArgErr` members were removed and `hresult` member was added.
The now missing information in `COMException` was moved to `COMInvokeException`.

Release 4.5.0
=============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.sun.jna.Pointer;

import com.sun.jna.platform.win32.COM.COMException;
import com.sun.jna.platform.win32.COM.COMInvokeException;
import com.sun.jna.platform.win32.COM.Helper;
import com.sun.jna.platform.win32.Ole32;
import com.sun.jna.platform.win32.WinDef.LONG;
Expand Down Expand Up @@ -102,11 +103,10 @@ public void testMSWord() throws IOException {
msWord.insertText("Hello some changes from JNA!\n");
// save the document and prompt the user
msWord.Save(false, wdPromptUser);
} catch (COMInvokeException e) {
System.out.println("bstrSource: " + e.getSource());
System.out.println("bstrDescription: " + e.getDescription());
} catch (COMException e) {
if (e.getExcepInfo() != null) {
System.out.println("bstrSource: " + e.getExcepInfo().bstrSource);
System.out.println("bstrDescription: " + e.getExcepInfo().bstrDescription);
}
} finally {
if (msWord != null) {
msWord.quit();
Expand Down
46 changes: 37 additions & 9 deletions contrib/platform/nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
application.args=
application.title=jna-platform
application.vendor=JNA project
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/jna-platform.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
file.reference.hamcrest-core-1.3.jar=../../lib/hamcrest-core-1.3.jar
file.reference.jna.jar=../../build/jna.jar
file.reference.jna-test.jar=../../build/jna-test.jar
libs.junit.classpath=\
../../lib/junit.jar:\
../../lib/hamcrest-core-1.3.jar
file.reference.junit.jar=../../lib/junit.jar
includes=**
javac.external.vm=false
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.test.modulepath=\
${javac.modulepath}
javadoc.html5=false
jlink.launcher=false
jlink.launcher.name=platform
jar.compress=false
javac.classpath=\
${file.reference.jna.jar}:\
${file.reference.jna.build}/test-classes
${file.reference.jna.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.source=1.5
javac.target=1.5
javac.source=1.6
javac.target=1.6
javac.test.classpath=\
${javac.classpath}:\
${file.reference.jna-test.jar}:\
${build.classes.dir}:\
${libs.junit.classpath}
${file.reference.junit.jar}:\
${file.reference.hamcrest-core-1.3.jar}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=
Expand All @@ -49,6 +72,7 @@ javadoc.windowtitle=
main.class=
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
Expand All @@ -57,6 +81,8 @@ run.classpath=\
# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
# or test-sys-prop.name=value to set system properties for unit tests):
run.jvmargs=
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
../../lib/test/reflections-0.9.8.jar:\
Expand All @@ -65,7 +91,9 @@ run.test.classpath=\
../../lib/test/slf4j-api-1.6.1.jar:\
../../lib/test/dom4j-1.6.1.jar:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
src.dir=src
test.src.dir=test
source.encoding=UTF-8
file.encoding=UTF-8
file.encoding=UTF-8
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,6 @@ protected HRESULT oleMethod(int nType, VARIANT.ByReference pvResult,
* the hr
*/
protected void checkFailed(HRESULT hr) {
COMUtils.checkRC(hr, null, null);
COMUtils.checkRC(hr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
*/
package com.sun.jna.platform.win32.COM;

import com.sun.jna.platform.win32.OaIdl.EXCEPINFO;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.platform.win32.WinNT.HRESULT;

// TODO: Auto-generated Javadoc
/**
* Exception class for all COM related classes.
*
Expand All @@ -35,93 +33,75 @@
public class COMException extends RuntimeException {
private static final long serialVersionUID = 1L;

/** The p excep info. */
private EXCEPINFO pExcepInfo;

/** The pu arg err. */
private IntByReference puArgErr;

private int uArgErr;
private final HRESULT hresult;

/**
* Instantiates a new automation exception.
*/
public COMException() {
super();
this("", (Throwable) null);
}

/**
* Instantiates a new automation exception.
*
* @param message
* the message
* @param cause
* the cause
*/
public COMException(String message, Throwable cause) {
super(message, cause);
public COMException(String message) {
this(message, (Throwable) null);
}


/**
* Instantiates a new automation exception.
*
* @param message
* the message
* @param cause
* the cause
*/
public COMException(String message) {
super(message);
public COMException(Throwable cause) {
this(null, cause);
}

/**
* Instantiates a new automation exception.
*
* @param message
* the message
* @param pExcepInfo
* the excep info
* @param puArgErr
* the pu arg err
* @param cause
* the cause
*/
public COMException(String message, EXCEPINFO pExcepInfo,
IntByReference puArgErr) {
super(message + " (puArgErr=" + (null==puArgErr?"":puArgErr.getValue()) + ")");
this.pExcepInfo = pExcepInfo;
this.puArgErr = puArgErr;
public COMException(String message, Throwable cause) {
super(message, cause);
this.hresult = null;
}

/**
* Instantiates a new automation exception.
*
* @param cause
* the cause
* @param message
* the message
* @param hresult
* HRESULT that lead to the creation of the COMException
*/
public COMException(Throwable cause) {
super(cause);
public COMException(String message, HRESULT hresult) {
super(message);
this.hresult = hresult;
}

/**
* Gets the excep info.
*
* @return the excep info
* @return the HRESULT that lead to thie COMException or NULL if the COMException as not directly caused by a native call
*/
public EXCEPINFO getExcepInfo() {
return pExcepInfo;
public HRESULT getHresult() {
return hresult;
}

/**
* Gets the arg err.
*
* @return the arg err
* @param errorCode
* @return true if the exception has an associated HRESULT and that HRESULT
* matches the supplied error code
*/
public IntByReference getArgErr() {
return puArgErr;
}

public int getuArgErr() {
return uArgErr;
}

public void setuArgErr(int uArgErr) {
this.uArgErr = uArgErr;
public boolean matchesErrorCode(int errorCode) {
return hresult != null && hresult.intValue() == errorCode;
}
}
Loading

0 comments on commit 1ad4606

Please sign in to comment.