Search in sources :

Example 41 with JavaClass

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

the class ClassInspectionService method inspectClass.

private JavaClass inspectClass(ClassLoader classLoader, String className) {
    JavaClass d = null;
    Class<?> clazz = null;
    try {
        clazz = classLoader.loadClass(className);
        d = inspectClass(classLoader, clazz);
    } catch (ClassNotFoundException cnfe) {
        d = AtlasJavaModelFactory.createJavaClass();
        d.setClassName(className);
        d.setStatus(FieldStatus.NOT_FOUND);
    }
    return d;
}
Also used : JavaClass(io.atlasmap.java.v2.JavaClass)

Example 42 with JavaClass

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

the class ClassInspectionService method inspectClass.

public JavaClass inspectClass(ClassLoader classLoader, Class<?> clazz) {
    if (clazz == null) {
        throw new IllegalArgumentException("Class must be specified");
    }
    JavaClass javaClass = AtlasJavaModelFactory.createJavaClass();
    Set<String> cachedClasses = new HashSet<>();
    // we cache ourself
    cachedClasses.add(clazz.getCanonicalName());
    inspectClass(classLoader, clazz, javaClass, cachedClasses, null);
    return javaClass;
}
Also used : JavaClass(io.atlasmap.java.v2.JavaClass) HashSet(java.util.HashSet)

Example 43 with JavaClass

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

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

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

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