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

xy.reflect.ui.info.method.ParameterizedFieldsMethodInfo Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (C) 2018 OTK Software
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * The GNU General Public License allows you also to freely redistribute 
 * the libraries under the same license, if you provide the terms of the 
 * GNU General Public License with them and add the following 
 * copyright notice at the appropriate place (with a link to 
 * http://javacollection.net/reflectionui/ web site when possible).
 ******************************************************************************/
package xy.reflect.ui.info.method;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import xy.reflect.ui.ReflectionUI;
import xy.reflect.ui.info.field.IFieldInfo;
import xy.reflect.ui.info.parameter.FieldAsParameterInfo;
import xy.reflect.ui.info.parameter.IParameterInfo;
import xy.reflect.ui.info.type.ITypeInfo;
import xy.reflect.ui.undo.IrreversibleModificationException;
import xy.reflect.ui.util.FututreActionBuilder;
import xy.reflect.ui.util.ReflectionUIError;
import xy.reflect.ui.util.ReflectionUIUtils;

public class ParameterizedFieldsMethodInfo extends MethodInfoProxy {

	protected ReflectionUI reflectionUI;
	protected List parameterizedFields;
	protected List generatedParameters;
	protected ITypeInfo containingType;
	protected FututreActionBuilder undoJobBuilder;

	public ParameterizedFieldsMethodInfo(ReflectionUI reflectionUI, IMethodInfo method,
			List parameterizedFields, ITypeInfo containingType) {
		super(method);
		this.reflectionUI = reflectionUI;
		this.parameterizedFields = parameterizedFields;
		this.containingType = containingType;
		this.generatedParameters = generateParameters();
	}

	protected List generateParameters() {
		List result = new ArrayList();
		int startPosition = super.getParameters().size();
		for (int i = 0; i < parameterizedFields.size(); i++) {
			final IFieldInfo field = parameterizedFields.get(i);
			final int position = i + startPosition;
			result.add(new FieldAsParameterInfo(reflectionUI, field, position) {

				@Override
				public boolean isHidden() {
					return false;
				}
				
			});
		}
		return result;
	}

	@Override
	public List getParameters() {
		List result = new ArrayList(super.getParameters());
		result.addAll(generatedParameters);
		return result;
	}

	@Override
	public Object invoke(Object object, InvocationData invocationData) {
		for (FieldAsParameterInfo generatedParameter : generatedParameters) {
			Object value = invocationData.getParameterValue(generatedParameter.getPosition());
			invocationData.withdrawParameterValue(generatedParameter.getPosition());
			if (undoJobBuilder != null) {
				undoJobBuilder.setOption(getUndoJobName(generatedParameter),
						ReflectionUIUtils.getUndoJob(object, generatedParameter.getSourceField(), value));
			}
			generatedParameter.getSourceField().setValue(object, value);
		}
		try {
			super.validateParameters(object, invocationData);
		} catch (Exception e) {
			throw new ReflectionUIError(e);
		}
		if (undoJobBuilder != null) {
			undoJobBuilder.setOption(getBaseMethodUndoJobName(),
					super.getNextInvocationUndoJob(object, invocationData));
			undoJobBuilder.build();
			undoJobBuilder = null;
		}
		return super.invoke(object, invocationData);
	}

	protected String getBaseMethodUndoJobName() {
		return "baseMethodUndoJob";
	}

	protected String getUndoJobName(FieldAsParameterInfo generatedParameter) {
		return "field" + generatedParameter.getPosition() + "UndoJob";
	}

	@Override
	public Runnable getNextInvocationUndoJob(Object object, InvocationData invocationData) {
		undoJobBuilder = new FututreActionBuilder();
		return undoJobBuilder.will(new FututreActionBuilder.FuturePerformance() {
			@Override
			public void perform(Map options) {
				Runnable baseMethodUndoJob = (Runnable) options.get(getBaseMethodUndoJobName());
				if (baseMethodUndoJob == null) {
					throw new IrreversibleModificationException();
				}
				for (int i = generatedParameters.size() - 1; i >= 0; i--) {
					FieldAsParameterInfo generatedParameter = generatedParameters.get(i);
					Runnable fieldUndoJob = (Runnable) options.get(getUndoJobName(generatedParameter));
					fieldUndoJob.run();
				}
			}
		});
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((containingType == null) ? 0 : containingType.hashCode());
		result = prime * result + ((parameterizedFields == null) ? 0 : parameterizedFields.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParameterizedFieldsMethodInfo other = (ParameterizedFieldsMethodInfo) obj;
		if (containingType == null) {
			if (other.containingType != null)
				return false;
		} else if (!containingType.equals(other.containingType))
			return false;
		if (parameterizedFields == null) {
			if (other.parameterizedFields != null)
				return false;
		} else if (!parameterizedFields.equals(other.parameterizedFields))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ParameterizedFieldsMethodInfo [parameterizedFields=" + parameterizedFields + ", containingType="
				+ containingType + ", base=" + base + "]";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy