Skip to content

Commit

Permalink
fix assisted builder
Browse files Browse the repository at this point in the history
  • Loading branch information
stCarolas committed Mar 29, 2021
1 parent 3d6326b commit bf36f5c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .mvn/maven.config
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-Drevision=0.3.3
-Drevision=0.3.5
1 change: 1 addition & 0 deletions micronaut-example/pom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ build:
Adebug: true
AuseVavr: true
AfactoryMethodName: build
AfactoryVisibility: public
dependencies:
- {groupId: org.ow2.asm, artifactId: asm, version: 7.3.1, type: jar}
annotationProcessorPaths:
Expand Down
1 change: 1 addition & 0 deletions processor/pom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ modelVersion: 4.0.0
properties:
maven.compiler.target: 1.8
maven.compiler.source: 1.8
maven.compile.nowarn: on
revision: 0.0.1

dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import com.google.auto.service.AutoService;


@AutoService(javax.annotation.processing.Processor.class)
@SupportedAnnotationTypes("com.github.stcarolas.enrichedbeans.annotations.Enrich")
@SupportedAnnotationTypes({
"com.github.stcarolas.enrichedbeans.annotations.Enrich",
"com.github.stcarolas.enrichedbeans.annotations.Assisted"
})
public class Processor extends AbstractProcessor {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javax.inject.Inject;
import javax.inject.Named;
import javax.lang.model.element.Modifier;

import com.github.stcarolas.enrichedbeans.annotations.Assisted;
import com.github.stcarolas.enrichedbeans.annotations.Enrich;
Expand Down Expand Up @@ -46,7 +47,8 @@
return targetBean().packageName();
}

@Derived public String name(){
@Derived
public String name(){
return targetBean().name() + "Factory";
}

Expand Down Expand Up @@ -112,6 +114,9 @@ private MethodWithSpec factoryMethodUsingBuilder(BeanBuilder beanBuilder,Boolean
.map(Variable::asFieldSpec)
.toJavaList()
);
if (visibility()!= Modifier.DEFAULT){
spec = spec.addModifiers(visibility());
}
spec = methods().foldLeft(
spec,
(specBuilder,method) -> specBuilder.addMethod(method.spec())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,40 @@
import javax.inject.Named;

import com.github.stcarolas.enrichedbeans.processor.spec.CanProcessBeans;
import com.github.stcarolas.enrichedbeans.processor.spec.JavaClass;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import io.vavr.Function2;
import io.vavr.collection.Iterator;
import io.vavr.collection.List;
import io.vavr.collection.Seq;
import io.vavr.control.Try;

import static io.vavr.API.*;

@Named("FindAndEnrichBeans")
@SuppressWarnings("serial")
public class FindAndEnrichBeans
implements Function2<RoundEnvironment,ProcessingEnvironment,Boolean> {

public Boolean apply(
RoundEnvironment roundEnv,
ProcessingEnvironment processingEnv
){
return For(
List<JavaClass> createdClasses = For(
findBeans.apply(roundEnv),
processors
)
.yield( (bean, processor) -> processor.apply(processingEnv, bean) )
.toList();
createdClasses.forEach(created -> log.info("created: {}", created.name()));
List<Try<Void>> errors = createdClasses
.map(created -> created.writeTo(processingEnv))
.filter(Try::isFailure)
.isEmpty();
.filter(Try::isFailure);
errors.forEach(it -> log.info("error: {}",it));
return errors.isEmpty();
}

@Inject public FindAndEnrichBeans(
Expand All @@ -42,4 +52,5 @@ public Boolean apply(

private final FindBeans findBeans;
private final Seq<CanProcessBeans> processors;
static final Logger log = LogManager.getLogger();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,38 @@
import com.github.stcarolas.enrichedbeans.annotations.Enrich;
import com.github.stcarolas.enrichedbeans.processor.java.Bean;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import io.vavr.Function1;
import io.vavr.collection.List;
import io.vavr.collection.Seq;

@Named("FindBeans")
@SuppressWarnings("serial")
public class FindBeans
implements Function1<RoundEnvironment, Seq<Bean>> {

@Override
public Seq<Bean> apply(RoundEnvironment env) {
log.info("scaning for beans to enrich");
return listEnrichedFields(env)
.distinct()
.map(Bean::new);
}

@SuppressWarnings("unchecked")
private List<TypeElement> listEnrichedFields(RoundEnvironment env){
return List.ofAll((java.util.Set<Element>)env.getElementsAnnotatedWith(Enrich.class))
List<Element> targetBeans = List.ofAll((java.util.Set<Element>)env.getElementsAnnotatedWith(Enrich.class))
.map(element -> element.getEnclosingElement())
.appendAll(
List.ofAll(env.getElementsAnnotatedWith(Assisted.class))
)
.map(element -> (TypeElement)element);
);
targetBeans.forEach(bean -> log.info("enriched bean: {}", bean.getSimpleName()));
return targetBeans.map(element -> (TypeElement)element);
}

@Inject public FindBeans(){}

static final Logger log = LogManager.getLogger();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public interface JavaClass {

String packageName();
String name();
TypeSpec spec();

default Try<Void> writeTo(ProcessingEnvironment env){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private Option<HasMethodSpec> proxyFactoryMethod(

private Modifier detectVisibility(ProcessingEnvironment env) {
return "package".equals(env.getOptions().get("factoryVisibility"))
? Modifier.PUBLIC
: Modifier.DEFAULT;
? Modifier.DEFAULT
: Modifier.PUBLIC;
}

static final Logger log = LogManager.getLogger();
Expand Down

0 comments on commit bf36f5c

Please sign in to comment.