Skip to content

Commit

Permalink
WIP annotation processing
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Jan 19, 2023
1 parent 443bf01 commit 9d77036
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package io.vena.bosk.refs;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;

import static java.util.Collections.singletonList;
import static javax.lang.model.SourceVersion.RELEASE_8;

@SupportedAnnotationTypes("io.vena.bosk.refs.annotations.Refs")
@SupportedAnnotationTypes("io.vena.bosk.refs.annotations.Ref")
@SupportedSourceVersion(RELEASE_8)
public class RefsProcessor extends AbstractProcessor {
public class RefProcessor extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Map<TypeElement, List<Element>> fieldsByClass = new LinkedHashMap<>();
annotations.forEach(annotation -> {
for (Element element: roundEnv.getElementsAnnotatedWith(annotation)) {
TypeMirror type = element.asType();
if (type.getKind() == TypeKind.DECLARED) {
generateRefsClass(element);
}
TypeElement enclosingType = ((TypeElement)element.getEnclosingElement());
fieldsByClass.computeIfAbsent(enclosingType, et -> new ArrayList<>())
.add(element);
}
});
if (!fieldsByClass.isEmpty()) {
generateRefsClass(fieldsByClass);
}
return false;
}

private static void generateRefsClass(Element type) {
System.out.println("----- Processing " + type);
for (VariableElement field: ElementFilter.fieldsIn(singletonList(type))) {
System.out.println("\tField " + field);
}
private static void generateRefsClass(Map<TypeElement, List<Element>> fieldsByClass) {
System.out.println("RefProcessor: " + fieldsByClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface Refs {
public @interface Ref {
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.vena.bosk.refs.RefsProcessor
io.vena.bosk.refs.RefProcessor
4 changes: 2 additions & 2 deletions bosk-refs/src/test/java/io/vena/bosk/refs/NodeWithRefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import io.vena.bosk.AbstractBoskTest.TestRoot;
import io.vena.bosk.StateTreeNode;
import io.vena.bosk.refs.annotations.Refs;
import io.vena.bosk.refs.annotations.Ref;
import lombok.Value;

@Value
@Refs
public class NodeWithRefs implements StateTreeNode {
@Ref
TestRoot root;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.vena.bosk.AbstractBoskTest;
import org.junit.jupiter.api.Test;

public class RefsTest extends AbstractBoskTest {
public class RefTest extends AbstractBoskTest {

@Test
void test() {
Expand Down

0 comments on commit 9d77036

Please sign in to comment.