Skip to content

Commit 5615610

Browse files
committed
Update ConversionUtils usage
All Contextual consumers of ConversionUtils are now updated to use the ConversionService when appropriate. Also updated the ConversionUtilsTest to make private test classes public.
1 parent 2a58a58 commit 5615610

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

src/main/java/org/scijava/module/DefaultModuleService.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import org.scijava.service.Service;
6060
import org.scijava.thread.ThreadService;
6161
import org.scijava.util.ClassUtils;
62-
import org.scijava.util.ConversionUtils;
62+
import org.scijava.util.conversion.ConversionService;
6363

6464
/**
6565
* Default service for keeping track of and executing available modules.
@@ -91,6 +91,9 @@ public class DefaultModuleService extends AbstractService implements
9191
@Parameter
9292
private PrefService prefService;
9393

94+
@Parameter
95+
private ConversionService conversionService;
96+
9497
/** Index of registered modules. */
9598
private ModuleIndex moduleIndex;
9699

@@ -266,7 +269,7 @@ public <T> void save(final ModuleItem<T> item, final T value) {
266269
final String sValue = value == null ? "" : value.toString();
267270

268271
// do not persist if object cannot be converted back from a string
269-
if (!ConversionUtils.canConvert(sValue, item.getType())) return;
272+
if (!conversionService.supports(sValue, item.getType())) return;
270273

271274
final String persistKey = item.getPersistKey();
272275
if (persistKey == null || persistKey.isEmpty()) {
@@ -304,7 +307,7 @@ public <T> T load(final ModuleItem<T> item) {
304307
// if persisted value has never been set before return null
305308
if (sValue == null) return null;
306309

307-
return ConversionUtils.convert(sValue, item.getType());
310+
return conversionService.convert(sValue, item.getType());
308311
}
309312

310313
// -- Service methods --
@@ -406,7 +409,7 @@ private void assignInputs(final Module module,
406409
}
407410
else {
408411
final Class<?> type = input.getType();
409-
converted = ConversionUtils.convert(value, type);
412+
converted = conversionService.convert(value, type);
410413
if (value != null && converted == null) {
411414
log.error("For input " + name + ": incompatible object " +
412415
value.getClass().getName() + " for type " + type.getName());

src/main/java/org/scijava/module/process/LoadInputsPreprocessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.scijava.plugin.Parameter;
3838
import org.scijava.plugin.Plugin;
3939
import org.scijava.util.ConversionUtils;
40+
import org.scijava.util.conversion.ConversionService;
4041
import org.scijava.widget.InputHarvester;
4142

4243
/**
@@ -57,6 +58,9 @@ public class LoadInputsPreprocessor extends AbstractPreprocessorPlugin {
5758
@Parameter
5859
private ModuleService moduleService;
5960

61+
@Parameter
62+
private ConversionService conversionService;
63+
6064
// -- ModuleProcessor methods --
6165

6266
@Override
@@ -84,9 +88,9 @@ private <T> void loadValue(final Module module, final ModuleItem<T> item) {
8488
private <T> T getBestValue(final Object prefValue,
8589
final Object defaultValue, final Class<T> type)
8690
{
87-
if (prefValue != null) return ConversionUtils.convert(prefValue, type);
91+
if (prefValue != null) return conversionService.convert(prefValue, type);
8892
if (defaultValue != null) {
89-
return ConversionUtils.convert(defaultValue, type);
93+
return conversionService.convert(defaultValue, type);
9094
}
9195
return ConversionUtils.getNullValue(type);
9296
}

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
import org.scijava.module.DefaultMutableModuleItem;
5555
import org.scijava.module.ModuleException;
5656
import org.scijava.plugin.Parameter;
57-
import org.scijava.util.ConversionUtils;
5857
import org.scijava.util.DigestUtils;
5958
import org.scijava.util.FileUtils;
59+
import org.scijava.util.conversion.ConversionService;
6060

6161
/**
6262
* Metadata about a script.
@@ -84,6 +84,9 @@ public class ScriptInfo extends AbstractModuleInfo implements Contextual {
8484
@Parameter
8585
private ScriptService scriptService;
8686

87+
@Parameter
88+
private ConversionService conversionService;
89+
8790
/**
8891
* Creates a script metadata object which describes the given script file.
8992
*
@@ -366,7 +369,7 @@ private HashMap<String, String> parseAttrs(final String attrs)
366369
}
367370

368371
private boolean isIOType(final String token) {
369-
return ConversionUtils.convert(token, ItemIO.class) != null;
372+
return conversionService.convert(token, ItemIO.class) != null;
370373
}
371374

372375
private void checkValid(final boolean valid, final String param)
@@ -408,7 +411,7 @@ else if ("choices".equalsIgnoreCase(key)) {
408411
// item.setChoices(choices);
409412
}
410413
else if ("columns".equalsIgnoreCase(key)) {
411-
item.setColumnCount(ConversionUtils.convert(value, int.class));
414+
item.setColumnCount(conversionService.convert(value, int.class));
412415
}
413416
else if ("description".equalsIgnoreCase(key)) {
414417
item.setDescription(value);
@@ -417,41 +420,41 @@ else if ("initializer".equalsIgnoreCase(key)) {
417420
item.setInitializer(value);
418421
}
419422
else if ("type".equalsIgnoreCase(key)) {
420-
item.setIOType(ConversionUtils.convert(value, ItemIO.class));
423+
item.setIOType(conversionService.convert(value, ItemIO.class));
421424
}
422425
else if ("label".equalsIgnoreCase(key)) {
423426
item.setLabel(value);
424427
}
425428
else if ("max".equalsIgnoreCase(key)) {
426-
item.setMaximumValue(ConversionUtils.convert(value, item.getType()));
429+
item.setMaximumValue(conversionService.convert(value, item.getType()));
427430
}
428431
else if ("min".equalsIgnoreCase(key)) {
429-
item.setMinimumValue(ConversionUtils.convert(value, item.getType()));
432+
item.setMinimumValue(conversionService.convert(value, item.getType()));
430433
}
431434
else if ("name".equalsIgnoreCase(key)) {
432435
item.setName(value);
433436
}
434437
else if ("persist".equalsIgnoreCase(key)) {
435-
item.setPersisted(ConversionUtils.convert(value, boolean.class));
438+
item.setPersisted(conversionService.convert(value, boolean.class));
436439
}
437440
else if ("persistKey".equalsIgnoreCase(key)) {
438441
item.setPersistKey(value);
439442
}
440443
else if ("required".equalsIgnoreCase(key)) {
441-
item.setRequired(ConversionUtils.convert(value, boolean.class));
444+
item.setRequired(conversionService.convert(value, boolean.class));
442445
}
443446
else if ("softMax".equalsIgnoreCase(key)) {
444-
item.setSoftMaximum(ConversionUtils.convert(value, item.getType()));
447+
item.setSoftMaximum(conversionService.convert(value, item.getType()));
445448
}
446449
else if ("softMin".equalsIgnoreCase(key)) {
447-
item.setSoftMinimum(ConversionUtils.convert(value, item.getType()));
450+
item.setSoftMinimum(conversionService.convert(value, item.getType()));
448451
}
449452
else if ("stepSize".equalsIgnoreCase(key)) {
450453
// FIXME
451-
item.setStepSize(ConversionUtils.convert(value, Number.class));
454+
item.setStepSize(conversionService.convert(value, Number.class));
452455
}
453456
else if ("visibility".equalsIgnoreCase(key)) {
454-
item.setVisibility(ConversionUtils.convert(value, ItemVisibility.class));
457+
item.setVisibility(conversionService.convert(value, ItemVisibility.class));
455458
}
456459
else if ("value".equalsIgnoreCase(key)) {
457460
item.setWidgetStyle(value);

src/main/java/org/scijava/script/ScriptModule.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
import org.scijava.module.Module;
5050
import org.scijava.module.ModuleItem;
5151
import org.scijava.plugin.Parameter;
52-
import org.scijava.util.ConversionUtils;
5352
import org.scijava.util.FileUtils;
53+
import org.scijava.util.conversion.ConversionService;
5454

5555
/**
5656
* A {@link Module} which executes a script.
@@ -70,6 +70,9 @@ public class ScriptModule extends AbstractModule implements Contextual {
7070
@Parameter
7171
private ScriptService scriptService;
7272

73+
@Parameter
74+
private ConversionService conversionService;
75+
7376
@Parameter
7477
private LogService log;
7578

@@ -190,7 +193,7 @@ public void run() {
190193
if (isResolved(name)) continue;
191194
final Object value = engine.get(name);
192195
final Object decoded = language.decode(value);
193-
final Object typed = ConversionUtils.convert(decoded, item.getType());
196+
final Object typed = conversionService.convert(decoded, item.getType());
194197
setOutput(name, typed);
195198
}
196199

src/test/java/org/scijava/util/ConversionUtilsTest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,10 @@ private <T> List<T> getValueList(final T... values) {
729729
* Helper class for testing conversion of one {@link ArrayList} subclass to
730730
* another.
731731
*/
732-
private static class HisList extends ArrayList<String> {
732+
public static class HisList extends ArrayList<String> {
733733
public HisList() {
734734
super();
735735
}
736-
@SuppressWarnings("unused")
737736
public HisList(final Collection<? extends String> c) {
738737
super(c);
739738
}
@@ -743,8 +742,7 @@ public HisList(final Collection<? extends String> c) {
743742
* Helper class for testing conversion of one {@link ArrayList} subclass to
744743
* another.
745744
*/
746-
private static class HerList extends ArrayList<String> {
747-
@SuppressWarnings("unused")
745+
public static class HerList extends ArrayList<String> {
748746
public HerList(final Collection<? extends String> c) {
749747
super(c);
750748
}
@@ -754,8 +752,7 @@ public HerList(final Collection<? extends String> c) {
754752
* Helper class for testing conversion of one {@link ArrayList} subclass to
755753
* another.
756754
*/
757-
private static class ObjectList extends ArrayList<Object> {
758-
@SuppressWarnings("unused")
755+
public static class ObjectList extends ArrayList<Object> {
759756
public ObjectList(final Collection<? extends Object> c) {
760757
super(c);
761758
}
@@ -765,13 +762,12 @@ public ObjectList(final Collection<? extends Object> c) {
765762
* Helper class for testing conversion of one {@link ArrayList} subclass to
766763
* another.
767764
*/
768-
private static class NumberList extends ArrayList<Number> implements
765+
public static class NumberList extends ArrayList<Number> implements
769766
INumberList
770767
{
771768
public NumberList() {
772769
super();
773770
}
774-
@SuppressWarnings("unused")
775771
public NumberList(final Collection<? extends Number> c) {
776772
super(c);
777773
}
@@ -790,7 +786,7 @@ private static interface INumberList extends List<Number> {
790786
* recursively convert arrays doesn't consume the array improperly when it
791787
* should be used in the constructor of an object.
792788
*/
793-
private static class ArrayWrapper {
789+
public static class ArrayWrapper {
794790

795791
@SuppressWarnings("unused")
796792
public ArrayWrapper(final int[] gonnaWrapThisArray) {
@@ -803,7 +799,7 @@ public ArrayWrapper(final int[] gonnaWrapThisArray) {
803799
* convert collections doesn't consume the list improperly when it should be
804800
* used in the constructor of an object.
805801
*/
806-
private static class ListWrapper {
802+
public static class ListWrapper {
807803

808804
@SuppressWarnings("unused")
809805
public ListWrapper(final List<?> gonnaWrapThisList) {

0 commit comments

Comments
 (0)