Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertions for java.lang.reflect #3130

Open
wjbakker opened this issue Jul 31, 2023 · 5 comments · May be fixed by #3131
Open

Assertions for java.lang.reflect #3130

wjbakker opened this issue Jul 31, 2023 · 5 comments · May be fixed by #3131
Assignees
Labels
type: new feature A new feature
Milestone

Comments

@wjbakker
Copy link

wjbakker commented Jul 31, 2023

Feature summary

Add assertions for classes in the java.lang.reflect package:

  • Field
  • Method
  • Constructor

In this issue, we will add support for visiblity modifiers, static / final modifiers and annotations:

  • MemberAssert: isPublic(), isProtected(), isPackagePrivate()
  • AnnotatedElementAssert: hasAnnotation(Class), hasAnnotations(Class...)
  • MethodAssert: isAbstract(), isStatic(), isNotStatic(), isFinal(), isNotFinal()
  • FieldAssert: isStatic(), isNotStatic(), isFinal(), isNotFinal()
Method ClassAssert MethodAssert FieldAssert ConstructorAssert
isPublic()
isProtected()
isPackagePrivate()
hasAnnotation(Class)
hasAnnotations(Class...)
isStatic()
isNotStatic()
isFinal()
isNotFinal()
isAbstract()

Example

Field field = Math.class.getDeclaredField("PI");
assertThat(field).isPublic();
assertThat(field).isStatic();
assertThat(field).isFinal();

Method method = Math.class.getDeclaredMethod("abs", long.class);
assertThat(method).isPublic();
assertThat(method).isStatic();
assertThat(method).isNotFinal();

Constructor<?> constructor = String.class.getDeclaredConstructor();
assertThat(constructor).isPublic();

Limitations / Future considerations

  • Not all methods and properties of Field, Method and Constructor are covered in this issue. Further features can be added in later issues.
  • Not all classes in the java.lang.reflect package are covered in this issue. Support for Parameter can be added in later issues.
  • You should obtain a reference to the Field, Method and Constructor. In later iterations, we could consider adding a method to ClassAssert to extract members, so one can navigate fluently to the Field, Method and Constructor. This is left out of scope as it requires further thought about visibility, exceptions and method overloading. For example:
class ClassAssert {
    FieldAssert extractingField(String name);
    MethodAssert extractingMethod(String name);
    ConstructorAssert extractingConstructor(String name);
}
  • The Assertions and BDDAssertions entry points are very large. We could introduce an interface with static methods for the assertion methods related to the java.lang.reflect package and implement this interface in the Assertions entry point.

See also:

wjbakker pushed a commit to wjbakker/assertj-core that referenced this issue Jul 31, 2023
This changes adds assertions for classes:
* Field
* Method
* Constructor
@wjbakker wjbakker linked a pull request Jul 31, 2023 that will close this issue
wjbakker pushed a commit to wjbakker/assertj-core that referenced this issue Jul 31, 2023
This changes adds assertions for classes:
* Field
* Method
* Constructor

Improve javadoc.
@scordio
Copy link
Member

scordio commented Jul 31, 2023

Thanks a lot for putting together such a summary, @wjbakker. It makes totally sense to move in this direction.

wjbakker pushed a commit to wjbakker/assertj-core that referenced this issue Aug 1, 2023
This changes adds assertions for classes:
* Field
* Method
* Constructor

Fix binary compatibility for ShouldHaveAnnotations
Improve Javadoc in AbstractMethodAssert
wjbakker pushed a commit to wjbakker/assertj-core that referenced this issue Aug 1, 2023
This changes adds assertions for classes:
* Field
* Method
* Constructor

Formatting
@onacit
Copy link
Contributor

onacit commented Feb 1, 2024

Do we have any progress, here? Thanks.

@joel-costigliola
Copy link
Member

I did not follow it so not much on my side, @scordio is that something you have looked into ?

@scordio
Copy link
Member

scordio commented Feb 2, 2024

Not in detail but I plan to as soon as we start focusing on 3.26.0.

@scordio scordio added this to the 3.26.0 milestone Feb 2, 2024
@scordio scordio added the type: new feature A new feature label Feb 2, 2024
@scordio scordio self-assigned this Feb 2, 2024
@onacit
Copy link
Contributor

onacit commented Feb 2, 2024

Why don't we go with following signatures?

hasAnyAnnotation(final Class<?> annotation, final Class<?>... otherAnnotations);
doesNotHasAnyAnnotation(final Class<?> annotation, final Class<?>... otherAnnotations);

hasAllAnnotations(final Class<?> annotation, final Class<?>... otherAnnotations);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: new feature A new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants