package lombok.core;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class AgentLauncher {
public interface AgentLaunchable {
void runAgent(String agentArgs, Instrumentation instrumentation, boolean injected, Class<?> launchingContext) throws Exception;
}
public static void runAgents(String agentArgs, Instrumentation instrumentation, boolean injected, Class<?> launchingContext) throws Throwable {
for (AgentInfo info : AGENTS) {
try {
Class<?> agentClass = Class.forName(info.className());
AgentLaunchable agent = (AgentLaunchable) agentClass.getConstructor().newInstance();
agent.runAgent(agentArgs, instrumentation, injected, launchingContext);
} catch (Throwable t) {
if (t instanceof InvocationTargetException) t = t.getCause();
info.problem(t, instrumentation);
}
}
}
private static final List<AgentInfo> AGENTS = Collections.unmodifiableList(Arrays.<AgentInfo>asList(
new EclipsePatcherInfo()
));
private static abstract class AgentInfo {
abstract String className();
void problem(Throwable t, Instrumentation instrumentation) throws Throwable {
if (t instanceof ClassNotFoundException) {
return;
}
if (t instanceof ClassCastException) {
throw new InternalError("Lombok bug. Class: " + className() + " is not an implementation of lombok.core.Agent");
}
if (t instanceof IllegalAccessError) {
throw new InternalError("Lombok bug. Class: " + className() + " is not public");
}
if (t instanceof InstantiationException) {
throw new InternalError("Lombok bug. Class: " + className() + " is not concrete or has no public no-args constructor");
}
throw t;
}
}
private static class EclipsePatcherInfo extends AgentInfo {
@Override String className() {
return "lombok.eclipse.agent.EclipsePatcher";
}
}
}