use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.
the class DocumentJavaFieldReader method resolveGetMethod.
private Method resolveGetMethod(Object sourceObject, Field field) throws AtlasException {
Object parentObject = sourceObject;
AtlasPath atlasPath = new AtlasPath(field.getPath());
Method getter = null;
if (atlasPath.hasParent()) {
parentObject = ClassHelper.parentObjectForPath(sourceObject, atlasPath, true);
}
if (parentObject == null) {
return null;
}
List<Class<?>> classTree = resolveMappableClasses(parentObject.getClass());
for (Class<?> clazz : classTree) {
try {
if (field instanceof JavaField && ((JavaField) field).getGetMethod() != null) {
getter = clazz.getMethod(((JavaField) field).getGetMethod());
getter.setAccessible(true);
return getter;
}
} catch (NoSuchMethodException e) {
// no getter method specified in mapping file
}
for (String m : Arrays.asList("get", "is")) {
String cleanedLastSegment = AtlasPath.cleanPathSegment(atlasPath.getLastSegment());
String getterMethod = m + capitalizeFirstLetter(cleanedLastSegment);
try {
getter = clazz.getMethod(getterMethod);
getter.setAccessible(true);
return getter;
} catch (NoSuchMethodException e) {
// method does not exist
}
}
}
return null;
}
use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.
the class DocumentJavaFieldWriter method getClassForField.
private Class<?> getClassForField(Field field, SegmentContext segmentContext, Object parentObject, boolean unwrapCollectionType) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Looking up class to use for segment: " + segmentContext + "\n\tparentObject: " + parentObject);
}
Class<?> clz = null;
if (LOG.isDebugEnabled()) {
LOG.debug("Looking for configured class for field: " + field + ".");
}
String className = null;
if (field instanceof JavaField) {
className = ((JavaField) field).getClassName();
} else if (field instanceof JavaEnumField) {
className = ((JavaEnumField) field).getClassName();
}
if (className != null) {
try {
clz = className == null ? null : Class.forName(className);
} catch (Exception e) {
throw new AtlasException("Could not find class for '" + className + "', for segment: " + segmentContext + ", on field: " + field, e);
}
}
if (clz == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't find class on field. Looking for configured class for segment: " + segmentContext + ".");
}
String normalizedSegment = AtlasPath.removeCollectionIndexes(segmentContext.getSegmentPath());
clz = this.classesForFields.get(normalizedSegment);
}
Type clzType = null;
if (clz == null) {
// attempt to determine it from the parent object.
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't find configured class for segment: " + segmentContext + ", looking up getter method.");
}
Method m = null;
try {
String methodName = "get" + JavaWriterUtil.capitalizeFirstLetter(AtlasPath.cleanPathSegment(segmentContext.getSegment()));
m = ClassHelper.detectGetterMethod(parentObject.getClass(), methodName);
} catch (NoSuchMethodException e) {
// it's ok, we didnt find a getter.
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't find getter method for segment: " + segmentContext, e);
}
}
clz = m == null ? null : m.getReturnType();
clzType = m.getGenericReturnType();
}
if (clz == null) {
throw new AtlasException("Could not create object, can't find class to instantiate for segment: " + segmentContext);
}
if (unwrapCollectionType) {
clz = unwrapCollectionType(field, segmentContext, parentObject, clz, clzType);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Found class '" + clz.getName() + "' to use for segment: " + segmentContext);
}
return clz;
}
use of io.atlasmap.java.v2.JavaField 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;
}
use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.
the class BaseDocumentWriterTest method createField.
public JavaField createField(String path, Object value, FieldType fieldType) {
JavaField field = new JavaField();
field.setFieldType(fieldType);
field.setValue(value);
field.setPath(path);
return field;
}
use of io.atlasmap.java.v2.JavaField 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;
}
Aggregations