-
Notifications
You must be signed in to change notification settings - Fork 34
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
[proposal] check class name against keywords and java.lang #71
Comments
in the specific case of refering to java.lang.Object it's an issue, not an enhancement. |
Makes total sense. Thanks for pointing this out. Part of 3.3.0 release |
I think it's a bit more complex than that (Maybe I'm wrong). In some cases people may want to create eg an Object class to represent something. That is, nothing in the java language prevents from creating my.pck.Object . The ideal solution would be to ensure the java.lang package is not removed when the class refereed to has the same name as a class declared in the current class or its subclasses. example if I want to create
Using JCodemodel, I can't, because JCodemodel will translate to Ensuring the JCodemodel does not do it may require a lot of additional modifications, so in a first and simpler pass I advise to allow the user to specifically prevent the usage of a class name that is already present in java.lang. I think you misunderstood the part "reaction" that I wrote. I specifically wrote that throwing an exception (or returning false) is a bad behaviour unless specifically required by the user. The optimal solution is to make JCodeModel refer to not remove the java.lang. prefix when the class name is overriden by a local class. |
I will make more of an example of what should compile correctly, gimme some more time. |
This is valid java code, that should be produceable by jcm.
|
It does it, but the other way around: package jcodemodel;
public class MyClass {
public void call(Object obj) {
}
public void call(MyClass.Object obj) {
}
public void call(Number obj) {
}
public void call(MyClass.Number obj) {
}
public static class Number {
}
public static class Object {
}
} That is semantically equivalent I think ok.... |
No because the java compiler will refuse to compile it. Number and Myclass.Number are equivalent in those cases, therefore you have two methods with the same signature. That's why I gave the link to the memory compiler, because it shows that the compilation of the generated classes fails. |
here is the code that shows this :
Here is the output result :
If you remove the last call to findClass you still have the output but it does not fail because it does not care if the class is badly defined. failure :
|
Ah okay - got it. I will check the dynamic compilation thingy separate... package jcodemodel;
public class MyClass {
public void call(java.lang.Object obj) {
}
public void call(MyClass.Object obj) {
}
public void call(java.lang.Number obj) {
}
public void call(MyClass.Number obj) {
}
public void call(Byte obj) {
}
public void call(Long obj) {
}
public static class Number {
}
public static class Object {
}
} |
Note that if you can fix that quickly, there is no point failing when the user requests to create an Object class anymore. Ideally you want the previous result to replace the Myclass.Number by Number, because then MyClass.number is the highest priority in the resolution. |
Keywords
see #70 for the keywords
java.lang
Issue
This is an issue because this package is already imported and as such not referenced to.
Typically if I create a my.package.Object class and then I create a method
The object other actually refers to the class I am crating, and not the java.lang.Object class.
This issue literally happened to me when compiling against a openapi web service that produces "Object" responses.
Detection
This issue is very complicated : since the java reflection works bottom-up, that is the classes know their package but the package doesn't know its classes, it's difficult to find if the class exists without a java.lang.class.forName("java.lang."+sName), with trycatch around. This expensive call requires to cache the result in a hasmap<String, Boolean>
here is my code
Looking at how fast it is to use JCodeModel, I consider the use of synchronization to be worthless (I usually don't and thus would have synced over the hashmap in the method)
It should check for specific exception (security exception means I can't tell)
Reaction
In the case the class C is in java.lang already, two possibilities :
IMO both should be available. mark the class C as misleading , AND allow users to avoid this with a compilation option.
Especially, I'm not sure how to make the misleading part, since the writing of the class names may be done without the knowledge of where that name is used.
The text was updated successfully, but these errors were encountered: