Skip to content

Commit

Permalink
Adds documentation for dealing with modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Nov 1, 2023
1 parent 715313b commit 85a4b89
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
25 changes: 25 additions & 0 deletions docs/_errormessages/class-or-field-is-not-accessible-jpms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: "Class or field is not accessible via the Java Module System"
---
The class is not accessible via the Java Module system.
Consider opening the module that contains it.

<br/>

Field foo of type Bar is not accessible via the Java Module System.
Consider opening the module that contains it, or add prefab values for type Bar.

Either the class that EqualsVerifier is testing, or one of its fields, cannot be accessed due to restrictions from the Java Platform Module System.

If it's the class that EqualsVerifier is testing, you must modify your `module-info.java` such that the package containing the class, is accessible for reflection via the `open` keyword.

If it's one of the fields in the class, you can either modify your `module-info.java` as above, or add prefab values for the given type:


{% highlight java %}
EqualsVerifier.forClass(TheClassThatImTesting.class)
.withPrefabValues(Bar.class, new Bar(1), new Bar(2))
.verify();
{% endhighlight %}

See also the [section in the manual](/equalsverifier/manual/jpms) about modules.
4 changes: 4 additions & 0 deletions docs/_errormessages/unable-to-make-field-accessible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: 'Unable to make field foo accessible: module bar does not "opens bar" to baz'
---
This error message occurs in older versions of EqualsVerifier. If you upgrade, the error message wil probably be replaced with "The class is not accessible via the Java Module system" or "Field foo of type Bar is not accessible via the Java Module system". If so, please check [that error message](/equalsverifier/errormessages/class-or-field-is-not-accessible-jpms)
59 changes: 59 additions & 0 deletions docs/_manual/13-jpms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: "The Java Platform Module System"
permalink: /manual/jpms/
---
EqualsVerifier is compatible with the Java Platform Module System (JPMS). However, since it does some reflection, you have to open up some packages. Perhaps you have already done so, as test frameworks like JUnit also require this.

The recommended approach is to put a `module-info.java` file in your `src/test/java` folder, that copies the content the `module-info.java` file in `src/main/java`. Let's say this is your `src/main/java/module-info.java`:

{% highlight java %}
module my.module {
exports my.module;
}
{% endhighlight %}

The easiest way to use EqualsVerifier, is to put this `module-info.java` in your `src/test/java` folder:

{% highlight java %}
open module my.module { // Note: open
exports my.module; // Same as before

requires org.junit.jupiter.api; // For JUnit
requires nl.jqno.equalsverifier; // For EqualsVerifier
requires net.bytebuddy; // Dependency of EqualsVerifier
}
{% endhighlight %}

Note that this approach opens up the entire module for reflection. If you do not want this, even in your tests, you can be more precise in what you open up:

{% highlight java %}
open module my.module {
exports my.module;
opens my.module.package.model; // Open model package

requires org.junit.jupiter.api;
requires nl.jqno.equalsverifier;
requires net.bytebuddy;
}
{% endhighlight %}

Note that if you do this, and you have model classes or dependencies for model classes in other packages, you will have to open these packages as well, or provide prefab values for these dependencies:

{% highlight java %}
import my.module.package.somewhere.inaccessible.Bar;

EqualsVerifier.forClass(Foo.class)
.withPrefabValues(Bar.class, new Bar(1), new Bar(2))
.verify();
{% endhighlight %}

When the class that EqualsVerifier is testing is inaccessible, you will get this error message:

The class is not accessible via the Java Module system.
Consider opening the module that contains it.

If the class is accessible, but the class for one of its fields isn't, you will get this error message:

Field foo of type Bar is not accessible via the Java Module System.
Consider opening the module that contains it, or add prefab values for type Bar.

5 changes: 4 additions & 1 deletion docs/_pages/errormessages.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ This is not a complete list. I'll add to it as needed, so if you need help with
* [Abstract delegation](/equalsverifier/errormessages/abstract-delegation)
* [BigDecimal equality](/equalsverifier/errormessages/bigdecimal-equality)
* [ClassCastException: java.lang.Object cannot be cast to …](/equalsverifier/errormessages/classcastexception)
* [Class is not accessible via the Java Module system. Consider opening the module that contains it.](/equalsverifier/errormessages/class-or-field-is-not-accessible-jpms)
* [Coverage is not 100%](/equalsverifier/errormessages/coverage-is-not-100-percent)
* [Double: equals doesn't use Double.compare for field foo](/equalsverifier/errormessages/double-equals-doesnt-use-doublecompare-for-field-foo)
* [Field foo of type Bar is not accessible via the Java Module System.](/equalsverifier/errormessages/class-or-field-is-not-accessible-jpms)
* [Float: equals doesn't use Float.compare for field foo](/equalsverifier/errormessages/float-equals-doesnt-use-floatcompare-for-field-foo)
* [JPA Entity: direct reference to field ... used instead of getter](/equalsverifier/errormessages/jpa-direct-reference-instead-of-getter)
* [JPA Entity: direct reference to field foo used instead of getter](/equalsverifier/errormessages/jpa-direct-reference-instead-of-getter)
* [Mutability: equals depends on mutable field](/equalsverifier/errormessages/mutability-equals-depends-on-mutable-field)
* [NoClassDefFoundError](/equalsverifier/errormessages/noclassdeffounderror)
* [Non-nullity: equals/hashCode/toString throws NullPointerException](/equalsverifier/errormessages/non-nullity-equals-hashcode-tostring-throws-nullpointerexception)
Expand All @@ -38,4 +40,5 @@ This is not a complete list. I'll add to it as needed, so if you need help with
* [Subclass: … equals subclass instance …](/equalsverifier/errormessages/subclass-equals-subclass-instance)
* [Subclass: object is not equal to an instance of a trivial subclass with equal fields](/equalsverifier/errormessages/subclass-object-is-not-equal-to-an-instance-of-a-trivial-subclass-with-equal-fields)
* [Symmetry: … does not equal superclass instance …](/equalsverifier/errormessages/symmetry-does-not-equal-superclass-instance)
* [Unable to make field foo accessible: module bar does not "opens bar" to baz](/equalsverifier/errormessages/unable-to-make-field-accessible)
* [Unsupported class file major version _x_](/equalsverifier/errormessages/unsupported-class-file-major-version)
1 change: 1 addition & 0 deletions docs/_pages/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ These pages will quickly get you up and running with EqualsVerifier, and help yo
* [Cached hashCodes](/equalsverifier/manual/caching-hashcodes)
* [Relaxed equality](/equalsverifier/manual/relaxed-equality)
* [Dealing with legacy systems](/equalsverifier/manual/legacy-systems)
* [The Java Platform Module System](/equalsverifier/manual/jpms)
* [Additional resources](/equalsverifier/resources)

0 comments on commit 85a4b89

Please sign in to comment.