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;
}
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;
}
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());
}
}
}
}
}
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;
}
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());
}
}
}
Aggregations