Skip to content

Commit

Permalink
feat: merge ORM from Gluu (#1200)
Browse files Browse the repository at this point in the history
* fix: use lower table names in inspected tables metadata

* feat: allow to create inner beans

* feat: ignore methods with Transient annotation
  • Loading branch information
yurem authored Apr 13, 2022
1 parent 3ab6a11 commit 685a159
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,12 @@ protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotatio
T entry;
List<String> customObjectClasses = null;
try {
entry = ReflectHelper.createObjectByDefaultConstructor(entryClass);
Class<?> declaringClass = entryClass.getDeclaringClass();
if (declaringClass == null) {
entry = ReflectHelper.createObjectByDefaultConstructor(entryClass);
} else {
entry = (T) ReflectHelper.getConstructor(entryClass, declaringClass).newInstance((Object) null);
}
} catch (Exception ex) {
throw new MappingException(String.format("Entry %s should has default constructor", entryClass));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package io.jans.orm.reflect.property;

import java.beans.Introspector;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

Expand Down Expand Up @@ -153,7 +154,6 @@ private static BasicSetter getSetterOrNull(Class<?> theClass, String propertyNam
}

Method method = setterMethod(theClass, propertyName);

if (method != null) {
if (!ReflectHelper.isPublic(theClass, method)) {
method.setAccessible(true);
Expand Down Expand Up @@ -181,14 +181,21 @@ private static Method setterMethod(Class<?> theClass, String propertyName) {
Method[] methods = theClass.getDeclaredMethods();
Method potentialSetter = null;
for (int i = 0; i < methods.length; i++) {
String methodName = methods[i].getName();
Method method = methods[i];

boolean isTransient = isTransient(method);
if (isTransient) {
continue;
}

if (methods[i].getParameterTypes().length == 1 && methodName.startsWith("set")) {
String methodName = method.getName();

if (method.getParameterTypes().length == 1 && methodName.startsWith("set")) {
String testStdMethod = Introspector.decapitalize(methodName.substring(3));
String testOldMethod = methodName.substring(3);
if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {
potentialSetter = methods[i];
if (returnType == null || methods[i].getParameterTypes()[0].equals(returnType)) {
potentialSetter = method;
if (returnType == null || method.getParameterTypes()[0].equals(returnType)) {
return potentialSetter;
}
}
Expand Down Expand Up @@ -216,6 +223,16 @@ private static BasicGetter getGetterOrNull(Class<?> theClass, String propertyNam
}

Method method = getterMethod(theClass, propertyName);
if (method != null) {
Annotation[] methodAnnotations = method.getAnnotations();
if (methodAnnotations != null) {
for (Annotation methodAnnotation : methodAnnotations) {
if (methodAnnotation.annotationType().equals(java.beans.Transient.class)) {
method = null;
}
}
}
}

if (method != null) {
if (!ReflectHelper.isPublic(theClass, method)) {
Expand All @@ -241,14 +258,21 @@ private static Method getterMethod(Class<?> theClass, String propertyName) {
for (int i = 0; i < methods.length; i++) {
// only carry on if the method has no parameters
if (methods[i].getParameterTypes().length == 0) {
String methodName = methods[i].getName();
Method method = methods[i];

boolean isTransient = isTransient(method);
if (isTransient) {
continue;
}

String methodName = methods[i].getName();

// try "get"
if (methodName.startsWith("get")) {
String testStdMethod = Introspector.decapitalize(methodName.substring(3));
String testOldMethod = methodName.substring(3);
if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {
return methods[i];
return method;
}

}
Expand All @@ -258,12 +282,25 @@ private static Method getterMethod(Class<?> theClass, String propertyName) {
String testStdMethod = Introspector.decapitalize(methodName.substring(2));
String testOldMethod = methodName.substring(2);
if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {
return methods[i];
return method;
}
}
}
}
return null;
}

private static boolean isTransient(Method method) {
Annotation[] methodAnnotations = method.getAnnotations();
if (methodAnnotations != null) {
for (Annotation methodAnnotation : methodAnnotations) {
if (methodAnnotation.annotationType().equals(java.beans.Transient.class)) {
return true;
}
}
}

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
Expand Down Expand Up @@ -246,7 +244,7 @@ private void loadTableMetaData(DatabaseMetaData databaseMetaData, Connection con
tableColumns.put(columnName, columTypeName);
}

tableColumnsMap.put(tableName, tableColumns);
tableColumnsMap.put(StringHelper.toLowerCase(tableName), tableColumns);
}

takes = System.currentTimeMillis() - takes;
Expand Down Expand Up @@ -398,7 +396,7 @@ public SQLQueryFactory getSqlQueryFactory() {

public TableMapping getTableMappingByKey(String key, String objectClass) {
String tableName = objectClass;
Map<String, String> columTypes = tableColumnsMap.get(tableName);
Map<String, String> columTypes = tableColumnsMap.get(StringHelper.toLowerCase(tableName));
if ("_".equals(key)) {
return new TableMapping("", tableName, objectClass, columTypes);
}
Expand Down

0 comments on commit 685a159

Please sign in to comment.