tech.harmonysoft.oss.traute.javac.text.ExceptionTextGeneratorFactory 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 org.jetbrains.annotations.Nullable;
import tech.harmonysoft.oss.traute.common.instrumentation.InstrumentationType;
import tech.harmonysoft.oss.traute.common.util.TrauteConstants;
import tech.harmonysoft.oss.traute.javac.instrumentation.parameter.ParameterToInstrumentInfo;
import tech.harmonysoft.oss.traute.javac.log.TrautePluginLogger;
import java.util.*;
import java.util.function.Function;
public class ExceptionTextGeneratorFactory {
private final Map> spis = new HashMap<>();
private final Map> contextClasses = new HashMap<>();
public ExceptionTextGeneratorFactory() {
spis.put(InstrumentationType.METHOD_PARAMETER, new ParameterCheckExceptionTextGeneratorSpi());
contextClasses.put(InstrumentationType.METHOD_PARAMETER, ParameterToInstrumentInfo.class);
}
@SuppressWarnings("unchecked")
@NotNull
public Optional> build(@NotNull InstrumentationType type,
@NotNull String pattern,
@Nullable TrautePluginLogger logger)
{
Class> contextClass = contextClasses.get(type);
if (contextClass == null) {
if (logger != null) {
logger.report(String.format(
"Unsupported instrumentation type %s - no context class for it is configured. "
+ "Known mappings: %s. Skipping custom exception text pattern", type, contextClasses
));
}
return Optional.empty();
}
int start = 0;
List> generators = new ArrayList<>();
while (start < pattern.length()) {
int end = pattern.indexOf("${", start);
if (end < 0) {
String snippet = pattern.substring(start);
if (!snippet.isEmpty()) {
generators.add(c -> snippet);
}
break;
}
if (end > start) {
String snippet = pattern.substring(start, end);
generators.add(c -> snippet);
}
start = end + 2;
end = pattern.indexOf("}");
if (end < 0) {
if (logger != null) {
logger.report(String.format(
"Invalid custom error text pattern for type '%s' - '%s'. It contains unclosed "
+ "variable - '${'. Skipping custom exception text pattern", type.getShortName(), pattern
));
}
return Optional.empty();
}
Function