diff --git a/rcljava/src/main/java/org/ros2/rcljava/action/ActionServerImpl.java b/rcljava/src/main/java/org/ros2/rcljava/action/ActionServerImpl.java index ed379c6b..bd6e5f2d 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/action/ActionServerImpl.java +++ b/rcljava/src/main/java/org/ros2/rcljava/action/ActionServerImpl.java @@ -16,6 +16,7 @@ package org.ros2.rcljava.action; import java.lang.ref.WeakReference; +import java.lang.SuppressWarnings; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -171,6 +172,8 @@ public synchronized void abort(ResultDefinition result) { /** * {@inheritDoc} */ + // TODO(ivanpauno): Improve generated code API so we don't need this. + @SuppressWarnings("unchecked") public synchronized void publishFeedback(FeedbackDefinition feedback) { Class feedbackMessageType = ActionServerImpl.this.actionTypeInstance.getFeedbackMessageType(); @@ -202,7 +205,7 @@ public final long getHandle() { private synchronized final void toTerminalState(byte status, ResultDefinition result) { nativeUpdateGoalState(this.handle, status); - ResultResponseDefinition resultResponse = ActionServerImpl.this.createResultResponse(); + ResultResponseDefinition resultResponse = ActionServerImpl.this.createResultResponseUnchecked(); resultResponse.setGoalStatus(status); resultResponse.setResult(result); ActionServerImpl.this.sendResult(goalInfo.getGoalId().getUuidAsList(), resultResponse); @@ -337,6 +340,12 @@ public boolean isReady(long waitSetHandle) { return false; } + @SuppressWarnings("unchecked") + private GoalCallback.GoalResponse + handleGoalUnchecked(GoalRequestDefinition requestMessage) { + return this.goalCallback.handleGoal(requestMessage); + } + private ActionServerGoalHandle executeGoalRequest( RMWRequestId rmwRequestId, GoalRequestDefinition requestMessage, @@ -358,7 +367,7 @@ private ActionServerGoalHandle executeGoalRequest( } // Call user callback - GoalCallback.GoalResponse response = this.goalCallback.handleGoal(requestMessage); + GoalCallback.GoalResponse response = this.handleGoalUnchecked(requestMessage); boolean accepted = GoalCallback.GoalResponse.ACCEPT_AND_DEFER == response || GoalCallback.GoalResponse.ACCEPT_AND_EXECUTE == response; @@ -542,17 +551,6 @@ private action_msgs.msg.GoalInfo createGoalInfo(List goalUuid) { return goalInfo; } - private ResultResponseDefinition createResultResponse() { - ResultResponseDefinition resultResponse; - try { - resultResponse = - this.actionTypeInstance.getGetResultResponseType().getDeclaredConstructor().newInstance(); - } catch (ReflectiveOperationException ex) { - throw new IllegalStateException("Failed to instantiate provided action type: ", ex); - } - return resultResponse; - } - // This will store the result, so it can be sent to future result requests, and // will also send a result response to all requests that were already made. private void sendResult(List goalUuid, ResultResponseDefinition resultResponse) { @@ -571,23 +569,61 @@ private void sendResult(List goalUuid, ResultResponseDefinition resultR } } + // TODO(ivanpauno): Improve generated code API so we don't need this. + @SuppressWarnings("unchecked") + private ResultRequestDefinition createResultRequestUnchecked() { + ResultRequestDefinition resultRequest; + try { + resultRequest = + this.actionTypeInstance.getGetResultRequestType().getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException ex) { + throw new IllegalStateException("Failed to instantiate provided action type: ", ex); + } + return resultRequest; + } + + // TODO(ivanpauno): Improve generated code API so we don't need this. + @SuppressWarnings("unchecked") + private ResultResponseDefinition createResultResponseUnchecked() { + ResultResponseDefinition resultResponse; + try { + resultResponse = + this.actionTypeInstance.getGetResultResponseType().getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException ex) { + throw new IllegalStateException("Failed to instantiate provided action type: ", ex); + } + return resultResponse; + } + + // TODO(ivanpauno): Improve generated code API so we don't need this. + @SuppressWarnings("unchecked") + private GoalRequestDefinition newRequestUnchecked() { + Class requestType = this.actionTypeInstance.getSendGoalRequestType(); + try { + return requestType.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException ex) { + throw new IllegalStateException("Failed to instantiate request: ", ex); + } + } + + // TODO(ivanpauno): Improve generated code API so we don't need this. + @SuppressWarnings("unchecked") + private GoalResponseDefinition newResponseUnchecked() { + Class responseType = this.actionTypeInstance.getSendGoalResponseType(); + try { + return responseType.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException ex) { + throw new IllegalStateException("Failed to instantiate responce: ", ex); + } + } + /** * {@inheritDoc} */ public void execute() { if (this.isGoalRequestReady()) { - Class requestType = this.actionTypeInstance.getSendGoalRequestType(); - Class responseType = this.actionTypeInstance.getSendGoalResponseType(); - - GoalRequestDefinition requestMessage = null; - GoalResponseDefinition responseMessage = null; - - try { - requestMessage = requestType.getDeclaredConstructor().newInstance(); - responseMessage = responseType.getDeclaredConstructor().newInstance(); - } catch (ReflectiveOperationException ex) { - throw new IllegalStateException("Failed to instantiate request or responce: ", ex); - } + GoalRequestDefinition requestMessage = newRequestUnchecked(); + GoalResponseDefinition responseMessage = newResponseUnchecked(); if (requestMessage != null && responseMessage != null) { long requestFromJavaConverterHandle = requestMessage.getFromJavaConverterInstance(); @@ -643,14 +679,7 @@ public void execute() { } if (this.isResultRequestReady()) { - Class requestType = this.actionTypeInstance.getGetResultRequestType(); - - ResultRequestDefinition requestMessage = null; - try { - requestMessage = requestType.getDeclaredConstructor().newInstance(); - } catch (ReflectiveOperationException ex) { - throw new IllegalArgumentException("Failed to instantiate action result request: ", ex); - } + ResultRequestDefinition requestMessage = createResultRequestUnchecked(); if (requestMessage != null) { long requestFromJavaConverterHandle = requestMessage.getFromJavaConverterInstance(); @@ -672,7 +701,7 @@ public void execute() { ResultResponseDefinition resultResponse = null; if (!goalExists) { - resultResponse = this.createResultResponse(); + resultResponse = this.createResultResponseUnchecked(); resultResponse.setGoalStatus(action_msgs.msg.GoalStatus.STATUS_UNKNOWN); } else { resultResponse = this.goalResults.get(goalUuid); @@ -682,7 +711,7 @@ public void execute() { List requestIds = null; requestIds = this.goalRequests.get(goalUuid); if (requestIds == null) { - requestIds = new ArrayList(); + requestIds = new ArrayList(); this.goalRequests.put(goalUuid, requestIds); } requestIds.add(rmwRequestId); diff --git a/rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java b/rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java index 3ca6cfb6..0ac0ee9f 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java +++ b/rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java @@ -879,7 +879,7 @@ public rcl_interfaces.msg.ListParametersResult listParameters( } public final Collection getNodeNames() { - ArrayList nodeNames = new ArrayList(); + ArrayList nodeNames = new ArrayList(); nativeGetNodeNames(this.handle, nodeNames); return nodeNames; } @@ -887,7 +887,7 @@ public final Collection getNodeNames() { private native static final void nativeGetNodeNames(long handle, ArrayList nodeNames); public final Collection getTopicNamesAndTypes() { - Collection namesAndTypes = new ArrayList(); + Collection namesAndTypes = new ArrayList(); nativeGetTopicNamesAndTypes(this.handle, namesAndTypes); return namesAndTypes; } @@ -896,7 +896,7 @@ private static native final void nativeGetTopicNamesAndTypes( long handle, Collection namesAndTypes); public final Collection getServiceNamesAndTypes() { - Collection namesAndTypes = new ArrayList(); + Collection namesAndTypes = new ArrayList(); nativeGetServiceNamesAndTypes(this.handle, namesAndTypes); return namesAndTypes; } @@ -905,7 +905,7 @@ private static native final void nativeGetServiceNamesAndTypes( long handle, Collection namesAndTypes); public final Collection getPublishersInfo(final String topicName) { - ArrayList returnValue = new ArrayList(); + ArrayList returnValue = new ArrayList(); nativeGetPublishersInfo(this.handle, topicName, returnValue); return returnValue; } @@ -914,7 +914,7 @@ private native static final void nativeGetPublishersInfo( final long handle, final String topicName, ArrayList endpointInfo); public final Collection getSubscriptionsInfo(final String topicName) { - ArrayList returnValue = new ArrayList(); + ArrayList returnValue = new ArrayList(); nativeGetSubscriptionsInfo(this.handle, topicName, returnValue); return returnValue; } @@ -925,7 +925,7 @@ private native static final void nativeGetSubscriptionsInfo( public final Collection getPublisherNamesAndTypesByNode( String nodeName, String nodeNamespace) { - Collection namesAndTypes = new ArrayList(); + Collection namesAndTypes = new ArrayList(); nativeGetPublisherNamesAndTypesByNode(this.handle, nodeName, nodeNamespace, namesAndTypes); return namesAndTypes; } @@ -936,7 +936,7 @@ private static native final Collection nativeGetPublisherNamesAndT public final Collection getSubscriptionNamesAndTypesByNode( String nodeName, String nodeNamespace) { - Collection namesAndTypes = new ArrayList(); + Collection namesAndTypes = new ArrayList(); nativeGetSubscriptionNamesAndTypesByNode(this.handle, nodeName, nodeNamespace, namesAndTypes); return namesAndTypes; } @@ -947,7 +947,7 @@ private static native final Collection nativeGetSubscriptionNamesA public final Collection getServiceNamesAndTypesByNode( String nodeName, String nodeNamespace) { - Collection namesAndTypes = new ArrayList(); + Collection namesAndTypes = new ArrayList(); nativeGetServiceNamesAndTypesByNode(this.handle, nodeName, nodeNamespace, namesAndTypes); return namesAndTypes; } @@ -958,7 +958,7 @@ private static native final Collection nativeGetServiceNamesAndTyp public final Collection getClientNamesAndTypesByNode( String nodeName, String nodeNamespace) { - Collection namesAndTypes = new ArrayList(); + Collection namesAndTypes = new ArrayList(); nativeGetClientNamesAndTypesByNode(this.handle, nodeName, nodeNamespace, namesAndTypes); return namesAndTypes; } diff --git a/rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java b/rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java index 7911916b..1bd493fa 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java +++ b/rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java @@ -117,8 +117,8 @@ public final WeakReference getNodeReference() { public final EventHandler createEventHandler(Supplier factory, Consumer callback) { - final WeakReference> weakEventHandlers = new WeakReference( - this.eventHandlers); + final WeakReference> weakEventHandlers = + new WeakReference>(this.eventHandlers); Consumer disposeCallback = new Consumer() { public void accept(EventHandler eventHandler) { Collection eventHandlers = weakEventHandlers.get(); @@ -129,7 +129,7 @@ public void accept(EventHandler eventHandler) { }; T status = factory.get(); long eventHandle = nativeCreateEvent(this.handle, status.getPublisherEventType()); - EventHandler eventHandler = new EventHandlerImpl( + EventHandler eventHandler = new EventHandlerImpl( new WeakReference(this), eventHandle, factory, callback, disposeCallback); this.eventHandlers.add(eventHandler); return eventHandler; diff --git a/rcljava/src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java b/rcljava/src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java index 7a969af0..34c3e3cf 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java +++ b/rcljava/src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java @@ -129,8 +129,8 @@ public final WeakReference getNodeReference() { public final EventHandler createEventHandler(Supplier factory, Consumer callback) { - final WeakReference> weakEventHandlers = new WeakReference( - this.eventHandlers); + final WeakReference> weakEventHandlers = + new WeakReference>(this.eventHandlers); Consumer disposeCallback = new Consumer() { public void accept(EventHandler eventHandler) { Collection eventHandlers = weakEventHandlers.get(); @@ -141,8 +141,9 @@ public void accept(EventHandler eventHandler) { }; T status = factory.get(); long eventHandle = nativeCreateEvent(this.handle, status.getSubscriptionEventType()); - EventHandler eventHandler = new EventHandlerImpl( - new WeakReference(this), eventHandle, factory, callback, disposeCallback); + EventHandler eventHandler = + new EventHandlerImpl( + new WeakReference(this), eventHandle, factory, callback, disposeCallback); this.eventHandlers.add(eventHandler); return eventHandler; } diff --git a/rcljava/src/test/java/org/ros2/rcljava/client/ClientTest.java b/rcljava/src/test/java/org/ros2/rcljava/client/ClientTest.java index d7a15bbd..867d4eaf 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/client/ClientTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/client/ClientTest.java @@ -54,7 +54,7 @@ public static void setupOnce() throws Exception { { // Configure log4j. Doing this dynamically so that Android does not complain about missing // the log4j JARs, SLF4J uses Android's native logging mechanism instead. - Class c = Class.forName("org.apache.log4j.BasicConfigurator"); + Class c = Class.forName("org.apache.log4j.BasicConfigurator"); Method m = c.getDeclaredMethod("configure", (Class[]) null); Object o = m.invoke(null, (Object[]) null); } diff --git a/rcljava/src/test/java/org/ros2/rcljava/node/NodeTest.java b/rcljava/src/test/java/org/ros2/rcljava/node/NodeTest.java index 29597325..cb668421 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/node/NodeTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/node/NodeTest.java @@ -112,7 +112,7 @@ public static void setupOnce() throws Exception { { // Configure log4j. Doing this dynamically so that Android does not complain about missing // the log4j JARs, SLF4J uses Android's native logging mechanism instead. - Class c = Class.forName("org.apache.log4j.BasicConfigurator"); + Class c = Class.forName("org.apache.log4j.BasicConfigurator"); Method m = c.getDeclaredMethod("configure", (Class[]) null); Object o = m.invoke(null, (Object[]) null); } @@ -975,19 +975,19 @@ public void accept(final Collection namesAndTypes) { namesAndTypes.contains( new NameAndTypes( "/test_get_topic_names_and_types_one", - new ArrayList(Arrays.asList("rcljava/msg/UInt32"))))); + Arrays.asList("rcljava/msg/UInt32")))); assertTrue( "topic 'test_get_topic_names_and_types_two' was not discovered", namesAndTypes.contains( new NameAndTypes( "/test_get_topic_names_and_types_two", - new ArrayList(Arrays.asList("rcljava/msg/UInt32"))))); + Arrays.asList("rcljava/msg/UInt32")))); assertTrue( "topic 'test_get_topic_names_and_types_three' was not discovered", namesAndTypes.contains( new NameAndTypes( "/test_get_topic_names_and_types_three", - new ArrayList(Arrays.asList("rcljava/msg/Empty"))))); + Arrays.asList("rcljava/msg/Empty")))); } }; @@ -1044,13 +1044,13 @@ public void accept(final Collection namesAndTypes) { namesAndTypes.contains( new NameAndTypes( "/test_service_names_and_types_one", - new ArrayList(Arrays.asList("rcljava/srv/AddTwoInts"))))); + Arrays.asList("rcljava/srv/AddTwoInts")))); assertTrue( "service 'test_service_names_and_types_two' was not discovered", namesAndTypes.contains( new NameAndTypes( "/test_service_names_and_types_two", - new ArrayList(Arrays.asList("rcljava/srv/AddTwoInts"))))); + Arrays.asList("rcljava/srv/AddTwoInts")))); } }; @@ -1218,13 +1218,13 @@ public void accept(final Collection local, Collection local, Collection local, Collection local, Collection local, Collection local, Collection local, Collection local, Collection c = Class.forName("org.apache.log4j.BasicConfigurator"); Method m = c.getDeclaredMethod("configure", (Class[]) null); Object o = m.invoke(null, (Object[]) null); } @@ -115,7 +115,9 @@ public final void testSetParameters() throws Exception { RCLFuture> future = new RCLFuture>(); - parametersClient.setParameters(parameters, new TestConsumer(future)); + parametersClient.setParameters( + parameters, + new TestConsumer>(future)); RCLJava.spinUntilComplete(node, future); List setParametersResults = future.get(); @@ -143,7 +145,9 @@ public final void testGetParameters() throws Exception { Arrays.asList(new String[] {"foo", "bar", "baz", "foo.first", "foo.second", "foobar"}); RCLFuture> future = new RCLFuture>(); - parametersClient.getParameters(parameterNames, new TestConsumer(future)); + parametersClient.getParameters( + parameterNames, + new TestConsumer>(future)); RCLJava.spinUntilComplete(node, future); assertEquals(parameters, future.get()); @@ -159,9 +163,12 @@ public final void testListParameters() throws Exception { node.setParameters(parameters); RCLFuture future = - new RCLFuture(); + new RCLFuture(); parametersClient.listParameters( - Arrays.asList(new String[] {"foo", "bar"}), 10, new TestConsumer(future)); + Arrays.asList( + new String[] {"foo", "bar"}), + 10, + new TestConsumer(future)); RCLJava.spinUntilComplete(node, future); assertArrayEquals(new String[] {"foo.first", "foo.second"}, future.get().getNames()); @@ -180,7 +187,8 @@ public final void testDescribeParameters() throws Exception { RCLFuture> future = new RCLFuture>(); parametersClient.describeParameters( - Arrays.asList(new String[] {"foo", "bar"}), new TestConsumer(future)); + Arrays.asList(new String[] {"foo", "bar"}), + new TestConsumer>(future)); List expected = Arrays.asList(new rcl_interfaces.msg.ParameterDescriptor[] { diff --git a/rcljava/src/test/java/org/ros2/rcljava/parameters/SyncParametersClientTest.java b/rcljava/src/test/java/org/ros2/rcljava/parameters/SyncParametersClientTest.java index 21d91688..89b3c346 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/parameters/SyncParametersClientTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/parameters/SyncParametersClientTest.java @@ -59,7 +59,7 @@ public static void setupOnce() throws Exception { { // Configure log4j. Doing this dynamically so that Android does not complain about missing // the log4j JARs, SLF4J uses Android's native logging mechanism instead. - Class c = Class.forName("org.apache.log4j.BasicConfigurator"); + Class c = Class.forName("org.apache.log4j.BasicConfigurator"); Method m = c.getDeclaredMethod("configure", (Class[]) null); Object o = m.invoke(null, (Object[]) null); } diff --git a/rcljava/src/test/java/org/ros2/rcljava/time/TimeSourceTest.java b/rcljava/src/test/java/org/ros2/rcljava/time/TimeSourceTest.java index 84958b86..86bbddff 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/time/TimeSourceTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/time/TimeSourceTest.java @@ -15,6 +15,8 @@ package org.ros2.rcljava.time; +import java.lang.SuppressWarnings; + import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; @@ -162,6 +164,8 @@ public final void testSetRosTimeIsActiveNoNode() { assertTrue(timeSource.getRosTimeIsActive()); } + // there's no generic parameter type information at runtime, so we cannot write any(Consumer.class) + @SuppressWarnings("unchecked") @Test public final void testSetRosTimeIsActiveWithNode() { when(mockedNode.getParameter("use_sim_time")).thenReturn(new ParameterVariant("use_sim_time", false));