Skip to content

Generate Value Classes When on Valhalla #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void buildMethod(Append append) {
appendProvides(append, "autoProvides", autoProvides);
}
append.append(")").append(NEWLINE);
append.append(" private void build_").append(buildName()).append("() {").append(NEWLINE);
append.append(" private void build_").append(buildName()).append("(Builder builder) {").append(NEWLINE);
if (hasMethod()) {
append.append(" ").append(Util.shortMethod(method)).append("(builder");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,23 @@ private void writeClassStart() {
if (!beanReader.hasTargetFactory()) {
writer.append("public ");
}
writer.append("final class ").append(name).append(suffix);
var valhallaStr = Util.valhalla();
if (!valhallaStr.isBlank() && hasAssistedFieldsOrParams()) {
valhallaStr = "";
}

writer.append("final %sclass ", valhallaStr).append(name).append(suffix);
writeImplementsOrExtends();
writer.append(" {").eol().eol();
}

private boolean hasAssistedFieldsOrParams() {
return beanReader.injectFields().stream().anyMatch(FieldReader::assisted)
|| beanReader.injectMethods().stream()
.flatMap(m -> m.params().stream())
.anyMatch(MethodParam::assisted);
}

private void writeImplementsOrExtends() {
TypeElement targetInterface = beanReader.targetInterface();
writer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,21 +333,26 @@ private void writeClassEnd() {
}

private void writeClassStart() {
if (beanReader.isRequestScopedController()) {
final var requestScopedController = beanReader.isRequestScopedController();
if (requestScopedController) {
writer.append(CODE_COMMENT_FACTORY, shortName).eol();
} else {
writer.append(CODE_COMMENT, shortName).eol();
}
writer.append(beanReader.generatedType()).append(Constants.AT_GENERATED_COMMENT).eol();
if (beanReader.isRequestScopedController()) {
if (requestScopedController) {
writer.append(Constants.AT_SINGLETON).eol();
}
String shortName = this.shortName;
if (beanReader.beanType().getNestingKind().isNested()) {
shortName = shortName.replace(".", "$");
}
writer.append("public final class ").append(shortName).append(suffix).append(" ");
if (beanReader.isRequestScopedController()) {
writer
.append("public final %sclass ", requestScopedController ? "" : Util.valhalla())
.append(shortName)
.append(suffix)
.append(" ");
if (requestScopedController) {
writer.append("implements ");
beanReader.factoryInterface(writer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,11 @@ private void writeBuildMethod() {
if (scopeInfo.addWithBeans()) {
writeWithBeans();
}
writer.append(" this.builder = builder;").eol();
writer.append(" // create beans in order based on constructor dependencies").eol();
writer.append(" // i.e. \"provides\" followed by \"dependsOn\"").eol();
for (MetaData metaData : ordering.ordered()) {
if (!metaData.isGenerateProxy()) {
writer.append(" build_%s();", metaData.buildName()).eol();
writer.append(" build_%s(builder);", metaData.buildName()).eol();
}
}
writer.append(" }").eol();
Expand Down Expand Up @@ -219,8 +218,7 @@ private void writeStartClass() {
scopeInfo.buildAtInjectModule(writer);

String interfaceType = scopeInfo.type().type();
writer.append("public final class %s implements %s {", shortName, interfaceType).eol().eol();
writer.append(" private Builder builder;").eol().eol();
writer.append("public final %sclass %s implements %s {", Util.valhalla(), shortName, interfaceType).eol().eol();
if (scopeInfo.addModuleConstructor()) {
writeConstructor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void writeStartClass() {
" */\n"
);
writer.append(Constants.AT_GENERATED).eol();
writer.append("public final class %s implements ModuleOrdering {", shortName).eol().eol();
writer.append("public final %sclass %s implements ModuleOrdering {", Util.valhalla(), shortName).eol().eol();

writer.append(" private final AvajeModule[] sortedModules = new AvajeModule[%s];", ordering.size()).eol();
writer.append(" private static final Map<String, Integer> INDEXES = Map.ofEntries(").eol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
Expand Down Expand Up @@ -368,4 +369,12 @@ static List<String> addQualifierSuffix(List<String> provides, String name) {
.collect(toList());
}

static String valhalla() {
try {
if (Modifier.valueOf("VALUE") != null && APContext.previewEnabled()) return "value ";
} catch (IllegalArgumentException e) {
// no valhalla
}
return "";
}
}
Loading