Created
December 16, 2019 17:58
-
-
Save d-baranowski/53a4341f004d991b24d22fb3e26e07b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sportinglife.application; | |
import org.apache.commons.lang3.reflect.MethodUtils; | |
import org.skife.jdbi.v2.sqlobject.SqlQuery; | |
import org.skife.jdbi.v2.sqlobject.SqlUpdate; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.jar.JarEntry; | |
import java.util.jar.JarInputStream; | |
public class Reflector { | |
public static final List<Class<?>> getClassesInPackage(String packageName) { | |
String path = packageName.replaceAll("\\.", File.separator); | |
List<Class<?>> classes = new ArrayList<>(); | |
String[] classPathEntries = System.getProperty("java.class.path").split( | |
System.getProperty("path.separator") | |
); | |
String name; | |
for (String classpathEntry : classPathEntries) { | |
if (classpathEntry.endsWith(".jar")) { | |
File jar = new File(classpathEntry); | |
try { | |
JarInputStream is = new JarInputStream(new FileInputStream(jar)); | |
JarEntry entry; | |
while ((entry = is.getNextJarEntry()) != null) { | |
name = entry.getName(); | |
if (name.endsWith(".class")) { | |
if (name.contains(path) && name.endsWith(".class")) { | |
String classPath = name.substring(0, entry.getName().length() - 6); | |
classPath = classPath.replaceAll("[\\|/]", "."); | |
classes.add(Class.forName(classPath)); | |
} | |
} | |
} | |
} catch (Exception ex) { | |
// Silence is gold | |
} | |
} else { | |
try { | |
File base = new File(classpathEntry + File.separatorChar + path); | |
for (File file : base.listFiles()) { | |
name = file.getName(); | |
if (name.endsWith(".class")) { | |
name = name.substring(0, name.length() - 6); | |
classes.add(Class.forName(packageName + "." + name)); | |
} | |
} | |
} catch (Exception ex) { | |
// Silence is gold | |
} | |
} | |
} | |
return classes; | |
} | |
public static void main(final String[] args) throws Exception { | |
String serviceName = "cms-media-service"; | |
List<Class<?>> classesInPackage = getClassesInPackage("com.sportinglife.dao"); | |
for (Class<?> cl : classesInPackage) { | |
Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(cl, SqlUpdate.class); | |
for (Method method : methodsWithAnnotation) { | |
System.out.println(serviceName + ", " + cl.getName() + ", " + method.getAnnotation(SqlUpdate.class).value().replaceAll("\\s+", " ")); | |
} | |
} | |
for (Class<?> cl : classesInPackage) { | |
Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(cl, SqlQuery.class); | |
for (Method method : methodsWithAnnotation) { | |
System.out.println(serviceName + ", " + cl.getName() + ", " + method.getAnnotation(SqlQuery.class).value().replaceAll("\\s+", " ")); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment