Skip to content
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

Add support for varargs in AutoFactories #348

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -139,6 +139,7 @@ public boolean apply(VariableElement parameter) {
.name("create")
.returnType(classElement.asType())
.publicMethod(classElement.getModifiers().contains(PUBLIC))
.isVarargs(constructor.isVarArgs())
.providedParameters(providedParameters)
.passedParameters(passedParameters)
.creationParameters(Parameter.forParameterList(constructor.getParameters(), types))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class FactoryMethodDescriptor {
abstract TypeMirror returnType();
abstract boolean publicMethod();
abstract boolean overridingMethod();
abstract boolean isVarargs();
abstract ImmutableSet<Parameter> passedParameters();
abstract ImmutableSet<Parameter> providedParameters();
abstract ImmutableSet<Parameter> creationParameters();
Expand All @@ -49,7 +50,8 @@ static Builder builder(AutoFactoryDeclaration declaration) {
return new AutoValue_FactoryMethodDescriptor.Builder()
.declaration(checkNotNull(declaration))
.publicMethod(false)
.overridingMethod(false);
.overridingMethod(false)
.isVarargs(false);
}

@AutoValue.Builder
Expand All @@ -59,6 +61,7 @@ static abstract class Builder {
abstract Builder returnType(TypeMirror returnType);
abstract Builder publicMethod(boolean publicMethod);
abstract Builder overridingMethod(boolean overridingMethod);
abstract Builder isVarargs(boolean varargs);
abstract Builder passedParameters(Iterable<Parameter> passedParameters);
abstract Builder providedParameters(Iterable<Parameter> providedParameters);
abstract Builder creationParameters(Iterable<Parameter> creationParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public String apply(Parameter parameter) {
"return new $T($L)",
methodDescriptor.returnType(),
argumentJoiner.join(creationParameterNames));
method.varargs(methodDescriptor.isVarargs());
factory.addMethod(method.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public void nestedClasses() {
.and().generatesSources(
JavaFileObjects.forResource("expected/SimpleClassPassedDepsFactory.java"));
}

@Test public void simpleClassVarargsDeps() {
assertAbout(javaSource())
.that(JavaFileObjects.forResource("good/SimpleClassVarargsDeps.java"))
.processedWith(new AutoFactoryProcessor())
.compilesWithoutError()
.and().generatesSources(
JavaFileObjects.forResource("expected/SimpleClassVarargsDepsFactory.java"));
}

@Test public void simpleClassProvidedDeps() {
assertAbout(javaSources())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2013 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tests;

import javax.annotation.Generated;
import javax.inject.Inject;

@Generated(
value = "com.google.auto.factory.processor.AutoFactoryProcessor",
comments = "https://github.com/google/auto/tree/master/factory"
)
final class SimpleClassVarargsDepsFactory {
@Inject
SimpleClassVarargsDepsFactory() {
}

SimpleClassVarargsDeps create(String... deps) {
return new SimpleClassVarargsDeps(deps);
}
}
27 changes: 27 additions & 0 deletions factory/src/test/resources/good/SimpleClassVarargsDeps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2013 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tests;

import com.google.auto.factory.AutoFactory;

/**
* @author Matthias Muth
*/
@AutoFactory
class SimpleClassVarargsDeps{
public SimpleClassVarargsDeps(String... deps) {
}
}