This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch karaf-4.4.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.4.x by this push:
     new 8df799d77f Check for annotation in ActionCommand constructor
8df799d77f is described below

commit 8df799d77f92e1640e0a808d347b2a6ed7214abf
Author: pingpingy1 <pingpi...@kaist.ac.kr>
AuthorDate: Mon Mar 18 18:05:38 2024 +0900

    Check for annotation in ActionCommand constructor
    
    The `ActionCommand` class takes an `actionClass` parameter for its
    constructor, whose `@Command` annotation accessed in the other
    methods. Thus, if the provided `actionClass` has no such annotations,
    then it will throw an NPE at a later, unspecified time.
    This commit implements a fail-fast guard against this scenarios.
    
    (cherry picked from commit 5ad710722c38bd3f1b0487a584c8dfd71d5f7931)
---
 .../shell/impl/action/command/ActionCommand.java   |  4 +++
 .../impl/action/command/ActionCommandTest.java     | 35 ++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git 
a/shell/core/src/main/java/org/apache/karaf/shell/impl/action/command/ActionCommand.java
 
b/shell/core/src/main/java/org/apache/karaf/shell/impl/action/command/ActionCommand.java
index 0b815f648e..4126225978 100644
--- 
a/shell/core/src/main/java/org/apache/karaf/shell/impl/action/command/ActionCommand.java
+++ 
b/shell/core/src/main/java/org/apache/karaf/shell/impl/action/command/ActionCommand.java
@@ -35,6 +35,10 @@ public class ActionCommand implements 
org.apache.karaf.shell.api.console.Command
     private final Class<? extends Action> actionClass;
 
     public ActionCommand(ManagerImpl manager, Class<? extends Action> 
actionClass) {
+        if (actionClass.getAnnotation(Command.class) == null) {
+            throw new IllegalArgumentException("actionClass must have an 
@Command annotation.");
+        }
+        
         this.manager = manager;
         this.actionClass = actionClass;
     }
diff --git 
a/shell/core/src/test/java/org/apache/karaf/shell/impl/action/command/ActionCommandTest.java
 
b/shell/core/src/test/java/org/apache/karaf/shell/impl/action/command/ActionCommandTest.java
new file mode 100644
index 0000000000..9f67c5a23f
--- /dev/null
+++ 
b/shell/core/src/test/java/org/apache/karaf/shell/impl/action/command/ActionCommandTest.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.karaf.shell.impl.action.command;
+
+import org.apache.karaf.shell.api.action.Action;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertThrows;
+
+public class ActionCommandTest {
+
+       abstract static class ExampleClass implements Action {}
+
+       @Test
+       public void testUnannotatedActionClass() {
+               assertThrows(IllegalArgumentException.class, () -> new 
ActionCommand(null, ExampleClass.class));
+       }
+}
\ No newline at end of file

Reply via email to