me.shakiba.jdbi.annotation.AnnoClass Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jdbi-annotation Show documentation
Show all versions of jdbi-annotation Show documentation
JPA annotation extension for jDBI
The newest version!
package me.shakiba.jdbi.annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class AnnoClass {
private final static WeakHashMap, AnnoClass>> cache = new WeakHashMap, AnnoClass>>();
@SuppressWarnings("unchecked")
public static AnnoClass get(Class clazz) {
AnnoClass> get = null;
if ((get = cache.get(clazz)) == null) {
synchronized (cache) {
if ((get = cache.get(clazz)) == null) {
cache.put(clazz, get = new AnnoClass(clazz));
}
}
}
return (AnnoClass) get;
}
private final List setters = new ArrayList();
private final List getters = new ArrayList();
private AnnoClass(Class clazz) {
if (logger.isDebugEnabled()) {
logger.debug("init " + clazz);
}
inspectClass(clazz);
inspectSuperclasses(clazz);
if (logger.isDebugEnabled()) {
logger.debug("init " + clazz + ": " + setters.size()
+ " setters and " + getters.size() + " getters.");
}
}
private void inspectSuperclasses(Class super C> clazz) {
while ((clazz = clazz.getSuperclass()) != null) {
if (clazz.isAnnotationPresent(MappedSuperclass.class)) {
inspectClass(clazz);
}
}
}
private void inspectClass(Class super C> clazz) {
for (Field member : clazz.getDeclaredFields()) {
if (member.getAnnotation(Column.class) != null) {
setters.add(new AnnoMember(clazz, member));
getters.add(new AnnoMember(clazz, member));
}
}
for (Method member : clazz.getDeclaredMethods()) {
if (member.getAnnotation(Column.class) == null) {
continue;
}
if (member.getParameterTypes().length == 1) {
setters.add(new AnnoMember(clazz, member));
} else if (member.getParameterTypes().length == 0) {
getters.add(new AnnoMember(clazz, member));
}
}
}
public List setters() {
return setters;
}
public List getters() {
return getters;
}
private static final Logger logger = LoggerFactory.getLogger(AnnoClass.class);
}