Search in sources :

Example 11 with JavaClass

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

the class MapTest method testInspectJavaMapRoot.

@Test
public void testInspectJavaMapRoot() {
    JavaClass c = classInspectionService.inspectClass(Map.class);
    assertEquals("java.util.Map", c.getClassName());
    assertEquals(c.getCollectionType().value(), "Map");
}
Also used : JavaClass(io.atlasmap.java.v2.JavaClass) Test(org.junit.Test)

Example 12 with JavaClass

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

the class MapTest method testInspectJavaMapField.

@Test
public void testInspectJavaMapField() {
    JavaClass c = classInspectionService.inspectClass(TestMapOrders.class);
    assertNotNull(c);
    assertEquals("io.atlasmap.java.test.TestMapOrders", c.getClassName());
    assertNotNull(c.getJavaFields().getJavaField());
    assertEquals(c.getJavaFields().getJavaField().size(), 1);
    JavaField field = c.getJavaFields().getJavaField().get(0);
    assertEquals(field.getCollectionType().value(), "Map");
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) Test(org.junit.Test)

Example 13 with JavaClass

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

the class TestListOrdersTest method testInspectJavaList.

@Test
public void testInspectJavaList() {
    JavaClass c = classInspectionService.inspectClass(TestListOrders.class);
    assertNotNull(c);
    assertNull(c.getAnnotations());
    assertNull(c.getArrayDimensions());
    assertEquals("io.atlasmap.java.test.TestListOrders", c.getClassName());
    assertNull(c.getCollectionClassName());
    assertNull(c.getCollectionType());
    assertNull(c.getGetMethod());
    assertNotNull(c.getJavaEnumFields());
    assertNotNull(c.getJavaEnumFields().getJavaEnumField());
    assertEquals(Integer.valueOf(0), Integer.valueOf(c.getJavaEnumFields().getJavaEnumField().size()));
    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.TestListOrders"), c.getUri());
    assertNull(c.getValue());
    assertEquals(Integer.valueOf(2), new Integer(c.getJavaFields().getJavaField().size()));
    JavaField f1 = c.getJavaFields().getJavaField().get(0);
    assertNotNull(f1);
    assertTrue(f1 instanceof JavaClass);
    JavaClass c2 = (JavaClass) f1;
    assertNotNull(c2.getCollectionClassName());
    assertEquals("java.util.List", c2.getCollectionClassName());
    assertEquals(CollectionType.LIST, c2.getCollectionType());
    boolean foundAddress = false;
    boolean foundContact = false;
    boolean foundCreated = false;
    for (JavaField c2f : c2.getJavaFields().getJavaField()) {
        if (c2f instanceof JavaClass) {
            if ("io.atlasmap.java.test.BaseAddress".equals(((JavaClass) c2f).getClassName())) {
                ClassValidationUtil.validateSimpleTestAddress(((JavaClass) c2f));
                foundAddress = true;
            } else if ("io.atlasmap.java.test.BaseContact".equals(((JavaClass) c2f).getClassName())) {
                ClassValidationUtil.validateSimpleTestContact(((JavaClass) c2f));
                foundContact = true;
            } else if ("java.util.Date".equals(((JavaClass) c2f).getClassName())) {
                ClassValidationUtil.validateCreated(c2f);
                foundCreated = true;
            } else {
                fail("Unexpected class: " + ((JavaClass) c2f).getClassName());
            }
        } else if ("java.util.Date".equals(c2f.getClassName())) {
            ClassValidationUtil.validateCreated(c2f);
            foundCreated = true;
        }
    }
    assertTrue(foundAddress);
    assertTrue(foundContact);
    assertTrue(foundCreated);
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) Test(org.junit.Test)

Example 14 with JavaClass

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

use of io.atlasmap.java.v2.JavaClass 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

JavaClass (io.atlasmap.java.v2.JavaClass)47 JavaField (io.atlasmap.java.v2.JavaField)21 Test (org.junit.Test)18 ClassInspectionService (io.atlasmap.java.inspect.ClassInspectionService)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 TargetAddress (io.atlasmap.java.test.TargetAddress)2 TargetOrder (io.atlasmap.java.test.TargetOrder)2 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)2 ModifierList (io.atlasmap.java.v2.ModifierList)2 BaseMapping (io.atlasmap.v2.BaseMapping)2 Field (io.atlasmap.v2.Field)2 Method (java.lang.reflect.Method)2 ApplicationPath (javax.ws.rs.ApplicationPath)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 AtlasException (io.atlasmap.api.AtlasException)1 AtlasValidationException (io.atlasmap.api.AtlasValidationException)1 DocumentJavaFieldWriter (io.atlasmap.java.core.DocumentJavaFieldWriter)1