Skip to content

Commit 46a1511

Browse files
committed
refactoring
1 parent afb9f43 commit 46a1511

6 files changed

Lines changed: 187 additions & 138 deletions

File tree

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package net.jbock.annotated;
22

33
import java.util.List;
4-
import java.util.stream.Stream;
54

65
public final class AnnotatedMethods {
76

8-
private final Step3 step3;
7+
private final AnnotatedMethodsBuilder.Step3 step3;
98
private final List<AnnotatedVarargsParameter> repeatablePositionalParameters;
109

11-
private AnnotatedMethods(
12-
Step3 step3,
10+
AnnotatedMethods(
11+
AnnotatedMethodsBuilder.Step3 step3,
1312
List<AnnotatedVarargsParameter> repeatablePositionalParameters) {
1413
this.step3 = step3;
1514
this.repeatablePositionalParameters = repeatablePositionalParameters;
@@ -26,63 +25,4 @@ public List<AnnotatedParameter> positionalParameters() {
2625
public List<AnnotatedVarargsParameter> repeatablePositionalParameters() {
2726
return repeatablePositionalParameters;
2827
}
29-
30-
static Step1 builder(List<AnnotatedMethod> annotatedMethods) {
31-
return new Step1(annotatedMethods);
32-
}
33-
34-
static final class Step1 {
35-
36-
private final List<AnnotatedMethod> annotatedMethods;
37-
38-
private Step1(List<AnnotatedMethod> annotatedMethods) {
39-
this.annotatedMethods = annotatedMethods;
40-
}
41-
42-
Stream<AnnotatedMethod> annotatedMethods() {
43-
return annotatedMethods.stream();
44-
}
45-
46-
Step2 withNamedOptions(List<AnnotatedOption> namedOptions) {
47-
return new Step2(this, namedOptions);
48-
}
49-
}
50-
51-
static final class Step2 {
52-
53-
private final Step1 step1;
54-
private final List<AnnotatedOption> namedOptions;
55-
56-
private Step2(Step1 step1, List<AnnotatedOption> namedOptions) {
57-
this.step1 = step1;
58-
this.namedOptions = namedOptions;
59-
}
60-
61-
Stream<AnnotatedMethod> annotatedMethods() {
62-
return step1.annotatedMethods.stream();
63-
}
64-
65-
Step3 withPositionalParameters(List<AnnotatedParameter> positionalParameters) {
66-
return new Step3(this, positionalParameters);
67-
}
68-
}
69-
70-
static final class Step3 {
71-
72-
private final Step2 step2;
73-
private final List<AnnotatedParameter> positionalParameters;
74-
75-
private Step3(Step2 step2, List<AnnotatedParameter> positionalParameters) {
76-
this.step2 = step2;
77-
this.positionalParameters = positionalParameters;
78-
}
79-
80-
Stream<AnnotatedMethod> annotatedMethods() {
81-
return step2.step1.annotatedMethods.stream();
82-
}
83-
84-
AnnotatedMethods withRepeatablePositionalParameters(List<AnnotatedVarargsParameter> repeatablePositionalParameters) {
85-
return new AnnotatedMethods(this, repeatablePositionalParameters);
86-
}
87-
}
8828
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package net.jbock.annotated;
2+
3+
import java.util.List;
4+
import java.util.stream.Stream;
5+
6+
final class AnnotatedMethodsBuilder {
7+
8+
private AnnotatedMethodsBuilder() {
9+
}
10+
11+
static Step1 builder(List<AnnotatedMethod> annotatedMethods) {
12+
return new Step1(annotatedMethods);
13+
}
14+
15+
static final class Step1 {
16+
17+
private final List<AnnotatedMethod> annotatedMethods;
18+
19+
private Step1(List<AnnotatedMethod> annotatedMethods) {
20+
this.annotatedMethods = annotatedMethods;
21+
}
22+
23+
Stream<AnnotatedMethod> annotatedMethods() {
24+
return annotatedMethods.stream();
25+
}
26+
27+
Step2 withNamedOptions(List<AnnotatedOption> namedOptions) {
28+
return new Step2(this, namedOptions);
29+
}
30+
}
31+
32+
static final class Step2 {
33+
34+
final Step1 step1;
35+
final List<AnnotatedOption> namedOptions;
36+
37+
private Step2(Step1 step1, List<AnnotatedOption> namedOptions) {
38+
this.step1 = step1;
39+
this.namedOptions = namedOptions;
40+
}
41+
42+
Stream<AnnotatedMethod> annotatedMethods() {
43+
return step1.annotatedMethods.stream();
44+
}
45+
46+
Step3 withPositionalParameters(List<AnnotatedParameter> positionalParameters) {
47+
return new Step3(this, positionalParameters);
48+
}
49+
}
50+
51+
static final class Step3 {
52+
53+
final Step2 step2;
54+
final List<AnnotatedParameter> positionalParameters;
55+
56+
private Step3(Step2 step2, List<AnnotatedParameter> positionalParameters) {
57+
this.step2 = step2;
58+
this.positionalParameters = positionalParameters;
59+
}
60+
61+
Stream<AnnotatedMethod> annotatedMethods() {
62+
return step2.step1.annotatedMethods.stream();
63+
}
64+
65+
AnnotatedMethods withRepeatablePositionalParameters(List<AnnotatedVarargsParameter> repeatablePositionalParameters) {
66+
return new AnnotatedMethods(this, repeatablePositionalParameters);
67+
}
68+
}
69+
}

compiler/src/main/java/net/jbock/annotated/AnnotatedMethodsFactory.java

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import io.jbock.util.Either;
44
import jakarta.inject.Inject;
55
import net.jbock.common.SnakeName;
6-
import net.jbock.common.Util;
76
import net.jbock.common.ValidationFailure;
87
import net.jbock.processor.SourceElement;
98
import net.jbock.validate.ValidateScope;
109

11-
import javax.lang.model.element.ExecutableElement;
1210
import javax.lang.model.element.Name;
13-
import javax.lang.model.type.DeclaredType;
1411
import java.util.Comparator;
1512
import java.util.HashMap;
1613
import java.util.HashSet;
@@ -20,45 +17,33 @@
2017
import java.util.Optional;
2118
import java.util.Set;
2219

23-
import static io.jbock.util.Either.right;
24-
import static io.jbock.util.Eithers.allFailures;
2520
import static java.util.stream.Collectors.toList;
26-
import static javax.lang.model.element.Modifier.PRIVATE;
27-
import static javax.lang.model.element.Modifier.STATIC;
28-
import static javax.lang.model.element.NestingKind.MEMBER;
29-
import static net.jbock.common.Annotations.methodLevelAnnotations;
3021
import static net.jbock.common.Constants.instancesOf;
31-
import static net.jbock.common.TypeTool.AS_DECLARED;
32-
import static net.jbock.common.TypeTool.AS_TYPE_ELEMENT;
3322

3423
@ValidateScope
3524
public class AnnotatedMethodsFactory {
3625

3726
private final Comparator<AnnotatedParameter> indexComparator =
3827
Comparator.comparingInt(AnnotatedParameter::index);
3928

40-
private final Util util;
4129
private final SourceElement sourceElement;
4230
private final ExecutableElementsFinder executableElementsFinder;
4331

4432
@Inject
4533
AnnotatedMethodsFactory(
46-
Util util,
4734
SourceElement sourceElement,
4835
ExecutableElementsFinder executableElementsFinder) {
49-
this.util = util;
5036
this.sourceElement = sourceElement;
5137
this.executableElementsFinder = executableElementsFinder;
5238
}
5339

5440
public Either<List<ValidationFailure>, AnnotatedMethods> createAnnotatedMethods() {
5541
return executableElementsFinder.findExecutableElements()
56-
.map(EnumNames::builder)
42+
.map(EnumNamesBuilder::builder)
43+
.map(builder -> builder.withSourceElement(sourceElement))
5744
.map(builder -> builder.withEnumNames(createEnumNames(builder.methods())))
58-
.flatMap(names -> names.methods().stream()
59-
.map(sourceMethod -> createAnnotatedMethod(sourceMethod, names))
60-
.collect(allFailures()))
61-
.map(AnnotatedMethods::builder)
45+
.flatMap(EnumNames::createAnnotatedMethods)
46+
.map(AnnotatedMethodsBuilder::builder)
6247
.map(builder -> builder.withNamedOptions(builder.annotatedMethods()
6348
.flatMap(instancesOf(AnnotatedOption.class))
6449
.collect(toList())))
@@ -98,39 +83,4 @@ private Map<Name, String> createEnumNames(List<Executable> methods) {
9883
}
9984
return result;
10085
}
101-
102-
private Either<ValidationFailure, AnnotatedMethod> createAnnotatedMethod(
103-
Executable sourceMethod,
104-
EnumNames enumNames) {
105-
String enumName = enumNames.enumNameFor(sourceMethod.simpleName());
106-
ExecutableElement method = sourceMethod.method();
107-
return util.checkNoDuplicateAnnotations(method, methodLevelAnnotations())
108-
.<Either<ValidationFailure, AnnotatedMethod>>map(Either::left)
109-
.orElseGet(() -> right(sourceMethod.annotatedMethod(sourceElement, enumName)))
110-
.filter(this::checkAccessibleReturnType);
111-
}
112-
113-
private Optional<ValidationFailure> checkAccessibleReturnType(
114-
AnnotatedMethod annotatedMethod) {
115-
return AS_DECLARED.visit(annotatedMethod.returnType())
116-
.filter(this::isInaccessible)
117-
.map(type -> annotatedMethod.fail("inaccessible type: " +
118-
util.typeToString(type)));
119-
}
120-
121-
private boolean isInaccessible(DeclaredType declared) {
122-
if (declared.asElement().getModifiers().contains(PRIVATE)) {
123-
return true;
124-
}
125-
if (AS_TYPE_ELEMENT.visit(declared.asElement())
126-
.filter(t -> t.getNestingKind() == MEMBER)
127-
.filter(t -> !t.getModifiers().contains(STATIC))
128-
.isPresent()) {
129-
return true;
130-
}
131-
return declared.getTypeArguments().stream()
132-
.map(AS_DECLARED::visit)
133-
.flatMap(Optional::stream)
134-
.anyMatch(this::isInaccessible);
135-
}
13686
}
Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,86 @@
11
package net.jbock.annotated;
22

3+
import io.jbock.util.Either;
4+
import net.jbock.common.Util;
5+
import net.jbock.common.ValidationFailure;
6+
import net.jbock.processor.SourceElement;
7+
8+
import javax.lang.model.element.ExecutableElement;
39
import javax.lang.model.element.Name;
10+
import javax.lang.model.type.DeclaredType;
411
import java.util.List;
512
import java.util.Map;
13+
import java.util.Optional;
14+
15+
import static io.jbock.util.Either.right;
16+
import static io.jbock.util.Eithers.allFailures;
17+
import static javax.lang.model.element.Modifier.PRIVATE;
18+
import static javax.lang.model.element.Modifier.STATIC;
19+
import static javax.lang.model.element.NestingKind.MEMBER;
20+
import static net.jbock.common.Annotations.methodLevelAnnotations;
21+
import static net.jbock.common.TypeTool.AS_DECLARED;
22+
import static net.jbock.common.TypeTool.AS_TYPE_ELEMENT;
23+
import static net.jbock.common.Util.checkNoDuplicateAnnotations;
624

725
final class EnumNames {
826

9-
private final Builder builder;
27+
private final EnumNamesBuilder.Step2 step2;
1028
private final Map<Name, String> enumNames;
1129

12-
private EnumNames(Builder builder, Map<Name, String> enumNames) {
13-
this.builder = builder;
30+
EnumNames(EnumNamesBuilder.Step2 step2, Map<Name, String> enumNames) {
31+
this.step2 = step2;
1432
this.enumNames = enumNames;
1533
}
1634

17-
static Builder builder(List<Executable> methods) {
18-
return new Builder(methods);
35+
Either<List<ValidationFailure>, List<AnnotatedMethod>> createAnnotatedMethods() {
36+
return methods().stream()
37+
.map(this::createAnnotatedMethod)
38+
.collect(allFailures());
1939
}
2040

21-
static final class Builder {
41+
private Either<ValidationFailure, AnnotatedMethod> createAnnotatedMethod(
42+
Executable sourceMethod) {
43+
String enumName = enumNameFor(sourceMethod.simpleName());
44+
ExecutableElement method = sourceMethod.method();
45+
return checkNoDuplicateAnnotations(method, methodLevelAnnotations())
46+
.<Either<ValidationFailure, AnnotatedMethod>>map(Either::left)
47+
.orElseGet(() -> right(sourceMethod.annotatedMethod(sourceElement(), enumName)))
48+
.filter(this::checkAccessibleReturnType);
49+
}
2250

23-
private final List<Executable> methods;
51+
private Optional<ValidationFailure> checkAccessibleReturnType(
52+
AnnotatedMethod annotatedMethod) {
53+
return AS_DECLARED.visit(annotatedMethod.returnType())
54+
.filter(this::isInaccessible)
55+
.map(type -> annotatedMethod.fail("inaccessible type: " +
56+
Util.typeToString(type)));
57+
}
2458

25-
private Builder(List<Executable> methods) {
26-
this.methods = methods;
59+
private boolean isInaccessible(DeclaredType declared) {
60+
if (declared.asElement().getModifiers().contains(PRIVATE)) {
61+
return true;
2762
}
28-
29-
List<Executable> methods() {
30-
return methods;
63+
if (AS_TYPE_ELEMENT.visit(declared.asElement())
64+
.filter(t -> t.getNestingKind() == MEMBER)
65+
.filter(t -> !t.getModifiers().contains(STATIC))
66+
.isPresent()) {
67+
return true;
3168
}
69+
return declared.getTypeArguments().stream()
70+
.map(AS_DECLARED::visit)
71+
.flatMap(Optional::stream)
72+
.anyMatch(this::isInaccessible);
73+
}
3274

33-
EnumNames withEnumNames(Map<Name, String> enumNames) {
34-
return new EnumNames(this, enumNames);
35-
}
75+
private SourceElement sourceElement() {
76+
return step2.sourceElement;
3677
}
3778

38-
String enumNameFor(Name sourceMethodName) {
79+
private String enumNameFor(Name sourceMethodName) {
3980
return enumNames.get(sourceMethodName);
4081
}
4182

42-
List<Executable> methods() {
43-
return builder.methods;
83+
private List<Executable> methods() {
84+
return step2.methods();
4485
}
4586
}

0 commit comments

Comments
 (0)