0

I have this requirement.

There are a bunch of classes in a package. Each class has constructors. I want to print out all the constructors of those classes in that package.

NOTE: I checked reflections, where it allows to print the constructor names for any class, just at the class level.

try {
      Constructor<?>[] publicConstructors = Class.forName("class_name").getConstructors();
      System.out.println(Arrays.toString(publicConstructors));
    } catch (ClassNotFoundException e) {
       e.printStackTrace();
    }

But I want to print all the constructors at the package level.

Is this possible? Please help.

Eajaz
  • 469
  • 4
  • 21
  • I think you'll have to find classes inside a package first and than load/find constructor. Former isnt possible unless you write your own classloader. – ajay.patel Jun 26 '17 at 23:34
  • Since you seem to know how to find constructors of a class, it seems your question is actually about how to find the classes of a package, so closing as duplicate of [Can you find all classes in a package using reflection?](https://stackoverflow.com/q/520328/5221149) --- You do know how to nest two loops to then get all the constructors, right? – Andreas Jun 26 '17 at 23:52

3 Answers3

2

You have to use .getDeclaredConstructors() instead.

try {
            Constructor<?>[] constructors = Class.forName("class_name").getDeclaredConstructors();
            System.out.println(Arrays.toString(constructors));
        } catch (Exception e) {
            e.printStackTrace();
        }

Prints

[st.Construct(java.lang.String), public st.Construct(int), st.Construct()]

dehasi
  • 2,644
  • 1
  • 19
  • 31
2

You can't just "search" for classes within a package in vanilla java unfortunately (not in any reasonable sense). You have the right idea using reflection, you could try using the org.reflections library

 Set<Class<? extends Object>> classesInPackage = 
      new Reflections("package.path").getSubTypesOf(Object.class);

This third party library does allow you to search the existing classpath. Once you have that set of classes, you can use ordinary reflection to get it's constructor, then do what you will with it.

Julian
  • 1,665
  • 2
  • 15
  • 33
1

Open Source Reflections Library: https://github.com/ronmamo/reflections

You need to start by setting up the index through Reflection:

List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());

Reflections reflections = new Reflections(new ConfigurationBuilder()
    .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())
    .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
    .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.your.package"))));

Then you can perform a query in a package:

Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
cela
  • 2,352
  • 3
  • 21
  • 43