|
| 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.Type; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import org.scijava.core.Priority; |
| 36 | +import org.scijava.ops.OpUtils; |
| 37 | +import org.scijava.ops.core.Op; |
| 38 | +import org.scijava.param.ParameterStructs; |
| 39 | +import org.scijava.param.ValidityException; |
| 40 | +import org.scijava.plugin.Plugin; |
| 41 | +import org.scijava.struct.Member; |
| 42 | +import org.scijava.struct.Struct; |
| 43 | +import org.scijava.struct.StructInstance; |
| 44 | +import org.scijava.util.Types; |
| 45 | + |
| 46 | +/** |
| 47 | + * Metadata about an op implementation defined as a class. |
| 48 | + * |
| 49 | + * @author Curtis Rueden |
| 50 | + * @author David Kolb |
| 51 | + */ |
| 52 | +public class OpClassInfo implements OpInfo { |
| 53 | + |
| 54 | + private final Class<? extends Op> opClass; |
| 55 | + private Struct struct; |
| 56 | + private ValidityException validityException; |
| 57 | + |
| 58 | + public OpClassInfo(final Class<? extends Op> opClass) { |
| 59 | + this.opClass = opClass; |
| 60 | + try { |
| 61 | + struct = ParameterStructs.structOf(opClass); |
| 62 | + } catch (ValidityException e) { |
| 63 | + validityException = e; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // -- OpInfo methods -- |
| 68 | + |
| 69 | + @Override |
| 70 | + public Type opType() { |
| 71 | + // TODO: Check whether this is correct! |
| 72 | + return Types.parameterizeRaw(opClass); |
| 73 | + //return opClass; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Struct struct() { |
| 78 | + return struct; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public List<Member<?>> inputs() { |
| 83 | + return OpUtils.inputs(struct()); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public List<Member<?>> outputs() { |
| 88 | + return OpUtils.outputs(struct()); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public double priority() { |
| 93 | + final Plugin opAnnotation = opClass.getAnnotation(Plugin.class); |
| 94 | + return opAnnotation == null ? Priority.NORMAL : opAnnotation.priority(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String implementationName() { |
| 99 | + return opClass.getName(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public StructInstance<?> createOp() { |
| 104 | + final Object object; |
| 105 | + try { |
| 106 | + // TODO: Consider whether this is really the best way to |
| 107 | + // instantiate the op class here. No framework usage? |
| 108 | + // E.g., what about pluginService.createInstance? |
| 109 | + object = opClass.newInstance(); |
| 110 | + } catch (final InstantiationException | IllegalAccessException e) { |
| 111 | + // TODO: Think about whether exception handling here should be |
| 112 | + // different. |
| 113 | + throw new IllegalStateException("Unable to instantiate op: '" + opClass.getName() |
| 114 | + + "' Each op must have a no-args constructor.", e); |
| 115 | + } |
| 116 | + return struct().createInstance(object); |
| 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 OpClassInfo)) |
| 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