use of io.atlasmap.core.AtlasPath 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.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);
}
Aggregations