[FEAT] continue refacto of the generation code

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,4 +5,15 @@ public class ClassEnumModel extends ClassModel {
protected ClassEnumModel(final Class<?> clazz) { protected ClassEnumModel(final Class<?> clazz) {
this.originClasses.add(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) { public ClassListModel(final ClassModel valueModel) {
this.valueModel = 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 { public ClassListModel(final ParameterizedType listType, final ModelGroup previousModel) throws IOException {
final Type model = listType.getActualTypeArguments()[0]; final Type model = listType.getActualTypeArguments()[0];
this.valueModel = getModel(model, previousModel); 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 class ClassMapModel extends ClassModel {
public ClassModel keyModel; public ClassModel keyModel;
public ClassModel valueModel; public ClassModel valueModel;
public ClassMapModel(final ClassModel keyModel, final ClassModel valueModel) { public ClassMapModel(final ClassModel keyModel, final ClassModel valueModel) {
this.keyModel = keyModel; this.keyModel = keyModel;
this.valueModel = valueModel; this.valueModel = valueModel;
@ -17,4 +17,9 @@ public class ClassMapModel extends ClassModel {
this.valueModel = getModel(listType.getActualTypeArguments()[1], previousModel); 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); 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 == List.class) {
if (type instanceof final ParameterizedType parameterizedType) { if (type instanceof final ParameterizedType parameterizedType) {
return new ClassListModel(parameterizedType, previousModel); return new ClassListModel(parameterizedType, previousModel);
@ -31,5 +31,28 @@ public abstract class ClassModel {
} }
return previousModel.add((Class<?>) type); 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) { public ClassObjectModel(final Class<?> clazz) {
this.originClasses.add(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.ArrayList;
import java.util.List; import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ModelGroup { public class ModelGroup {
static final Logger LOGGER = LoggerFactory.getLogger(ModelGroup.class);
public List<ClassModel> previousModel = new ArrayList<>(); public List<ClassModel> previousModel = new ArrayList<>();
public ModelGroup() {} public ModelGroup() {}
public ModelGroup(final List<ClassModel> models) { public ModelGroup(final List<ClassModel> models) {
this.previousModel = models; this.previousModel = models;
} }
public ClassModel add(final Class<?> clazz) { public ClassModel add(final Class<?> clazz) {
//LOGGER.trace("Search element {}", clazz.getCanonicalName());
for (final ClassModel value : this.previousModel) { for (final ClassModel value : this.previousModel) {
if (value.isCompatible(clazz)) { if (value.isCompatible(clazz)) {
//LOGGER.trace(" ==> return {}", value);
return value; return value;
} }
} }
if (clazz.isEnum()) { if (clazz.isEnum()) {
final ClassModel elem = new ClassEnumModel(clazz); final ClassModel elem = new ClassEnumModel(clazz);
this.previousModel.add(elem);
//LOGGER.trace(" ==> return enum {}", elem);
return elem;
} }
// create new model: // create new model:
final ClassModel elem = new ClassObjectModel(clazz);
return null; 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); DataAccess.delete(TypesTable.class, insertedData.id);
} }
} }