Search in sources :

Example 11 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class DocumentJavaFieldReader method resolveGetMethod.

private Method resolveGetMethod(Object sourceObject, Field field) throws AtlasException {
    Object parentObject = sourceObject;
    AtlasPath atlasPath = new AtlasPath(field.getPath());
    Method getter = null;
    if (atlasPath.hasParent()) {
        parentObject = ClassHelper.parentObjectForPath(sourceObject, atlasPath, true);
    }
    if (parentObject == null) {
        return null;
    }
    List<Class<?>> classTree = resolveMappableClasses(parentObject.getClass());
    for (Class<?> clazz : classTree) {
        try {
            if (field instanceof JavaField && ((JavaField) field).getGetMethod() != null) {
                getter = clazz.getMethod(((JavaField) field).getGetMethod());
                getter.setAccessible(true);
                return getter;
            }
        } catch (NoSuchMethodException e) {
        // no getter method specified in mapping file
        }
        for (String m : Arrays.asList("get", "is")) {
            String cleanedLastSegment = AtlasPath.cleanPathSegment(atlasPath.getLastSegment());
            String getterMethod = m + capitalizeFirstLetter(cleanedLastSegment);
            try {
                getter = clazz.getMethod(getterMethod);
                getter.setAccessible(true);
                return getter;
            } catch (NoSuchMethodException e) {
            // method does not exist
            }
        }
    }
    return null;
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) AtlasPath(io.atlasmap.core.AtlasPath) Method(java.lang.reflect.Method)

Example 12 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class DocumentJavaFieldWriter method getClassForField.

private Class<?> getClassForField(Field field, SegmentContext segmentContext, Object parentObject, boolean unwrapCollectionType) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking up class to use for segment: " + segmentContext + "\n\tparentObject: " + parentObject);
    }
    Class<?> clz = null;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking for configured class for field: " + field + ".");
    }
    String className = null;
    if (field instanceof JavaField) {
        className = ((JavaField) field).getClassName();
    } else if (field instanceof JavaEnumField) {
        className = ((JavaEnumField) field).getClassName();
    }
    if (className != null) {
        try {
            clz = className == null ? null : Class.forName(className);
        } catch (Exception e) {
            throw new AtlasException("Could not find class for '" + className + "', for segment: " + segmentContext + ", on field: " + field, e);
        }
    }
    if (clz == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Couldn't find class on field. Looking for configured class for segment: " + segmentContext + ".");
        }
        String normalizedSegment = AtlasPath.removeCollectionIndexes(segmentContext.getSegmentPath());
        clz = this.classesForFields.get(normalizedSegment);
    }
    Type clzType = null;
    if (clz == null) {
        // attempt to determine it from the parent object.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Couldn't find configured class for segment: " + segmentContext + ", looking up getter method.");
        }
        Method m = null;
        try {
            String methodName = "get" + JavaWriterUtil.capitalizeFirstLetter(AtlasPath.cleanPathSegment(segmentContext.getSegment()));
            m = ClassHelper.detectGetterMethod(parentObject.getClass(), methodName);
        } catch (NoSuchMethodException e) {
            // it's ok, we didnt find a getter.
            if (LOG.isDebugEnabled()) {
                LOG.debug("Couldn't find getter method for segment: " + segmentContext, e);
            }
        }
        clz = m == null ? null : m.getReturnType();
        clzType = m.getGenericReturnType();
    }
    if (clz == null) {
        throw new AtlasException("Could not create object, can't find class to instantiate for segment: " + segmentContext);
    }
    if (unwrapCollectionType) {
        clz = unwrapCollectionType(field, segmentContext, parentObject, clz, clzType);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Found class '" + clz.getName() + "' to use for segment: " + segmentContext);
    }
    return clz;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) FieldType(io.atlasmap.v2.FieldType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 13 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class JavaConstructService method constructClassIgnoreCollection.

private Object constructClassIgnoreCollection(JavaClass javaClass, List<String> pathFilters) throws ConstructException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    Object targetObject = instantiateClass(javaClass.getClassName());
    filterFields(javaClass, pathFilters);
    if (javaClass.getJavaFields() == null || javaClass.getJavaFields().getJavaField() == null || javaClass.getJavaFields().getJavaField().isEmpty()) {
        return targetObject;
    }
    for (JavaField f : javaClass.getJavaFields().getJavaField()) {
        if (!(f instanceof JavaClass)) {
            continue;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Constructing complex child p=%s c=%s", f.getPath(), f.getClassName()));
        }
        Object parentObject = targetObject;
        /*
             * We aren't using the path for construction for now JavaPath javaPath = new
             * JavaPath(f.getPath()); if(javaPath.hasParent()) {
             *
             * }
             */
        Method setter = null;
        boolean doSetter = true;
        if (f.getGetMethod() != null) {
            Method getter = null;
            Object getterResult = null;
            try {
                getter = ClassHelper.detectGetterMethod(parentObject.getClass(), f.getGetMethod());
                getter.setAccessible(true);
                getterResult = getter.invoke(parentObject);
            } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException e) {
                LOG.warn(String.format("Error invoking getter for field p=%s c=%s msg=%s", f.getPath(), f.getClassName(), e.getMessage()), e);
                continue;
            }
            if (getterResult != null) {
                doSetter = false;
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("Field instantiated by parent class p=%s c=%s", f.getPath(), f.getClassName()));
                }
            }
        }
        // instantiate class
        if (doSetter && f.getSetMethod() != null) {
            try {
                setter = ClassHelper.detectSetterMethod(parentObject.getClass(), f.getSetMethod(), null);
                setter.setAccessible(true);
                setter.invoke(parentObject, constructClass((JavaClass) f, pathFilters));
            } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException e) {
                LOG.warn(String.format("Error invoking setter for field p=%s c=%s msg=%s", f.getPath(), f.getClassName(), e.getMessage()), e);
                continue;
            }
        }
    }
    return targetObject;
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 14 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class BaseDocumentWriterTest method createField.

public JavaField createField(String path, Object value, FieldType fieldType) {
    JavaField field = new JavaField();
    field.setFieldType(fieldType);
    field.setValue(value);
    field.setPath(path);
    return field;
}
Also used : JavaField(io.atlasmap.java.v2.JavaField)

Example 15 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class ClassInspectionService method convertJavaFieldToJavaClass.

private JavaClass convertJavaFieldToJavaClass(JavaField javaField) {
    JavaClass javaClass = AtlasJavaModelFactory.createJavaClass();
    javaClass.setArrayDimensions(javaField.getArrayDimensions());
    javaClass.setArraySize(javaField.getArraySize());
    javaClass.setCollectionClassName(javaField.getCollectionClassName());
    javaClass.setCollectionType(javaField.getCollectionType());
    javaClass.setDocId(javaField.getDocId());
    javaClass.setPrimitive(javaField.isPrimitive());
    javaClass.setSynthetic(javaField.isSynthetic());
    javaClass.setClassName(javaField.getClassName());
    javaClass.setGetMethod(javaField.getGetMethod());
    javaClass.setName(javaField.getName());
    javaClass.setPath(javaField.getPath());
    javaClass.setRequired(javaField.isRequired());
    javaClass.setSetMethod(javaField.getSetMethod());
    javaClass.setStatus(javaField.getStatus());
    javaClass.setFieldType(javaField.getFieldType());
    if (javaField.getClassName() != null) {
        javaClass.setUri(String.format(AtlasJavaModelFactory.URI_FORMAT, javaField.getClassName()));
    }
    javaClass.setValue(javaField.getValue());
    javaClass.setAnnotations(javaField.getAnnotations());
    javaClass.setModifiers(javaField.getModifiers());
    javaClass.setParameterizedTypes(javaField.getParameterizedTypes());
    return javaClass;
}
Also used : JavaClass(io.atlasmap.java.v2.JavaClass)

Aggregations

JavaField (io.atlasmap.java.v2.JavaField)53 Test (org.junit.Test)19 JavaClass (io.atlasmap.java.v2.JavaClass)16 Mapping (io.atlasmap.v2.Mapping)16 AtlasMapping (io.atlasmap.v2.AtlasMapping)15 Validation (io.atlasmap.v2.Validation)8 FieldType (io.atlasmap.v2.FieldType)7 Method (java.lang.reflect.Method)7 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)6 AtlasException (io.atlasmap.api.AtlasException)5 DataSource (io.atlasmap.v2.DataSource)5 Field (io.atlasmap.v2.Field)5 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)4 ValidationScope (io.atlasmap.v2.ValidationScope)4 ValidationStatus (io.atlasmap.v2.ValidationStatus)4 List (java.util.List)4 Logger (org.slf4j.Logger)4 AtlasConstants (io.atlasmap.api.AtlasConstants)3 AtlasMappingUtil (io.atlasmap.core.AtlasMappingUtil)3 AtlasPath (io.atlasmap.core.AtlasPath)3