[FEAT] continue refacto of the generation code

This commit is contained in:
Edouard DUPIN 2024-05-18 08:27:47 +02:00
parent b64825eaba
commit be1bb9c52a
17 changed files with 165 additions and 197 deletions

View File

@ -5,6 +5,7 @@
<artifactId>archidata</artifactId>
<version>0.8.9</version>
<properties>
<java.version>21</java.version>
<maven.compiler.version>3.1</maven.compiler.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>

View File

@ -1867,4 +1867,4 @@ public class DataAccess {
}
return outs;
}
}
}

View File

@ -8,7 +8,6 @@ import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -31,11 +30,11 @@ import jakarta.ws.rs.core.Response;
public class DataFactoryTsApi {
static final Logger LOGGER = LoggerFactory.getLogger(DataFactoryTsApi.class);
record APIModel(
String data,
String className) {}
/** Request the generation of the TypeScript file for the "Zod" export model
* @param classs List of class used in the model
* @throws Exception */
@ -59,7 +58,7 @@ public class DataFactoryTsApi {
RESTRequestVoid
} from "./rest-tools"
import {""";
for (final Class<?> clazz : classs) {
final Set<Class<?>> includeModel = new HashSet<>();
final Set<Class<?>> includeCheckerModel = new HashSet<>();
@ -97,7 +96,7 @@ public class DataFactoryTsApi {
}
generatedData.append("\n} from \"./model\"\n");
generatedData.append(api.data());
String fileName = api.className();
fileName = fileName.replaceAll("([A-Z])", "-$1").toLowerCase();
fileName = fileName.replaceAll("^\\-*", "");
@ -108,11 +107,11 @@ public class DataFactoryTsApi {
}
return apis;
}
record OrderedElement(
String methodName,
Method method) {}
public static APIModel createSingleApi(
final Class<?> clazz,
final Set<Class<?>> includeModel,
@ -122,12 +121,12 @@ public class DataFactoryTsApi {
// the basic path has no specific elements...
final String basicPath = ApiTool.apiAnnotationGetPath(clazz);
final String classSimpleName = clazz.getSimpleName();
builder.append("export namespace ");
builder.append(classSimpleName);
builder.append(" {\n");
LOGGER.info("Parse Class for path: {} => {}", classSimpleName, basicPath);
final List<OrderedElement> orderedElements = new ArrayList<>();
for (final Method method : clazz.getDeclaredMethods()) {
final String methodName = method.getName();
@ -135,7 +134,7 @@ public class DataFactoryTsApi {
}
final Comparator<OrderedElement> comparator = Comparator.comparing(OrderedElement::methodName);
Collections.sort(orderedElements, comparator);
for (final OrderedElement orderedElement : orderedElements) {
final Method method = orderedElement.method();
final String methodName = orderedElement.methodName();
@ -174,22 +173,8 @@ public class DataFactoryTsApi {
produces = null;
} else if (returnTypeModelRaw == Map.class) {
LOGGER.warn("Not manage the Map Model ... set any");
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
final Type typeKey = listType.getActualTypeArguments()[0];
final Type typeValue = listType.getActualTypeArguments()[1];
if (typeKey == String.class) {
if (typeValue instanceof ParameterizedType) {
final Type typeSubKey = listType.getActualTypeArguments()[0];
final Type typeSubValue = listType.getActualTypeArguments()[1];
if (typeKey == String.class) {
}
}
} else {
LOGGER.warn("Not manage the Map Model ... set any");
returnTypeModel = new Class<?>[] { Map.class };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
}
returnTypeModel = new Class<?>[] { Map.class };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
} else if (returnTypeModelRaw == List.class) {
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
returnTypeModelRaw = (Class<?>) listType.getActualTypeArguments()[0];
@ -311,7 +296,7 @@ public class DataFactoryTsApi {
LOGGER.trace(" data type: {}", emptyElement.get(0));
}
// ALL is good can generate the Elements
if (methodDescription != null) {
builder.append("\n\t/**\n\t * ");
builder.append(methodDescription);
@ -383,7 +368,7 @@ public class DataFactoryTsApi {
String isFist = null;
for (final String elem : produces) {
String lastElement = null;
if (MediaType.APPLICATION_JSON.equals(elem)) {
lastElement = "HTTPMimeType.JSON";
}
@ -495,7 +480,7 @@ public class DataFactoryTsApi {
builder.append("\n}\n");
return new APIModel(builder.toString(), classSimpleName);
}
public static void generatePackage(
final List<Class<?>> classApi,
final List<Class<?>> classModel,
@ -507,7 +492,7 @@ public class DataFactoryTsApi {
FileWriter myWriter = new FileWriter(pathPackage + File.separator + "model.ts");
myWriter.write(packageApi.toString());
myWriter.close();
final StringBuilder index = new StringBuilder("""
/**
* Global import of the package
@ -535,5 +520,5 @@ public class DataFactoryTsApi {
myWriter.close();
return;
}
}

View File

@ -17,7 +17,7 @@ public class AnalyzeApi {
final ApiGroupModel parsed = new ApiGroupModel(clazz, previousModel);
this.apiModels.add(parsed);
}
}
}

View File

@ -1,5 +1,5 @@
package org.kar.archidata.externalRestApi;
public class GeneratePythonModel {
}

View File

@ -1,5 +1,5 @@
package org.kar.archidata.externalRestApi;
public class GenerateTsApi {
}

View File

@ -24,14 +24,14 @@ public class ApiGroupModel {
// Origin class reference
Class<?> originClass;
// List of all API
public List<ApiModel> interfaces;
public List<ApiModel> interfaces = new ArrayList<>();
public ApiGroupModel(final Class<?> clazz, final ModelGroup previousModel) throws Exception {
this.originClass = clazz;
// the basic path has no specific elements...
this.restEndPoint = ApiTool.apiAnnotationGetPath(clazz);
this.name = clazz.getSimpleName();
final List<String> consumes = ApiTool.apiAnnotationGetConsumes(clazz);
final List<String> produces = ApiTool.apiAnnotationProduces(clazz);
@ -44,7 +44,7 @@ public class ApiGroupModel {
}
final Comparator<OrderedElement> comparator = Comparator.comparing(OrderedElement::methodName);
Collections.sort(orderedElements, comparator);
for (final OrderedElement orderedElement : orderedElements) {
// Check if the path is available
final RestTypeRequest methodType = ApiTool.apiAnnotationGetTypeRequest2(orderedElement.method());

View File

@ -10,19 +10,15 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.kar.archidata.dataAccess.DataFactoryZod;
import org.kar.archidata.dataAccess.DataFactoryZod.ClassElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.ws.rs.core.Response;
public class ApiModel {
static final Logger LOGGER = LoggerFactory.getLogger(ApiModel.class);
Class<?> originClass;
Method orignMethod;
// Name of the REST end-point name
public String restEndPoint;
// Type of the request:
@ -31,7 +27,7 @@ public class ApiModel {
public String description;
// need to generate the progression of stream (if possible)
boolean needGenerateProgress;
// List of types returned by the API
public List<ClassModel> returnTypes = new ArrayList<>();;
// Name of the API (function name)
@ -46,10 +42,10 @@ public class ApiModel {
// Possible output type of the REST API
public List<String> produces = new ArrayList<>();
private void updateReturnTypes(final Method method, final ModelGroup previousModel) {
private void updateReturnTypes(final Method method, final ModelGroup previousModel) throws Exception {
// get return type from the user specification:
final Class<?>[] returnTypeModel = ApiTool.apiAnnotationGetAsyncType(method);
LOGGER.info("Get return Type async = {}", returnTypeModel);
if (returnTypeModel != null) {
if (returnTypeModel.length == 0) {
throw new IOException("Create a @AsyncType with empty elements ...");
@ -67,23 +63,29 @@ public class ApiModel {
}
return;
}
final Class<?> returnTypeModelRaw = method.getReturnType();
LOGGER.info("Get type: {}", returnTypeModelRaw);
LOGGER.info("Get return Type RAW = {}", returnTypeModelRaw.getCanonicalName());
if (returnTypeModelRaw == Map.class) {
LOGGER.warn("Not manage the Map Model ... set any");
LOGGER.warn("Model Map");
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
this.returnTypes.add(new ClassMapModel(listType, previousModel));
return;
}
if (returnTypeModelRaw == List.class) {
} else if (returnTypeModelRaw == List.class) {
LOGGER.warn("Model List");
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
this.returnTypes.add(new ClassListModel(listType, previousModel));
return;
} else {
LOGGER.warn("Model Object");
this.returnTypes.add(previousModel.add(returnTypeModelRaw));
}
LOGGER.warn("List of returns elements:");
for (final ClassModel elem : this.returnTypes) {
LOGGER.warn(" - {}", elem);
}
this.returnTypes.add(previousModel.add(returnTypeModelRaw));
}
public ApiModel(final Class<?> clazz, final Method method, final String baseRestEndPoint,
final List<String> consume, final List<String> produce, final ModelGroup previousModel) throws Exception {
this.originClass = clazz;
@ -98,81 +100,17 @@ public class ApiModel {
this.produces = ApiTool.apiAnnotationProduces2(produce, method);
LOGGER.trace(" [{}] {} => {}/{}", methodType, methodName, baseRestEndPoint, methodPath);
this.needGenerateProgress = ApiTool.apiAnnotationTypeScriptProgress(method);
Class<?>[] returnTypeModel = ApiTool.apiAnnotationGetAsyncType(method);
boolean isUnmanagedReturnType = false;
boolean returnModelIsArray = false;
List<ClassElement> tmpReturn;
if (returnTypeModel == null) {
Class<?> returnTypeModelRaw = method.getReturnType();
LOGGER.info("Get type: {}", returnTypeModelRaw);
if (returnTypeModelRaw == Response.class) {
LOGGER.info("Get type: {}", returnTypeModelRaw);
}
if (returnTypeModelRaw == Response.class || returnTypeModelRaw == void.class
|| returnTypeModelRaw == Void.class) {
if (returnTypeModelRaw == Response.class) {
isUnmanagedReturnType = true;
}
returnTypeModel = new Class<?>[] { Void.class };
tmpReturn = new ArrayList<>();
this.produces = null;
} else if (returnTypeModelRaw == Map.class) {
LOGGER.warn("Not manage the Map Model ... set any");
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
final Type typeKey = listType.getActualTypeArguments()[0];
final Type typeValue = listType.getActualTypeArguments()[1];
if (typeKey == String.class) {
if (typeValue instanceof ParameterizedType) {
final Type typeSubKey = listType.getActualTypeArguments()[0];
final Type typeSubValue = listType.getActualTypeArguments()[1];
if (typeKey == String.class) {
}
}
} else {
LOGGER.warn("Not manage the Map Model ... set any");
returnTypeModel = new Class<?>[] { Map.class };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
}
} else if (returnTypeModelRaw == List.class) {
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
returnTypeModelRaw = (Class<?>) listType.getActualTypeArguments()[0];
returnModelIsArray = true;
returnTypeModel = new Class<?>[] { returnTypeModelRaw };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
} else {
returnTypeModel = new Class<?>[] { returnTypeModelRaw };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
}
} else if (returnTypeModel.length >= 0 && (returnTypeModel[0] == Response.class
|| returnTypeModel[0] == Void.class || returnTypeModel[0] == void.class)) {
if (returnTypeModel[0] == Response.class) {
isUnmanagedReturnType = true;
}
returnTypeModel = new Class<?>[] { Void.class };
tmpReturn = new ArrayList<>();
this.produces = null;
} else if (returnTypeModel.length > 0 && returnTypeModel[0] == Map.class) {
LOGGER.warn("Not manage the Map Model ...");
returnTypeModel = new Class<?>[] { Map.class };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
} else {
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
}
for (final ClassElement elem : tmpReturn) {
includeModel.add(elem.model[0]);
includeCheckerModel.add(elem.model[0]);
}
LOGGER.trace(" return: {}", tmpReturn.size());
for (final ClassElement elem : tmpReturn) {
LOGGER.trace(" - {}", elem.tsTypeName);
updateReturnTypes(method, previousModel);
LOGGER.trace(" return: {}", this.returnTypes.size());
for (final ClassModel elem : this.returnTypes) {
LOGGER.trace(" - {}", elem);
}
final Map<String, String> queryParams = new HashMap<>();
final Map<String, String> pathParams = new HashMap<>();
final Map<String, String> formDataParams = new HashMap<>();
final List<String> emptyElement = new ArrayList<>();
final Map<String, List<ClassModel>> queryParams = new HashMap<>();
final Map<String, List<ClassModel>> pathParams = new HashMap<>();
final Map<String, List<ClassModel>> formDataParams = new HashMap<>();
final List<ClassModel> emptyElement = new ArrayList<>();
// LOGGER.info(" Parameters:");
for (final Parameter parameter : method.getParameters()) {
// Security context are internal parameter (not available from API)
@ -180,59 +118,33 @@ public class ApiModel {
continue;
}
final Class<?> parameterType = parameter.getType();
String parameterTypeString;
final List<ClassModel> parameterModel = new ArrayList<>();
final Class<?>[] asyncType = ApiTool.apiAnnotationGetAsyncType(parameter);
if (parameterType == List.class) {
if (asyncType == null) {
LOGGER.warn("Detect List param ==> not managed type ==> any[] !!!");
parameterTypeString = "any[]";
} else {
final List<ClassElement> tmp = DataFactoryZod.createTables(asyncType, previous);
for (final ClassElement elem : tmp) {
includeModel.add(elem.model[0]);
}
parameterTypeString = ApiTool.convertInTypeScriptType(tmp, true);
if (asyncType != null) {
for (final Class<?> elem : asyncType) {
parameterModel.add(new ClassListModel(elem, previousModel));
}
} else if (asyncType == null) {
final ClassElement tmp = DataFactoryZod.createTable(parameterType, previous);
includeModel.add(tmp.model[0]);
parameterTypeString = tmp.tsTypeName;
} else if (parameterType == List.class) {
final Type parameterrizedType = parameter.getParameterizedType();
parameterModel.add(ClassModel.getModelBase(parameterType, parameterrizedType, previousModel));
} else if (parameterType == Map.class) {
final Type parameterrizedType = parameter.getParameterizedType();
parameterModel.add(ClassModel.getModelBase(parameterType, parameterrizedType, previousModel));
} else {
final List<ClassElement> tmp = DataFactoryZod.createTables(asyncType, previous);
for (final ClassElement elem : tmp) {
includeModel.add(elem.model[0]);
}
parameterTypeString = ApiTool.convertInTypeScriptType(tmp, true);
parameterModel.add(ClassModel.getModel(parameterType, previousModel));
}
final String pathParam = ApiTool.apiAnnotationGetPathParam(parameter);
final String queryParam = ApiTool.apiAnnotationGetQueryParam(parameter);
final String formDataParam = ApiTool.apiAnnotationGetFormDataParam(parameter);
if (queryParam != null) {
queryParams.put(queryParam, parameterTypeString);
queryParams.put(queryParam, parameterModel);
} else if (pathParam != null) {
pathParams.put(pathParam, parameterTypeString);
pathParams.put(pathParam, parameterModel);
} else if (formDataParam != null) {
formDataParams.put(formDataParam, parameterTypeString);
} else if (asyncType != null) {
final List<ClassElement> tmp = DataFactoryZod.createTables(asyncType, previous);
parameterTypeString = "";
for (final ClassElement elem : tmp) {
includeModel.add(elem.model[0]);
if (parameterTypeString.length() != 0) {
parameterTypeString += " | ";
}
parameterTypeString += elem.tsTypeName;
}
emptyElement.add(parameterTypeString);
} else if (parameterType == List.class) {
parameterTypeString = "any[]";
final Class<?> plop = parameterType.arrayType();
LOGGER.info("ArrayType = {}", plop);
emptyElement.add(parameterTypeString);
formDataParams.put(formDataParam, parameterModel);
} else {
final ClassElement tmp = DataFactoryZod.createTable(parameterType, previous);
includeModel.add(tmp.model[0]);
emptyElement.add(tmp.tsTypeName);
emptyElement.addAll(parameterModel);
}
}
}

View File

@ -25,7 +25,7 @@ import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
public class ApiTool {
public static String apiAnnotationGetPath(final Class<?> element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Path.class);
if (annotation.length == 0) {
@ -33,7 +33,7 @@ public class ApiTool {
}
return ((Path) annotation[0]).value();
}
public static List<String> apiAnnotationProduces(final Class<?> element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Produces.class);
if (annotation.length == 0) {
@ -41,7 +41,7 @@ public class ApiTool {
}
return Arrays.asList(((Produces) annotation[0]).value());
}
public static List<String> apiAnnotationProduces(final Method element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Produces.class);
if (annotation.length == 0) {
@ -49,7 +49,7 @@ public class ApiTool {
}
return Arrays.asList(((Produces) annotation[0]).value());
}
public static boolean apiAnnotationTypeScriptProgress(final Method element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(TypeScriptProgress.class);
if (annotation.length == 0) {
@ -74,7 +74,7 @@ public class ApiTool {
}
return parentProduce;
}
public static String apiAnnotationGetOperationDescription(final Method element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Operation.class);
if (annotation.length == 0) {
@ -82,7 +82,7 @@ public class ApiTool {
}
return ((Operation) annotation[0]).description();
}
public static String apiAnnotationGetPath(final Method element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Path.class);
if (annotation.length == 0) {
@ -128,7 +128,7 @@ public class ApiTool {
}
return null;
}
public static String apiAnnotationGetPathParam(final Parameter element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(PathParam.class);
if (annotation.length == 0) {
@ -136,7 +136,7 @@ public class ApiTool {
}
return ((PathParam) annotation[0]).value();
}
public static String apiAnnotationGetQueryParam(final Parameter element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(QueryParam.class);
if (annotation.length == 0) {
@ -144,7 +144,7 @@ public class ApiTool {
}
return ((QueryParam) annotation[0]).value();
}
public static String apiAnnotationGetFormDataParam(final Parameter element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(FormDataParam.class);
if (annotation.length == 0) {
@ -152,7 +152,7 @@ public class ApiTool {
}
return ((FormDataParam) annotation[0]).value();
}
public static Class<?>[] apiAnnotationGetAsyncType(final Parameter element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(AsyncType.class);
if (annotation.length == 0) {
@ -160,7 +160,7 @@ public class ApiTool {
}
return ((AsyncType) annotation[0]).value();
}
public static Class<?>[] apiAnnotationGetAsyncType(final Method element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(AsyncType.class);
if (annotation.length == 0) {
@ -168,7 +168,7 @@ public class ApiTool {
}
return ((AsyncType) annotation[0]).value();
}
public static List<String> apiAnnotationGetConsumes(final Method element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Consumes.class);
if (annotation.length == 0) {
@ -176,7 +176,7 @@ public class ApiTool {
}
return Arrays.asList(((Consumes) annotation[0]).value());
}
public static List<String> apiAnnotationGetConsumes(final Class<?> element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Consumes.class);
if (annotation.length == 0) {
@ -184,7 +184,7 @@ public class ApiTool {
}
return Arrays.asList(((Consumes) annotation[0]).value());
}
public static List<String> apiAnnotationGetConsumes(final Class<?> clazz, final Method method) throws Exception {
final List<String> data = apiAnnotationGetConsumes(method);
if (data != null) {
@ -192,7 +192,7 @@ public class ApiTool {
}
return apiAnnotationGetConsumes(clazz);
}
public static List<String> apiAnnotationGetConsumes2(final List<String> parentConsume, final Method method)
throws Exception {
final List<String> data = apiAnnotationGetConsumes(method);
@ -201,11 +201,11 @@ public class ApiTool {
}
return parentConsume;
}
public static boolean apiAnnotationIsContext(final Parameter element) throws Exception {
return element.getDeclaredAnnotationsByType(Context.class).length != 0;
}
public static String convertInTypeScriptType(final List<ClassElement> tmp, final boolean isList) {
String out = "";
for (final ClassElement elem : tmp) {
@ -219,7 +219,7 @@ public class ApiTool {
}
return out;
}
public static String convertInTypeScriptCheckType(final List<ClassElement> tmp) {
String out = "";
for (final ClassElement elem : tmp) {

View File

@ -5,4 +5,15 @@ public class ClassEnumModel extends ClassModel {
protected ClassEnumModel(final Class<?> clazz) {
this.originClasses.add(clazz);
}
@Override
public String toString() {
final StringBuilder out = new StringBuilder();
out.append("ClassEnumModel [");
for (final Class<?> elem : this.originClasses) {
out.append(elem.getCanonicalName());
}
out.append("]");
return out.toString();
}
}

View File

@ -10,10 +10,19 @@ public class ClassListModel extends ClassModel {
public ClassListModel(final ClassModel valueModel) {
this.valueModel = valueModel;
}
public ClassListModel(final Class<?> clazz, final ModelGroup previousModel) throws IOException {
this.valueModel = getModel(clazz, previousModel);
}
public ClassListModel(final ParameterizedType listType, final ModelGroup previousModel) throws IOException {
final Type model = listType.getActualTypeArguments()[0];
this.valueModel = getModel(model, previousModel);
}
@Override
public String toString() {
return "ClassListModel [valueModel=" + this.valueModel + "]";
}
}

View File

@ -6,7 +6,7 @@ import java.lang.reflect.ParameterizedType;
public class ClassMapModel extends ClassModel {
public ClassModel keyModel;
public ClassModel valueModel;
public ClassMapModel(final ClassModel keyModel, final ClassModel valueModel) {
this.keyModel = keyModel;
this.valueModel = valueModel;
@ -17,4 +17,9 @@ public class ClassMapModel extends ClassModel {
this.valueModel = getModel(listType.getActualTypeArguments()[1], previousModel);
}
@Override
public String toString() {
return "ClassMapModel [keyModel=" + this.keyModel + ", valueModel=" + this.valueModel + "]";
}
}

View File

@ -14,7 +14,7 @@ public abstract class ClassModel {
return this.originClasses.contains(clazz);
}
public ClassModel getModel(final Type type, final ModelGroup previousModel) throws IOException {
public static ClassModel getModel(final Type type, final ModelGroup previousModel) throws IOException {
if (type == List.class) {
if (type instanceof final ParameterizedType parameterizedType) {
return new ClassListModel(parameterizedType, previousModel);
@ -31,5 +31,28 @@ public abstract class ClassModel {
}
return previousModel.add((Class<?>) type);
}
public static ClassModel getModelBase(
final Class<?> clazz,
final Type parameterizedType,
final ModelGroup previousModel) throws IOException {
if (clazz == List.class) {
return new ClassListModel((ParameterizedType) parameterizedType, previousModel);
}
if (clazz == Map.class) {
return new ClassMapModel((ParameterizedType) parameterizedType, previousModel);
}
return previousModel.add(clazz);
}
public static ClassModel getModel(final Class<?> type, final ModelGroup previousModel) throws IOException {
if (type == List.class) {
throw new IOException("Fail to manage parametrized type...");
}
if (type == Map.class) {
throw new IOException("Fail to manage parametrized type...");
}
return previousModel.add(type);
}
}

View File

@ -5,4 +5,15 @@ public class ClassObjectModel extends ClassModel {
public ClassObjectModel(final Class<?> clazz) {
this.originClasses.add(clazz);
}
@Override
public String toString() {
final StringBuilder out = new StringBuilder();
out.append("ClassObjectModel [");
for (final Class<?> elem : this.originClasses) {
out.append(elem.getCanonicalName());
}
out.append("]");
return out.toString();
}
}

View File

@ -3,26 +3,37 @@ package org.kar.archidata.externalRestApi.model;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ModelGroup {
static final Logger LOGGER = LoggerFactory.getLogger(ModelGroup.class);
public List<ClassModel> previousModel = new ArrayList<>();
public ModelGroup() {}
public ModelGroup(final List<ClassModel> models) {
this.previousModel = models;
}
public ClassModel add(final Class<?> clazz) {
//LOGGER.trace("Search element {}", clazz.getCanonicalName());
for (final ClassModel value : this.previousModel) {
if (value.isCompatible(clazz)) {
//LOGGER.trace(" ==> return {}", value);
return value;
}
}
if (clazz.isEnum()) {
final ClassModel elem = new ClassEnumModel(clazz);
this.previousModel.add(elem);
//LOGGER.trace(" ==> return enum {}", elem);
return elem;
}
// create new model:
return null;
final ClassModel elem = new ClassObjectModel(clazz);
this.previousModel.add(elem);
//LOGGER.trace(" ==> return object {}", elem);
return elem;
}
}

View File

@ -118,4 +118,4 @@ public class TestRawQuery {
}
}
}
}

View File

@ -468,4 +468,4 @@ public class TestTypes {
DataAccess.delete(TypesTable.class, insertedData.id);
}
}
}