tech.harmonysoft.oss.traute.javac.text.ExceptionTextGeneratorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of traute-javac Show documentation
Show all versions of traute-javac Show documentation
A Javac plugin which inserts null-checks into generated bytecode
package tech.harmonysoft.oss.traute.javac.text;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class ExceptionTextGeneratorImpl implements ExceptionTextGenerator {
private final List> generators = new ArrayList<>();
@NotNull private final Class contextClass;
public ExceptionTextGeneratorImpl(@NotNull Class contextClass, @NotNull List> generators) {
this.contextClass = contextClass;
this.generators.addAll(generators);
}
@SuppressWarnings("unchecked")
@NotNull
@Override
public String generate(@NotNull Object context) {
if (!contextClass.isInstance(context)) {
throw new IllegalStateException(String.format(
"Can't generate exception text - expected to get a context of type %s but got %s",
contextClass.getName(), context.getClass().getName()
));
}
T typedContext = (T) context;
StringBuilder buffer = new StringBuilder();
for (Function generator : generators) {
buffer.append(generator.apply(typedContext));
}
return buffer.toString();
}
}