Search in sources :

Example 31 with AtlasPath

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()));
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) AtlasPath(io.atlasmap.core.AtlasPath) AtlasException(io.atlasmap.api.AtlasException)

Example 32 with AtlasPath

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);
    }
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 33 with AtlasPath

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);
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasPath(io.atlasmap.core.AtlasPath)

Example 34 with AtlasPath

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);
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) Method(java.lang.reflect.Method)

Example 35 with AtlasPath

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);
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) SourceContact(io.atlasmap.java.test.SourceContact) Test(org.junit.Test)

Aggregations

AtlasPath (io.atlasmap.core.AtlasPath)49 Field (io.atlasmap.v2.Field)27 FieldGroup (io.atlasmap.v2.FieldGroup)26 AtlasException (io.atlasmap.api.AtlasException)17 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)14 JavaField (io.atlasmap.java.v2.JavaField)12 ArrayList (java.util.ArrayList)12 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)11 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)11 JsonField (io.atlasmap.json.v2.JsonField)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 JsonEnumField (io.atlasmap.json.v2.JsonEnumField)4 Method (java.lang.reflect.Method)4 List (java.util.List)4 Test (org.junit.Test)4 SourceAddress (io.atlasmap.java.test.SourceAddress)3 SourceOrder (io.atlasmap.java.test.SourceOrder)3 KafkaConnectEnumField (io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField)3 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)3 Head (io.atlasmap.spi.AtlasInternalSession.Head)3