[英]How to access java-classes in the default-package?
I'm working now together with others in a grails project.我现在和其他人一起在一个 grails 项目中工作。 I have to write some Java-classes.我必须编写一些 Java 类。 But I need access to an searchable object created with groovy.但是我需要访问使用 groovy 创建的可搜索对象。 It seems, that this object has to be placed in the default-package.看来,这个对象必须放在 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 类访问默认包中的这个对象?
You can't use classes in the default package from a named package.您不能使用来自命名包的默认包中的类。
( Technically you can, as shown in Sharique Abdullah's answer through reflection API, but classes from the unnamed namespace are not in scope in an import declaration ) (从技术上讲,您可以通过反射 API,如 Sharique Abdullah 的回答所示,但未命名命名空间中的类不在导入声明的范围内)
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 stricter prohibition 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.TypeName
必须是类类型、接口类型、枚举类型或注释类型的规范名称。
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 .该类型必须是命名包的成员,或者其最外层词法封闭类型是命名包的成员的类型的成员,否则会发生编译时错误。
Andreas points out in the comments : Andreas 在评论中指出:
"why is [the default package] there in the first place? design error?" “为什么 [默认包] 首先存在?设计错误?”
No, it's deliberate.不,是故意的。
JLS 7.4.2. JLS 7.4.2。 Unnamed Packages says: "Unnamed packages are provided by the Java SE platform principally for convenience when developing small or temporary applications or when just beginning development". Unnamed Packages说:“Java SE 平台提供未命名包主要是为了在开发小型或临时应用程序或刚开始开发时提供便利”。
In fact, you can.事实上,你可以。
Using reflections API you can access any class so far.到目前为止,您可以使用反射 API 访问任何类。 At least I was able to :)至少我能够:)
Class fooClass = Class.forName("FooBar");
Method fooMethod = fooClass.getMethod("fooMethod", String.class);
String fooReturned = (String)fooMethod.invoke(fooClass.newInstance(), "I did it");
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.这可能意味着在grails-app
下对代码进行小幅重组,一开始会有点痛苦,但在大型 grails 项目中,将内容组织在包中是有意义的。 We use the Java standard package naming convention com.foo.<app>.<package>
.我们使用 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.控制器似乎是一种拒绝放入 Java 包的 Grails 工件(或工件)。 Probably I just haven't figured out the Convention
for that yet.可能我只是还没有弄清楚Convention
。 ;-) ;-)
just to complete the idea:只是为了完成这个想法:
From inside default-package you can access objects resided in named packages.从default-package内部,您可以访问位于命名包中的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.