-
Notifications
You must be signed in to change notification settings - Fork 375
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
Add Reflect.hasField() and Reflect.hasMethod() #70
Comments
Thanks for your suggestions. I agree this kind of idea could be useful. What's the specific use-case you have in mind? |
For the time being, of course, the workaround to check for field existence is to catch the exception if the field doesn't exist. |
We developed a small test suite to (semi)automatically grade our students based on JUnit tests. Sometimes it is necessary to check submissions using reflection. Usually, our tests are two-staged: (1) check if certain fields and/or methods are available, (2) call particular methods with test data. JOOR is well suited for task (2) as it reduced the amount of required boilerplate code. However, it lacks features for task (1). Catching exceptions does not fit our needs because an exception may also be thrown by the student's code. Indeed, we could use other libraries like Javaparser or Spoon to analyze the source code, but sometimes we don't have access to the original source files. In the following you will find some constraints we check in (1):
|
Hmm, your use-case is so specific, why not just write a little wrapper around the reflection API yourself? You will run into edge cases all the time anyway... While I can see the usage of simple |
If JOOR provides some sort of "has" methods and allows to access the actual reflection API, modifier checking should be straight forward. For example: on("...")
.accessField("field") // Checks if field exists and returns Optional<Field>
.ifPresent(f -> {
// further checks
}); |
That's an entirely alternative jOOR that you have in mind. The current jOOR, when calling You would like for jOOR to somehow remember that it had accessed a Surely, some other library can already do what you need (e.g. there are libraries and frameworks that allow for querying Java metadata using a SQL like language, such as https://www.jarchitect.com) - or you write a small library yourself. It should really just take 2-3 hours of effort for you, if you scratch your immediate itch. For jOOR, this is too big of a change, I'm afraid. In any case, it cannot be prioritised for your immediate use anyway. Sorry for that. I'm leaving the |
... to give this rejection some additional context: In jOOQ, we do quite a few additional reflection queries that would be generally useful to others, but are very difficult to formalise in an API as focused and simple as jOOR currently is. And that's perfectly OK. It's fine to have some reflection logic outside of jOOR despite having the jOOR dependency. |
Ok. Nevertheless, many thanks for your efforts :). |
Expected behavior and actual behavior:
Something similar to:
Steps to reproduce the problem:
Currently not available.
Versions:
jOOR:
All versions.
Java:
All versions.
The text was updated successfully, but these errors were encountered: