|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2014 - 2018 ImageJ 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.matcher; |
| 31 | + |
| 32 | +import java.lang.reflect.Field; |
| 33 | +import java.lang.reflect.Modifier; |
| 34 | +import java.lang.reflect.Type; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +import org.scijava.core.Priority; |
| 39 | +import org.scijava.ops.OpField; |
| 40 | +import org.scijava.ops.OpUtils; |
| 41 | +import org.scijava.param.ParameterStructs; |
| 42 | +import org.scijava.param.ValidityException; |
| 43 | +import org.scijava.param.ValidityProblem; |
| 44 | +import org.scijava.struct.Struct; |
| 45 | +import org.scijava.struct.StructInstance; |
| 46 | + |
| 47 | +/** |
| 48 | + * Metadata about an op implementation defined as a field. |
| 49 | + * |
| 50 | + * @author Curtis Rueden |
| 51 | + */ |
| 52 | +public class OpFieldInfo implements OpInfo { |
| 53 | + |
| 54 | + private final Field field; |
| 55 | + private Struct struct; |
| 56 | + private ValidityException validityException; |
| 57 | + |
| 58 | + public OpFieldInfo(final Field field) { |
| 59 | + |
| 60 | + List<ValidityProblem> problems = new ArrayList<>(); |
| 61 | + // Reject all non public fields |
| 62 | + if (!Modifier.isPublic(field.getModifiers())) { |
| 63 | + problems.add(new ValidityProblem("Field to parse: " + field + " must be public.")); |
| 64 | + } |
| 65 | + |
| 66 | + // NB: Subclassing a collection and inheriting its fields is NOT |
| 67 | + // ALLOWED! |
| 68 | + this.field = field; |
| 69 | + try { |
| 70 | + struct = ParameterStructs.structOf(field.getDeclaringClass(), field); |
| 71 | + } catch (ValidityException e) { |
| 72 | + problems.addAll(e.problems()); |
| 73 | + } |
| 74 | + if (!problems.isEmpty()) { |
| 75 | + validityException = new ValidityException(problems); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // -- OpInfo methods -- |
| 80 | + |
| 81 | + @Override |
| 82 | + public Type opType() { |
| 83 | + return field.getGenericType(); |
| 84 | + // return Types.fieldType(field, subClass); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Struct struct() { |
| 89 | + return struct; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public double priority() { |
| 94 | + final OpField opField = field.getAnnotation(OpField.class); |
| 95 | + return opField == null ? Priority.NORMAL : opField.priority(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String implementationName() { |
| 100 | + return field.getDeclaringClass().getName() + "." + field.getName(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public StructInstance<?> createOp() { |
| 105 | + // 1. Can we create another instance of the same function by calling |
| 106 | + // clone()? |
| 107 | + // 2. _SHOULD_ we do that? Or can we simply reuse the same function |
| 108 | + // instance every time? |
| 109 | + try { |
| 110 | + final Object object = field.get(null); // NB: value of static field! |
| 111 | + return struct().createInstance(object); |
| 112 | + } catch (final IllegalAccessException exc) { |
| 113 | + // FIXME |
| 114 | + exc.printStackTrace(); |
| 115 | + throw new RuntimeException(exc); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public ValidityException getValidityException() { |
| 121 | + return validityException; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean isValid() { |
| 126 | + return validityException == null; |
| 127 | + } |
| 128 | + |
| 129 | + // -- Object methods -- |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean equals(final Object o) { |
| 133 | + if (!(o instanceof OpFieldInfo)) |
| 134 | + return false; |
| 135 | + final OpInfo that = (OpInfo) o; |
| 136 | + return struct().equals(that.struct()); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int hashCode() { |
| 141 | + return struct().hashCode(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String toString() { |
| 146 | + return OpUtils.opString(this); |
| 147 | + } |
| 148 | +} |
0 commit comments