-
Notifications
You must be signed in to change notification settings - Fork 858
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
please support java class access, eg: java.lang.Object.class #757
Comments
This would be nice to have, but there are a few workarounds if you need it right now. If you have an instance of the class you want, you can call getClass() on it
You can get the current Thread's context ClassLoader and load the class from there. If you pass
|
just found another way to do it
|
thank you very mach! |
rhino/src/org/mozilla/javascript/NativeJavaClass.java Lines 33 to 34 in 5cf8b7f
|
@tuchida thanks for pointing that out! Is there any reason why the property shouldn't just be |
I don't know why
The ECMAScript specification allows for the use of reserved words in property names. var a = { class: 123 };
a.class; // 123 Also, Rhino specification allows shorthand notation for beans, so you can write var o = java.lang.Object();
o.class; // class java.lang.Object
o.getClass() === o.class; // true |
I meant "class" should be prohibited as a java static field or method on the underlying java class, so I think there should not be a conflict when trying to access a "class" property of the NativeJavaClass via the The NativeJavaClass represents the class itself, which is used to access static members and constructors. It doesn't have a getClass method, as only instances of Java classes do, so the shorthand does not work in this case. |
I guess another reason to believe there is no chance of conflict is that |
I see. |
It seems that even now, depending on your settings, you cannot use reserved words as identifiers. rhino/src/org/mozilla/javascript/TokenStream.java Lines 755 to 761 in b0fb5f8
|
I don't think properties are identifiers, so that shouldn't be a problem. |
This works in Rhino, at least when using Context.VERSION_ES6 as language level: Can this issue be closed? |
@p-bakker I think this is still an issue to be addressed. It's already been pointed out above that it works on an instance of a class (because it ends up calling the I don't see any reason to not add another property to NativeJavaObject called |
Ahh, missed that detail Well, as all classes extend Object, Object defines a getClass() method and static methods cannot mirror instance methods and because 'class' in Java is a reserved word, so it cannot be used as the name of a (static) field, I personally don't see a (backwards or forward) compatibility issue with adding a 'class' property to NativeJavaObject that mimics the 'class' property of types in Java. More self-explanatory than the obscure (and I think undocumented) '__javaObject__'. Only question is from which Context.VERSION_Xxxx onwards we should, as using 'class' as a propertyName want always allowed I think |
Mmm, I guess there is sort of an odd scenario: if there is a class out there that is used icw Rhino and accessing that class as a NativeJavaObject and a class defines a Likelyhood of that happening? Very slim if you'd ask me. We could support that scenario by in that case making the NativeJavaObject's |
Other question: ES6 is from 2015. We are in 2024 now. Are there still use cases, where you use rhino not in ES6 mode? |
For us at ServiceNow, keeping old versions around and executing code in the same way is a fundamental requirement. |
No description provided.
The text was updated successfully, but these errors were encountered: