Skip to content

Commit 28b88dc

Browse files
committed
Merge branch 'op-exceptions-162' into main
Closes #162 Closes #156
2 parents 55a0a7a + 6f3eb29 commit 28b88dc

8 files changed

Lines changed: 185 additions & 16 deletions

File tree

‎scijava/scijava-ops-api/src/main/java/org/scijava/ops/api/OpEnvironment.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,10 @@ default OpBuilder.Arity16 arity16(final String opName, final Hints hints) {
784784
* Creates an {@link OpInfo} from an {@link Class}.
785785
*
786786
* @param opClass the {@link Class} to derive the Op from
787+
* @param names - the name(s) of the Op
787788
* @return an {@link OpInfo} which can make instances of {@code opClass}
788789
*/
789-
OpInfo opify(Class<?> opClass);
790+
OpInfo opify(Class<?> opClass, String... names);
790791

791792
/**
792793
* Creates an {@link OpInfo} from an {@link Class} with the given priority.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*-
2+
* #%L
3+
* SciJava Operations Engine: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2016 - 2023 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ops.engine.exceptions.impl;
31+
32+
import org.scijava.ops.engine.exceptions.InvalidOpException;
33+
34+
/**
35+
* An {@link InvalidOpException} arising when no names are given for the Op
36+
*
37+
* @author Mark Hiner
38+
*/
39+
public class UnnamedOpException extends InvalidOpException {
40+
41+
public UnnamedOpException(Object op) {
42+
super("No names declared for Op: " + op + " - all Ops must have at least one name");
43+
}
44+
}

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/impl/DefaultOpEnvironment.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ public Type genericType(Object obj) {
301301
}
302302

303303
@Override
304-
public OpInfo opify(final Class<?> opClass) {
305-
return opify(opClass, Priority.NORMAL);
304+
public OpInfo opify(final Class<?> opClass, String... names) {
305+
return opify(opClass, Priority.NORMAL, names);
306306
}
307307

308308
@Override
309309
public OpInfo opify(final Class<?> opClass, final double priority,
310-
final String... names)
310+
String... names)
311311
{
312312
return new OpClassInfo(opClass, Versions.getVersion(opClass), new Hints(), priority,
313313
names);

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/util/Infos.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@
3232
import java.lang.reflect.ParameterizedType;
3333
import java.lang.reflect.Type;
3434
import java.lang.reflect.TypeVariable;
35-
import java.util.ArrayList;
36-
import java.util.Arrays;
37-
import java.util.List;
38-
import java.util.Optional;
35+
import java.util.*;
3936
import java.util.stream.Collectors;
4037

4138
import org.scijava.ops.api.OpInfo;
4239
import org.scijava.ops.engine.OpDependencyMember;
40+
import org.scijava.ops.engine.exceptions.InvalidOpException;
4341
import org.scijava.ops.engine.exceptions.impl.MultipleOutputsOpException;
42+
import org.scijava.ops.engine.exceptions.impl.UnnamedOpException;
4443
import org.scijava.struct.ItemIO;
4544
import org.scijava.struct.Member;
4645
import org.scijava.types.Types;
@@ -96,6 +95,9 @@ public static void validate(final OpInfo info) {
9695
if (numOutputs > 1) {
9796
throw new MultipleOutputsOpException(info.implementationName());
9897
}
98+
if (Objects.isNull(info.names()) || info.names().isEmpty()) {
99+
throw new UnnamedOpException(info.implementationName());
100+
}
99101
}
100102

101103
/**

‎scijava/scijava-ops-engine/src/test/java/org/scijava/ops/engine/OpEnvironmentTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public class OpEnvironmentTest extends AbstractTestEnvironment {
4747

4848
@Test
4949
public void testClassOpification() {
50-
OpInfo opifyOpInfo = ops.opify(OpifyOp.class);
50+
OpInfo opifyOpInfo = ops.opify(OpifyOp.class, OpifyOp.class.getName());
5151
Assertions.assertEquals(OpifyOp.class.getName(), opifyOpInfo.implementationName());
5252
// assert default priority
5353
Assertions.assertEquals(Priority.NORMAL, opifyOpInfo.priority(), 0.);
5454
}
5555

5656
@Test
5757
public void testClassOpificationWithPriority() {
58-
OpInfo opifyOpInfo = ops.opify(OpifyOp.class, Priority.HIGH);
58+
OpInfo opifyOpInfo = ops.opify(OpifyOp.class, Priority.HIGH, OpifyOp.class.getName());
5959
Assertions.assertEquals(OpifyOp.class.getName(), opifyOpInfo.implementationName());
6060
// assert default priority
6161
Assertions.assertEquals(Priority.HIGH, opifyOpInfo.priority(), 0.);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*-
2+
* #%L
3+
* SciJava Operations Engine: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2016 - 2023 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ops.engine.impl;
31+
32+
import org.junit.jupiter.api.Test;
33+
import org.scijava.ops.engine.AbstractTestEnvironment;
34+
import org.scijava.ops.engine.exceptions.impl.UnnamedOpException;
35+
import org.scijava.ops.spi.OpCollection;
36+
37+
import java.util.function.Function;
38+
39+
import static org.junit.jupiter.api.Assertions.assertThrows;
40+
41+
/**
42+
* Tests for {@link UnnamedOpException}
43+
*/
44+
public class UnnamedOpExceptionTest extends AbstractTestEnvironment
45+
implements OpCollection {
46+
47+
@Test
48+
public void testUnnamedOpification() {
49+
assertThrows(UnnamedOpException.class, () -> ops.opify(UnnamedOp.class));
50+
}
51+
}
52+
53+
class UnnamedOp implements Function<String, Long> {
54+
55+
@Override
56+
public Long apply(String s) {
57+
return 2l;
58+
}
59+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*-
2+
* #%L
3+
* SciJava Ops Indexer: An annotation processor for indexing Ops
4+
* %%
5+
* Copyright (C) 2021 - 2023 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ops.indexer;
31+
32+
/**
33+
* Compile-time parallel to org.scijava.ops.engine.exceptions.InvalidOpException
34+
* in scijava-ops-engine.
35+
*/
36+
class InvalidOpImplException extends RuntimeException {
37+
38+
protected InvalidOpImplException(final String message) { super(message); }
39+
}

‎scijava/scijava-ops-indexer/src/main/java/org/scijava/ops/indexer/OpImplData.java‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@
3333
import static org.scijava.ops.indexer.ProcessingUtils.tagElementSeparator;
3434

3535
import java.net.URI;
36-
import java.util.ArrayList;
37-
import java.util.Arrays;
38-
import java.util.Collections;
39-
import java.util.HashMap;
40-
import java.util.List;
41-
import java.util.Map;
36+
import java.util.*;
4237
import java.util.stream.Collectors;
4338

4439
import javax.annotation.processing.ProcessingEnvironment;
@@ -113,6 +108,31 @@ public OpImplData(Element source, String doc, ProcessingEnvironment env) {
113108
if (!remaining.isEmpty()) {
114109
parseAdditionalTags(source, remaining);
115110
}
111+
validateOpImpl();
112+
}
113+
114+
/**
115+
* Helper method to ensure this OpImpl is valid. Throws
116+
* {@link InvalidOpImplException} i
117+
* problems are detected.
118+
*
119+
* Parallel compile-time implementation of org.scijava.ops.engine.util.Infos#validate
120+
* in scijava-ops-engine
121+
*/
122+
private void validateOpImpl() {
123+
if (Objects.isNull(names) || names.isEmpty()) {
124+
throw new InvalidOpImplException(
125+
"Invalid Op defined in : " + source + ". Op names cannot be empty!");
126+
}
127+
128+
int outputs = 0;
129+
for (OpParameter p : params) {
130+
if (p.ioType.equals(OpParameter.IO_TYPE.OUTPUT)) outputs++;
131+
}
132+
if (outputs > 1) {
133+
throw new InvalidOpImplException(
134+
"Invalid Op defined in : " + source + ". Ops cannot have more than one output!");
135+
}
116136
}
117137

118138
private List<String[]> parseUniversalTags(List<String[]> tags) {
@@ -147,6 +167,10 @@ else if (tag[0].equals("@implNote")) {
147167

148168
abstract String formulateSource(Element source);
149169

170+
/**
171+
* Method for parsing the actual "@implNote" tag
172+
* @param implTag Tag to parse
173+
*/
150174
private void parseImplNote(String implTag) {
151175
var implElements = tagElementSeparator.split(implTag);
152176
if (implElements.length > 1) {

0 commit comments

Comments
 (0)