[DEV] update log model

This commit is contained in:
Edouard DUPIN 2022-04-11 00:49:22 +02:00
parent 6ced097f47
commit f0738ed05c
6 changed files with 353 additions and 341 deletions

View File

@ -10,14 +10,18 @@ public class ActionList {
private final Map<String, Class<?>> subActions = new HashMap<>();
public void add(final Class<?> clazz) {
String name = PropertiesInterface.getCommand(clazz);
final String name = PropertiesInterface.getCommand(clazz);
if (name == null || name.isEmpty()) {
Log.error("Can not add an action without name. for: " + clazz.getCanonicalName());
Log.error("Can not add an action without name. for: {}", clazz.getCanonicalName());
return;
}
this.subActions.put(name, clazz);
}
public boolean exist(final String value) {
return this.subActions.get(value) != null;
}
public Set<String> getActions() {
return this.subActions.keySet();
}
@ -26,8 +30,4 @@ public class ActionList {
return this.subActions.get(action);
}
public boolean exist(final String value) {
return this.subActions.get(value) != null;
}
}

View File

@ -38,75 +38,75 @@ public class ArgumentManager {
private void configure(final Object tmp) {
boolean endParsing = false;
for (int iii = 0; iii < this.arguments.size(); iii++) {
String value = this.arguments.get(iii);
final String value = this.arguments.get(iii);
if (endParsing) {
this.argumentResidual.add(value);
continue;
}
if (value.startsWith("--")) {
String[] elements = value.split("=");
final String[] elements = value.split("=");
elements[0] = elements[0].substring(2);
Log.verbose("find element (--) : " + Arrays.toString(elements));
String parameter = elements[0];
Log.verbose("find element (--) : {}", Arrays.toString(elements));
final String parameter = elements[0];
String newValue = null;
if (!this.properties.existParameter(parameter)) {
Log.critical("Parameter Does not exist for '" + value + "'");
Log.critical("Parameter Does not exist for '{}'", value);
}
if (!this.properties.haveParameter(parameter)) {
// Boolean value
if (elements.length != 1) {
Log.critical("Boolean parameter can not have parameters for '" + value + "'");
Log.critical("Boolean parameter can not have parameters for '{}", value);
}
} else if (elements.length == 1) {
if (iii == this.arguments.size() - 1) {
Log.critical("Missing parameters for '" + value + "' (no more arguments)");
Log.critical("Missing parameters for '{}' (no more arguments)", value);
}
newValue = this.arguments.get(iii + 1);
if (newValue.startsWith("-")) {
Log.critical("Missing parameters for '" + value + "' (next argument is a parameter (start with '-'))");
Log.critical("Missing parameters for '{}' (next argument is a parameter (start with '-'))", value);
}
iii++;
} else if (elements.length == 2) {
newValue = elements[1];
} else {
Log.critical("too much parameters for '" + value + "' (only 1 = is authorized)");
Log.critical("too much parameters for '{}' (only 1 = is authorized)", value);
}
this.properties.setParameter(tmp, parameter, newValue);
} else if (value.startsWith("-")) {
String[] elements = value.split("=");
final String[] elements = value.split("=");
elements[0] = elements[0].substring(1);
Log.warning("find element (-) : " + Arrays.toString(elements));
Log.warning("find element (-) : {}", Arrays.toString(elements));
if (elements[0].length() != 1) {
Log.critical("Can not parse alias argument with a size != 1 for '" + value + "'");
Log.critical("Can not parse alias argument with a size != 1 for '{}'", value);
}
char parameter = elements[0].charAt(0);
final char parameter = elements[0].charAt(0);
String newValue = null;
if (!this.properties.existParameterAlias(parameter)) {
Log.critical("Parameter Does not exist for '" + value + "'");
Log.critical("Parameter Does not exist for '{}'", value);
}
if (!this.properties.haveParameterAlias(parameter)) {
// Boolean value
if (elements.length != 1) {
Log.critical("Boolean parameter can not have parameters for '" + value + "'");
Log.critical("Boolean parameter can not have parameters for '{}'", value);
}
} else if (elements.length == 1) {
if (iii == this.arguments.size() - 1) {
Log.critical("Missing parameters for '" + value + "' (no more arguments)");
Log.critical("Missing parameters for '{}' (no more arguments)", value);
}
newValue = this.arguments.get(iii + 1);
if (newValue.startsWith("-")) {
Log.critical("Missing parameters for '" + value + "' (next argument is a parameter (start with '-'))");
Log.critical("Missing parameters for '{}' (next argument is a parameter (start with '-'))", value);
}
iii++;
} else if (elements.length == 2) {
newValue = elements[1];
} else {
Log.critical("too much parameters for '" + value + "' (only 1 = is authorized)");
Log.critical("too much parameters for '{}' (only 1 = is authorized)", value);
}
this.properties.setParameterAlias(tmp, parameter, newValue);
} else {
Log.verbose("find element ( ) : " + value);
Log.verbose("find element ( ) : {}", value);
if (this.properties.actions != null && this.properties.actions.exist(value)) {
// Find end of parsing ...
endParsing = true;
@ -123,7 +123,7 @@ public class ArgumentManager {
if (this.actionDetected == null) {
return;
}
Class<?> actionClass = this.properties.getAction(this.actionDetected);
final Class<?> actionClass = this.properties.getAction(this.actionDetected);
if (actionClass == null) {
return;
}
@ -135,17 +135,17 @@ public class ArgumentManager {
e.printStackTrace();
return;
}
ArgumentManager localManager = new ArgumentManager(this.argumentResidual.toArray(new String[0]), obj);
final ArgumentManager localManager = new ArgumentManager(this.argumentResidual.toArray(new String[0]), obj);
//localManager.showHelp();
int nbParameters = localManager.argumentPostConfigure.size();
List<Method> executor = localManager.properties.getExecutor();
for (Method exec : executor) {
final int nbParameters = localManager.argumentPostConfigure.size();
final List<Method> executor = localManager.properties.getExecutor();
for (final Method exec : executor) {
if (nbParameters != exec.getParameterCount()) {
Log.warning("Rejected executor, " + exec.getName() + " nbParameter=" + exec.getParameterCount() + " require=" + nbParameters);
Log.warning("Rejected executor, {} nbParameter={} require={}", exec.getName(), exec.getParameterCount(), nbParameters);
continue;
}
Log.verbose("find executor, " + exec.getName());
Log.verbose("find executor, {}", exec.getName());
try {
switch (nbParameters) {
case 0:

View File

@ -9,11 +9,16 @@ import org.atriasoft.death.internal.Log;
public class FieldProperty {
public record ChoiceElement(
String origin,
String destination) {}
public Field field;
public String name;
public Character alias;
public String description;
public String[] choice;
public Class<?>[] types;
public FieldProperty(final Field elem) {
@ -27,12 +32,12 @@ public class FieldProperty {
this.field = null;
}
this.field = elem;
} catch (Exception e) {
} catch (final Exception e) {
this.name = null;
this.alias = null;
this.description = null;
this.choice = null;
Class<?>[] types = null;
final Class<?>[] types = null;
if (this.name == null && this.alias == null) {
this.field = null;
}
@ -40,48 +45,13 @@ public class FieldProperty {
e.printStackTrace();
}
}
public boolean isValid() {
return this.field != null;
}
public void print() {
if (this.name != null) {
Log.print("Detect element: --" + this.name);
}
if (this.alias != null) {
Log.print(" alias: -" + this.alias);
}
if (this.types[1] == null) {
Log.print(" type: " + this.types[0].getCanonicalName());
} else {
Log.print(" type: " + this.types[0].getCanonicalName() + " / " + this.types[1].getCanonicalName());
}
Log.print(" description: " + this.description);
if (this.choice != null) {
Log.print(" choice: " + Arrays.toString(this.choice));
}
}
public record ChoiceElement(String origin, String destination) {}
List<ChoiceElement> getChoiceList() {
List<ChoiceElement> out = new ArrayList<>();
for (String value : this.choice) {
String[] split = value.split(">>");
if (split.length == 1) {
out.add(new ChoiceElement(split[0], null));
} else {
out.add(new ChoiceElement(split[0], split[1]));
}
}
return out;
}
public String convertChoice(final String newValue) {
if (this.choice == null) {
return newValue;
}
List<ChoiceElement> available = getChoiceList();
for (ChoiceElement elem : available) {
final List<ChoiceElement> available = getChoiceList();
for (final ChoiceElement elem : available) {
if (elem.destination == null) {
if (elem.origin.contentEquals(newValue)) {
return newValue;
@ -96,7 +66,7 @@ public class FieldProperty {
}
}
String list = null;
for (ChoiceElement elem : available) {
for (final ChoiceElement elem : available) {
if (list == null) {
list = "[";
} else {
@ -105,7 +75,42 @@ public class FieldProperty {
list += elem;
}
list += "]";
Log.critical("Can not find property '" + newValue + "'in the choice values : " + list);
Log.critical("Can not find property '{}'in the choice values : {}", newValue, list);
return null;
}
List<ChoiceElement> getChoiceList() {
final List<ChoiceElement> out = new ArrayList<>();
for (final String value : this.choice) {
final String[] split = value.split(">>");
if (split.length == 1) {
out.add(new ChoiceElement(split[0], null));
} else {
out.add(new ChoiceElement(split[0], split[1]));
}
}
return out;
}
public boolean isValid() {
return this.field != null;
}
public void print() {
if (this.name != null) {
Log.print("Detect element: --{}", this.name);
}
if (this.alias != null) {
Log.print(" alias: -{}", this.alias);
}
if (this.types[1] == null) {
Log.print(" type: {}", this.types[0].getCanonicalName());
} else {
Log.print(" type: {} / {}", this.types[0].getCanonicalName(), this.types[1].getCanonicalName());
}
Log.print(" description: {}", this.description);
if (this.choice != null) {
Log.print(" choice: {}", Arrays.toString(this.choice));
}
}
}

View File

@ -11,45 +11,34 @@ import org.atriasoft.death.FieldProperty.ChoiceElement;
import org.atriasoft.death.internal.Log;
public class PropertiesInterface {
final List<FieldProperty> properties = new ArrayList<>();
ActionList actions = null;
final String classDescription;
final String classCommand;
final List<Method> executes = new ArrayList<>();
public List<Method> getExecutor() {
return this.executes;
}
public Class<?> getAction(final String action) {
if (this.actions == null) {
return null;
}
return this.actions.getInterface(action);
}
public List<FieldProperty> parseFields() {
return this.properties;
}
public static String getCommand(final Class<?> clazz) {
try {
return ReflectTools.getCommand(clazz);
} catch (Exception ex) {
} catch (final Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
return null;
}
public static String getDescription(final Class<?> clazz) {
try {
return ReflectTools.getDescription(clazz);
} catch (Exception ex) {
} catch (final Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
return null;
}
final List<FieldProperty> properties = new ArrayList<>();
ActionList actions = null;
final String classDescription;
final String classCommand;
final List<Method> executes = new ArrayList<>();
public PropertiesInterface(final Class<?> clazz) {
// ------------------------------------------------------------------------
// -- Find class description
@ -60,25 +49,25 @@ public class PropertiesInterface {
// -- Find property Field
// ------------------------------------------------------------------------
final Field[] fields = clazz.getFields();
Log.verbose(" Fields: (" + fields.length + ")");
Log.verbose(" Fields: ({})", fields.length);
for (final Field elem : fields) {
// we does not manage static field
if (Modifier.isStatic(elem.getModifiers())) {
try {
boolean subAction = ReflectTools.isSubAction(elem);
final boolean subAction = ReflectTools.isSubAction(elem);
if (!subAction) {
continue;
}
Class<?>[] type = ReflectTools.getTypeField(elem);
final Class<?>[] type = ReflectTools.getTypeField(elem);
if (type[0] == ActionList.class) {
Object data = elem.get(null);
final Object data = elem.get(null);
if (data == null) {
Log.error("set a @ArgSubActions on a wrong field");
} else if (data instanceof ActionList tmp) {
} else if (data instanceof final ActionList tmp) {
this.actions = tmp;
}
}
} catch (Exception e) {
} catch (final Exception e) {
Log.error("get error on @ArgSubActions !!!");
e.printStackTrace();
}
@ -93,19 +82,9 @@ public class PropertiesInterface {
if (!element.isValid()) {
continue;
}
if (element.types[0] == boolean.class
|| element.types[0] == Boolean.class
|| element.types[0] == int.class
|| element.types[0] == Integer.class
|| element.types[0] == short.class
|| element.types[0] == Short.class
|| element.types[0] == long.class
|| element.types[0] == Long.class
|| element.types[0] == float.class
|| element.types[0] == Float.class
|| element.types[0] == double.class
|| element.types[0] == Double.class
|| element.types[0] == String.class) {
if (element.types[0] == boolean.class || element.types[0] == Boolean.class || element.types[0] == int.class || element.types[0] == Integer.class || element.types[0] == short.class
|| element.types[0] == Short.class || element.types[0] == long.class || element.types[0] == Long.class || element.types[0] == float.class || element.types[0] == Float.class
|| element.types[0] == double.class || element.types[0] == Double.class || element.types[0] == String.class) {
// all is good
} else {
Log.error("Can not manage other type than: [boolean,Boolean,String,int,Integer,long,Long,short,Short,float,Float,double,Double]");
@ -118,7 +97,7 @@ public class PropertiesInterface {
// ------------------------------------------------------------------------
final Method[] methods = clazz.getMethods();
for (final Method elem : methods) {
Log.verbose("Detect function : " + elem.getName());
Log.verbose("Detect function : {}", elem.getName());
// we does not manage static field
if (Modifier.isStatic(elem.getModifiers())) {
continue;
@ -132,28 +111,106 @@ public class PropertiesInterface {
if (ReflectTools.isExecutable(elem)) {
this.executes.add(elem);
}
} catch (Exception e) {
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void displayHelp() {
Log.print("Usage:");
final StringBuilder applicationLine = new StringBuilder(" ");
if (this.classCommand != null) {
applicationLine.append(this.classCommand).append(" ");
} else {
applicationLine.append("XXX ");
}
if (this.properties.size() != 0) {
applicationLine.append("[options] ");
}
if (this.actions != null && this.actions.getActions().size() != 0) {
applicationLine.append("[actions] ...");
}
Log.print(applicationLine.toString());
if (this.classDescription != null) {
Log.print("{}", this.classDescription);
}
if (this.properties.size() != 0) {
Log.print(" [options]");
for (final FieldProperty prop : this.properties) {
final String parameterExpect = haveParameter(prop) ? " [PARAMETER]" : "";
if (prop.alias != null && prop.name != null) {
Log.print(" -{} / --{}{}", prop.alias, prop.name, parameterExpect);
} else if (prop.alias != null) {
Log.print(" -{}{}", prop.alias, parameterExpect);
} else if (prop.name != null) {
Log.print(" --{}{}", prop.name, parameterExpect);
}
if (prop.description != null) {
Log.print(" {}", prop.description);
}
if (prop.choice != null) {
for (final ChoiceElement elem : prop.getChoiceList()) {
if (elem.destination() == null) {
Log.print(" - {}", elem.origin());
} else {
Log.print(" - {} ==> {}", elem.origin(), elem.destination());
}
}
}
}
}
if (this.actions != null && this.actions.getActions().size() != 0) {
Log.print(" [actions]");
final Set<String> actions = this.actions.getActions();
for (final String action : actions) {
final Class<?> clazz = this.actions.getInterface(action);
final String description = PropertiesInterface.getDescription(clazz);
if (description == null || description.isEmpty()) {
Log.print(" {}", action);
} else {
Log.print(" {}: {}", action, description);
}
}
}
}
public boolean existParameter(final String parameterName) {
return findField(parameterName) != null;
}
public boolean existParameterAlias(final char parameterName) {
return findFieldAlias(parameterName) != null;
}
public FieldProperty findField(final String parameterName) {
for (final FieldProperty elem : this.properties) {
if (elem.name != null && elem.name.contentEquals(parameterName)) {
return elem;
}
}
return null;
}
public FieldProperty findFieldAlias(final char parameterName) {
for (FieldProperty elem : this.properties) {
for (final FieldProperty elem : this.properties) {
if (elem.alias != null && elem.alias == parameterName) {
return elem;
}
}
return null;
}
public FieldProperty findField(final String parameterName) {
for (FieldProperty elem : this.properties) {
if (elem.name != null && elem.name.contentEquals(parameterName)) {
return elem;
}
public Class<?> getAction(final String action) {
if (this.actions == null) {
return null;
}
return null;
return this.actions.getInterface(action);
}
public List<Method> getExecutor() {
return this.executes;
}
public boolean haveParameter(final FieldProperty field) {
@ -162,43 +219,43 @@ public class PropertiesInterface {
}
return true;
}
public boolean haveParameter(final String parameterName) {
FieldProperty field = findField(parameterName);
final FieldProperty field = findField(parameterName);
return haveParameter(field);
}
public boolean haveParameterAlias(final char parameterName) {
FieldProperty field = findFieldAlias(parameterName);
final FieldProperty field = findFieldAlias(parameterName);
return haveParameter(field);
}
public boolean existParameter(final String parameterName) {
return findField(parameterName) != null;
}
public boolean existParameterAlias(final char parameterName) {
return findFieldAlias(parameterName) != null;
public List<FieldProperty> parseFields() {
return this.properties;
}
public void setParameter(final Object tmp, final String parameter, String newValue) {
try {
FieldProperty property = findField(parameter);
final FieldProperty property = findField(parameter);
newValue = property.convertChoice(newValue);
if (property.types[0] == boolean.class || property.types[0] == Boolean.class) {
//Boolean value = Boolean.valueOf(newValue);
//property.field.setBoolean(tmp, value);
property.field.setBoolean(tmp, true);
} else if (property.types[0] == int.class || property.types[0] == Integer.class) {
Integer value = Integer.valueOf(newValue);
final Integer value = Integer.valueOf(newValue);
property.field.setInt(tmp, value);
} else if (property.types[0] == short.class || property.types[0] == Short.class) {
Short value = Short.valueOf(newValue);
final Short value = Short.valueOf(newValue);
property.field.setShort(tmp, value);
} else if (property.types[0] == long.class || property.types[0] == Long.class) {
Long value = Long.valueOf(newValue);
final Long value = Long.valueOf(newValue);
property.field.setLong(tmp, value);
} else if (property.types[0] == float.class || property.types[0] == Float.class) {
Float value = Float.valueOf(newValue);
final Float value = Float.valueOf(newValue);
property.field.setFloat(tmp, value);
} else if (property.types[0] == double.class || property.types[0] == Double.class) {
Double value = Double.valueOf(newValue);
final Double value = Double.valueOf(newValue);
property.field.setDouble(tmp, value);
} else if (property.types[0] == String.class) {
property.field.set(tmp, newValue);
@ -208,70 +265,13 @@ public class PropertiesInterface {
}
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
Log.critical("Can not SET the value : " + parameter + " : " + newValue);
Log.critical("Can not SET the value : {}: {}", parameter, newValue);
}
}
public void setParameterAlias(final Object tmp, final char parameter, final String newValue) {
FieldProperty property = findFieldAlias(parameter);
final FieldProperty property = findFieldAlias(parameter);
setParameter(tmp, property.name, newValue);
}
public void displayHelp() {
Log.print("Usage:");
String applicationLine = " ";
if (this.classCommand != null) {
applicationLine += this.classCommand + " ";
} else {
applicationLine += "XXX ";
}
if (this.properties.size() != 0) {
applicationLine += "[options] ";
}
if (this.actions != null && this.actions.getActions().size() != 0) {
applicationLine += "[actions] ...";
}
Log.print(applicationLine);
if (this.classDescription != null) {
Log.print("" + this.classDescription);
}
if (this.properties.size() != 0) {
Log.print(" [options]");
for (FieldProperty prop : this.properties) {
String parameterExpect = haveParameter(prop) ? " [PARAMETER]" : "";
if (prop.alias != null && prop.name != null) {
Log.print(" -" + prop.alias + " / --" + prop.name + parameterExpect);
} else if (prop.alias != null) {
Log.print(" -" + prop.alias + parameterExpect);
} else if (prop.name != null) {
Log.print(" --" + prop.name + parameterExpect);
}
if (prop.description != null) {
Log.print(" " + prop.description);
}
if (prop.choice != null) {
for (ChoiceElement elem: prop.getChoiceList()) {
if (elem.destination() == null) {
Log.print(" - " + elem.origin());
} else {
Log.print(" - " + elem.origin() + " ==> " + elem.destination());
}
}
}
}
}
if (this.actions != null && this.actions.getActions().size() != 0) {
Log.print(" [actions]");
Set<String> actions = this.actions.getActions();
for (String action : actions) {
Class<?> clazz = this.actions.getInterface(action);
String description = PropertiesInterface.getDescription(clazz);
if (description == null || description.isEmpty()) {
Log.print(" " + action);
} else {
Log.print(" " + action + ": " + description );
}
}
}
}
}

View File

@ -20,88 +20,6 @@ import org.atriasoft.death.annotation.ArgSubActions;
import org.atriasoft.death.internal.Log;
public class ReflectTools {
private ReflectTools() {}
public static Class<?>[] getTypeField(final Field fieldDescription) {
Class<?> type = fieldDescription.getType();
Class<?> subType = null;
Type empppe = fieldDescription.getGenericType();
if (empppe instanceof ParameterizedType plopppppp) {
Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
try {
subType = Class.forName(realType[0].getTypeName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return new Class<?>[] {type, subType};
}
public static Class<?>[] getTypeReturnFunction(/*@NotNull*/ final Method getter) throws Exception {
Class<?> type = null;
Class<?> subType = null;
type = getter.getReturnType();
if (!Enum.class.isAssignableFrom(type)) {
Type empppe = getter.getGenericReturnType();
if (empppe instanceof ParameterizedType plopppppp) {
Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
subType = Class.forName(realType[0].getTypeName());
}
}
}
return new Class<?>[] {type, subType};
}
public static Class<?>[] getTypeParameterfunction(final Method setter) throws Exception {
Class<?> type = null;
Class<?> subType = null;
type = setter.getParameters()[0].getType();
if (List.class.isAssignableFrom(type)) {
Class<?> internalModelClass = null;
Type[] empppe = setter.getGenericParameterTypes();
if (empppe.length > 0) {
if (empppe[0] instanceof ParameterizedType plopppppp) {
Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
Log.warning(" -->> " + realType[0]);
internalModelClass = Class.forName(realType[0].getTypeName());
}
}
}
subType = internalModelClass;
}
return new Class<?>[] {type, subType};
}
public static Class<?>[] getTypeParameterfunction(final Constructor<?> elem, final int paramId) throws ClassNotFoundException {
Class<?> type = null;
Class<?> subType = null;
type = elem.getParameters()[paramId].getType();
if (List.class.isAssignableFrom(type)) {
Class<?> internalModelClass = null;
Type[] empppe = elem.getGenericParameterTypes();
if (empppe.length > paramId) {
if (empppe[paramId] instanceof ParameterizedType plopppppp) {
Type[] realType = plopppppp.getActualTypeArguments();
//Log.info("ppplllppp: " + realType.length);
if (realType.length > 0) {
Log.warning(" -->> " + realType[0]);
internalModelClass = Class.forName(realType[0].getTypeName());
}
}
}
subType = internalModelClass;
}
return new Class<?>[] {type, subType};
}
public static Character getAlias(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgAlias.class);
if (annotation.length == 0) {
@ -123,36 +41,7 @@ public class ReflectTools {
}
return ((ArgChoice) annotation[0]).value();
}
public static String getDescription(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgDescription.class);
if (annotation.length == 0) {
return null;
}
if (annotation.length > 1) {
throw new Exception("Must not have more than 1 element @ArgDescription on " + element.getClass().getCanonicalName());
}
return ((ArgDescription) annotation[0]).value();
}
public static String getDescription(final Class<?> clazz) throws Exception {
final Annotation[] annotation = clazz.getDeclaredAnnotationsByType(ArgDescription.class);
if (annotation.length == 0) {
return null;
}
if (annotation.length > 1) {
throw new Exception("Must not have more than 1 element @ArgDescription on " + clazz.getCanonicalName());
}
return ((ArgDescription) annotation[0]).value();
}
public static String getName(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgName.class);
if (annotation.length == 0) {
return null;
}
if (annotation.length > 1) {
throw new Exception("Must not have more than 1 element @ArgName on " + element.getClass().getCanonicalName());
}
return ((ArgName) annotation[0]).value();
}
public static String getCommand(final Class<?> clazz) throws Exception {
final Annotation[] annotation = clazz.getDeclaredAnnotationsByType(ArgCommand.class);
if (annotation.length == 0) {
@ -163,6 +52,40 @@ public class ReflectTools {
}
return ((ArgCommand) annotation[0]).value();
}
public static String getDescription(final Class<?> clazz) throws Exception {
final Annotation[] annotation = clazz.getDeclaredAnnotationsByType(ArgDescription.class);
if (annotation.length == 0) {
return null;
}
if (annotation.length > 1) {
throw new Exception("Must not have more than 1 element @ArgDescription on " + clazz.getCanonicalName());
}
return ((ArgDescription) annotation[0]).value();
}
public static String getDescription(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgDescription.class);
if (annotation.length == 0) {
return null;
}
if (annotation.length > 1) {
throw new Exception("Must not have more than 1 element @ArgDescription on " + element.getClass().getCanonicalName());
}
return ((ArgDescription) annotation[0]).value();
}
public static String getName(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgName.class);
if (annotation.length == 0) {
return null;
}
if (annotation.length > 1) {
throw new Exception("Must not have more than 1 element @ArgName on " + element.getClass().getCanonicalName());
}
return ((ArgName) annotation[0]).value();
}
public static String[] getParams(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgParams.class);
if (annotation.length == 0) {
@ -173,6 +96,7 @@ public class ReflectTools {
}
return ((ArgParams) annotation[0]).value();
}
public static String[] getSample(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgSample.class);
if (annotation.length == 0) {
@ -183,13 +107,86 @@ public class ReflectTools {
}
return ((ArgSample) annotation[0]).value();
}
public static boolean isSubAction(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgSubActions.class);
if (annotation.length == 0) {
return false;
public static Class<?>[] getTypeField(final Field fieldDescription) {
final Class<?> type = fieldDescription.getType();
Class<?> subType = null;
final Type empppe = fieldDescription.getGenericType();
if (empppe instanceof final ParameterizedType plopppppp) {
final Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
try {
subType = Class.forName(realType[0].getTypeName());
} catch (final ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return true;
return new Class<?>[] { type, subType };
}
public static Class<?>[] getTypeParameterfunction(final Constructor<?> elem, final int paramId) throws ClassNotFoundException {
Class<?> type = null;
Class<?> subType = null;
type = elem.getParameters()[paramId].getType();
if (List.class.isAssignableFrom(type)) {
Class<?> internalModelClass = null;
final Type[] empppe = elem.getGenericParameterTypes();
if (empppe.length > paramId) {
if (empppe[paramId] instanceof final ParameterizedType plopppppp) {
final Type[] realType = plopppppp.getActualTypeArguments();
//Log.info("ppplllppp: {}", realType.length);
if (realType.length > 0) {
Log.warning(" -->> {}", realType[0]);
internalModelClass = Class.forName(realType[0].getTypeName());
}
}
}
subType = internalModelClass;
}
return new Class<?>[] { type, subType };
}
public static Class<?>[] getTypeParameterfunction(final Method setter) throws Exception {
Class<?> type = null;
Class<?> subType = null;
type = setter.getParameters()[0].getType();
if (List.class.isAssignableFrom(type)) {
Class<?> internalModelClass = null;
final Type[] empppe = setter.getGenericParameterTypes();
if (empppe.length > 0) {
if (empppe[0] instanceof final ParameterizedType plopppppp) {
final Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
Log.warning(" -->> {}", realType[0]);
internalModelClass = Class.forName(realType[0].getTypeName());
}
}
}
subType = internalModelClass;
}
return new Class<?>[] { type, subType };
}
public static Class<?>[] getTypeReturnFunction(/*@NotNull*/ final Method getter) throws Exception {
Class<?> type = null;
Class<?> subType = null;
type = getter.getReturnType();
if (!Enum.class.isAssignableFrom(type)) {
final Type empppe = getter.getGenericReturnType();
if (empppe instanceof final ParameterizedType plopppppp) {
final Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
subType = Class.forName(realType[0].getTypeName());
}
}
}
return new Class<?>[] { type, subType };
}
public static boolean isExecutable(final Method element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgExecute.class);
if (annotation.length == 0) {
@ -197,4 +194,14 @@ public class ReflectTools {
}
return true;
}
public static boolean isSubAction(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ArgSubActions.class);
if (annotation.length == 0) {
return false;
}
return true;
}
private ReflectTools() {}
}

View File

@ -15,12 +15,6 @@ public class Log {
private static final boolean PRINT_TODO = Logger.getNeedPrint(LIB_NAME, LogLevel.TODO);
private static final boolean PRINT_PRINT = Logger.getNeedPrint(LIB_NAME, LogLevel.PRINT);
public static void critical(final String data) {
if (PRINT_CRITICAL) {
Logger.critical(LIB_NAME_DRAW, data);
}
}
public static void critical(final String data, final Exception e) {
e.printStackTrace();
if (PRINT_CRITICAL) {
@ -28,45 +22,51 @@ public class Log {
}
}
public static void debug(final String data) {
public static void critical(final String data, final Object... objects) {
if (PRINT_CRITICAL) {
Logger.critical(LIB_NAME_DRAW, data, objects);
}
}
public static void debug(final String data, final Object... objects) {
if (PRINT_DEBUG) {
Logger.debug(LIB_NAME_DRAW, data);
Logger.debug(LIB_NAME_DRAW, data, objects);
}
}
public static void error(final String data) {
public static void error(final String data, final Object... objects) {
if (PRINT_ERROR) {
Logger.error(LIB_NAME_DRAW, data);
Logger.error(LIB_NAME_DRAW, data, objects);
}
}
public static void info(final String data) {
public static void info(final String data, final Object... objects) {
if (PRINT_INFO) {
Logger.info(LIB_NAME_DRAW, data);
Logger.info(LIB_NAME_DRAW, data, objects);
}
}
public static void print(final String data) {
public static void print(final String data, final Object... objects) {
if (PRINT_PRINT) {
System.out.println(data);
Logger.print(LIB_NAME_DRAW, data, objects);
}
}
public static void todo(final String data) {
public static void todo(final String data, final Object... objects) {
if (PRINT_TODO) {
Logger.todo(LIB_NAME_DRAW, data);
Logger.todo(LIB_NAME_DRAW, data, objects);
}
}
public static void verbose(final String data) {
public static void verbose(final String data, final Object... objects) {
if (PRINT_VERBOSE) {
Logger.verbose(LIB_NAME_DRAW, data);
Logger.verbose(LIB_NAME_DRAW, data, objects);
}
}
public static void warning(final String data) {
public static void warning(final String data, final Object... objects) {
if (PRINT_WARNING) {
Logger.warning(LIB_NAME_DRAW, data);
Logger.warning(LIB_NAME_DRAW, data, objects);
}
}