All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.projectmonkey.object.mapper.analysis.resolver.JavaBeanBasedPropertyResolver Maven / Gradle / Ivy

Go to download

Object mapping implementation written as an alternative to modelmapper which is able to support inheritance, handles flattening / expanding in a precise way, and is extensible / configurable

The newest version!
package net.projectmonkey.object.mapper.analysis.resolver;

import net.projectmonkey.object.mapper.context.ExecutionContext;
import net.projectmonkey.object.mapper.util.Logger;
import org.apache.commons.lang3.StringUtils;
import net.projectmonkey.object.mapper.analysis.result.JavaBeanBasedPropertyPathElement;
import net.projectmonkey.object.mapper.analysis.result.PropertyPath;
import net.projectmonkey.object.mapper.analysis.result.PropertyPathElement;
import net.projectmonkey.object.mapper.util.ReflectionUtil;
import net.projectmonkey.object.mapper.util.Types;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;

/*
 *
 *  * Copyright 2012 the original author or authors.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

/**
 * @author Andy Moody
 */
public final class JavaBeanBasedPropertyResolver implements PropertyResolver
{

	public static final JavaBeanBasedPropertyResolver INSTANCE = new JavaBeanBasedPropertyResolver();
	private static final Logger logger = Logger.getLogger(JavaBeanBasedPropertyResolver.class);

	private JavaBeanBasedPropertyResolver(){}

	@Override
	public List resolvePropertyPaths(Class clazz)
	{

		return getPropertyPaths(null, clazz);
	}

	private List getPropertyPaths(final PropertyPath parent, final Class clazz)
	{
		List propertyPaths = ExecutionContext.getPaths(clazz);
		if(propertyPaths == null)
		{
			propertyPaths = new ArrayList();
			ExecutionContext.registerPaths(clazz, propertyPaths); //register as soon as we create the paths list.

			if(!Types.isKnownType(clazz))
			{
				Map methodPairs = retrieveJavaBeanStylePairs(clazz);
				for (Map.Entry entry : methodPairs.entrySet())
				{
					String propertyName = entry.getKey();
					MethodPair methods = entry.getValue();
					if(methods.accessor == null || methods.mutator == null)
					{
						logger.warn("Ignoring single accessor / mutator for property "+propertyName+" on class "+clazz);
					}
					else
					{
						propertyPaths.add(resolvePath(clazz, parent, methods));
					}
				}
			}
		}
		return propertyPaths;
	}

	private Map retrieveJavaBeanStylePairs(final Class clazz)
	{
		Method[] declaredMethods = ReflectionUtil.getAllMethods(clazz);

		Map methodPairs = new HashMap();
		for(Method method : declaredMethods)
		{
			int modifiers = method.getModifiers();
			if(!Modifier.isStatic(modifiers) && !Modifier.isAbstract(modifiers))
			{
				String methodName = method.getName();
				boolean accessor = methodName.startsWith("get") || methodName.startsWith("is");
				boolean mutator = methodName.startsWith("set");
				if(accessor || mutator)
				{
					String propertyName = translate(methodName, accessor);
					if(!methodPairs.containsKey(propertyName))
					{
						methodPairs.put(propertyName, new MethodPair(propertyName));
					}
					MethodPair pair = methodPairs.get(propertyName);
					if(accessor)
					{
						if(pair.accessor != null)
						{
							throw new IllegalStateException("Duplicate accessor methods ["+methodName+", "+pair.accessor.getName());
						}
						pair.accessor = method;
					}
					else
					{
						if(pair.mutator != null)
						{
							throw new IllegalStateException("Duplicate mutator methods ["+methodName+", "+pair.mutator.getName());
						}
						pair.mutator = method;
					}
				}
			}
		}
		return methodPairs;
	}

	private String translate(final String name, final boolean accessor)
	{
		String toReturn;
		if(accessor && name.startsWith("is"))
		{
			toReturn = name.substring(2);
		}
		else
		{
			toReturn = name.substring(3);
		}
		return StringUtils.uncapitalize(toReturn);
	}

	private PropertyPath resolvePath(final Class clazz, final PropertyPath parent, final MethodPair methods)
	{
		Method accessor = methods.accessor;
		PropertyPathElement property = new JavaBeanBasedPropertyPathElement(clazz, methods.propertyName, accessor, methods.mutator);
		PropertyPath toReturn = new PropertyPath(parent, property);

		Class propertyType = property.getType();
		//This is not a type that we know how to convert by default - so we need to map it's internals too.
		if(!Types.isKnownType(propertyType))
		{
			List childPaths = getPropertyPaths(toReturn, propertyType);
			toReturn.setChildren(childPaths);
		}
		return toReturn;
	}

	private static class MethodPair {
		private Method accessor;
		private Method mutator;
		private final String propertyName;

		public MethodPair(final String propertyName)
		{
			this.propertyName = propertyName;
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy