de.tsl2.nano.bean.annotation.ConstraintValueSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.descriptor Show documentation
Show all versions of tsl2.nano.descriptor Show documentation
TSL2 Framework Descriptor (currency-handling, generic formatter, descriptors for beans, collections, actions and values)
/*
* File: $HeadURL$
* Id : $Id$
*
* created by: Tom
* created on: 31.03.2017
*
* Copyright: (c) Thomas Schneider 2017, all rights reserved
*/
package de.tsl2.nano.bean.annotation;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import de.tsl2.nano.action.IAction;
import de.tsl2.nano.bean.def.AttributeFinder;
import de.tsl2.nano.bean.def.Constraint;
import de.tsl2.nano.bean.def.MethodAction;
import de.tsl2.nano.collection.CollectionUtil;
import de.tsl2.nano.core.ENV;
import de.tsl2.nano.core.ITransformer;
import de.tsl2.nano.core.cls.ClassFinder;
import de.tsl2.nano.core.util.FileUtil;
import de.tsl2.nano.core.util.StringUtil;
import de.tsl2.nano.core.util.Util;
/**
* provides some value sets as allowed values for a Constraint Annotation
*
* @author Tom
* @version $Revision$
*/
public class ConstraintValueSet {
public static final String NULL_CLASS = Void.class.getName();
// pre-defined allowed value sets (providing a dynamic postfix after ':')
/** provides all classes of current classloader */
public static final String ALLOWED_CLASSES = "ALLOWED_CLASSES:";
/** provides all methods of current classloader */
public static final String ALLOWED_METHODS = "ALLOWED_METHODS:";
/** provides all fields of current classloader */
public static final String ALLOWED_FIELDS = "ALLOWED_FIELDS:";
/** provides all generated application classes */
public static final String ALLOWED_APPCLASSES = "ALLOWED_APPCLASSES";
/** provides all loaded beans */
public static final String ALLOWED_BEANS = "ALLOWED_BEANS:";
/** provides all loaded bean-attributes */
public static final String ALLOWED_BEANATTRS = "ALLOWED_BEANATTRS:";
/** provides all loaded bean-attributes (without bean prefix) */
public static final String ALLOWED_BEANATTR_NAMES = "ALLOWED_BEANATTR_NAMES:";
/** provides all generated application bean-attributes */
public static final String ALLOWED_APPBEANATTRS = "ALLOWED_APPBEANATTRS";
/** provides all files inside current ENV-path, matching given reg-exp postfix */
public static final String ALLOWED_ENVFILES = "ALLOWED_ENVFILES:";
/** reads from given fileName as postfix, and splits the data through '\n' */
public static final String ALLOWED_FROMFILE = "ALLOWED_FROMFILE:";
/**
* fill pre-defined value sets, given by Constraint annotation
*
* @param allowed Constraint annotation
* @return value set
*/
public static String[] preDefined(String[] allowed) {
LinkedList preAllowed = new LinkedList();
preAllowed.add(NULL_CLASS);
for (int i = 0; i < allowed.length; i++) {
if (allowed[i].equals(ALLOWED_CLASSES)) {
String exp = StringUtil.substring(allowed[i], ":", null);
preAllowed.addAll((Collection extends String>) CollectionUtil.getList(CollectionUtil.getTransforming(
ClassFinder.self().fuzzyFind(exp).values(), new ITransformer() {
@Override
public String transform(Class toTransform) {
return toTransform.getName();
}
}).iterator()));
} else if (allowed[i].equals(ALLOWED_APPCLASSES)) {
String exp = ENV.get("bean.generation.packagename", "");
preAllowed.addAll((Collection extends String>) CollectionUtil.getList(CollectionUtil.getTransforming(
ClassFinder.self().fuzzyFind(exp).values(), new ITransformer() {
@Override
public String transform(Class toTransform) {
return toTransform.getName();
}
}).iterator()));
} else if (allowed[i].equals(ALLOWED_METHODS)) {
String exp = StringUtil.substring(allowed[i], ":", null);
preAllowed.addAll(CollectionUtil
.toStringTransformed(ClassFinder.self().fuzzyFind(exp, Method.class, -1, null).values()));
} else if (allowed[i].equals(ALLOWED_FIELDS)) {
String exp = StringUtil.substring(allowed[i], ":", null);
preAllowed.addAll(CollectionUtil
.toStringTransformed(ClassFinder.self().fuzzyFind(exp, Field.class, -1, null).values()));
} else if (allowed[i].equals(ALLOWED_BEANS)) {
String exp = ENV.get("bean.generation.packagename", "");
preAllowed.addAll(CollectionUtil.toStringTransformed(new AttributeFinder().fuzzyFind(exp).values()));
} else if (allowed[i].equals(ALLOWED_BEANATTRS)) {
String exp = StringUtil.substring(allowed[i], ":", null);
preAllowed.addAll(CollectionUtil.toStringTransformed(new AttributeFinder().fuzzyFind(exp).values()));
} else if (allowed[i].equals(ALLOWED_APPBEANATTRS)) {
String exp = ENV.get("bean.generation.packagename", "");
preAllowed.addAll(CollectionUtil.toStringTransformed(new AttributeFinder().fuzzyFind(exp).values()));
} else if (allowed[i].equals(ALLOWED_BEANATTR_NAMES)) {
String exp = StringUtil.substring(allowed[i], ":", null);
preAllowed.addAll((Collection extends String>) CollectionUtil.getList(CollectionUtil.getTransforming(
new AttributeFinder().fuzzyFind(exp).values(),
new ITransformer() {
@Override
public String transform(String toTransform) {
return StringUtil.substring(toTransform, ".", null, true);
}
}).iterator()));
} else if (allowed[i].startsWith(ALLOWED_ENVFILES)) {
String regExp = StringUtil.substring(allowed[i], ":", null);
List files = FileUtil.getTreeFiles(ENV.getConfigPath(), regExp, false);
preAllowed.addAll(CollectionUtil.toStringTransformed(files));
} else if (allowed[i].startsWith(ALLOWED_FROMFILE)) {
String fileName = StringUtil.substring(allowed[i], ":", null);
String data = String.valueOf(FileUtil.getFileData(fileName, null));
preAllowed.addAll(Arrays.asList(data.split("\n")));
} else {
preAllowed.add(allowed[i]);
}
}
String[] values = preAllowed.toArray(new String[0]);
Arrays.sort(values);
return values;
}
public static Object transformEmptyToVoid(IAction a, int parameterIndex, Object value) {
if (a instanceof MethodAction) {
MethodAction ma = (MethodAction)a;
Constraint c = ma.getConstraints() != null && ma.getConstraints().length > parameterIndex ? ma.getConstraints()[parameterIndex] : null;
if (c != null && c.getAllowedValues() != null && c.getAllowedValues().contains(Void.class.getName()))
return value instanceof String && Util.isEmpty(value) ? NULL_CLASS : value;
}
return value;
}
}