it.auties.protobuf.serialization.instrumentation.ProtobufInstrumentationVisitor Maven / Gradle / Ivy
package it.auties.protobuf.serialization.instrumentation;
import it.auties.protobuf.serialization.message.ProtobufMessageElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public abstract class ProtobufInstrumentationVisitor {
protected final ProtobufMessageElement message;
protected final PrintWriter writer;
protected ProtobufInstrumentationVisitor(ProtobufMessageElement message, PrintWriter writer) {
this.message = message;
this.writer = writer;
}
public void instrument() {
if (!shouldInstrument()) {
return;
}
writer.print(" ");
writer.print(String.join(" ", modifiers()));
writer.print(" ");
writer.print(returnType());
writer.print(" ");
writer.print(name());
writer.print("(");
var parametersTypes = parametersTypes();
var parametersNames = parametersNames();
if(parametersTypes.size() != parametersNames.size()) {
throw new IllegalArgumentException("Parameters mismatch");
}
var parametersTypesIterator = parametersTypes.iterator();
var parametersNamesIterator = parametersNames.iterator();
var parameters = new ArrayList();
while (parametersTypesIterator.hasNext()) {
parameters.add("%s %s".formatted(parametersTypesIterator.next(), parametersNamesIterator.next()));
}
writer.print(String.join(", ", parameters));
writer.print(") {\n");
doInstrumentation();
writer.println(" }\n");
}
public abstract boolean shouldInstrument();
protected abstract void doInstrumentation();
protected abstract List modifiers();
protected abstract String returnType();
protected abstract String name();
protected abstract List parametersTypes();
protected abstract List parametersNames();
protected String getSpecName(TypeMirror typeMirror) {
var declaredType = (DeclaredType) typeMirror;
var element = (TypeElement) declaredType.asElement();
var parent = element.getEnclosingElement();
String packageName = null;
var name = new StringBuilder();
while (parent != null) {
if(parent instanceof TypeElement typeElement) {
name.append(typeElement.getSimpleName());
}else if(parent instanceof PackageElement packageElement) {
packageName = packageElement.getQualifiedName().toString();
break;
}
parent = parent.getEnclosingElement();
}
name.append(declaredType.asElement().getSimpleName());
var result = new StringBuilder();
if(packageName != null) {
result.append(packageName);
result.append(".");
}
result.append(name);
result.append("Spec");
return result.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy