use of io.atlasmap.core.AtlasPath 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.core.AtlasPath in project atlasmap by atlasmap.
the class BaseDocumentWriterTest method setupPath.
public void setupPath(String fieldPath) {
this.segmentContexts = new AtlasPath(fieldPath).getSegmentContexts(true);
for (SegmentContext ctx : this.segmentContexts) {
addClassForFieldPath(ctx.getSegmentPath(), String.class);
}
this.lastSegmentContext = segmentContexts.get(segmentContexts.size() - 1);
this.field = createField(fieldPath, DEFAULT_VALUE);
}
use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.
the class DocumentJavaFieldReader method populateSourceFieldValue.
private void populateSourceFieldValue(AtlasInternalSession session, Field field, Object source, Method m) throws Exception {
Method getter = m;
Object parentObject = source;
AtlasPath atlasPath = new AtlasPath(field.getPath());
Object sourceValue = null;
if (atlasPath.isRoot()) {
sourceValue = source;
} else {
if (atlasPath.hasParent()) {
parentObject = ClassHelper.parentObjectForPath(source, atlasPath, true);
}
getter = (getter == null) ? resolveGetMethod(source, field) : getter;
if (getter != null) {
sourceValue = getter.invoke(parentObject);
}
}
// TODO: support doing parent stuff at field level vs getter
if (sourceValue == null) {
String cleanedLastSegment = AtlasPath.cleanPathSegment(atlasPath.getLastSegment());
sourceValue = getValueFromMemberField(session, parentObject, cleanedLastSegment);
}
if (sourceValue != null) {
sourceValue = extractFromCollection(session, sourceValue, atlasPath);
if (conversionService.isPrimitive(sourceValue.getClass()) || conversionService.isBoxedPrimitive(sourceValue.getClass())) {
sourceValue = conversionService.copyPrimitive(sourceValue);
}
}
field.setValue(sourceValue);
}
use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.
the class ClassHelperTest method testParentObjectForPathParamChecking.
@Test
public void testParentObjectForPathParamChecking() throws Exception {
assertNull(ClassHelper.parentObjectForPath(null, null, true));
assertNull(ClassHelper.parentObjectForPath(null, new AtlasPath("foo.bar"), true));
SourceContact targetObject = new SourceContact();
Object parentObject = ClassHelper.parentObjectForPath(targetObject, null, true);
assertNotNull(parentObject);
assertTrue(parentObject instanceof SourceContact);
assertEquals(targetObject, parentObject);
}
use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.
the class ClassHelperTest method testParentObjectForPathList.
@Test
public void testParentObjectForPathList() throws Exception {
SourceOrderList sourceOrderList = new SourceOrderList();
List<BaseOrder> sourceOrders = new ArrayList<>();
sourceOrderList.setOrders(sourceOrders);
SourceAddress sourceAddress = new SourceAddress();
SourceOrder sourceOrder = new SourceOrder();
sourceOrder.setAddress(sourceAddress);
sourceOrderList.getOrders().add(sourceOrder);
Object parentObject = ClassHelper.parentObjectForPath(sourceOrderList, new AtlasPath("orders<>"), true);
assertNotNull(parentObject);
assertTrue(parentObject instanceof List<?>);
assertEquals(sourceOrders, parentObject);
}
Aggregations