In my Java application, I use a third-party library.
However, I found something strange, there are some nested packages, and some classes whose name may be the same as the name of the package.
I am afraid I can not make it clear. Here is an example:
package
com.xx.a
com.xx.a.a
And there is a class named 'a' inside the 'com.xx.a'.
So if I want to call this class 'a'...
I write:
a ma = new com.xx.a.a();
Then the IDE will think that I mean the package 'com.xx.a.a'.
Then I can not call it.
I wonder why?
By the way, it seems that the library provider did not want us to use these kinds of classes.
How do they do this?
You need to do this:
Or import the package:
The Java language allows class identifiers to be obscured by package identifiers. In your case the class
com.xx.a
is obscured by the packagecom.xx.a
.From the Java Language Specification:
I must say that the rules in §6.5 for classifying the meaning of an identifier are far from clear though.
The reason why you still happen to have a copy of a library that violates this rule is because the rule does not apply for class files / JAR files and the JVM.
This means that you can have such naming conflicts in JAR files, but you'll never see it as output from
javac
. The tool that has produced these class / package names is most likely a code obfuscator which produces this kind of messy code to compress the size of the files and to obfuscate the code to prevent reverse engineering.PS. At a closer look it may actually be a bug on the Eclipse side (assuming that's the IDE you're talking about). By letting an empty package name collide with a class name, Eclipse chokes on something javac accepts. The spec is hard to follow, but from what I can see, javac follows the spec in this case.
Sometimes you want to access the package and not the class. But the order for the compiler is variable type package. I had to rename all the packages, classes and variables.
For example: int a would become int a_var class a would become class a_class package package a would become a_package
I used eclipse refactoring functions for this.
You still have to go through the classes and rename all conflicts by appending _var, _class or _package. Hard work, but since there is no way for Eclipse to know by usage(String a.a.b.c, import a.b.c.d) he will still try to find first variable/type even when it is a package.
The library is likely obfuscated (e.g. using proguard) to reduce size, prevent reverse engineering and "hide" stuff you're not supposed to use. Even if you manage to create an instance of this class, I would recommend against it, as you don't know what it will do or how it can/should be used.
we can not do this in java:
the package name clashes with a class in the parent package,.