use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class TargetValueConverter method resolveTargetSetMethod.
private Method resolveTargetSetMethod(Object sourceObject, Field field, Class<?> targetType) throws AtlasException {
AtlasPath atlasPath = new AtlasPath(field.getPath());
Object parentObject = sourceObject;
List<Class<?>> classTree = resolveMappableClasses(parentObject.getClass());
if (field instanceof JavaField) {
JavaField javaField = (JavaField) field;
for (Class<?> clazz : classTree) {
try {
String setterMethodName = javaField.getSetMethod();
if (setterMethodName == null) {
setterMethodName = "set" + capitalizeFirstLetter(atlasPath.getLastSegment());
}
return ClassHelper.detectSetterMethod(clazz, setterMethodName, targetType);
} catch (NoSuchMethodException e) {
// method does not exist
}
// Try the boxUnboxed version
if (conversionService.isPrimitive(targetType) || conversionService.isBoxedPrimitive(targetType)) {
try {
String setterMethodName = javaField.getSetMethod();
if (setterMethodName == null) {
setterMethodName = "set" + capitalizeFirstLetter(atlasPath.getLastSegment());
}
return ClassHelper.detectSetterMethod(clazz, setterMethodName, conversionService.boxOrUnboxPrimitive(targetType));
} catch (NoSuchMethodException e) {
// method does not exist
}
}
}
} else if (field instanceof JavaEnumField) {
for (Class<?> clazz : classTree) {
try {
String setterMethodName = "set" + capitalizeFirstLetter(atlasPath.getLastSegment());
return ClassHelper.detectSetterMethod(clazz, setterMethodName, targetType);
} catch (NoSuchMethodException e) {
// method does not exist
}
}
}
throw new AtlasException(String.format("Unable to resolve setter for path=%s", field.getPath()));
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class ClassHelper method parentObjectForPath.
public static Object parentObjectForPath(Object targetObject, AtlasPath pathUtil, boolean skipCollectionWrapper) throws AtlasException {
try {
if (targetObject == null) {
return null;
}
if (pathUtil == null) {
return targetObject;
}
if (!pathUtil.hasParent() && !pathUtil.hasCollection()) {
return targetObject;
}
Object parentObject = targetObject;
AtlasPath parentPath = pathUtil.getLastSegmentParentPath();
if (parentPath == null) {
parentPath = pathUtil;
}
for (String segment : parentPath.getSegments()) {
List<String> getters = getterMethodNames(AtlasPath.cleanPathSegment(segment));
Method getterMethod = null;
for (String getter : getters) {
try {
getterMethod = detectGetterMethod(parentObject.getClass(), getter);
break;
} catch (NoSuchMethodException e) {
// exhaust options
}
}
if (getterMethod == null) {
throw new NoSuchMethodException("Unable to detect getter method for " + segment);
}
getterMethod.setAccessible(true);
parentObject = getterMethod.invoke(parentObject);
if (skipCollectionWrapper) {
if (AtlasPath.isListSegment(segment) && pathUtil.isIndexedCollection()) {
int index = AtlasPath.indexOfSegment(segment);
parentObject = ((List<?>) parentObject).get(index);
} else if (AtlasPath.isArraySegment(segment)) {
int index = AtlasPath.indexOfSegment(segment);
parentObject = Array.get(parentObject, index);
}
}
}
return parentObject;
} catch (Exception e) {
throw new AtlasException(e);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class BaseDocumentWriterTest method reset.
@Before
public void reset() {
classLoader = Thread.currentThread().getContextClassLoader();
writer = new DocumentJavaFieldWriter(conversionService);
writer.setTargetValueConverter(new TargetValueConverter(classLoader, conversionService) {
public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
return targetField.getValue();
}
});
field = null;
segmentContexts = new LinkedList<>();
targetTestClassInstance = new TargetTestClass();
targetTestClassInstance.setContact(new TargetContact());
targetTestClassInstance.setAddress(new TargetAddress());
targetOrderListInstance = new TestListOrders();
targetOrderListInstance.setOrders(new LinkedList<>());
targetOrderListInstance.getOrders().add(new TargetOrder());
targetOrderListInstance.getOrders().add(new TargetOrder());
targetTestClassInstance.setListOrders(targetOrderListInstance);
targetOrderArrayInstance = new TargetOrderArray();
targetOrderArrayInstance.setOrders(new BaseOrder[2]);
targetOrderArrayInstance.getOrders()[0] = new TargetOrder();
targetOrderArrayInstance.getOrders()[1] = new TargetOrder();
targetTestClassInstance.setOrderArray(targetOrderArrayInstance);
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DocumentJavaFieldWriter method writeDocumentToString.
private String writeDocumentToString(boolean stripSpaces, Object object) throws AtlasException {
try {
if (object == null) {
return "";
}
String result = object.toString();
if (stripSpaces) {
result = result.replaceAll("\n|\r", "");
result = result.replaceAll("> *?<", "><");
}
return result;
} catch (Exception e) {
throw new AtlasException(e);
}
}
Aggregations