Search in sources :

Example 1 with ModifierList

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

the class ClassInspectionService method inspectClass.

private void inspectClass(ClassLoader classLoader, Class<?> clazz, JavaClass javaClass, Set<String> cachedClasses, String pathPrefix) {
    Class<?> clz = clazz;
    if (clazz.isArray()) {
        javaClass.setArrayDimensions(detectArrayDimensions(clazz));
        javaClass.setCollectionType(CollectionType.ARRAY);
        clz = detectArrayClass(clazz);
    } else {
        clz = clazz;
    }
    if (isMapList(clz.getCanonicalName())) {
        javaClass.setCollectionType(CollectionType.MAP);
    }
    javaClass.setClassName(clz.getCanonicalName());
    javaClass.setPackageName((clz.getPackage() != null ? clz.getPackage().getName() : null));
    javaClass.setAnnotation(clz.isAnnotation());
    javaClass.setAnnonymous(clz.isAnonymousClass());
    javaClass.setEnumeration(clz.isEnum());
    javaClass.setInterface(clz.isInterface());
    javaClass.setLocalClass(clz.isLocalClass());
    javaClass.setMemberClass(clz.isMemberClass());
    javaClass.setPrimitive(clz.isPrimitive());
    javaClass.setSynthetic(clz.isSynthetic());
    if (javaClass.getUri() == null) {
        javaClass.setUri(String.format(AtlasJavaModelFactory.URI_FORMAT, clz.getCanonicalName()));
    }
    if (clz.isPrimitive() || JdkPackages.contains(clz.getPackage().getName())) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Skipping class " + clz.getName() + " which is a Jdk core class");
        }
        return;
    }
    // Process super class fields and methods first, so child class fields
    // and methods override
    Class<?> tmpClazz = clz;
    Class<?> superClazz = tmpClazz.getSuperclass();
    while (superClazz != null) {
        if (JdkPackages.contains(superClazz.getPackage().getName())) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Skipping SuperClass " + superClazz.getName() + " which is a Jdk core class");
            }
            superClazz = null;
        } else {
            inspectClassFields(classLoader, superClazz, javaClass, cachedClasses, pathPrefix);
            inspectClassMethods(classLoader, superClazz, javaClass, cachedClasses, pathPrefix);
            tmpClazz = superClazz;
            superClazz = tmpClazz.getSuperclass();
        }
    }
    inspectClassFields(classLoader, clz, javaClass, cachedClasses, pathPrefix);
    Object[] enumConstants = clz.getEnumConstants();
    if (enumConstants != null) {
        javaClass.setEnumeration(true);
        for (Object o : enumConstants) {
            JavaEnumField out = new JavaEnumField();
            if (o instanceof Enum) {
                Enum<?> in = (Enum<?>) o;
                out.setName(in.name());
                out.setOrdinal(in.ordinal());
                javaClass.getJavaEnumFields().getJavaEnumField().add(out);
                out.setStatus(FieldStatus.SUPPORTED);
            } else {
                out.setClassName(o.getClass().getCanonicalName());
                out.setStatus(FieldStatus.ERROR);
            }
        }
    } else {
        javaClass.setEnumeration(false);
    }
    inspectClassMethods(classLoader, clz, javaClass, cachedClasses, pathPrefix);
    if (javaClass.getModifiers() == null) {
        javaClass.setModifiers(new ModifierList());
    } else {
        javaClass.getModifiers().getModifier().clear();
    }
    javaClass.getModifiers().getModifier().addAll(detectModifiers(clz.getModifiers()));
// TODO: annotations, generics, enums, class modifiers (public,
// synchronized, etc),
// more of these here:
// https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isPrimitive--
// TODO: exceptions
// TODO: lists
// return javaClass;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) ModifierList(io.atlasmap.java.v2.ModifierList)

Example 2 with ModifierList

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

Aggregations

ModifierList (io.atlasmap.java.v2.ModifierList)2 JavaClass (io.atlasmap.java.v2.JavaClass)1 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)1 JavaField (io.atlasmap.java.v2.JavaField)1 StringList (io.atlasmap.v2.StringList)1 Annotation (java.lang.annotation.Annotation)1