Skip to content

Commit b33ea4f

Browse files
Treiblesschorlectrueden
authored andcommitted
Allow non-static fields
1 parent 026300f commit b33ea4f

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/main/java/org/scijava/ops/matcher/OpFieldInfo.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class OpFieldInfo implements OpInfo {
5454
private final Field field;
5555
private Struct struct;
5656
private ValidityException validityException;
57+
private Object instance;
5758

5859
public OpFieldInfo(final Field field) {
5960

@@ -68,12 +69,19 @@ public OpFieldInfo(final Field field) {
6869
this.field = field;
6970
try {
7071
struct = ParameterStructs.structOf(field.getDeclaringClass(), field);
72+
if (!Modifier.isStatic(field.getModifiers())) {
73+
instance = field.getDeclaringClass().newInstance();
74+
}
75+
// NB: Contextual parameters not supported for now.
7176
} catch (ValidityException e) {
7277
problems.addAll(e.problems());
78+
} catch (InstantiationException | IllegalAccessException e) {
79+
problems.add(new ValidityProblem("Could not instantiate field's class", e));
7380
}
7481
if (!problems.isEmpty()) {
7582
validityException = new ValidityException(problems);
7683
}
84+
7785
}
7886

7987
// -- OpInfo methods --
@@ -107,7 +115,7 @@ public StructInstance<?> createOp() {
107115
// 2. _SHOULD_ we do that? Or can we simply reuse the same function
108116
// instance every time?
109117
try {
110-
final Object object = field.get(null); // NB: value of static field!
118+
final Object object = field.get(instance); // NB: value of static field!
111119
return struct().createInstance(object);
112120
} catch (final IllegalAccessException exc) {
113121
// FIXME

src/main/java/org/scijava/param/ParameterStructs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static List<Member<?>> parse(final Class<?> c, final Field field) throws
8585
final Set<String> names = new HashSet<>();
8686
final Type fieldType = Types.fieldType(field, c);
8787

88-
checkModifiers(field.toString() + ": ", problems, field.getModifiers(), false, Modifier.STATIC, Modifier.FINAL);
88+
checkModifiers(field.toString() + ": ", problems, field.getModifiers(), false, Modifier.FINAL);
8989
parseFunctionalParameters(items, names, problems, field, fieldType);
9090

9191
// Fail if there were any problems.

0 commit comments

Comments
 (0)