Merge remote-tracking branch 'origin/GP-3554_dev747368_upwtm_1L_checkCancelled'

This commit is contained in:
Ryan Kurtz 2023-06-21 13:03:16 -04:00
commit 231c56d328
6 changed files with 74 additions and 25 deletions

View file

@ -89,7 +89,7 @@ public class DtbAnalyzer extends FileFormatAnalyzer {
monitor.initialize(header.getEntries().size());
for (int i = 0; i < header.getEntries().size(); ++i) {
monitor.checkCanceled();
monitor.checkCancelled();
monitor.incrementProgress(1);
DtTableEntry entry = header.getEntries().get(i);

View file

@ -79,7 +79,7 @@ public class FdtAnalyzer extends FileFormatAnalyzer {
Address address = program.getMinAddress();
while (true) {
monitor.checkCanceled();
monitor.checkCancelled();
if (address.compareTo(program.getMaxAddress()) >= 0) {
break;

View file

@ -24,6 +24,9 @@ import ghidra.util.exception.CancelledException;
* <p>
* This class supports cancelling and cancel listener notification. Cancelling must be enabled
* via {@link #setCancelEnabled(boolean)}.
* <p>
* Use {@link WrappingTaskMonitor} if you need to override an existing TaskMonitor
* instance's behavior.
*/
public class TaskMonitorAdapter implements TaskMonitor {
@ -54,6 +57,7 @@ public class TaskMonitorAdapter implements TaskMonitor {
return cancelled;
}
@Deprecated(since = "10.3")
@Override
public void checkCanceled() throws CancelledException {
if (cancelled) {
@ -61,6 +65,13 @@ public class TaskMonitorAdapter implements TaskMonitor {
}
}
@Override
public void checkCancelled() throws CancelledException {
if (cancelled) {
throw new CancelledException();
}
}
@Override
public void setMessage(String message) {
// do nothing

View file

@ -133,11 +133,17 @@ public class WrappingTaskMonitor implements TaskMonitor {
return delegate.isIndeterminate();
}
@Deprecated(since = "10.3")
@Override
public void checkCanceled() throws CancelledException {
delegate.checkCancelled();
}
@Override
public void checkCancelled() throws CancelledException {
delegate.checkCancelled();
}
@Override
public void incrementProgress(long incrementAmount) {
delegate.incrementProgress(incrementAmount);

View file

@ -15,48 +15,29 @@
*/
package ghidra.util.task;
import ghidra.util.exception.CancelledException;
/**
* A class that is meant to wrap a {@link TaskMonitor} when you do not know the maximum value
* of the progress.
*/
public class UnknownProgressWrappingTaskMonitor extends TaskMonitorAdapter {
private TaskMonitor delegate;
public class UnknownProgressWrappingTaskMonitor extends WrappingTaskMonitor {
public UnknownProgressWrappingTaskMonitor(TaskMonitor delegate, long startMaximum) {
this.delegate = delegate;
super(delegate);
delegate.setMaximum(startMaximum);
}
@Override
public void setMessage(String message) {
delegate.setMessage(message);
}
@Override
public void setProgress(long value) {
delegate.setProgress(value);
super.setProgress(value);
maybeUpdateMaximum();
}
@Override
public void incrementProgress(long incrementAmount) {
delegate.incrementProgress(incrementAmount);
super.incrementProgress(incrementAmount);
maybeUpdateMaximum();
}
@Override
public synchronized boolean isCancelled() {
return delegate.isCancelled();
}
@Override
public void checkCancelled() throws CancelledException {
delegate.checkCancelled();
}
private void maybeUpdateMaximum() {
long currentMaximum = delegate.getMaximum();
long progress = delegate.getProgress();

View file

@ -0,0 +1,51 @@
/* ###
* 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.util.task;
import static org.junit.Assert.*;
import org.junit.Test;
import generic.test.AbstractGenericTest;
import ghidra.util.exception.CancelledException;
public class UnknownProgressWrappingTaskMonitorTest extends AbstractGenericTest {
@Test
public void testUPWTM_checkCanceled_1L_vs_2L() {
TaskMonitorAdapter monitor = new TaskMonitorAdapter(true);
monitor.cancel();
UnknownProgressWrappingTaskMonitor upwtm =
new UnknownProgressWrappingTaskMonitor(monitor, 100);
try {
upwtm.checkCanceled();
fail();
}
catch (CancelledException e) {
// good
}
try {
upwtm.checkCancelled();
fail();
}
catch (CancelledException e) {
// good
}
}
}