0

Is there a way to reference a package in java in code without using a String?

Let me explain myself:

I've a function that fetches all object contained in a package, but I've to reference that package using a String, lets say "com.google.guava". This works, however, If I change the package name, the IDE is not able to automatically refractor the references, or if the package is external and dissapears after a major version change it is not detected at compile time.

With classes, I'd use a Class object directly when possible or Class#getName if what I need is a String representing the FQDN of the class, but I don't know if there's a way to do so with packages.

Sometimes I just do

Class<?> clazz = Foo.class;
Package p = clazz.getPackage();

This is more a curiosity than a real issue, I usually just reference by String and write a junit test in order to detect nom-existant packages used this way

DGoiko
  • 346
  • 2
  • 19
  • 1
    `Foo.class.getPackage()` is already the best you can get. But “all object contained in a package” makes no sense. A package contains classes, not objects. – Holger Jun 02 '20 at 14:26
  • @Holger Well, I get config files and other stuff, I know they're not conceptually in the package (concerning to Java), but they're in the same directory and the package is the way I use to find the folder. English is not my mother languaje and I wanted to keep the post as simple as possible. My function returns all java property files, xml configs and classes (it is for a code generator, the function gets the existing generated files in order to update them. I know they should be in a specific resources folder, but I'm inheriting a bad project structure :(). This question was mostly curiosity – DGoiko Jun 04 '20 at 19:11
  • 1
    I don’t think that resources have to be in a specific resources folder. But if you mean “resources” you should not have said “object”. When you want to access resources, there is no point in getting the `Package` object, as the class `Package` does not offer anything for resource lookup. The `Package` class only exists to allow accessing meta information. Resources have to be accessed via `Foo.class.getResource(…)` anyway. The standard way to iterate over all resources, is [to open a filesystem](https://stackoverflow.com/a/36021165/2711488). – Holger Jun 05 '20 at 08:11

1 Answers1

2

I can think of the following ways to get a Package object:

  1. Call classLoader.getPackage(java.lang.String) or Package.getPackage(java.lang.String). These methods are deprecated.

  2. Call Package classLoader.getDefinedPackage(java.lang.String)

  3. Call Package class.getPackage()

  4. Call Package[] Package.getPackages() and then scan through the resulting array for the one that you want.

The last two approaches qualify as finding a Package with using a String.

There is no equivalent to a Java "class literal" (i.e. SomeClass.class) for packages.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • All solutions have the same "problem", however, the "There is no equivalent to a Java "class literal" (i.e. SomeClass.class) for packages." part answers my question. Thank you. – DGoiko May 30 '20 at 22:14