l2j_mobius/trunk/java/com/l2jmobius/gameserver/model/eventengine/EventMethodNotification.java
2016-06-12 01:34:09 +00:00

86 lines
2.3 KiB
Java

/*
* This file is part of the L2J Mobius project.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.model.eventengine;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
/**
* @author UnAfraid
*/
public class EventMethodNotification
{
private final AbstractEventManager<?> _manager;
private final Method _method;
private final Object[] _args;
/**
* @param manager
* @param methodName
* @param args
* @throws NoSuchMethodException
*/
public EventMethodNotification(AbstractEventManager<?> manager, String methodName, List<Object> args) throws NoSuchMethodException
{
_manager = manager;
_method = manager.getClass().getDeclaredMethod(methodName, args.stream().map(Object::getClass).toArray(Class[]::new));
_args = args.toArray();
}
public AbstractEventManager<?> getManager()
{
return _manager;
}
public Method getMethod()
{
return _method;
}
public void execute() throws Exception
{
if (Modifier.isStatic(_method.getModifiers()))
{
invoke(null);
}
else
{
// Attempt to find getInstance() method
for (Method method : _manager.getClass().getMethods())
{
if (Modifier.isStatic(method.getModifiers()) && (_manager.getClass().isAssignableFrom(method.getReturnType())) && (method.getParameterCount() == 0))
{
final Object instance = method.invoke(null);
invoke(instance);
}
}
}
}
private void invoke(Object instance) throws Exception
{
final boolean wasAccessible = _method.isAccessible();
if (!wasAccessible)
{
_method.setAccessible(true);
}
_method.invoke(instance, _args);
_method.setAccessible(wasAccessible);
}
}