Clean up LegacyEnvironment's source code

This trick was brought to you by the Eclipse Foundation.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Fix NullPointerException

This was introduced by the lazy loading...

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Ensure that the legacy environment is initialized before setMacroOptions

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Do not access LegacyHooks._pluginClasspath directly

We are about to make this field private, to prevent direct
usage of that field. Instead, it should be accessed via the
handleExtraPluginJars() method.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Refactor the callback handling in LegacyInjector

The previous approach was doomed because it was limited to one
callback, and that callback was required as a parameter at injection
time. However, we are likely to want more callbacks, and we are
likely to know whether we want them to be called *way* earlier
than injection time.

So let's restructure the thing: make a list of callbacks to call
before, and a list to call after, applying the runtime patches.

They are still package-local as they are not considered part of the
public API (we might need to refactor them yet another time before
the API stabilizes).

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Use the Callback method to disable the ij1.plugin.dirs handling

We added a separate method to the LegacyInjector just to be able to
disable the ij1.plugin.dirs handling. This method does not scale well:
whenever there is a configuration we would want to specify between
instantiating the LegacyEnvironment class and actually using it, we
would have to add a method to both: the LegacyEnvironment and the
LegacyInjector.

Instead, let's use the powerful Callback method to add a patch to the
constructor of the EssentialLegacyHooks class.

Technically, removing the public method from LegacyInjector requires
bumping the major version as per SemVer. However, it was a simple bug
to make this method public instead of package-local (because only the
methods in LegacyEnvironment are really intended for public
consumption). Additionally, we're still in the "0.x" phase and
therefore this developer believes he can get away with claiming that
the removal of the public API that was never intended to be public --
and was in effect only for one minor version anyway -- is actually
a bug fix.

:-)

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Use the Callback method to add extra plugin class path elements

This switches paradigms: instead of accessing the LegacyHooks at
runtime to augment the list of directories/files to add to the
PluginClassLoader's class path, we now do it at constructor time.

This solves a couple of issues:

- despite addPluginClasspath() clearly being a configuration, it
  forced the runtime patches to be applied already, leading to
  surprises e.g. when the caller wanted to add a plugin class path
  and then disable the ij1.plugin.dirs handling

- when writing out a .jar file from the patched legacy environment,
  the extra plugin class path was forgotten without any apology

- the Callback is a method to add configuration in a robust manner
  and should therefore be used consistently

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Describe the Callback-based configuration in the README

To increase the bus number of this project, let's keep the README
updated as the internals of the project change. Of course, we describe
the technical details only broadly; the interested user/developer is
supposed to learn about the philosphy from the README (the exact details
can be inferred easily from the source code).

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Make sure that LegacyHooks' fields are retained when installing new ones

We install a patch into ij.IJ._hooks(LegacyHooks) that allows to override
the initially-installed ones. Now that we configure parts of the legacy
environment via fields of the LegacyHooks instance, we have to take care
to copy those settings when installing new hooks.

To make that character of the fields clear, we mark them as private, too.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Merge branch 'callbacks'

This topic branch switches to a new paradigm to configure instances
of the LegacyEnvironment class: whenever the user asks for special
settings -- such as extra class path elements for ImageJ 1.x'
PluginClassLoader -- the environment adds a callback to its
LegacyInjector instance that will augment the constructor of the
EssentialLegacyHooks class.

Since the EssentialLegacyHooks class is private to the encapsulated
class loader, these settings will be persisted when using
LegacyInjector's writeJar() method.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>