如何访问默认包中的 java 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/283816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access java-classes in the default-package?
提问by Mnementh
I'm working now together with others in a grails project. I have to write some Java-classes. But I need access to an searchable object created with groovy. It seems, that this object has to be placed in the default-package.
我现在和其他人一起在一个 grails 项目中工作。我必须编写一些 Java 类。但是我需要访问使用 groovy 创建的可搜索对象。看来,这个对象必须放在 default-package 中。
My question is: Is there a way to access this object in the default-package from a Java-class in a named package?
我的问题是:有没有办法从命名包中的 Java 类访问默认包中的这个对象?
采纳答案by VonC
You can't useclasses in the default package from a named package.
(Technicallyyou can, as shown in Sharique Abdullah's answerthrough reflection API, butclasses from the unnamed namespace are not in scopein an import declaration)
您不能使用来自命名包的默认包中的类。
(从技术上就可以了,如图Sharique阿卜杜拉的回答通过反射API,但是从无名命名空间的类是不在范围内以进口申报)
Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:
在 J2SE 1.4 之前,您可以使用如下语法从默认包中导入类:
import Unfinished;
That's no longer allowed. So to access a default package class from within a packaged class requires moving the default package class into a package of its own.
那已经不允许了。因此,要从打包类中访问默认包类,需要将默认包类移动到它自己的包中。
If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning.
如果您可以访问由 groovy 生成的源代码,则需要进行一些后处理以将文件移动到专用包中并在其开头添加此“包”指令。
Update 2014: bug 6975015, for JDK7 and JDK8, describe an even stricterprohibition against import from unnamed package.
2014 年更新:错误 6975015,对于 JDK7 和 JDK8,描述了更严格的禁止从未命名包导入。
The
TypeName
must be the canonical name of a class type, interface type, enum type, or annotation type.
The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type is a member of a named package, or a compile-time error occurs.
的
TypeName
必须是类的类型,接口类型,枚举类型,或注释类型的规范名称。
该类型必须是命名包的成员,或者其最外层词法封闭类型是命名包的成员的类型的成员,否则会发生编译时错误。
Andreaspoints out in the comments:
"why is [the default package] there in the first place? design error?"
No, it's deliberate.
JLS 7.4.2. Unnamed Packagessays: "Unnamed packages are provided by the Java SE platform principally for convenience when developing small or temporary applications or when just beginning development".
“为什么 [默认包] 首先存在?设计错误?”
不,是故意的。
JLS 7.4.2。Unnamed Packages说:“Java SE 平台提供未命名包主要是为了在开发小型或临时应用程序或刚开始开发时提供便利”。
回答by Ken Gentle
You can use packages in the Groovy
code, and things will work just fine.
您可以在Groovy
代码中使用包,一切都会好起来的。
It may mean a minor reorganization of code under grails-app
and a little bit of a pain at first, but on a large grails project, it just make sense to organize things in packages. We use the Java standard package naming convention com.foo.<app>.<package>
.
这可能意味着对代码进行小幅重组,grails-app
一开始会有点痛苦,但在大型 grails 项目中,将内容组织在包中是有意义的。我们使用 Java 标准包命名约定com.foo.<app>.<package>
。
Having everything in the default package becomes a hindrance to integration, as you're finding.
正如您所发现的那样,将所有内容都放在默认包中会成为集成的障碍。
Controllers seem to be the one Grails artifact (or artefact) that resists being put in a Java package. Probably I just haven't figured out the Convention
for that yet. ;-)
控制器似乎是一种拒绝放入 Java 包的 Grails 工件(或工件)。可能我只是还没有弄清楚Convention
。;-)
回答by Ken Gentle
In fact, you can.
事实上,你可以。
Using reflections API you can access any class so far. At least I was able to :)
到目前为止,您可以使用反射 API 访问任何类。至少我能够:)
Class fooClass = Class.forName("FooBar");
Method fooMethod = fooClass.getMethod("fooMethod", String.class);
String fooReturned = (String)fooMethod.invoke(fooClass.newInstance(), "I did it");
回答by Sean
回答by Duracell De Monaco
just to complete the idea:
只是为了完成这个想法:
From insidedefault-package you can access objects resided in named packages.
从default-package内部,您可以访问位于命名包中的对象。