Skip to content

Commit 8728af3

Browse files
committed
Progress on scijava-context
1 parent 650fb05 commit 8728af3

11 files changed

Lines changed: 278 additions & 20 deletions

File tree

context/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,17 @@
9090
<license.licenseName>bsd_2</license.licenseName>
9191
<license.copyrightOwners>SciJava developers.</license.copyrightOwners>
9292
</properties>
93+
94+
<dependencies>
95+
<dependency>
96+
<groupId>org.scijava</groupId>
97+
<artifactId>scijava-core</artifactId>
98+
<version>${project.version}</version>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.scijava</groupId>
102+
<artifactId>scijava-plugin</artifactId>
103+
<version>${project.version}</version>
104+
</dependency>
105+
</dependencies>
93106
</project>

context/src/main/java/org/scijava/context/AbstractContextual.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
package org.scijava.context;
3434

3535
import org.scijava.event.EventHandler;
36-
import org.scijava.plugin.Parameter;
3736

3837
/**
3938
* Abstract base class for {@link Contextual} objects.
@@ -48,7 +47,7 @@
4847
*/
4948
public abstract class AbstractContextual implements Contextual {
5049

51-
@Parameter
50+
@Inject
5251
private Context context;
5352

5453
// -- Contextual methods --
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.context;
34+
35+
import org.scijava.core.Priority;
36+
import org.scijava.plugin.PluginInfo;
37+
38+
/**
39+
* Abstract base class for {@link RichPlugin} implementations.
40+
*
41+
* @author Curtis Rueden
42+
*/
43+
public abstract class AbstractRichPlugin extends AbstractContextual implements
44+
RichPlugin
45+
{
46+
47+
/** The priority of the plugin. */
48+
private double priority = Priority.NORMAL;
49+
50+
/** The metadata associated with the plugin. */
51+
private PluginInfo<?> info;
52+
53+
// -- Object methods --
54+
55+
@Override
56+
public String toString() {
57+
final PluginInfo<?> pi = getInfo();
58+
return pi == null ? super.toString() : pi.getTitle();
59+
}
60+
61+
// -- Prioritized methods --
62+
63+
@Override
64+
public double getPriority() {
65+
return priority;
66+
}
67+
68+
@Override
69+
public void setPriority(final double priority) {
70+
this.priority = priority;
71+
}
72+
73+
// -- HasPluginInfo methods --
74+
75+
@Override
76+
public PluginInfo<?> getInfo() {
77+
return info;
78+
}
79+
80+
@Override
81+
public void setInfo(final PluginInfo<?> info) {
82+
this.info = info;
83+
}
84+
85+
}

context/src/main/java/org/scijava/context/Context.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,10 @@ public Service getService(final String className) {
352352
* distinct things:
353353
* <ol>
354354
* <li>If the given object has any non-final {@link Context} fields annotated
355-
* with @{@link Parameter}, sets the value of those fields to this context.
355+
* with @{@link Inject}, sets the value of those fields to this context.
356356
* </li>
357357
* <li>If the given object has any non-final {@link Service} fields annotated
358-
* with @{@link Parameter}, sets the value of those fields to the
358+
* with @{@link Inject}, sets the value of those fields to the
359359
* corresponding service available from this context.</li>
360360
* <li>Calls {@link EventService#subscribe(Object)} with the object to
361361
* register any @{@link EventHandler} annotated methods as event subscribers.
@@ -365,14 +365,14 @@ public Service getService(final String className) {
365365
* @param o The object to which the context should be assigned.
366366
* @throws IllegalStateException If the object already has a context.
367367
* @throws IllegalArgumentException If the object has a required
368-
* {@link Service} parameter (see {@link Parameter#required()})
368+
* {@link Service} parameter (see {@link Inject#required()})
369369
* which is not available from this context.
370370
*/
371371
public void inject(final Object o) {
372372
// Ensure parameter fields and event handler methods are cached for this
373373
// object.
374374
final Query query = new Query();
375-
query.put(Parameter.class, Field.class);
375+
query.put(Inject.class, Field.class);
376376
query.put(EventHandler.class, Method.class);
377377
ClassUtils.cacheAnnotatedObjects(o.getClass(), query);
378378

@@ -392,12 +392,12 @@ public void inject(final Object o) {
392392
* a consequence of calling {@link #inject(Object)}.
393393
* <p>
394394
* This method is notably useful for downstream code to discern between
395-
* {@link Parameter} fields whose values would be injected, versus those whose
395+
* {@link Inject} fields whose values would be injected, versus those whose
396396
* values would not, without needing to hardcode type comparison checks
397397
* against the {@link Service} and {@link Context} types.
398398
* </p>
399399
*
400-
* @param type The type of the @{@link Parameter}-annotated field.
400+
* @param type The type of the @{@link Inject}-annotated field.
401401
* @return True iff a member field of the given type would have its value
402402
* assigned.
403403
*/
@@ -440,7 +440,7 @@ public static List<Class<? extends Service>> serviceClassList(
440440

441441
private List<Field> getParameterFields(final Object o) {
442442
try {
443-
return ClassUtils.getAnnotatedFields(o.getClass(), Parameter.class);
443+
return ClassUtils.getAnnotatedFields(o.getClass(), Inject.class);
444444
}
445445
catch (final Throwable t) {
446446
handleSafely(t);
@@ -465,7 +465,7 @@ private void inject(final Field f, final Object o) {
465465
final Class<? extends Service> serviceType =
466466
(Class<? extends Service>) type;
467467
final Service service = getService(serviceType);
468-
if (service == null && f.getAnnotation(Parameter.class).required()) {
468+
if (service == null && f.getAnnotation(Inject.class).required()) {
469469
throw new IllegalArgumentException(//
470470
createMissingServiceMessage(serviceType));
471471
}

context/src/main/java/org/scijava/context/Contextual.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232

3333
package org.scijava.context;
3434

35-
import org.scijava.plugin.Parameter;
36-
import org.scijava.service.Service;
37-
3835
/**
3936
* An object that belongs to a SciJava application context.
4037
*
@@ -72,7 +69,7 @@ public interface Contextual {
7269
* @see Context#inject(Object)
7370
* @throws IllegalStateException If the object already has a context.
7471
* @throws IllegalArgumentException If the object has a required
75-
* {@link Service} parameter (see {@link Parameter#required()})
72+
* {@link Service} parameter (see {@link Inject#required()})
7673
* which is not available from the context.
7774
*/
7875
default void setContext(final Context context) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.context;
33+
34+
import java.lang.annotation.ElementType;
35+
import java.lang.annotation.Retention;
36+
import java.lang.annotation.RetentionPolicy;
37+
import java.lang.annotation.Target;
38+
39+
/**
40+
* An annotation for injecting {@link Context} and {@link Service} instances.
41+
*
42+
* @author Curtis Rueden
43+
*/
44+
@Retention(RetentionPolicy.RUNTIME)
45+
@Target(ElementType.FIELD)
46+
public @interface Inject {
47+
48+
/** Defines whether to fail if an injectable object is not available. */
49+
boolean required() default true;
50+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.context;
34+
35+
import org.scijava.core.Identifiable;
36+
import org.scijava.core.Locatable;
37+
import org.scijava.core.Prioritized;
38+
import org.scijava.log.LogService;
39+
import org.scijava.log.Logged;
40+
import org.scijava.plugin.HasPluginInfo;
41+
import org.scijava.plugin.SciJavaPlugin;
42+
import org.scijava.version.Versioned;
43+
44+
/**
45+
* Base interface for {@link Contextual}, {@link Prioritized} plugins that
46+
* retain access to their associated {@link PluginInfo} metadata via the
47+
* {@link HasPluginInfo} interface. This interface is intended as a convenient
48+
* extension point for new types of plugins.
49+
*
50+
* @author Curtis Rueden
51+
*/
52+
public interface RichPlugin extends SciJavaPlugin, Contextual, Prioritized,
53+
HasPluginInfo, Logged, Identifiable, Locatable, Versioned
54+
{
55+
56+
// -- Identifiable methods --
57+
58+
@Override
59+
default String getIdentifier() {
60+
return "plugin:" + getClass().getName();
61+
}
62+
63+
// -- Logged methods --
64+
65+
@Override
66+
default LogService log() {
67+
return context().getService(LogService.class);
68+
}
69+
}

context/src/main/java/org/scijava/context/Service.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232

3333
package org.scijava.context;
3434

35-
import org.scijava.Disposable;
36-
import org.scijava.Initializable;
35+
import org.scijava.core.Disposable;
36+
import org.scijava.core.Initializable;
3737
import org.scijava.event.EventService;
3838
import org.scijava.plugin.Plugin;
39-
import org.scijava.plugin.RichPlugin;
4039

4140
/**
4241
* A SciJava service, for a particular area of functionality.

context/src/main/java/org/scijava/context/ServiceHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private <S extends Service> S createServiceRecursively(final Class<S> c)
313313

314314
// populate service parameters
315315
final List<Field> fields =
316-
ClassUtils.getAnnotatedFields(c, Parameter.class);
316+
ClassUtils.getAnnotatedFields(c, Inject.class);
317317
for (final Field f : fields) {
318318
f.setAccessible(true); // expose private fields
319319

@@ -336,7 +336,7 @@ private <S extends Service> S createServiceRecursively(final Class<S> c)
336336
Service s = context().getService(serviceType);
337337
if (s == null) {
338338
// recursively obtain needed service
339-
final boolean required = f.getAnnotation(Parameter.class).required();
339+
final boolean required = f.getAnnotation(Inject.class).required();
340340
s = loadService(serviceType, required);
341341
// NB: Remember when there is an optional EventService parameter.
342342
if (s instanceof EventService) eventServiceRequired = required;

context/src/main/java/org/scijava/context/ServiceIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
import java.util.List;
3636

37-
import org.scijava.object.SortedObjectIndex;
37+
import org.scijava.collection.SortedObjectIndex;
3838

3939
/**
4040
* Data structure for tracking registered services.

0 commit comments

Comments
 (0)