aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java
diff options
context:
space:
mode:
author zaaarf <zaaarf@proton.me>2023-03-18 17:24:43 +0100
committer zaaarf <zaaarf@proton.me>2023-03-18 17:24:43 +0100
commit344e66061b31a83f7ace2ab887e80f782f560297 (patch)
treeb8841cdf15a97376cc75206818a989b0e1918e36 /src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java
parent8695612c58141b1d5e0ee274027ebbd2050de6f8 (diff)
parent909f5cfa07464f35814da1686b0ac1a6c3ea03dd (diff)
Merge branch 'version3' into dev
Diffstat (limited to 'src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java')
-rw-r--r--src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java b/src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java
new file mode 100644
index 0000000..18f9ed6
--- /dev/null
+++ b/src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java
@@ -0,0 +1,110 @@
+package ftbsc.lll.processor.tools;
+
+import com.squareup.javapoet.*;
+import ftbsc.lll.tools.DescriptorBuilder;
+
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.util.Elements;
+import java.lang.annotation.Annotation;
+import java.util.List;
+import java.util.function.Function;
+
+import static ftbsc.lll.processor.tools.ASTUtils.classArrayFromAnnotation;
+
+/**
+ * Collection of static utils that rely on JavaPoet to function.
+ */
+public class JavaPoetUtils {
+ /**
+ * Builds a {@link MethodSpec} for a public method whose body simply returns a {@link String}.
+ * @param name the name of the method
+ * @param returnString the {@link String} to return
+ * @return the built {@link MethodSpec}
+ */
+ public static MethodSpec buildStringReturnMethod(String name, String returnString) {
+ return MethodSpec.methodBuilder(name)
+ .addModifiers(Modifier.PUBLIC)
+ .addAnnotation(Override.class)
+ .returns(String.class)
+ .addStatement("return $S", returnString)
+ .build();
+ }
+
+ /**
+ * Builds a type descriptor from the given {@link TypeName}.
+ * @param type the {@link TypeName} representing the desired type
+ * @return a {@link String} containing the relevant descriptor
+ */
+ public static String descriptorFromType(TypeName type) {
+ StringBuilder desc = new StringBuilder();
+ //add array brackets
+ while(type instanceof ArrayTypeName) {
+ desc.append("[");
+ type = ((ArrayTypeName) type).componentType;
+ }
+ if(type instanceof ClassName || type instanceof ParameterizedTypeName) {
+ ClassName var = type instanceof ParameterizedTypeName ? ((ParameterizedTypeName) type).rawType : (ClassName) type;
+ desc.append(DescriptorBuilder.nameToDescriptor(var.canonicalName(), 0));
+ } else {
+ if(TypeName.BOOLEAN.equals(type))
+ desc.append("Z");
+ else if(TypeName.CHAR.equals(type))
+ desc.append("C");
+ else if(TypeName.BYTE.equals(type))
+ desc.append("B");
+ else if(TypeName.SHORT.equals(type))
+ desc.append("S");
+ else if(TypeName.INT.equals(type))
+ desc.append("I");
+ else if(TypeName.FLOAT.equals(type))
+ desc.append("F");
+ else if(TypeName.LONG.equals(type))
+ desc.append("J");
+ else if(TypeName.DOUBLE.equals(type))
+ desc.append("D");
+ else if(TypeName.VOID.equals(type))
+ desc.append("V");
+ }
+ return desc.toString();
+ }
+
+ /**
+ * Builds a type descriptor from the given {@link TypeMirror}.
+ * @param t the {@link TypeMirror} representing the desired type
+ * @return a {@link String} containing the relevant descriptor
+ */
+ public static String descriptorFromType(TypeMirror t) {
+ return descriptorFromType(TypeName.get(t));
+ }
+
+ /**
+ * Builds a method descriptor from the given {@link ExecutableElement}.
+ * @param m the {@link ExecutableElement} for the method
+ * @return a {@link String} containing the relevant descriptor
+ */
+ public static String descriptorFromExecutableElement(ExecutableElement m) {
+ StringBuilder methodSignature = new StringBuilder();
+ methodSignature.append("(");
+ m.getParameters().forEach(p -> methodSignature.append(descriptorFromType(p.asType())));
+ methodSignature.append(")");
+ methodSignature.append(descriptorFromType(m.getReturnType()));
+ return methodSignature.toString();
+ }
+
+ /**
+ * Builds a (partial, not including the return type) method descriptor from its parameters
+ * @param ann the annotation containing the class
+ * @param fun the annotation function returning the class
+ * @return the method descriptor
+ */
+ public static <T extends Annotation> String methodDescriptorFromParams(T ann, Function<T, Class<?>[]> fun, Elements elementUtils) {
+ List<TypeMirror> mirrors = classArrayFromAnnotation(ann, fun, elementUtils);
+ StringBuilder sb = new StringBuilder("(");
+ for(TypeMirror t : mirrors)
+ sb.append(descriptorFromType(t));
+ sb.append(")");
+ return sb.toString();
+ }
+}