Skip to content

Commit

Permalink
Remove unnecessary constructor and do general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Mar 11, 2016
1 parent 596022a commit 1714d29
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ void invokeOnThread(final DISPID dispIdMember, final REFIID riid, LCID lcid, WOR
PointerByReference ppvObject = new PointerByReference();
IID iid = com.sun.jna.platform.win32.COM.IUnknown.IID_IUNKNOWN;
dispatch.QueryInterface(new REFIID(iid), ppvObject);
Unknown rawUnk = new Unknown(ppvObject.getValue());
long unknownId = Pointer.nativeValue( rawUnk.getPointer() );
IUnknown unk = CallbackProxy.this.factory.createProxy(IUnknown.class, unknownId, dispatch);
IUnknown unk = CallbackProxy.this.factory.createProxy(IUnknown.class, dispatch);
if(targetClass.getAnnotation(ComInterface.class) != null) {
rjargs.add(unk.queryInterface(targetClass));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@
import java.util.List;

public class Factory {

/**
* Creates a utility COM Factory and a ComThread on which all COM calls are executed.
* NOTE: Remember to call factory.getComThread().terminate() at some appropriate point.
*
* Factory keeps track of COM objects - all objects created with this
* factory can be disposed by calling {@link Factory#disposeAll() }.
*/
public Factory() {
assert COMUtils.comIsInitialized() : "COM not initialized";
Expand Down Expand Up @@ -90,22 +88,6 @@ public <T> T createProxy(Class<T> comInterface, IDispatch dispatch) {
T result = comInterface.cast(proxy);
return result;
}

/** only for use when creating ProxyObjects from Callbacks
*
* @param comInterface
* @param unknownId
* @param dispatch
* @return proxy object
*/
<T> T createProxy(Class<T> comInterface, long unknownId, IDispatch dispatch) {
assert COMUtils.comIsInitialized() : "COM not initialized";

ProxyObject jop = new ProxyObject(comInterface, unknownId, dispatch, this);
Object proxy = Proxy.newProxyInstance(comInterface.getClassLoader(), new Class<?>[] { comInterface }, jop);
T result = comInterface.cast(proxy);
return result;
}

/**
* Creates a new COM object (CoCreateInstance) for the given progId and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,27 @@

/**
* This object acts as the invocation handler for interfaces annotated with
* ComInterface. It wraps all (necessary) low level COM calls and executes them
* on a 'ComThread' held by the Factory object.
* ComInterface. It wraps all (necessary) low level COM calls and dispatches
* them through the COM runtime.
*
* <p>The caller of the methods is responsible for correct initialization of the
* COM runtime and appropriate thread-handling - depending on the choosen
* handling model.</p>
*
* @see <a href="https://msdn.microsoft.com/de-de/library/windows/desktop/ms693344%28v=vs.85%29.aspx">MSDN - Processes, Threads, and Apartments</a>
* @see <a href="https://msdn.microsoft.com/en-us/library/ms809971.aspx">MSDN - Understanding and Using COM Threading Models</a>
*/
public class ProxyObject implements InvocationHandler, com.sun.jna.platform.win32.COM.util.IDispatch,
IRawDispatchHandle, IConnectionPoint {

// cached value of the IUnknown interface pointer
// Rules of COM state that querying for the IUnknown interface must return
// an identical pointer value
private long unknownId;
private final Class<?> theInterface;
private final Factory factory;
private final com.sun.jna.platform.win32.COM.IDispatch rawDispatch;

public ProxyObject(Class<?> theInterface, IDispatch rawDispatch, Factory factory) {
this.unknownId = -1;
this.rawDispatch = rawDispatch;
Expand All @@ -73,38 +88,11 @@ public ProxyObject(Class<?> theInterface, IDispatch rawDispatch, Factory factory
factory.register(this);
}

/** when proxy is created for arguments on a call back, they are already on the
* com thread, and hence calling 'getUnknownId' will not work as it uses the ComThread
* however, the unknown pointer value is passed in;
*
* @param theInterface
* @param unknownId
* @param rawDispatch
* @param factory
*/
ProxyObject(Class<?> theInterface, long unknownId, IDispatch rawDispatch, Factory factory) {
this.unknownId = unknownId;
this.rawDispatch = rawDispatch;
this.theInterface = theInterface;
this.factory = factory;
// make sure dispatch object knows we have a reference to it
// (for debug it is usefult to be able to see how many refs are present
int n = this.rawDispatch.AddRef();
factory.register(this);
}


// cached value of the IUnknown interface pointer
// Rules of COM state that querying for the IUnknown interface must return
// an identical pointer value
long unknownId;

long getUnknownId() {
private long getUnknownId() {
assert COMUtils.comIsInitialized() : "COM not initialized";

if (-1 == this.unknownId) {
try {

final PointerByReference ppvObject = new PointerByReference();

Thread current = Thread.currentThread();
Expand Down Expand Up @@ -144,12 +132,8 @@ public synchronized void dispose() {
}
}

Class<?> theInterface;
Factory factory;
com.sun.jna.platform.win32.COM.IDispatch rawDispatch;

@Override
public com.sun.jna.platform.win32.COM.IDispatch getRawDispatch() {
public com.sun.jna.platform.win32.COM.IDispatch getRawDispatch() {
return this.rawDispatch;
}

Expand All @@ -164,7 +148,7 @@ public com.sun.jna.platform.win32.COM.IDispatch getRawDispatch() {
* therefore we can compare the pointers
*/
@Override
public boolean equals(Object arg) {
public boolean equals(Object arg) {
if (null == arg) {
return false;
} else if (arg instanceof ProxyObject) {
Expand Down Expand Up @@ -261,7 +245,7 @@ public Object invoke(final Object proxy, final java.lang.reflect.Method method,
}

// ---------------------- IConnectionPoint ----------------------
ConnectionPoint fetchRawConnectionPoint(IID iid) throws InterruptedException, ExecutionException, TimeoutException {
private ConnectionPoint fetchRawConnectionPoint(IID iid) throws InterruptedException, ExecutionException, TimeoutException {
assert COMUtils.comIsInitialized() : "COM not initialized";

// query for ConnectionPointContainer
Expand Down Expand Up @@ -466,7 +450,7 @@ public <T> T queryInterface(Class<T> comInterface) throws COMException {
}
}

IID getIID(ComInterface annotation) {
private IID getIID(ComInterface annotation) {
String iidStr = annotation.iid();
if (null != iidStr && !iidStr.isEmpty()) {
return new IID(iidStr);
Expand Down Expand Up @@ -657,11 +641,11 @@ protected HRESULT oleMethod(final int nType, final VARIANT.ByReference pvResult,
}


HRESULT hr = pDisp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT,
new WinDef.WORD(finalNType), dp, pvResult, pExcepInfo, puArgErr);
HRESULT hr = pDisp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT,
new WinDef.WORD(finalNType), dp, pvResult, pExcepInfo, puArgErr);


COMUtils.checkRC(hr, pExcepInfo, puArgErr);
return hr;
COMUtils.checkRC(hr, pExcepInfo, puArgErr);
return hr;
}
}

0 comments on commit 1714d29

Please sign in to comment.