reiern70 commented on PR #1603:
URL: https://github.com/apache/wicket/pull/1603#issuecomment-5730310264
> Hello @reiern70
>
> There seems to be different logic for each `Index.java` page :( Maybe it
worth to be unified?
>
> In such case I would propose to list package classes dynamically instead
of hardcoding them into static list
>
> with the code like this in the `BasePage.java`:
>
> ```
> protected static List<Class<? extends BasePage>> getPackageClasses(Class<?
extends BasePage> clazz) {
> Set<Class<? extends BasePage>> classes = new HashSet<>();
> final String packageName = clazz.getPackageName();
> try {
> Enumeration<URL> resources =
clazz.getClassLoader().getResources(packageName.replace('.', '/'));
> while (resources.hasMoreElements()) {
> classes.addAll(findClasses(new
File(resources.nextElement().getFile()), packageName));
> }
> } catch (Exception e) {
> // no-op
> }
> classes.remove(clazz);
> return List.copyOf(classes);
> }
>
> @SuppressWarnings("unchecked")
> private static Set<Class<? extends BasePage>> findClasses(File base,
String packageName) {
> Set<Class<? extends BasePage>> classes = new HashSet<>();
> if (!base.exists()) {
> return Set.of();
> }
> File[] files = base.listFiles();
> if (files == null) {
> return Set.of();
> }
> for (File f : files) {
> if (f.isDirectory()) {
> classes.addAll(findClasses(f, packageName + "."
+ f.getName()));
> } else {
> try {
> final String name = f.getName();
> Class<?> pClazz =
Class.forName(packageName + "." + name.substring(0, name.lastIndexOf('.')));
> if
(BasePage.class.isAssignableFrom(pClazz)) {
> classes.add((Class<? extends
BasePage>)pClazz);
> }
> } catch (ClassNotFoundException e) {
> // no-op
> }
> }
> }
> return classes;
> }
> ```
>
> this way examples can be "just added" without maintaining various lists :)
Very good idea. I will do so. Thanks for feedback.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]