Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: merge ORM from Gluu #1200

Merged
merged 3 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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