use of io.atlasmap.java.v2.JavaClass 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;
}
use of io.atlasmap.java.v2.JavaClass 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;
}
use of io.atlasmap.java.v2.JavaClass 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;
}
use of io.atlasmap.java.v2.JavaClass 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()));
}
use of io.atlasmap.java.v2.JavaClass in project atlasmap by atlasmap.
the class ClassValidationUtil method validateFlatPrimitiveClass.
public static void validateFlatPrimitiveClass(ClassInspectionService classInspectionService, Class<?> clazz, String className) {
JavaClass flatClass = classInspectionService.inspectClass(clazz);
validateFlatClass(flatClass);
assertEquals(null, flatClass.getCollectionType());
assertEquals(null, flatClass.getArrayDimensions());
assertEquals(null, flatClass.getArraySize());
assertFalse(flatClass.isInterface());
assertEquals(className, flatClass.getClassName());
validateFlatPrimitiveFields(flatClass);
}
Aggregations