ormlite-android icon indicating copy to clipboard operation
ormlite-android copied to clipboard

OrmLiteConfigUtil doesn't recognize kotlin classes

Open daberni opened this issue 8 years ago • 6 comments

currently OrmLiteConfigUtil scans the project directory only for .java files and ignores the rest.

As kotlin is gaining more and more popularity it would make sense to support those files too (I am currently using kotlin, especially for all of my model classes).

There are at least 2 points which should get considered

  • include .kt files into search-filter
  • don't get classname from filename, as there could theoretically be multiple classes in one .kt file (although this isn't the standard from the guidelines. This would be possible in the case of .java-files too)

package name is already parsed from inside the file instead of the directory tree, so this should apply for the class name too.

Current workaround for people with the same issue: derive from OrmLiteConfigUtil overwrite main() and call writeConfigFile with all required classes directly.

daberni avatar Mar 06 '17 13:03 daberni

Looking into source files is not needed at all. And it can even cause all kinds of problems when doing it without full-blown code parsing.

Reflection can do everything that's needed there. For enumerating all classes, there's a helper library: https://github.com/ronmamo/reflections

sp-1234 avatar Jul 10 '17 13:07 sp-1234

Passing classes directly does not work for me. I still get ClassNotFoundException.

anri-vin avatar Sep 11 '17 09:09 anri-vin

Well I have another issue. Seems it is not possible to find any kotlin class at all, when I pass class directly, and everything ok during compile time but there is java.lang.NoClassDefFoundError during runtime.

memfis19 avatar Nov 20 '17 16:11 memfis19

Yeah you are right, there seems to be a problem with the classloader too, will have to investigate a bit further as my first approach at least worked once...

daberni avatar Nov 21 '17 18:11 daberni

As temporary solution I've added classpath for kotlin classes at runtime, maybe it can be useful for someone:

private static void addPath(String classPath) throws Exception {
        File file = new File(classPath);
        URL url = file.toURL();
        URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Class urlClass = URLClassLoader.class;
        Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class});
        method.setAccessible(true);
        method.invoke(urlClassLoader, new Object[]{url});
    }

And actual path for kotlin classes i.e. File kotlinClasses = new File("../../build/tmp/kotlin-classes/{selectedBuildType}")

memfis19 avatar Nov 21 '17 21:11 memfis19

Has anyone managed to write a version of OrmLiteConfigUtil that works with Kotlin classes?

ankushg avatar Mar 19 '19 05:03 ankushg