Skip to content

Commit

Permalink
Merge pull request #770 from gnanaprakash-ravi/ZEIssue230Annotations
Browse files Browse the repository at this point in the history
Update the Setter functions generated code in annotations
  • Loading branch information
sonalgoyal authored Jan 25, 2024
2 parents cee3b19 + 17baef2 commit d5159cc
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String getSchema() {
return schema;
}


@PythonMethod
public void setSchema(String schema) {
this.schema = schema;
}
Expand All @@ -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;
Expand All @@ -90,6 +92,7 @@ public Map<String, String> getProps() {
return props;
}

@PythonMethod
public void setProp(String k, String v) {
if (props == null) props = new HashMap<String, String>();
this.props.put(k, v);
Expand Down Expand Up @@ -134,6 +137,7 @@ public void setDataset(ZFrame<D, R, C> ds){
this.dataset = ds;
}

@PythonMethod
@Override
public String toString() {
StringRedactor redactor = new StringRedactor();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, List<String>> classMethodsMap = new HashMap<>();

private ProcessorContext() {
}

public static ProcessorContext getInstance() {
return INSTANCE;
}

public Map<String, List<String>> getClassMethodsMap() {
return classMethodsMap;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,6 +18,12 @@
public class PythonClassProcessor extends AbstractProcessor {

private boolean importsAndDeclarationsGenerated = false;
private Map<String, List<String>> classMethodsMap = new HashMap<>();

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Expand All @@ -29,28 +39,40 @@ public boolean process(Set<? extends TypeElement> 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<String> 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<String, List<String>> getClassMethodsMap() {
return classMethodsMap;
}

private void generateImportsAndDeclarations() {
Expand All @@ -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<VariableElement> 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_");
}

}
Original file line number Diff line number Diff line change
@@ -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<String, List<String>> classMethodsMap;
// private static final Logger LOG = Logger.getLogger(PythonMethodProcessor.class.getName());

@Override
public boolean process(Set<? extends TypeElement> 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<String> 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;
}

Expand Down Expand Up @@ -65,12 +80,21 @@ private String resolveType(TypeMirror typeMirror) {
}

private void generateFieldAssignment(ExecutableElement methodElement) {
List<? extends VariableElement> parameters = methodElement.getParameters();
if (!parameters.isEmpty()) {
VariableElement parameter = parameters.get(0);
String variableName = parameter.getSimpleName().toString();
System.out.println(" self." + variableName + " = " + variableName);
List<? extends VariableElement> 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 + ")");
}
}
}

}

0 comments on commit d5159cc

Please sign in to comment.