Skip to content

Commit

Permalink
Constructor parameters can be meta type
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Dec 23, 2024
1 parent 51420df commit db713a7
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
Expand All @@ -19,12 +20,32 @@
public final class FieldCollector {
private final ProcessingEnvironment processingEnv;
private final ProcessedClass processedClass;
private final TypeElement metadataStorageType;
private final TypeElement diagnosticStorageType;
private final TypeElement identifiedLocationType;
private final TypeElement uuidType;

// Mapped by field name
private Map<String, Field> fields;

public FieldCollector(ProcessingEnvironment processingEnv, ProcessedClass processedClass) {
this.processingEnv = processingEnv;
this.processedClass = processedClass;
this.metadataStorageType =
processingEnv.getElementUtils().getTypeElement("org.enso.compiler.core.ir.MetadataStorage");
this.diagnosticStorageType =
processingEnv
.getElementUtils()
.getTypeElement("org.enso.compiler.core.ir.DiagnosticStorage");
this.identifiedLocationType =
processingEnv
.getElementUtils()
.getTypeElement("org.enso.compiler.core.ir.IdentifiedLocation");
this.uuidType = processingEnv.getElementUtils().getTypeElement("java.util.UUID");
Objects.requireNonNull(metadataStorageType);
Objects.requireNonNull(diagnosticStorageType);
Objects.requireNonNull(identifiedLocationType);
Objects.requireNonNull(uuidType);
}

public List<Field> collectFields() {
Expand All @@ -46,6 +67,8 @@ private void collectFromCtor() {
field = processIrField(param, irFieldAnnot);
} else if (irChildAnnot != null) {
field = processIrChild(param, irChildAnnot);
} else if (Utils.hasNoAnnotations(param) && isMeta(param)) {
field = null;
} else {
var errMsg =
"Constructor parameter "
Expand All @@ -55,11 +78,20 @@ private void collectFromCtor() {
throw new IllegalStateException(errMsg);
}

assert field != null;
fields.put(paramName, field);
if (field != null) {
fields.put(paramName, field);
}
}
}

private boolean isMeta(VariableElement param) {
var typeUtils = processingEnv.getTypeUtils();
return typeUtils.isSameType(param.asType(), metadataStorageType.asType())
|| typeUtils.isSameType(param.asType(), diagnosticStorageType.asType())
|| typeUtils.isSameType(param.asType(), identifiedLocationType.asType())
|| typeUtils.isSameType(param.asType(), uuidType.asType());
}

private Field processIrField(VariableElement param, IRField irFieldAnnot) {
var isNullable = !irFieldAnnot.required();
var name = param.getSimpleName().toString();
Expand Down

0 comments on commit db713a7

Please sign in to comment.