@@ -259,7 +259,22 @@ void connect(boolean _registerSelf) throws DBusException {
259
259
}
260
260
}
261
261
262
- protected DBusInterface dynamicProxy (String _source , String _path ) throws DBusException {
262
+ /**
263
+ * Tries to resolve a proxy to a remote object.
264
+ * If a type class is given, it tries to convert the object using that class.
265
+ * If null is given as type, it tries to find a proper interface for this object.
266
+ *
267
+ * @param <T> object type (DBusInterface compatible)
268
+ * @param _source source
269
+ * @param _path path
270
+ * @param _type class of object type
271
+ *
272
+ * @return DBusInterface compatible object
273
+ *
274
+ * @throws DBusException when something goes wrong
275
+ */
276
+ @ SuppressWarnings ("unchecked" )
277
+ protected <T extends DBusInterface > T dynamicProxy (String _source , String _path , Class <T > _type ) throws DBusException {
263
278
logger .debug ("Introspecting {} on {} for dynamic proxy creation" , _path , _source );
264
279
try {
265
280
Introspectable intro = getRemoteObject (_source , _path , Introspectable .class );
@@ -278,39 +293,44 @@ protected DBusInterface dynamicProxy(String _source, String _path) throws DBusEx
278
293
})
279
294
.collect (Collectors .toList ());
280
295
List <Class <?>> ifcs = new ArrayList <>();
281
- for (String iface : ifaces ) {
282
-
283
- logger .debug ("Trying interface {}" , iface );
284
- int j = 0 ;
285
- while (j >= 0 ) {
286
- try {
287
- Class <?> ifclass = Class .forName (iface );
288
- if (!ifcs .contains (ifclass )) {
289
- ifcs .add (ifclass );
290
- }
291
- break ;
292
- } catch (Exception _ex ) {
293
- }
294
- j = iface .lastIndexOf ('.' );
295
- char [] cs = iface .toCharArray ();
296
- if (j >= 0 ) {
297
- cs [j ] = '$' ;
298
- iface = String .valueOf (cs );
299
- }
300
- }
296
+ if (_type == null ) {
297
+ for (String iface : ifaces ) {
298
+
299
+ logger .debug ("Trying interface {}" , iface );
300
+ int j = 0 ;
301
+ while (j >= 0 ) {
302
+ try {
303
+ Class <?> ifclass = Class .forName (iface );
304
+ if (!ifcs .contains (ifclass )) {
305
+ ifcs .add (ifclass );
306
+ }
307
+ break ;
308
+ } catch (Exception _ex ) {
309
+ }
310
+ j = iface .lastIndexOf ('.' );
311
+ char [] cs = iface .toCharArray ();
312
+ if (j >= 0 ) {
313
+ cs [j ] = '$' ;
314
+ iface = String .valueOf (cs );
315
+ }
316
+ }
317
+ }
318
+ }
319
+ else {
320
+ ifcs .add (_type );
301
321
}
302
322
303
323
// interface could not be found, we guess that this exported object at least support DBusInterface
304
324
if (ifcs .isEmpty ()) {
305
- // throw new DBusException("Could not find an interface to cast to");
306
325
ifcs .add (DBusInterface .class );
307
326
}
308
327
309
328
RemoteObject ro = new RemoteObject (_source , _path , null , false );
310
329
DBusInterface newi = (DBusInterface ) Proxy .newProxyInstance (ifcs .get (0 ).getClassLoader (),
311
330
ifcs .toArray (Class []::new ), new RemoteInvocationHandler (this , ro ));
312
331
getImportedObjects ().put (newi , ro );
313
- return newi ;
332
+
333
+ return (T ) newi ;
314
334
} catch (Exception e ) {
315
335
logger .debug ("" , e );
316
336
throw new DBusException (
@@ -319,23 +339,29 @@ protected DBusInterface dynamicProxy(String _source, String _path) throws DBusEx
319
339
}
320
340
}
321
341
342
+ @ SuppressWarnings ("unchecked" )
322
343
@ Override
323
- public DBusInterface getExportedObject (String _source , String _path ) throws DBusException {
324
- ExportedObject o ;
344
+ public < T extends DBusInterface > T getExportedObject (String _source , String _path , Class < T > _type ) throws DBusException {
345
+ ExportedObject o ;
325
346
synchronized (getExportedObjects ()) {
326
347
o = getExportedObjects ().get (_path );
327
348
}
328
- if (null != o && null == o .getObject ().get ()) {
349
+ if (null != o && o .getObject ().get () == null ) {
329
350
unExportObject (_path );
330
351
o = null ;
331
352
}
332
353
if (null != o ) {
333
- return o .getObject ().get ();
354
+ return ( T ) o .getObject ().get ();
334
355
}
335
356
if (null == _source ) {
336
357
throw new DBusException ("Not an object exported by this connection and no remote specified" );
337
358
}
338
- return dynamicProxy (_source , _path );
359
+ return (T ) dynamicProxy (_source , _path , _type );
360
+ }
361
+
362
+ @ Override
363
+ public DBusInterface getExportedObject (String _source , String _path ) throws DBusException {
364
+ return getExportedObject (_source , _path , null );
339
365
}
340
366
341
367
/**
@@ -456,7 +482,7 @@ public DBusInterface getPeerRemoteObject(String _busname, String _objectpath) th
456
482
457
483
String unique = dbus .GetNameOwner (_busname );
458
484
459
- return dynamicProxy (unique , _objectpath );
485
+ return dynamicProxy (unique , _objectpath , null );
460
486
}
461
487
462
488
/**
@@ -498,7 +524,7 @@ public DBusInterface getRemoteObject(String _busname, String _objectpath) throws
498
524
throw new DBusException ("Invalid object path: " + _objectpath );
499
525
}
500
526
501
- return dynamicProxy (_busname , _objectpath );
527
+ return dynamicProxy (_busname , _objectpath , null );
502
528
}
503
529
504
530
/**
0 commit comments