j256 / ormlite-android

ORMLite Android functionality used in conjunction with ormlite-core
http://ormlite.com/
ISC License
1.59k stars 367 forks source link

OrmLiteConfigUtil doesn't recognize kotlin classes #79

Open daberni opened 7 years ago

daberni commented 7 years ago

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

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.

sp-1234 commented 7 years ago

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

anri-vin commented 6 years ago

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

memfis19 commented 6 years ago

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.

daberni commented 6 years ago

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...

memfis19 commented 6 years ago

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}")

ankushg commented 5 years ago

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