Skip to content

Commit

Permalink
MetadataWrapper: remove plugin annotations
Browse files Browse the repository at this point in the history
All ReaderFilters define the Metadata they are bound to in their
constructor. There is really no reason to be trying to dynamically
discover these. The Filter should be the unit of discovery, with any
accessory classes bound by their use.

This also removes the messy @attr use in several MetadataWrapper
implementations.

The AbstractReaderFilter#setParent method has been updated to use the
known metadata class (if available) to create a new MetadataWrapper and
wrap the parent metadata. This logic is significantly simplified.
  • Loading branch information
hinerm committed Dec 11, 2013
1 parent e750b2b commit be67219
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 89 deletions.
54 changes: 21 additions & 33 deletions scifio/src/main/java/io/scif/filters/AbstractReaderFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.scijava.Context;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.plugin.PluginInfo;
import org.scijava.plugin.PluginService;
import org.scijava.util.ClassUtils;

/**
* Abstract superclass for all {@link io.scif.filters.Filter} that delegate to
Expand Down Expand Up @@ -82,7 +79,7 @@ public abstract class AbstractReaderFilter extends AbstractFilter<Reader>
/* Need to wrap each Reader's Metadata separately */
private Metadata wrappedMeta = null;

private final Class<? extends Metadata> metaClass;
private final Class<? extends MetadataWrapper> metaClass;

@Parameter
private PluginService pluginService;
Expand All @@ -93,7 +90,8 @@ public AbstractReaderFilter() {
this(null);
}

public AbstractReaderFilter(final Class<? extends Metadata> metaClass) {
public AbstractReaderFilter(final Class<? extends MetadataWrapper> metaClass)
{
super(Reader.class);
this.metaClass = metaClass;
}
Expand Down Expand Up @@ -151,36 +149,26 @@ public void setParent(final Object parent) {

final Reader r = (Reader) parent;

// TODO Maybe cache this result so we don't have to discover every time
// setparent is called
// because it will be called frequently, given how MasterFilterHelper is
// implemented

final List<PluginInfo<MetadataWrapper>> wrapperInfos =
getContext().getPluginIndex().getPlugins(MetadataWrapper.class);

// look for a compatible MetadataWrapper class
for (final PluginInfo<MetadataWrapper> info : wrapperInfos) {
final String wrapperClassName = info.get(MetadataWrapper.METADATA_KEY);

if (wrapperClassName != null) {
final Class<?> wrapperClass = ClassUtils.loadClass(wrapperClassName);
if (wrapperClass == null) {
log().error("Failed to find class: " + wrapperClassName);
continue;
}
if (wrapperClass.isAssignableFrom(getClass())) {
final MetadataWrapper metaWrapper =
getContext().getService(PluginService.class).createInstance(info);
metaWrapper.wrap(r.getMetadata());
wrappedMeta = metaWrapper;
return;
}
if (metaClass != null) {
MetadataWrapper wrapper = null;
try {
wrappedMeta = wrapper = metaClass.newInstance();
getContext().inject(wrapper);
wrapper.wrap(r.getMetadata());
}
catch (InstantiationException e) {
log()
.error("Failed to create MetadataWrapper of type: " + metaClass, e);
}
catch (IllegalAccessException e) {
log()
.error("Failed to create MetadataWrapper of type: " + metaClass, e);
}
}

// No Filter-specific wrapper found
wrappedMeta = r.getMetadata();
else {
// No Filter-specific wrapper found
wrappedMeta = r.getMetadata();
}
}

@Override
Expand Down
26 changes: 7 additions & 19 deletions scifio/src/main/java/io/scif/filters/ChannelFillerMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
import net.imglib2.display.ColorTable;
import net.imglib2.meta.Axes;

import org.scijava.plugin.Attr;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/**
* {@link io.scif.filters.MetadataWrapper} implementation specifically for use
Expand All @@ -63,15 +61,8 @@
* @see io.scif.filters.ChannelFiller
* @author Mark Hiner
*/
@Plugin(type = MetadataWrapper.class, attrs = { @Attr(
name = ChannelFillerMetadata.METADATA_KEY,
value = ChannelFillerMetadata.METADATA_VALUE) })
public class ChannelFillerMetadata extends AbstractMetadataWrapper {

// -- Constants --

public static final String METADATA_VALUE = "io.scif.filters.ChannelFiller";

// -- Fields --

@Parameter
Expand All @@ -82,16 +73,6 @@ public class ChannelFillerMetadata extends AbstractMetadataWrapper {
*/
private int lutLength;

// -- Constructors --

public ChannelFillerMetadata() {
this(null);
}

public ChannelFillerMetadata(final Metadata metadata) {
super(metadata);
}

// -- ChannelFiller API methods --

public int getLutLength() {
Expand Down Expand Up @@ -163,4 +144,11 @@ public void populateImageMetadata() {
add(iMeta, false);
}
}

// -- MetadataWrapper API --

@Override
public Class<? extends Filter> filterType() {
return io.scif.filters.ChannelFiller.class;
}
}
30 changes: 7 additions & 23 deletions scifio/src/main/java/io/scif/filters/DimensionSwapperMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@

package io.scif.filters;

import io.scif.Metadata;

import java.util.List;

import net.imglib2.meta.AxisType;

import org.scijava.plugin.Attr;
import org.scijava.plugin.Plugin;

/**
* {@link io.scif.filters.MetadataWrapper} implementation specifically for use
* with the {@link io.scif.filters.DimensionSwapper}.
Expand All @@ -53,30 +48,12 @@
* @see io.scif.filters.DimensionSwapper
* @author Mark Hiner
*/
@Plugin(type = MetadataWrapper.class, attrs = { @Attr(
name = DimensionSwapperMetadata.METADATA_KEY,
value = DimensionSwapperMetadata.METADATA_VALUE) })
public class DimensionSwapperMetadata extends AbstractMetadataWrapper {

// -- Constants --

public static final String METADATA_VALUE =
"io.scif.filters.DimensionSwapper";

// -- Fields --

private List<AxisType>[] outputOrder;

// -- Constructors --

public DimensionSwapperMetadata() {
this(null);
}

public DimensionSwapperMetadata(final Metadata metadata) {
super(metadata);
}

// -- DimensionSwapperMetadata API --

/**
Expand All @@ -99,4 +76,11 @@ public List<AxisType>[] getOutputOrder() {
public void setOutputOrder(final List<AxisType>[] outputOrder) {
this.outputOrder = outputOrder;
}

// -- MetadataWrapper API --

@Override
public Class<? extends Filter> filterType() {
return io.scif.filters.DimensionSwapper.class;
}
}
9 changes: 6 additions & 3 deletions scifio/src/main/java/io/scif/filters/MetadataWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@
*/
public interface MetadataWrapper extends Metadata {

public static final String METADATA_KEY = "Metadata Wrapper";
public static final String METADATA_VALUE = "java.lang.Object";

/**
* @return The {@code Metadata} used for delegation by this wrapper.
*/
Expand All @@ -79,6 +76,12 @@ public interface MetadataWrapper extends Metadata {
*/
void wrap(Metadata meta);

/**
* @return The class of the {@link Filter} this MetadataWrapper is compatible
* with.
*/
Class<? extends Filter> filterType();

// -- Setter Methods with passUp flag --

void setTable(final MetaTable table, final boolean passUp);
Expand Down
17 changes: 6 additions & 11 deletions scifio/src/main/java/io/scif/filters/PlaneSeparatorMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

import net.imglib2.meta.AxisType;

import org.scijava.plugin.Attr;
import org.scijava.plugin.Plugin;

/**
Expand All @@ -56,23 +55,14 @@
* @see io.scif.filters.PlaneSeparator
* @author Mark Hiner
*/
@Plugin(type = MetadataWrapper.class, attrs = { @Attr(
name = PlaneSeparatorMetadata.METADATA_KEY,
value = PlaneSeparatorMetadata.METADATA_VALUE) })
@Plugin(type = MetadataWrapper.class)
public class PlaneSeparatorMetadata extends AbstractMetadataWrapper {

// -- Constants --

public static final String METADATA_VALUE =
"io.scif.filters.PlaneSeparator";

// -- Fields --

/** List of Axes to separate. */
private Set<AxisType> splitTypes = new HashSet<AxisType>();

// -- Constructors --

// -- PlanarAxisSeparatorMetadata API Methods --

/** Returns the number of axes being separated. */
Expand Down Expand Up @@ -108,6 +98,11 @@ public void wrap(final Metadata meta) {
super.wrap(meta);
}

@Override
public Class<? extends Filter> filterType() {
return io.scif.filters.PlaneSeparator.class;
}

// -- Metadata API Methods --

@Override
Expand Down

0 comments on commit be67219

Please sign in to comment.