0

The documentation for Reflections gives the first line on how to use Reflections:

Reflections reflections = new Reflections("com.my.project");

Except i don't know the name of the package. This is a piece of library code that should just be able to scan everything in the current package.

What is the Reflections equivalent of the .NET:

//Find all methods in all classes tagged with @Test annotation, 
//and add them to a list.
List<MethodInfo> testMethods = new ArrayList<>();

//Enumerate all assemblies in the current application domain
for (Assembly a : AppDomain.currentDomain.getAssemblies()) {
   //Look at each type (i.e. class) in the assembly
   for (Type t : a.getTypes()) {
      ...
   }
}

in other words, the "current" package? Or in the parlance of .NET

  • AppDomain.currentDomain

Edit:

  • Is it a duplicate? No.
  • Is it not a programming question? No
  • Does it need details or clarity? No
  • Does it need more focus? No
  • Is it opinion based? No

Bonus Reading

And the final straw:

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • 3
    Are you looking for `this.getClass().getPackage().getName()`? Or are you looking for something like https://stackoverflow.com/questions/421280/how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection? – Progman Jul 20 '22 at 19:33
  • 1
    Please clearify what you mean by `currentPackage`. Is it of where code resides, or maybe where caller code resides or maybe something even else. – Antoniossss Jul 20 '22 at 19:40
  • @Antoniossss I don't know what i mean in Java. I mean the Java equivalent of `AppDomain.currentDomain` where i can then find, at runtime, every class in my "program". In other words: every piece of code that exists until [this](https://i.imgur.com/NFP2Fja.png) folder. – Ian Boyd Jul 20 '22 at 19:43
  • 1
    You need to add some more context. What piece of code are you calling this from? Why do you need to know the name of the package? You can have a package, with class from your source folder, and classes from elsewhere on the classpath. – matt Jul 20 '22 at 20:15

2 Answers2

0

Java's concept of 'package' is much weaker than .net. In Java, the package containing a class is, for most purposes, just the prefix of the classes name.

OSGi, and recent Java versions with Modules, have a stronger model.

If you read down the documentation to https://github.com/ronmamo/reflections#scan, you will see that there are a number of filters that you can apply to control what this library looks at. If you are writing a library, you can either expose that API to your callers and let them tell you what classes to look at, or decide for yourself what to look at. But there is not requirement to have a single focal Java package as far as I see in that documentation.

Calling this.getClass().getPackage().getName() will mean that reflection is only looking at your code, not the rest of the application using your library.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Does that mean that @Progman's suggestion is wrong? If so, *why* is it wrong - why won't it work? – Ian Boyd Jul 20 '22 at 19:54
  • Nevermind, i see why it's wrong. `getPackage()` only returns the "path" of the class i'm currently in. I need to be able to iterate all code in the project. – Ian Boyd Jul 22 '22 at 03:48
0

Cannot be done.

Java does not have a reflection system.

Research Effort


Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219