Skip to content

Commit 1e98018

Browse files
committed
Add compile-time OpImpl validation
While we already have Infos validation for Ops added in runtime ways, there are things we can validate easily at the OpImpl level as well, which will provide compile-time failures when possible.
1 parent e786f24 commit 1e98018

2 files changed

Lines changed: 69 additions & 6 deletions

File tree

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)