How to access java classes in a package by default?

I am currently working with others in a grails project. I have to write some Java classes. But I need access to a search object created using groovy. It seems that this object should be placed in the package by default.

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

+91
java groovy
Nov 12 '08 at
source share
4 answers

You cannot use classes in a default package from a named package.
(Technically, you can, as shown in Sharique Abdullah, respond via the reflection API, but classes from the nameless namespace are not within the scope of the import declaration )

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

import Unfinished; 

This is no longer permitted . Therefore, to access the default package class from the packaged class, you must move the default package class to your own package.

If you have access to the source generated by groovy, some post-processing is required to move the file to the selected package and add this “package” directive at the beginning.




Update 2014: bug 6975015 , for JDK7 and JDK8, describes an even stricter ban on importing from an unnamed package.

TypeName must be the canonical name of the class type, interface type, enumeration type, or annotation type.
A type must be either a member of a named package , or a member of a type whose outer lexically spanning type is a member of a named package , or a compile-time error occurs .




Andreas points out in the comments :

"why does [default package] exist in the first place? design error?"

No, this is intentional.
JLS 7.4.2. Unnamed packages state: "Unnamed packages are provided by the Java SE platform mainly for convenience when developing small or temporary applications or just at the beginning of development."

+120
Nov 12 '08 at
source share

Actually you can.

Using the reflection API, you can access any class. At least I could :)

 Class fooClass = Class.forName("FooBar"); Method fooMethod = fooClass.getMethod("fooMethod", String.class); String fooReturned = (String)fooMethod.invoke(fooClass.newInstance(), "I did it"); 
+59
Feb 18 '09 at 14:10
source share

Use jarjar to repackage a jar file with the following rule:

 rule * <target package name>.@1 

All classes in the default package of the source jar file will go to the target package, thus being able to access.

+7
Mar 13 '14 at 2:37
source share

You can use the packages in Groovy code and everything will work fine.

This may mean a small reorganization of the code for grails-app and a little pain at first, but in a project with large grails it just makes sense to organize things in packages. We use the standard com.foo.<app>.<package> naming convention.

Having everything in the package by default becomes an obstacle to integration, as you find.

The controllers appear to be one of the Grails artifacts (or artifacts) that cannot be placed in a Java package. I guess I have not yet understood the Convention .; -)

+3
Nov 12 '08 at 14:12
source share



All Articles