
net.alantea.viewml.VmlAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of viewml Show documentation
Show all versions of viewml Show documentation
XML view description system
The newest version!
package net.alantea.viewml;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.alantea.utils.exception.LntException;
import net.alantea.viewml.internal.Referencer;
/**
* The Class VmlAction.
*/
public class VmlAction
{
/** The name. */
private String name;
/** The method. */
private MethodInformation method;
/** The sub actions. */
private VmlAction[] subActions;
/** The arguments. */
// arguments
private Object[] arguments;
/**
* Instantiates a new vml action.
*
* @param method the method
* @param arguments the arguments
*/
public VmlAction(MethodInformation method, String[] arguments)
{
this.method = method;
this.arguments = arguments;
name = (method == null) ? "null" : method.getName();
}
/**
* Instantiates a new vml action.
*
* @param actions the actions
*/
public VmlAction(VmlAction...actions )
{
this.subActions = actions;
}
/**
* Invoke.
*/
public void invoke()
{
try
{
method.invoke(null);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
{
new LntException("Error invoking action", e);
}
}
/**
* Invoke.
*
* @param map the map
*/
public void invoke(Map map)
{
Map argsMap = map;
if (argsMap == null)
{
argsMap = new HashMap<>();
}
try
{
if (subActions != null)
{
for (VmlAction action : subActions)
{
action.invoke();
}
}
else if (method != null)
{
Object[] args = null;
if (arguments != null)
{
args = new Object[arguments.length];
List> types = method.getTypes();
for (int i = 0; i < arguments.length; i++)
{
if (arguments[i] instanceof String)
{
Object content = argsMap.get(arguments[i]);
try
{
args[i] = Referencer.getReference((String) arguments[i]);
}
catch (VmlException e)
{
}
if (args[i] == null)
{
if ((content != null) && (types.get(i).isAssignableFrom(content.getClass())))
{
args[i] = content;
}
else if (types.get(i).equals(String.class))
{
args[i] = arguments[i];
}
else if ((types.get(i).equals(Integer.class)) || (types.get(i).equals(Integer.TYPE)))
{
args[i] = Integer.parseInt((String) arguments[i]);
}
else if ((types.get(i).equals(Double.class)) || (types.get(i).equals(Double.TYPE)))
{
args[i] = Double.parseDouble((String) arguments[i]);
}
else if ((types.get(i).equals(Float.class)) || (types.get(i).equals(Float.TYPE)))
{
args[i] = Float.parseFloat((String) arguments[i]);
}
else if ((types.get(i).equals(Long.class)) || (types.get(i).equals(Long.TYPE)))
{
args[i] = Long.parseLong((String) arguments[i]);
}
else if ((types.get(i).equals(Boolean.class)) || (types.get(i).equals(Boolean.TYPE)))
{
args[i] = Boolean.parseBoolean((String) arguments[i]);
}
else
{
// TODO : nothing may be done at this point ?
}
}
}
else
{
args[i] = arguments[i];
}
}
}
// Manage the varargs case
int typesSize = method.getTypes().size();
boolean specCase = (args != null) && (typesSize > 0) && (method.getTypes().get(typesSize - 1)).isArray();
if ((args != null) && (method.getTypes().size() < args.length) || specCase)
{
Object[] realArgs = new Object[method.getTypes().size()];
Object[] vargs = (Object[]) Array.newInstance(method.getTypes().get(typesSize - 1).getComponentType(),
arguments.length - typesSize + 1);
for (int i = 0; i < method.getTypes().size() - 1; i++)
{
realArgs[i] = args[i];
}
for (int i = 0; i < arguments.length - typesSize + 1; i++)
{
Array.set(vargs, i, args[i + arguments.length - typesSize]);
}
realArgs[typesSize - 1] = vargs;
method.invoke(realArgs);
}
else
{
method.invoke(args);
}
}
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1)
{
System.err.println("invoke error on " + name + " : " + e1.getLocalizedMessage());
}
}
/**
* Gets the name.
*
* @return the name
*/
public String getName()
{
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name)
{
this.name = name;
}
/**
* To string.
*
* @return the string
*/
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return name;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy