use of io.jmix.core.DevelopmentException in project jmix by jmix-framework.
the class CubaInstanceNameProviderImpl method parseNamePattern.
@Nullable
@Override
public InstanceNameRec parseNamePattern(MetaClass metaClass) {
InstanceNameRec namePattern = super.parseNamePattern(metaClass);
if (namePattern != null) {
return namePattern;
}
Map attributes = (Map) metaClass.getAnnotations().get(NamePattern.class.getName());
if (attributes == null)
return null;
String pattern = (String) attributes.get("value");
if (StringUtils.isBlank(pattern))
return null;
int pos = pattern.indexOf("|");
if (pos < 0)
throw new DevelopmentException("Invalid name pattern: " + pattern);
String format = StringUtils.substring(pattern, 0, pos);
String trimmedFormat = format.trim();
String methodName = trimmedFormat.startsWith("#") ? trimmedFormat.substring(1) : null;
Method method = null;
if (methodName != null) {
try {
method = Stream.of(metaClass.getJavaClass().getDeclaredMethods()).filter(m -> m.getName().equals(methodName)).findFirst().orElseThrow(NoSuchMethodException::new);
} catch (NoSuchMethodException e) {
log.error("Instance name method {} not found in meta class {}", methodName, metaClass.getName(), e);
throw new RuntimeException(String.format("Instance name method %s not found in meta class %s", methodName, metaClass.getName()), e);
}
}
String fieldsStr = StringUtils.substring(pattern, pos + 1);
MetaProperty[] fields = INSTANCE_NAME_SPLIT_PATTERN.splitAsStream(fieldsStr).map(metaClass::getProperty).toArray(MetaProperty[]::new);
return new InstanceNameRec(format, method, fields);
}
Aggregations