Search in sources :

Example 16 with JavaField

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

the class ClassInspectionService method inspectSetMethod.

private JavaField inspectSetMethod(ClassLoader classLoader, Method m, JavaField s, Set<String> cachedClasses, String pathPrefix) {
    JavaField field = s;
    field.setName(StringUtil.removeSetterAndLowercaseFirstLetter(m.getName()));
    if (pathPrefix != null && pathPrefix.length() > 0) {
        field.setPath(pathPrefix + AtlasPath.PATH_SEPARATOR + StringUtil.removeSetterAndLowercaseFirstLetter(m.getName()));
    } else {
        field.setPath(StringUtil.removeSetterAndLowercaseFirstLetter(m.getName()));
    }
    if (m.getParameterCount() != 1) {
        field.setStatus(FieldStatus.UNSUPPORTED);
        return field;
    }
    if (!m.getReturnType().equals(Void.TYPE)) {
        field.setStatus(FieldStatus.UNSUPPORTED);
        return field;
    }
    Class<?>[] params = m.getParameterTypes();
    if (params == null || params.length != 1) {
        field.setStatus(FieldStatus.UNSUPPORTED);
        return field;
    }
    Class<?> paramType = params[0];
    if (paramType.isArray()) {
        field.setCollectionType(CollectionType.ARRAY);
        field.setArrayDimensions(detectArrayDimensions(paramType));
        paramType = detectArrayClass(paramType);
    }
    field.setClassName(paramType.getCanonicalName());
    field.setSetMethod(m.getName());
    field.setFieldType(getConversionService().fieldTypeFromClass(paramType));
    if (getConversionService().isPrimitive(paramType) || getConversionService().isBoxedPrimitive(paramType)) {
        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 (paramType.getCanonicalName() == null) {
            field.setStatus(FieldStatus.UNSUPPORTED);
        } else if (!cachedClasses.contains(paramType.getCanonicalName())) {
            try {
                complexClazz = classLoader.loadClass(paramType.getCanonicalName());
                cachedClasses.add(paramType.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 17 with JavaField

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

the class ClassInspectionService method inspectField.

private JavaField inspectField(ClassLoader classLoader, Field f, Set<String> cachedClasses, String pathPrefix) {
    JavaField s = AtlasJavaModelFactory.createJavaField();
    Class<?> clazz = f.getType();
    s.setName(f.getName());
    if (pathPrefix != null && pathPrefix.length() > 0) {
        s.setPath(pathPrefix + AtlasPath.PATH_SEPARATOR + f.getName());
    } else {
        s.setPath(f.getName());
    }
    if (isMapList(clazz.getCanonicalName())) {
        s.setCollectionType(CollectionType.MAP);
    }
    if (clazz.isArray()) {
        s.setCollectionType(CollectionType.ARRAY);
        s.setArrayDimensions(detectArrayDimensions(clazz));
        clazz = detectArrayClass(clazz);
    } else if (isFieldList(clazz.getCanonicalName())) {
        s.setCollectionType(CollectionType.LIST);
        s.setCollectionClassName(clazz.getCanonicalName());
        try {
            clazz = detectListClass(classLoader, f);
            if (clazz == null) {
                s.setStatus(FieldStatus.ERROR);
                return s;
            }
        } catch (ClassCastException | ClassNotFoundException cce) {
            LOG.debug("Error detecting inner listClass: " + cce.getMessage() + " for field: " + f.getName(), cce);
            s.setStatus(FieldStatus.ERROR);
            return s;
        }
    }
    s.setFieldType(getConversionService().fieldTypeFromClass(clazz));
    if (getConversionService().isPrimitive(clazz) || getConversionService().isBoxedPrimitive(clazz)) {
        s.setPrimitive(true);
        s.setStatus(FieldStatus.SUPPORTED);
    } else if (s.getFieldType() != FieldType.COMPLEX) {
        s.setPrimitive(false);
        s.setStatus(FieldStatus.SUPPORTED);
    } else {
        s.setPrimitive(false);
        Class<?> complexClazz = null;
        JavaClass tmpField = convertJavaFieldToJavaClass(s);
        s = tmpField;
        if (clazz.getCanonicalName() == null) {
            s.setStatus(FieldStatus.UNSUPPORTED);
        } else if (!cachedClasses.contains(clazz.getCanonicalName())) {
            try {
                complexClazz = classLoader.loadClass(clazz.getCanonicalName());
                cachedClasses.add(clazz.getCanonicalName());
                inspectClass(classLoader, complexClazz, tmpField, cachedClasses, s.getPath());
                if (tmpField.getStatus() == null) {
                    s.setStatus(FieldStatus.SUPPORTED);
                }
            } catch (ClassNotFoundException cnfe) {
                s.setStatus(FieldStatus.NOT_FOUND);
            }
        } else {
            s.setStatus(FieldStatus.CACHED);
        }
    }
    s.setClassName(clazz.getCanonicalName());
    s.setSynthetic(f.isSynthetic());
    Annotation[] annotations = f.getAnnotations();
    if (annotations != null) {
        for (Annotation a : annotations) {
            if (s.getAnnotations() == null) {
                s.setAnnotations(new StringList());
            }
            s.getAnnotations().getString().add(a.annotationType().getCanonicalName());
        }
    }
    if (s.getModifiers() == null) {
        s.setModifiers(new ModifierList());
    }
    s.getModifiers().getModifier().addAll(detectModifiers(f.getModifiers()));
    List<String> pTypes = detectParameterizedTypes(f, false);
    if (pTypes != null) {
        if (s.getParameterizedTypes() == null) {
            s.setParameterizedTypes(new StringList());
        }
        s.getParameterizedTypes().getString().addAll(pTypes);
    }
    populateGetterSetter(clazz, f, s);
    return s;
}
Also used : ModifierList(io.atlasmap.java.v2.ModifierList) JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) StringList(io.atlasmap.v2.StringList) JavaClass(io.atlasmap.java.v2.JavaClass) Annotation(java.lang.annotation.Annotation)

Example 18 with JavaField

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

the class DocumentJavaFieldWriterTest method testClassLookupFromField.

@Test
public void testClassLookupFromField() throws Exception {
    JavaField f = createField("/", null);
    f.setClassName(TestListOrders.class.getName());
    f.setFieldType(FieldType.COMPLEX);
    write(f);
    TestListOrders o = (TestListOrders) writer.getRootObject();
    ensureNotNullAndClass(o, TestListOrders.class);
    f = createField("/orders<4>", null);
    f.setClassName(TargetOrder.class.getName());
    f.setFieldType(FieldType.COMPLEX);
    write(f);
    ensureNotNullAndClass(o.getOrders(), LinkedList.class);
    assertEquals(5, o.getOrders().size());
    for (int i = 0; i < 5; i++) {
        System.out.println("Checking #" + i);
        TargetOrder order = (TargetOrder) o.getOrders().get(i);
        if (i == 4) {
            ensureNotNullAndClass(order, TargetOrder.class);
        } else {
            assertNull(order);
        }
    }
    f = createField("/orders<7>", null);
    f.setClassName(TargetOrder.class.getName());
    f.setFieldType(FieldType.COMPLEX);
    write(f);
    ensureNotNullAndClass(o.getOrders(), LinkedList.class);
    assertEquals(8, o.getOrders().size());
    for (int i = 0; i < 8; i++) {
        System.out.println("Checking #" + i);
        TargetOrder order = (TargetOrder) o.getOrders().get(i);
        if (i == 4 || i == 7) {
            ensureNotNullAndClass(order, TargetOrder.class);
        } else {
            assertNull(order);
        }
    }
    f = createField("/orders<7>/address", null);
    f.setClassName(TargetAddress.class.getName());
    f.setFieldType(FieldType.COMPLEX);
    write(f);
    ensureNotNullAndClass(o.getOrders().get(7).getAddress(), TargetAddress.class);
    write(createField("/orders<7>/address/addressLine1", "hello world1."));
    assertEquals("hello world1.", o.getOrders().get(7).getAddress().getAddressLine1());
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) TestListOrders(io.atlasmap.java.test.TestListOrders) TargetAddress(io.atlasmap.java.test.TargetAddress) TargetOrder(io.atlasmap.java.test.TargetOrder) Test(org.junit.Test)

Example 19 with JavaField

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

the class CachedComplexClassInspectClassTest method testCachedComplexClass.

@Test
public void testCachedComplexClass() {
    JavaClass c = classInspectionService.inspectClass(CachedComplexClass.class);
    assertNotNull(c);
    assertNull(c.getAnnotations());
    assertNull(c.getArrayDimensions());
    assertEquals("io.atlasmap.java.test.CachedComplexClass", c.getClassName());
    assertNull(c.getGetMethod());
    assertNotNull(c.getJavaEnumFields());
    assertNotNull(c.getJavaEnumFields().getJavaEnumField());
    assertTrue(c.getJavaEnumFields().getJavaEnumField().size() == 0);
    assertNotNull(c.getJavaFields());
    assertNotNull(c.getJavaFields().getJavaField());
    assertNull(c.getName());
    assertEquals("io.atlasmap.java.test", c.getPackageName());
    assertNull(c.getSetMethod());
    assertNull(c.getFieldType());
    assertNotNull(c.getUri());
    assertEquals(String.format(AtlasJavaModelFactory.URI_FORMAT, "io.atlasmap.java.test.CachedComplexClass"), c.getUri());
    assertNull(c.getValue());
    assertEquals(new Integer(3), new Integer(c.getJavaFields().getJavaField().size()));
    Integer validated = 0;
    for (JavaField f : c.getJavaFields().getJavaField()) {
        if ("io.atlasmap.java.test.BaseOrder".equals(f.getClassName())) {
            // ClassValidationUtil.validateSimpleTestContact((JavaClass)f);
            validated++;
        }
        if ("io.atlasmap.java.test.BaseContact".equals(f.getClassName())) {
            if (!FieldStatus.CACHED.equals(f.getStatus())) {
                ClassValidationUtil.validateSimpleTestContact((JavaClass) f);
            }
            validated++;
        }
        if ("io.atlasmap.java.test.BaseAddress".equals(f.getClassName())) {
            if (!FieldStatus.CACHED.equals(f.getStatus())) {
                ClassValidationUtil.validateSimpleTestAddress((JavaClass) f);
            }
            validated++;
        }
        if ("long".equals(f.getClassName())) {
            ClassValidationUtil.validateSerialVersionUID(f);
            validated++;
        }
    }
    assertEquals(validated, new Integer(c.getJavaFields().getJavaField().size()));
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) Test(org.junit.Test)

Example 20 with JavaField

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

the class ClassInspectionServiceTest method testDateTimeViaGetter.

@Test
public void testDateTimeViaGetter() {
    JavaClass javaClass = classInspectionService.inspectClass(DateTimeGetter.class);
    assertEquals(14, javaClass.getJavaFields().getJavaField().size());
    for (JavaField field : javaClass.getJavaFields().getJavaField()) {
        if ("year".equals(field.getName())) {
            assertEquals("java.time.Year", field.getClassName());
            assertEquals(FieldType.DATE, field.getFieldType());
            assertEquals("getYear", field.getGetMethod());
        } else if ("month".equals(field.getName())) {
            assertEquals("java.time.Month", field.getClassName());
            assertEquals(FieldType.DATE, field.getFieldType());
            assertEquals("getMonth", field.getGetMethod());
        } else if ("yearMonth".equals(field.getName())) {
            assertEquals("java.time.YearMonth", field.getClassName());
            assertEquals(FieldType.DATE, field.getFieldType());
            assertEquals("getYearMonth", field.getGetMethod());
        } else if ("monthDay".equals(field.getName())) {
            assertEquals("java.time.MonthDay", field.getClassName());
            assertEquals(FieldType.DATE, field.getFieldType());
            assertEquals("getMonthDay", field.getGetMethod());
        } else if ("localDate".equals(field.getName())) {
            assertEquals("java.time.LocalDate", field.getClassName());
            assertEquals(FieldType.DATE, field.getFieldType());
            assertEquals("getLocalDate", field.getGetMethod());
        } else if ("localTime".equals(field.getName())) {
            assertEquals("java.time.LocalTime", field.getClassName());
            assertEquals(FieldType.TIME, field.getFieldType());
            assertEquals("getLocalTime", field.getGetMethod());
        } else if ("localDateTime".equals(field.getName())) {
            assertEquals("java.time.LocalDateTime", field.getClassName());
            assertEquals(FieldType.DATE_TIME, field.getFieldType());
            assertEquals("getLocalDateTime", field.getGetMethod());
        } else if ("zonedDateTime".equals(field.getName())) {
            assertEquals("java.time.ZonedDateTime", field.getClassName());
            assertEquals(FieldType.DATE_TIME_TZ, field.getFieldType());
            assertEquals("getZonedDateTime", field.getGetMethod());
        } else if ("calendar".equals(field.getName())) {
            assertEquals("java.util.Calendar", field.getClassName());
            assertEquals(FieldType.DATE_TIME_TZ, field.getFieldType());
            assertEquals("getCalendar", field.getGetMethod());
        } else if ("date".equals(field.getName())) {
            assertEquals("java.util.Date", field.getClassName());
            assertEquals(FieldType.DATE_TIME, field.getFieldType());
            assertEquals("getDate", field.getGetMethod());
        } else if ("gregorianCalendar".equals(field.getName())) {
            assertEquals("java.util.GregorianCalendar", field.getClassName());
            assertEquals(FieldType.DATE_TIME_TZ, field.getFieldType());
            assertEquals("getGregorianCalendar", field.getGetMethod());
        } else if ("sqlDate".equals(field.getName())) {
            assertEquals("java.sql.Date", field.getClassName());
            assertEquals(FieldType.DATE, field.getFieldType());
            assertEquals("getSqlDate", field.getGetMethod());
        } else if ("sqlTime".equals(field.getName())) {
            assertEquals("java.sql.Time", field.getClassName());
            assertEquals(FieldType.TIME, field.getFieldType());
            assertEquals("getSqlTime", field.getGetMethod());
        } else if ("sqlTimestamp".equals(field.getName())) {
            assertEquals("java.sql.Timestamp", field.getClassName());
            assertEquals(FieldType.DATE_TIME, field.getFieldType());
            assertEquals("getSqlTimestamp", field.getGetMethod());
        } else {
            fail("Unsupported field was detected: " + field);
        }
    }
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) Test(org.junit.Test)

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