-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
WARNING: Illegal reflective access by com.github.jknack.handlebars.context.MemberValueResolver #667
Comments
+1 |
+1
|
would you like to submit a pull request? |
Just a note: I can reproduce this when running the tests in Java 11 basically complains about this line in ((AccessibleObject) m).setAccessible(true); This StackOverflow answer backs this up. It seems, starting with Java 9, you just can't use |
Same error with OpenJDK 11. |
Same error with OpenJDK 12 |
same error with OpenJDK 14 |
I am willing to write a PR to fix this issue, but AFAIK this would mean drop compatibility for Java < 9. |
Have closed the duplicate ticket for the same problem, here are my steps to reproduce: macOS, homebrew, swagger-codegen version 3.0.18 openapi: 3.0.0
info:
title: Thingies App
version: "1"
description: |+
...
paths:
/thingies/load_all:
post:
responses:
200:
description: Load all thingies
content:
application/json:
schema:
$ref: "#/components/schemas/Thingies"
servers:
- url: https://thingies.test/api
components:
schemas:
Thingies:
type: object
properties:
property_one:
type: string
|
This is a temporary fix to suppress the "WARNING: An illegal reflective access operation has occurred". The origin of the warning is `method.setAccessible(true);` (See reference). Though this fixes the warning, we should try to avoid `setAccessible()` to be future-proof. Reference: jknack/handlebars.java#667 Closes: strimzi#3595 Signed-off-by: Allen Bai <[email protected]>
@jknack The way to avoid this is error message for most folks is to make MapValueResolver smarter. MapValueResolver should resolve name = "empty" and name = "size" but currently does not. If we know the context object is an instanceof Map and name is size or empty it should resolve but currently does not:
Similar to how you are doing an check if its an enummap you would do the same if its a map in general (and then cast and call empty or size). There are probably more methods than that but that is what I see in general. |
I will put in a pull request later if there is interest. |
Whoops I guess you still need a custom JavaBeanValueResolver that respects public/private: Overriding members with something like this fixes it:
|
This has become a runtime exception on jdk 16. |
Unit tests fails with java >= 16 because handlebars is not compatible yet with java 16. See: jknack/handlebars.java#667
@jknack I found that using partials specifically causes exceptions in later JDK versions. There is some nuance here around the bean resolver identifying java.util.Collections$EmptyMap.isEmpty as a bean field for the name I came up with #870 |
@yuzawa-san and @jknack The solution is far simpler and less of a hack than using HashMap. All that really needs to be done is to not call The real fix is to fix private Map<String, M> cache(final Class<?> clazz) {
Map<String, M> mcache = this.cache.get(clazz);
if (mcache == null) {
mcache = new HashMap<>();
Set<M> members = members(clazz);
for (M m : members) {
// Mark as accessible.
if (m instanceof AccessibleObject) {
((AccessibleObject) m).setAccessible(true);
}
mcache.put(memberName(m), m);
}
this.cache.put(clazz, mcache);
}
return mcache;
} What we need to do is not call So I propose we do what JMustache does https://github.com/samskivert/jmustache and use a flag. A system property or something wired in that allows setAccesible to be called. The flag could be on by default unless its set to false or JDK 17 is detected and its not set. Now this is still doesn't filter all the Member (field and method) so you will still get an IllegalAccessException but at least it is an exception and not the glaring WARNING. @jknack I have had this fix for a long time in our own custom value resolvers. I can put in a PR if you are interested in releasing . Otherwise folks can simple just copy the MemberValueResolver code and make their own ValueResolvers. Also my solution uses MethodHandles since that is the future of reflection. |
I've been working on migrating some projects from java 8 to java 11. As I was compiling, I noticed a warning in the log:
So, I figured I'd create an issue and make sure you're aware. It works with java 11...not sure what will happen with java 12 which should be released March 2019.
The text was updated successfully, but these errors were encountered: