Java类名与嵌套包名相同 [英] Java class name same as the nested package name

查看:214
本文介绍了Java类名与嵌套包名相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java应用程序中,我使用的是第三方库。

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

并且'内部有一个名为'a'的类com.xx.a'。

And there is a class named 'a' inside the 'com.xx.a'.

所以如果我想把这个类称为'a'...

So if I want to call this class 'a'...

我写道:

a ma = new com.xx.a.a();

然后IDE会认为我的意思是包'com.xx.a.a'。

Then the IDE will think that I mean the package 'com.xx.a.a'.

然后我无法打电话。

我想知道为什么?

顺便说一句,图书馆提供者似乎不希望我们使用这些类。

By the way, it seems that the library provider did not want us to use these kinds of classes.

他们是如何做到的?

推荐答案

Java语言允许类标识符被包标识符隐藏。在您的情况下, <$ em> com。 xx.a

The Java language allows class identifiers to be obscured by package identifiers. In your case the class com.xx.a is obscured by the package com.xx.a.

来自 Java语言规范


6.3.2模糊的声明

一个简单的名称可能出现在上下文中,它可能被解释为变量的名称,a输入或一个包。在这些情况下,§6.5的规则指定将优先选择变量而不是类型,并且将优先选择类型而不是包。因此,有时可能无法通过其简单名称引用可见类型或包声明。 我们说这样的声明是模糊的。

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

我必须说§6.5中的规则进行分类标识符的含义远非清晰。

I must say that the rules in §6.5 for classifying the meaning of an identifier are far from clear though.

您仍然碰巧有一个违反此规则的库副本的原因是因为该规则不适用对于类文件/ JAR文件和JVM。

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.

这意味着你可以在JAR文件中有这样的命名冲突,但你永远不会将其视为 javac 的输出。生成这些类/包名称的工具很可能是代码混淆器,它会产生这种混乱的代码来压缩文件的大小并混淆代码以防止逆向工程。

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。仔细看看它实际上可能是Eclipse方面的一个错误(假设你正在谈论的是IDE)。通过让空包名称与类名冲突,Eclipse会对javac接受的内容进行扼杀。该规范很难遵循,但从我所看到的情况来看,javac在这种情况下遵循了规范。

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.

这篇关于Java类名与嵌套包名相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆