Search in sources :

Example 46 with JavaField

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

the class TargetValueConverter method convert.

public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
    FieldType sourceType = sourceField.getFieldType();
    Object sourceValue = sourceField.getValue();
    Object targetValue = null;
    FieldType targetType = targetField.getFieldType();
    if (LOG.isDebugEnabled()) {
        LOG.debug("processTargetMapping iPath=" + sourceField.getPath() + " iV=" + sourceValue + " iT=" + sourceType + " oPath=" + targetField.getPath() + " docId: " + targetField.getDocId());
    }
    if (sourceValue == null) {
        // TODO: Finish targetValue = null processing
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Null sourceValue for targetDocId=%s, targetPath=%s", targetField.getDocId(), targetField.getPath()), targetField.getPath(), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
        return null;
    }
    String targetClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : null;
    targetClassName = (targetField instanceof JavaEnumField) ? ((JavaEnumField) targetField).getClassName() : targetClassName;
    if (targetType == null || targetClassName == null) {
        try {
            Method setter = resolveTargetSetMethod(parentObject, targetField, null);
            if (setter != null && setter.getParameterCount() == 1) {
                if (targetField instanceof JavaField) {
                    ((JavaField) targetField).setClassName(setter.getParameterTypes()[0].getName());
                } else if (targetField instanceof JavaEnumField) {
                    ((JavaEnumField) targetField).setClassName(setter.getParameterTypes()[0].getName());
                }
                targetType = conversionService.fieldTypeFromClass(setter.getParameterTypes()[0]);
                targetField.setFieldType(targetType);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Auto-detected targetType as {} for class={} path={}", targetType, parentObject.toString(), targetField.getPath());
                }
            }
        } catch (Exception e) {
            LOG.debug("Unable to auto-detect targetType for class={} path={}", parentObject.toString(), targetField.getPath());
        }
    }
    if (sourceField instanceof JavaEnumField || targetField instanceof JavaEnumField) {
        if (!(sourceField instanceof JavaEnumField) || !(targetField instanceof JavaEnumField)) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Value conversion between enum fields and non-enum fields is not yet supported: sourceType=%s targetType=%s targetPath=%s msg=%s", sourceType, targetType, targetField.getPath()), targetField.getPath(), AuditStatus.ERROR, sourceValue != null ? sourceValue.toString() : null);
        }
        return populateEnumValue(session, lookupTable, (JavaEnumField) sourceField, (JavaEnumField) targetField);
    }
    Class<?> targetClazz = null;
    if (targetClassName == null) {
        if (targetType != null) {
            targetClazz = conversionService.classFromFieldType(targetType);
        } else {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Target field doesn't have fieldType nor className: automatic conversion won't work: targetPath=%s", targetField.getPath()), targetField.getPath(), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
        }
    } else if (conversionService.isPrimitive(targetClassName)) {
        targetClazz = conversionService.boxOrUnboxPrimitive(targetClassName);
    } else {
        try {
            targetClazz = classLoader.loadClass(targetClassName);
        } catch (ClassNotFoundException e) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Target field class '%s' was not found: sourceType=%s targetType=%s targetPath=%s msg=%s", ((JavaField) targetField).getClassName(), sourceType, targetType, targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue != null ? targetValue.toString() : null);
            return null;
        }
    }
    if (targetClazz != null) {
        targetValue = conversionService.convertType(sourceValue, null, targetClazz, null);
    } else {
        targetValue = sourceValue;
    }
    AtlasFieldActionService fieldActionService = session.getAtlasContext().getContextFactory().getFieldActionService();
    try {
        targetValue = fieldActionService.processActions(targetField.getActions(), targetValue, targetType);
        if (targetValue != null) {
            if (targetClazz != null) {
                targetValue = conversionService.convertType(targetValue, null, targetClazz, null);
            } else {
                FieldType conversionInputType = conversionService.fieldTypeFromClass(targetValue.getClass());
                targetValue = conversionService.convertType(targetValue, conversionInputType, targetType);
            }
        }
    } catch (AtlasConversionException e) {
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Unable to auto-convert for sourceType=%s targetType=%s targetPath=%s msg=%s", sourceType, targetType, targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue != null ? targetValue.toString() : null);
        return null;
    }
    return targetValue;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) AtlasFieldActionService(io.atlasmap.api.AtlasFieldActionService) AtlasConversionException(io.atlasmap.api.AtlasConversionException) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) FieldType(io.atlasmap.v2.FieldType)

Example 47 with JavaField

use of io.atlasmap.java.v2.JavaField 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 48 with JavaField

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

the class ClassInspectionService method inspectClassMethods.

private void inspectClassMethods(ClassLoader classLoader, Class<?> clazz, JavaClass javaClass, Set<String> cachedClasses, String pathPrefix) {
    Method[] methods = clazz.getDeclaredMethods();
    if (methods != null && !javaClass.isEnumeration()) {
        for (Method m : methods) {
            JavaField s = AtlasJavaModelFactory.createJavaField();
            s.setName(m.getName());
            s.setSynthetic(m.isSynthetic());
            if (m.isVarArgs() || m.isBridge() || m.isSynthetic() || m.isDefault()) {
                s.setStatus(FieldStatus.UNSUPPORTED);
                LOG.warn("VarArg, Bridge, Synthetic or Default method " + m.getName() + " detected");
                continue;
            }
            s.setSynthetic(m.isSynthetic());
            if (m.getName().startsWith("get") || m.getName().startsWith("is")) {
                s = inspectGetMethod(classLoader, m, s, cachedClasses, pathPrefix);
            }
            if (m.getName().startsWith("set")) {
                s = inspectSetMethod(classLoader, m, s, cachedClasses, pathPrefix);
            }
            boolean found = false;
            for (int i = 0; i < javaClass.getJavaFields().getJavaField().size(); i++) {
                JavaField exists = javaClass.getJavaFields().getJavaField().get(i);
                if (s.getName().equals(exists.getName())) {
                    found = true;
                    // have fields
                    if (exists.getGetMethod() == null && s.getGetMethod() != null) {
                        exists.setGetMethod(s.getGetMethod());
                    }
                    if (exists.getSetMethod() == null && s.getSetMethod() != null) {
                        exists.setSetMethod(s.getSetMethod());
                    }
                }
            }
            if (found) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Field already defined for method: " + m.getName() + " class: " + clazz.getName());
                }
            } else if (s.getGetMethod() != null || s.getSetMethod() != null) {
                javaClass.getJavaFields().getJavaField().add(s);
            } else {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Ignoring non-field method: " + m.getName() + " class: " + clazz.getName());
                }
            }
        }
    }
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method)

Example 49 with JavaField

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

the class ClassInspectionService method inspectGetMethod.

private JavaField inspectGetMethod(ClassLoader classLoader, Method m, JavaField s, Set<String> cachedClasses, String pathPrefix) {
    JavaField field = s;
    field.setName(StringUtil.removeGetterAndLowercaseFirstLetter(m.getName()));
    if (pathPrefix != null && pathPrefix.length() > 0) {
        field.setPath(pathPrefix + AtlasPath.PATH_SEPARATOR + StringUtil.removeGetterAndLowercaseFirstLetter(m.getName()));
    } else {
        field.setPath(StringUtil.removeGetterAndLowercaseFirstLetter(m.getName()));
    }
    if (m.getParameterCount() != 0) {
        field.setStatus(FieldStatus.UNSUPPORTED);
        return field;
    }
    if (m.getReturnType().equals(Void.TYPE)) {
        field.setStatus(FieldStatus.UNSUPPORTED);
        return field;
    }
    Class<?> returnType = m.getReturnType();
    if (returnType.isArray()) {
        field.setCollectionType(CollectionType.ARRAY);
        field.setArrayDimensions(detectArrayDimensions(returnType));
        returnType = detectArrayClass(returnType);
    }
    field.setClassName(returnType.getCanonicalName());
    field.setGetMethod(m.getName());
    field.setFieldType(getConversionService().fieldTypeFromClass(returnType));
    if (getConversionService().isPrimitive(returnType) || getConversionService().isBoxedPrimitive(returnType)) {
        field.setPrimitive(true);
        field.setStatus(FieldStatus.SUPPORTED);
    } else if (field.getFieldType() != FieldType.COMPLEX) {
        field.setPrimitive(false);
        field.setStatus(FieldStatus.SUPPORTED);
    } else {
        field.setPrimitive(false);
        Class<?> complexClazz = null;
        JavaClass tmpField = convertJavaFieldToJavaClass(field);
        field = tmpField;
        if (returnType.getCanonicalName() == null) {
            field.setStatus(FieldStatus.UNSUPPORTED);
        } else if (!cachedClasses.contains(returnType.getCanonicalName())) {
            try {
                complexClazz = classLoader.loadClass(returnType.getCanonicalName());
                cachedClasses.add(returnType.getCanonicalName());
                inspectClass(classLoader, complexClazz, tmpField, cachedClasses, field.getPath());
                if (tmpField.getStatus() == null) {
                    field.setStatus(FieldStatus.SUPPORTED);
                }
            } catch (ClassNotFoundException cnfe) {
                field.setStatus(FieldStatus.NOT_FOUND);
            }
        } else {
            field.setStatus(FieldStatus.CACHED);
        }
    }
    return field;
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) JavaClass(io.atlasmap.java.v2.JavaClass)

Example 50 with JavaField

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

the class ClassValidationUtil method validateSimpleTestContact.

public static void validateSimpleTestContact(JavaClass c) {
    assertNotNull(c);
    assertFalse(c.isAnnonymous());
    assertFalse(c.isAnnotation());
    assertTrue(c.getCollectionType() == null);
    assertFalse(c.isEnumeration());
    assertFalse(c.isInterface());
    assertFalse(c.isLocalClass());
    assertFalse(c.isMemberClass());
    assertFalse(c.isPrimitive());
    assertFalse(c.isSynthetic());
    assertNotNull(c.getUri());
    assertEquals(String.format(AtlasJavaModelFactory.URI_FORMAT, c.getClassName()), c.getUri());
    assertEquals("io.atlasmap.java.test.BaseContact", c.getClassName());
    assertEquals("io.atlasmap.java.test", c.getPackageName());
    assertNotNull(c.getJavaEnumFields());
    assertNotNull(c.getJavaEnumFields().getJavaEnumField());
    assertEquals(new Integer(0), new Integer(c.getJavaEnumFields().getJavaEnumField().size()));
    assertNotNull(c.getJavaFields());
    assertNotNull(c.getJavaFields().getJavaField());
    assertEquals(new Integer(5), new Integer(c.getJavaFields().getJavaField().size()));
    for (JavaField f : c.getJavaFields().getJavaField()) {
        switch(f.getName()) {
            case "serialVersionUID":
                validateSerialVersionUID(f);
                break;
            case "firstName":
                break;
            case "lastName":
                break;
            case "phoneNumber":
                break;
            case "zipCode":
                break;
            default:
                fail("Unexpected field detected: " + f.getName());
        }
    }
}
Also used : JavaField(io.atlasmap.java.v2.JavaField)

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