mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 19:58:00 +00:00
Cache trigger constructor instead of looking it up each time createTrigger is called. This was showing up in profiles of simulation code.
This commit is contained in:
@@ -71,9 +71,25 @@ public enum TriggerType {
|
|||||||
Vote(TriggerVote.class);
|
Vote(TriggerVote.class);
|
||||||
|
|
||||||
private final Class<? extends Trigger> classTrigger;
|
private final Class<? extends Trigger> classTrigger;
|
||||||
|
private final Constructor<? extends Trigger> constructor;
|
||||||
|
|
||||||
private TriggerType(Class<? extends Trigger> clasz) {
|
private TriggerType(Class<? extends Trigger> clasz) {
|
||||||
classTrigger = clasz;
|
classTrigger = clasz;
|
||||||
|
constructor = findConstructor(clasz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Constructor<? extends Trigger> findConstructor(Class<? extends Trigger> clasz) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Constructor<? extends Trigger>[] cc = (Constructor<? extends Trigger>[]) clasz.getDeclaredConstructors();
|
||||||
|
for (Constructor<? extends Trigger> c : cc) {
|
||||||
|
Class<?>[] pp = c.getParameterTypes();
|
||||||
|
if (pp[0].isAssignableFrom(Map.class)) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new RuntimeException("No constructor found that would take Map as 1st parameter in class " + clasz.getName());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: Write javadoc for this method.
|
* TODO: Write javadoc for this method.
|
||||||
* @param string
|
* @param string
|
||||||
@@ -109,21 +125,12 @@ public enum TriggerType {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Trigger createTrigger(Map<String, String> mapParams, Card host, boolean intrinsic) {
|
public Trigger createTrigger(Map<String, String> mapParams, Card host, boolean intrinsic) {
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Constructor<? extends Trigger>[] cc = (Constructor<? extends Trigger>[]) classTrigger.getDeclaredConstructors();
|
|
||||||
for (Constructor<? extends Trigger> c : cc) {
|
|
||||||
Class<?>[] pp = c.getParameterTypes();
|
|
||||||
if (pp[0].isAssignableFrom(Map.class)) {
|
|
||||||
try {
|
try {
|
||||||
Trigger res = c.newInstance(mapParams, host, intrinsic);
|
Trigger res = constructor.newInstance(mapParams, host, intrinsic);
|
||||||
res.setMode(this);
|
res.setMode(this);
|
||||||
return res;
|
return res;
|
||||||
} catch (IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
} catch (IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||||
// TODO Auto-generated catch block ignores the exception, but sends it to System.err and probably forge.log.
|
throw new RuntimeException(e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
throw new RuntimeException("No constructor found that would take Map as 1st parameter in class " + classTrigger.getName());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user