Skip to content

Commit

Permalink
annotation-processor uses of(String) factory methods by default (#1723
Browse files Browse the repository at this point in the history
)

annotation-processor uses `of(String)` factory methods by default
  • Loading branch information
carterkozak authored Mar 24, 2022
1 parent 0a6a533 commit c7bc93a
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 5 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1723.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: annotation-processor uses `of(String)` factory methods by default.
links:
- https://github.com/palantir/conjure-java/pull/1723
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,27 @@ static Optional<CodeBlock> getUnknownDecoderFactoryFunction(TypeMirror typeMirro
return Optional.of(CodeBlock.of("$T::parseShort", Short.class));
case DECLARED:
DeclaredType declaredType = (DeclaredType) typeMirror;
return getValueOfDecoderFactoryFunction(declaredType)
.or(() -> getStringConstructorDecoderFactoryFunction(declaredType));
return getValueOfDecoderFactoryFunction(declaredType, "valueOf")
.or(() -> getStringConstructorDecoderFactoryFunction(declaredType))
.or(() -> getValueOfDecoderFactoryFunction(declaredType, "of"));
default:
return Optional.empty();
}
}

private static Optional<CodeBlock> getValueOfDecoderFactoryFunction(DeclaredType declaredType) {
private static Optional<CodeBlock> getValueOfDecoderFactoryFunction(DeclaredType declaredType, String methodName) {
TypeElement typeElement = (TypeElement) declaredType.asElement();
// T valueOf(String)
return typeElement.getEnclosedElements().stream()
.filter(element -> element.getKind() == ElementKind.METHOD)
.map(ExecutableElement.class::cast)
.filter(element -> element.getModifiers().contains(Modifier.PUBLIC)
&& element.getModifiers().contains(Modifier.STATIC)
&& element.getSimpleName().contentEquals("valueOf")
&& element.getSimpleName().contentEquals(methodName)
&& element.getParameters().size() == 1
&& isStringMirror(element.getParameters().get(0).asType())
&& Objects.equals(TypeName.get(declaredType), TypeName.get(element.getReturnType())))
.map(_element -> CodeBlock.of("$T::valueOf", TypeName.get(declaredType)))
.map(_element -> CodeBlock.of("$T::" + methodName, TypeName.get(declaredType)))
.findFirst();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.palantir.conjure.java.undertow.processor.sample.MultipleBodyInterface;
import com.palantir.conjure.java.undertow.processor.sample.NameClashContextParam;
import com.palantir.conjure.java.undertow.processor.sample.NameClashExchangeParam;
import com.palantir.conjure.java.undertow.processor.sample.OfFactory;
import com.palantir.conjure.java.undertow.processor.sample.OptionalPrimitives;
import com.palantir.conjure.java.undertow.processor.sample.OverloadedResource;
import com.palantir.conjure.java.undertow.processor.sample.ParameterNotAnnotated;
Expand Down Expand Up @@ -77,6 +78,11 @@ public void testMultipleBodies() {
assertTestFileCompileAndMatches(TEST_CLASSES_BASE_DIR, MultipleBodyInterface.class);
}

@Test
public void testOfFactoryMethod() {
assertTestFileCompileAndMatches(TEST_CLASSES_BASE_DIR, OfFactory.class);
}

@Test
public void testPrimitiveQueryParams() {
assertTestFileCompileAndMatches(TEST_CLASSES_BASE_DIR, PrimitiveQueryParams.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved.
*
* 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 com.palantir.conjure.java.undertow.processor.sample;

import com.palantir.conjure.java.undertow.annotations.Handle;
import com.palantir.conjure.java.undertow.annotations.HttpMethod;

public interface OfFactory {

@Handle(method = HttpMethod.GET, path = "/{pathVar}")
void ping(@Handle.PathParam PathVariable pathVar);

final class PathVariable {
private final String value;

private PathVariable(String value) {
this.value = value;
}

public static PathVariable of(String value) {
return new PathVariable(value);
}

@Override
public String toString() {
return "PathVariable{value='" + value + '\'' + '}';
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c7bc93a

Please sign in to comment.