Skip to content

Commit 73239ad

Browse files
committed
Use ParseService
Thanks @ctrueden for the suggestion
1 parent 17da84f commit 73239ad

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

src/main/java/org/scijava/convert/StringToArrayConverter.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
package org.scijava.convert;
3131

3232
import org.scijava.Priority;
33+
import org.scijava.parse.Items;
34+
import org.scijava.parse.ParseService;
3335
import org.scijava.parsington.ExpressionParser;
3436
import org.scijava.parsington.SyntaxTree;
3537
import org.scijava.plugin.Parameter;
@@ -53,6 +55,9 @@ public class StringToArrayConverter extends AbstractConverter<String, Object> {
5355
@Parameter
5456
private ConvertService convertService;
5557

58+
@Parameter
59+
private ParseService parseService;
60+
5661
private final ExpressionParser parser = new ExpressionParser();
5762

5863
@Override
@@ -101,8 +106,9 @@ public Object convert(Object src, Type dest) {
101106
throw new IllegalArgumentException(dest + " is not an array type!");
102107
}
103108
try {
104-
SyntaxTree tree = parser.parseTree((String) src);
105-
return convertToArray(tree, Types.raw(componentType));
109+
return convertToArray( //
110+
parseService.parse((String) src), //
111+
Types.raw(componentType));
106112
}
107113
catch (IllegalArgumentException e) {
108114
return null;
@@ -135,21 +141,12 @@ public Class<String> getInputType() {
135141
* @return an array of {@code componentType} whose elements were created from
136142
* {@code src}
137143
*/
138-
private Object convertToArray(SyntaxTree tree, final Class<?> componentType) {
144+
private Object convertToArray(Items tree, final Class<?> componentType) {
139145
// Create the array
140-
final Object array = Array.newInstance(componentType, tree.count());
146+
final Object array = Array.newInstance(componentType, tree.size());
141147
// Set each element of the array
142-
for (int i = 0; i < tree.count(); i++) {
143-
SyntaxTree subTree = tree.child(i);
144-
Object element;
145-
// Case 1: Element is an array
146-
if (componentType.isArray()) {
147-
element = convertToArray(subTree, componentType.getComponentType());
148-
}
149-
// Case 2: Element is a single object
150-
else {
151-
element = convertService.convert(subTree.token(), componentType);
152-
}
148+
for (int i = 0; i < tree.size(); i++) {
149+
Object element = convertService.convert(tree.get(i).value(), componentType);
153150
Array.set(array, i, element);
154151
}
155152
return array;

src/test/java/org/scijava/convert/ArrayToStringConverterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.junit.Before;
3838
import org.junit.Test;
3939
import org.scijava.Context;
40+
import org.scijava.parse.ParseService;
4041

4142
/**
4243
* Tests {@link ArrayToStringConverter}.
@@ -50,7 +51,7 @@ public class ArrayToStringConverterTest {
5051

5152
@Before
5253
public void setUp() {
53-
context = new Context(ConvertService.class);
54+
context = new Context(ConvertService.class, ParseService.class);
5455
context.inject(converter);
5556
}
5657

src/test/java/org/scijava/convert/StringToArrayConverterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.Before;
3535
import org.junit.Test;
3636
import org.scijava.Context;
37+
import org.scijava.parse.ParseService;
3738

3839
import java.lang.reflect.Array;
3940
import java.util.Arrays;
@@ -52,7 +53,7 @@ public class StringToArrayConverterTest {
5253

5354
@Before
5455
public void setUp() {
55-
context = new Context(ConvertService.class);
56+
context = new Context(ParseService.class, ConvertService.class);
5657
context.inject(converter);
5758
convertService = context.getService(ConvertService.class);
5859
}

src/test/java/org/scijava/module/ModuleServiceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.junit.Before;
4747
import org.junit.Test;
4848
import org.scijava.Context;
49+
import org.scijava.parse.ParseService;
4950

5051
/**
5152
* Tests {@link ModuleService}.
@@ -58,7 +59,7 @@ public class ModuleServiceTest {
5859

5960
@Before
6061
public void setUp() {
61-
final Context context = new Context(ModuleService.class);
62+
final Context context = new Context(ModuleService.class, ParseService.class);
6263
moduleService = context.service(ModuleService.class);
6364
}
6465

0 commit comments

Comments
 (0)