ganchix/morphia-spring-boot-starter

When packaged into jar,MorphiaUtils.getClasses(packageName) returned empty list.

Closed this issue · 4 comments

ukid commented

wx20170726-145828

ReflecitonUtils.getFromJARFile
String className = jarEntry.getName();

className starts with "/BOOT-INF/classes" which will not match packageName from the parameter,can you help me out ? Thank you!

ukid commented

For now I overrode ReflecitonUtils.getFromJARFile,and it seems to work fine.Do you have any better idea?

public static Set<Class<?>> getFromJARFile(final ClassLoader loader, final String jar, final String packageName, final boolean mapSubPackages) throws IOException, ClassNotFoundException {
		final Set<Class<?>> classes = new HashSet<Class<?>>();
		final JarInputStream jarFile = new JarInputStream(new FileInputStream(jar));
		try {
			JarEntry jarEntry;
			do {
				jarEntry = jarFile.getNextJarEntry();
				if (jarEntry != null) {
					String className = jarEntry.getName();
					if (className.endsWith(".class")) {
						// add this line
						className = className.replace("BOOT-INF/classes/", "");
						String classPackageName = getPackageName(className);
						if (classPackageName.equals(packageName) || (mapSubPackages && isSubPackage(classPackageName, packageName))) {
							className = stripFilenameExtension(className);
							classes.add(Class.forName(className.replace('/', '.'), true, loader));
						}
					}
				}
			} while (jarEntry != null);
		} finally {
			jarFile.close();
		}
		return classes;
	}

Hi @ukid!

Sorry, I was in vacances, thanks for open the issue.

I am working on it.

The bug is resolved, you can use it now.

I'm going to upload the library in the maven central repository In the coming weeks.

Thanks for everything.
Regards

ukid commented

Thank you