From 17baef2ea8c9616bb0eeea510574ef2c30c25080 Mon Sep 17 00:00:00 2001 From: gnanaprakash-ravi Date: Thu, 25 Jan 2024 17:55:54 +0530 Subject: [PATCH] updated the setter functions in annotations --- .../java/zingg/common/client/pipe/Pipe.java | 6 +- .../py/processors/ProcessorContext.java | 22 ++++++ .../py/processors/PythonClassProcessor.java | 79 +++++++++++++------ .../py/processors/PythonMethodProcessor.java | 56 +++++++++---- 4 files changed, 124 insertions(+), 39 deletions(-) create mode 100644 common/py/src/main/java/zingg/common/py/processors/ProcessorContext.java diff --git a/common/client/src/main/java/zingg/common/client/pipe/Pipe.java b/common/client/src/main/java/zingg/common/client/pipe/Pipe.java index 99e80c9cc..aab0878b1 100644 --- a/common/client/src/main/java/zingg/common/client/pipe/Pipe.java +++ b/common/client/src/main/java/zingg/common/client/pipe/Pipe.java @@ -55,7 +55,7 @@ public String getSchema() { return schema; } - + @PythonMethod public void setSchema(String schema) { this.schema = schema; } @@ -71,10 +71,12 @@ public void setName(String name) { this.name = name; } + @PythonMethod public String getFormat() { return format; } + @PythonMethod @JsonValue public void setFormat(String sinkType) { this.format = sinkType; @@ -90,6 +92,7 @@ public Map getProps() { return props; } + @PythonMethod public void setProp(String k, String v) { if (props == null) props = new HashMap(); this.props.put(k, v); @@ -134,6 +137,7 @@ public void setDataset(ZFrame ds){ this.dataset = ds; } + @PythonMethod @Override public String toString() { StringRedactor redactor = new StringRedactor(); diff --git a/common/py/src/main/java/zingg/common/py/processors/ProcessorContext.java b/common/py/src/main/java/zingg/common/py/processors/ProcessorContext.java new file mode 100644 index 000000000..6131d51ed --- /dev/null +++ b/common/py/src/main/java/zingg/common/py/processors/ProcessorContext.java @@ -0,0 +1,22 @@ +package zingg.common.py.processors; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ProcessorContext { + private static final ProcessorContext INSTANCE = new ProcessorContext(); + + private Map> classMethodsMap = new HashMap<>(); + + private ProcessorContext() { + } + + public static ProcessorContext getInstance() { + return INSTANCE; + } + + public Map> getClassMethodsMap() { + return classMethodsMap; + } +} diff --git a/common/py/src/main/java/zingg/common/py/processors/PythonClassProcessor.java b/common/py/src/main/java/zingg/common/py/processors/PythonClassProcessor.java index 89d29ef55..e9cd37454 100644 --- a/common/py/src/main/java/zingg/common/py/processors/PythonClassProcessor.java +++ b/common/py/src/main/java/zingg/common/py/processors/PythonClassProcessor.java @@ -1,10 +1,14 @@ package zingg.common.py.processors; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; + import javax.annotation.processing.*; -import javax.lang.model.type.TypeMirror; -import javax.lang.model.type.TypeKind; import java.util.Set; +import java.util.stream.Collectors; + import javax.lang.model.element.*; import javax.lang.model.util.ElementFilter; @@ -14,6 +18,12 @@ public class PythonClassProcessor extends AbstractProcessor { private boolean importsAndDeclarationsGenerated = false; + private Map> classMethodsMap = new HashMap<>(); + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + } @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { @@ -29,28 +39,40 @@ public boolean process(Set annotations, RoundEnvironment for (Element element : roundEnv.getElementsAnnotatedWith(PythonClass.class)) { if (element.getKind() == ElementKind.CLASS) { TypeElement classElement = (TypeElement) element; - PackageElement packageElement = - (PackageElement) classElement.getEnclosingElement(); + PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); + List methodNames = new ArrayList<>(); + System.out.println("class " + element.getSimpleName() + ":"); // __init__ method System.out.println(" def __init__(self" + generateConstructorParameters(classElement) + "):"); - if (element.getSimpleName().contentEquals("pipe")) { - generateClassInitializationCode(classElement); + if (element.getSimpleName().contentEquals("Pipe")) { + generateClassInitializationCode(classElement, element); } for (VariableElement field : ElementFilter.fieldsIn(classElement.getEnclosedElements())) { if (!field.getSimpleName().contentEquals("serialVersionUID")) { - generateFieldInitializationCode(field); + generateFieldInitializationCode(field, element); + } + } + for (ExecutableElement methodElement : ElementFilter.methodsIn(classElement.getEnclosedElements())) { + if (methodElement.getAnnotation(PythonMethod.class) != null) { + methodNames.add(methodElement.getSimpleName().toString()); } } + classMethodsMap.put(element.getSimpleName().toString(), methodNames); } System.out.println(); - // rest of generated class contents + // rest of generated class contents } + ProcessorContext processorContext = ProcessorContext.getInstance(); + processorContext.getClassMethodsMap().putAll(classMethodsMap); return false; + } + Map> getClassMethodsMap() { + return classMethodsMap; } private void generateImportsAndDeclarations() { @@ -64,28 +86,41 @@ private void generateImportsAndDeclarations() { System.out.println(); } - private void generateClassInitializationCode(TypeElement classElement) { - System.out.println(" self.pipe = getJVM().zingg.spark.client.pipe.SparkPipe()"); + private void generateClassInitializationCode(TypeElement classElement, Element element) { + System.out.println(" self." + element.getSimpleName().toString().toLowerCase() + " = getJVM().zingg.spark.client.pipe.SparkPipe()"); } - // private void generateFieldInitializationCode(VariableElement field, ExecutableElement methodElement, TypeElement classElement) { - private void generateFieldInitializationCode(VariableElement field) { - System.out.println(" self.pipe." + field.getSimpleName() + " = " + field.getSimpleName()); - // String fieldName = field.getSimpleName().toString(); - // String methodName = methodElement.getSimpleName().toString(); - // System.out.println(" self." + fieldName + " = " + "getJVM()." + - // classElement.getQualifiedName().toString() + "." + methodName + "(" + fieldName + ")"); + private void generateFieldInitializationCode(VariableElement field, Element element) { + String fieldName = field.getSimpleName().toString(); + String fieldAssignment = "self." + element.getSimpleName().toString().toLowerCase() + "." + fieldName + " = " + fieldName; + + if (!fieldName.startsWith("FORMAT_")) { + System.out.println(" " + fieldAssignment); + } } private String generateConstructorParameters(TypeElement classElement) { StringBuilder parameters = new StringBuilder(); - for (VariableElement field : ElementFilter.fieldsIn(classElement.getEnclosedElements())) { - if (!field.getSimpleName().contentEquals("serialVersionUID")) { - parameters.append(", "); - parameters.append(field.getSimpleName()); - } + List fields = ElementFilter.fieldsIn(classElement.getEnclosedElements()); + + fields = fields.stream() + .filter(field -> !field.getSimpleName().contentEquals("serialVersionUID")) + .filter(this::isFieldForConstructor) + .collect(Collectors.toList()); + + for (VariableElement field : fields) { + parameters.append(", "); + parameters.append(field.getSimpleName()); } return parameters.toString(); } + + private boolean isFieldForConstructor(VariableElement field) { + String fieldName = field.getSimpleName().toString(); + + return !fieldName.equals(fieldName.toUpperCase()) + && !field.getModifiers().contains(Modifier.STATIC) + && !fieldName.startsWith("FORMAT_"); + } } diff --git a/common/py/src/main/java/zingg/common/py/processors/PythonMethodProcessor.java b/common/py/src/main/java/zingg/common/py/processors/PythonMethodProcessor.java index fe0b02747..5a499795f 100644 --- a/common/py/src/main/java/zingg/common/py/processors/PythonMethodProcessor.java +++ b/common/py/src/main/java/zingg/common/py/processors/PythonMethodProcessor.java @@ -1,36 +1,51 @@ package zingg.common.py.processors; import java.util.List; +import java.util.Map; + import javax.annotation.processing.*; import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeKind; import java.util.Set; -import javax.lang.model.element.*; -import javax.lang.model.util.ElementFilter; +import java.util.logging.Logger; +import javax.lang.model.element.*; import zingg.common.py.annotations.*; @SupportedAnnotationTypes("zingg.common.py.annotations.PythonMethod") public class PythonMethodProcessor extends AbstractProcessor { - private boolean importsAndDeclarationsGenerated = false; - + private Map> classMethodsMap; + // private static final Logger LOG = Logger.getLogger(PythonMethodProcessor.class.getName()); + @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { + ProcessorContext processorContext = ProcessorContext.getInstance(); + classMethodsMap = processorContext.getClassMethodsMap(); + // LOG.info("Processing PythonMethod annotations..."); + // process Services annotation for (Element element : roundEnv.getElementsAnnotatedWith(PythonMethod.class)) { if (element.getKind() == ElementKind.METHOD) { ExecutableElement methodElement = (ExecutableElement) element; - System.out.println(" def " + methodElement.getSimpleName() + "(self" + - generateMethodSignature(methodElement) + "):\n " + generateMethodReturn(methodElement)); - generateFieldAssignment(methodElement); - } - System.out.println(); + String className = methodElement.getEnclosingElement().getSimpleName().toString(); - // rest of generated class contents + if (classMethodsMap.containsKey(className)) { + List methodNames = classMethodsMap.get(className); + + if (methodNames.contains(methodElement.getSimpleName().toString())) { + // LOG.info("Generating Python method for: " + methodElement.getSimpleName()); + System.out.println(" def " + methodElement.getSimpleName() + "(self" + + generateMethodSignature(methodElement) + "):\n " + generateMethodReturn(methodElement)); + generateFieldAssignment(methodElement); + } + } + } + System.out.println(); } + // LOG.info("Processing complete."); return false; } @@ -65,12 +80,21 @@ private String resolveType(TypeMirror typeMirror) { } private void generateFieldAssignment(ExecutableElement methodElement) { - List parameters = methodElement.getParameters(); - if (!parameters.isEmpty()) { - VariableElement parameter = parameters.get(0); - String variableName = parameter.getSimpleName().toString(); - System.out.println(" self." + variableName + " = " + variableName); + List parameters = methodElement.getParameters(); + + if (!parameters.isEmpty()) { + String methodName = methodElement.getSimpleName().toString(); + String className = methodElement.getEnclosingElement().getSimpleName().toString(); + + StringBuilder parameterList = new StringBuilder(); + for (VariableElement parameter : parameters) { + if (parameterList.length() > 0) { + parameterList.append(", "); + } + parameterList.append(parameter.getSimpleName()); + } + System.out.println(" self." + className.toLowerCase() + "." + methodName + "(" + parameterList + ")"); + } } -} }