f) {
+ assertNotNull("Program cannot be null", program);
+
+ R result = null;
+ boolean commit = false;
+ int tx = program.startTransaction("Test");
+ try {
+ result = f.apply(program);
+ commit = true;
+ }
+ catch (Exception e) {
+ failWithException("Exception modifying program '" + program.getName() + "'", e);
+ }
+ finally {
+ program.endTransaction(tx, commit);
+ }
+ return result;
+ }
+
/**
* Undo the last transaction on the domain object and wait for all events to be flushed.
* @param dobj The domain object upon which to perform the undo.
@@ -371,6 +445,7 @@ public abstract class AbstractGhidraHeadlessIntegrationTest extends AbstractDock
*
* @param program the program to search.
* @param name the name of the symbol to find.
+ * @param namespace the parent namespace; may be null
* @return the symbol with the given name if and only if it is the only one in that namespace
*/
public Symbol getUniqueSymbol(Program program, String name, Namespace namespace) {
@@ -389,6 +464,8 @@ public abstract class AbstractGhidraHeadlessIntegrationTest extends AbstractDock
* sleeping.
*
* Do not leave this call in your test when committing changes.
+ * @param p the program
+ * @param address the address
*
* @throws Exception if there is an issue create a {@link TestEnv}
*/
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractProgramBasedTest.java b/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractProgramBasedTest.java
index 21056f04a8..790bbacdbe 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractProgramBasedTest.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractProgramBasedTest.java
@@ -25,6 +25,8 @@ import org.junit.After;
import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin;
import ghidra.framework.plugintool.PluginTool;
+import ghidra.generic.function.ExceptionalConsumer;
+import ghidra.generic.function.ExceptionalFunction;
import ghidra.program.model.address.*;
import ghidra.program.model.listing.*;
import ghidra.program.util.ProgramLocation;
@@ -68,6 +70,7 @@ public abstract class AbstractProgramBasedTest extends AbstractGhidraHeadedInteg
* Override this method if you need to build your own program.
*
* @return the program to use for this test.
+ * @throws Exception if an exceptioun is thrown opening the program
*/
protected Program getProgram() throws Exception {
return env.getProgram(getProgramName());
@@ -163,13 +166,13 @@ public abstract class AbstractProgramBasedTest extends AbstractGhidraHeadedInteg
*
* @param callback the code to execute
*/
- public void modifyProgram(ExceptionalCallback callback) {
+ public void modifyProgram(ExceptionalConsumer callback) {
assertNotNull("Program cannot be null", program);
boolean commit = false;
int tx = program.startTransaction("Test");
try {
- callback.call(program);
+ callback.accept(program);
commit = true;
}
catch (Exception e) {
@@ -187,7 +190,7 @@ public abstract class AbstractProgramBasedTest extends AbstractGhidraHeadedInteg
* @param f the function for modifying the program and creating the desired result
* @return the result
*/
- public R createInProgram(ExceptionalFunction f) {
+ public R createInProgram(ExceptionalFunction f) {
assertNotNull("Program cannot be null", program);
R result = null;
@@ -205,18 +208,4 @@ public abstract class AbstractProgramBasedTest extends AbstractGhidraHeadedInteg
}
return result;
}
-
-//==================================================================================================
-// Inner Classes
-//==================================================================================================
-
- // TODO promote this functional interface (do this later; there is an outstanding branch
- // that has other functional interfaces with which to fraternize)
- public interface ExceptionalCallback {
- public void call(T t) throws Exception;
- }
-
- public interface ExceptionalFunction {
- public R apply(T t) throws Exception;
- }
}
diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalCallback.java b/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalCallback.java
new file mode 100644
index 0000000000..36741294d9
--- /dev/null
+++ b/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalCallback.java
@@ -0,0 +1,33 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed 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 ghidra.generic.function;
+
+/**
+ * A generic functional interface that is more semantically sound than {@link Runnable}. Use
+ * anywhere you wish to have a generic callback function and you need to throw an exception.
+ *
+ * @param the exception of your choice
+ */
+@FunctionalInterface
+public interface ExceptionalCallback {
+
+ /**
+ * The method that will be called
+ *
+ * @throws Exception if the call throws an exception
+ */
+ public void call() throws E;
+}
diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalConsumer.java b/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalConsumer.java
new file mode 100644
index 0000000000..f39088f0a9
--- /dev/null
+++ b/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalConsumer.java
@@ -0,0 +1,35 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed 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 ghidra.generic.function;
+
+/**
+ * A generic functional interface that allows you to consume an item and potentially throw
+ * an exception.
+ *
+ * @param the input type
+ * @param the exception of your choice
+ */
+@FunctionalInterface
+public interface ExceptionalConsumer {
+
+ /**
+ * The method that will be called
+ *
+ * @param t the input
+ * @throws E if the call throws an exception
+ */
+ public void accept(T t) throws E;
+}
diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalFunction.java b/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalFunction.java
new file mode 100644
index 0000000000..82a0f2324d
--- /dev/null
+++ b/Ghidra/Framework/Generic/src/main/java/ghidra/generic/function/ExceptionalFunction.java
@@ -0,0 +1,37 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed 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 ghidra.generic.function;
+
+/**
+ * A generic functional interface that allows you to consume an item, return a result,
+ * and potentially throw an exception.
+ *
+ * @param the input type
+ * @param the result type
+ * @param the exception of your choice
+ */
+@FunctionalInterface
+public interface ExceptionalFunction {
+
+ /**
+ * The method that will be called
+ *
+ * @param i the input
+ * @return the result of the call
+ * @throws E if the call throws an exception
+ */
+ public R apply(I i) throws E;
+}