Skip to content

Optional parameters don't work outside of scijava-ops-engine #110

Closed
scijava/incubator
#73

Description

@hinerm

Declaring parameters @Optional outside of the scijava-ops-engine component leads to IllegalAccessError.

For example, DefaultMeanFilter should have an optional OutOfBoundsFactory parameter:

public void compute(final RandomAccessibleInterval<T> input, final Shape inputNeighborhoodShape,
		@Optional OutOfBoundsFactory<T, RandomAccessibleInterval<T>> outOfBoundsFactory,
		final RandomAccessibleInterval<V> output) {
	if (outOfBoundsFactory == null)
		outOfBoundsFactory = new OutOfBoundsBorderFactory<>();
	RandomAccessibleInterval<T> extended = Views.interval((Views.extend(input, outOfBoundsFactory)), input);
	mapper.compute(extended, inputNeighborhoodShape, statsOp, output);
}

But attempting to match this with:

ops.op("filter.mean").input(img, shape).output(output).compute();

produces the following exception:

java.lang.IllegalAccessError: class org.scijava.ops.engine.reduce.DefaultMeanFilterReduction1RandomAccessibleInterval_Shape_RandomAccessibleInterval_RandomAccessibleInterval (in module org.scijava.ops.engine) cannot access class net.imagej.ops2.filter.mean.DefaultMeanFilter (in module net.imagej.ops2) because module org.scijava.ops.engine does not read module net.imagej.ops2

	at org.scijava.ops.engine@0-SNAPSHOT/org.scijava.ops.engine.reduce.DefaultMeanFilterReduction1RandomAccessibleInterval_Shape_RandomAccessibleInterval_RandomAccessibleInterval.compute(DefaultMeanFilterReduction1RandomAccessibleInterval_Shape_RandomAccessibleInterval_RandomAccessibleInterval.java)
	at org.scijava.ops.engine@0-SNAPSHOT/org.scijava.ops.engine.matcher.impl.OpWrappers$Computer2OpWrapper$1GenericTypedComputer2.compute(OpWrappers.java:844)
	at org.scijava.ops.api@0-SNAPSHOT/org.scijava.ops.api.OpBuilder$Arity2_IV_OV.compute(OpBuilder.java:1020)

This means that scijava.ops.engine would need a requires xxxx on any package using @Optional in *its own module-info.java, which of course would be cyclic and also non-extensible.

Activity

  1. added this to the 1.0.0 milestone on Jan 20, 2023
  2. hinerm commented on Jan 20, 2023

    @hinerm
    MemberAuthor
  3. gselzer commented on Jan 20, 2023

    @gselzer
    Member

    The issue here is that when we are reducing optional parameters, we require the need for an Op of a different functional type. In this example, DefaultMeanFilter should be a Computers.Arity3 with the OutOfBoundsFactory, and a Computers.Arity2 without the OutOfBoundsFactory. Creating that Computers.Arity2 requires javassist, meaning we can likely fix this using a solution similar to scijava/incubator#69.

    I'll give it a shot.

  4. gselzer commented on Jan 20, 2023

    @gselzer
    Member

    Okay, so I have things working on that branch, but I still don't understand why we cannot directly call the functional method of the functional interface. I can do crazy things inside of the javassist reduced Op, like making a new instance of the Op class!

    I think we should do more investigation, but I think I've run out of time today to try...

  5. hinerm commented on Jan 27, 2023

    @hinerm
    MemberAuthor

    I think we should do more investigation, but I think I've run out of time today to try...

    So, ultimately, without this change, the compute method is being called in the javassist-created class:

    org.scijava.ops.engine.reduce.DefaultMeanFilterReduction1RandomAccessibleInterval_Shape_RandomAccessibleInterval_RandomAccessibleInterval
    

    Following JPMS rules of callability I would not expect this class to have access to the underlying op's compute, since it's not in the same package as the original op (net.imagej.ops2.filter.mean) and, as the error message indicates, the module it is in doesn't have read access to ops2.filter.mean.

    But, because compute is a method of an interface that is read by the javassist class's module, I don't understand why it doesn't work.

    I can do crazy things inside of the javassist reduced Op, like making a new instance of the Op class!

    Do you mean via reflection, e.g. Class.newInstance()? Or actually calling new ClassName()? If it's via reflection, that seems to be working as expected since opens..to.. gives reflection access (and thus the solution to use reflection seems reasonable).

    things I tried:

    • exports..to.. doesn't work since it isn't this mysterious "read" access that is only granted by requires
    • Parameterizing the op with core module classes (FAILED)
    • Running in python without JPMS (passed!)
    • Casting the Op to the functional interface that we have access to (FAILED)
    • Trying to learn more about Javassist and JPMS interactions (Couldn't find anything)
    • Forcing the Javassist Op to be in the same net.imagej... package as the original op (FAILED)

    Some thoughts:

    • It feels weird to me that our javassist-created Op is a class in an org.scijava package regardless of where it came from...
    • Is there a way to call the original Op's method without a reference to its class? Can we create a copy of that method inside our new Javassist op itself? and just delegate to ourselves?
    • The Javassist Op doesn't know any of the generic types of the interface it's implementing, right? Does that affect type safety guarantees of future matches in a given session?
  6. added 2 commits that reference this issue on May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions