如何访问default-package中的java-classes? [英] How to access java-classes in the default-package?

查看:313
本文介绍了如何访问default-package中的java-classes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在与Grails项目中的其他人一起工作。我必须编写一些Java类。但我需要访问使用groovy创建的可搜索对象。看来,这个对象必须放在default-package中。



我的问题是:有没有办法在指定包中的Java-class的default-package中访问此对象?

解决方案

您在命名包中的默认包中不能使用类。

技术上说)您可以像Sharique Abdullah的answer 通过反射API,来自未命名命名空间的类不在范围内 导入声明



在J2SE 1.4之前,您可以使用如下语法从默认包导入类:

  import未完成; 

这就是不再允许。因此,要从打包类中访问默认包类,需要将默认包类移动到它自己的包中。



如果您有权访问由groovy生成的源代码,需要一些后处理来将文件移动到专用包中,并在其开始处添加该包指令。






< 2014年更新:对于JDK7和JDK8, bug 6975015 描述了一个甚至禁止从未命名包中导入。


TypeName 必须是类类型,接口类型,枚举类型或注释类型的规范名称。

类型必须是命名包的成员或成员其中最外面的词法封闭类型是命名包或发生编译时错误的成员。



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.

My question is: Is there a way to access this object in the default-package from a Java-class in a named package?

解决方案

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)

Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:

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.


Update 2014: bug 6975015, for JDK7 and JDK8, describe an even stricter prohibition against import from unnamed package.

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.

这篇关于如何访问default-package中的java-classes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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