Skip to content

Commit c45a90c

Browse files
committed
Rename MethodJavadocKey -> MethodSignature
1 parent 7e0e27d commit c45a90c

File tree

5 files changed

+102
-78
lines changed

5 files changed

+102
-78
lines changed

Diff for: therapi-runtime-javadoc/src/main/java/com/github/therapi/runtimejavadoc/ClassJavadoc.java

+18-17
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
package com.github.therapi.runtimejavadoc;
1818

19-
import com.github.therapi.runtimejavadoc.internal.MethodJavadocKey;
20-
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.executableToMethodJavadocKey;
21-
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.getAllTypeAncestors;
19+
import com.github.therapi.runtimejavadoc.internal.MethodSignature;
20+
2221
import java.lang.reflect.Constructor;
2322
import java.lang.reflect.Field;
2423
import java.lang.reflect.Method;
@@ -30,11 +29,13 @@
3029
import java.util.List;
3130
import java.util.Map;
3231

32+
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.getAllTypeAncestors;
33+
3334
public class ClassJavadoc extends BaseJavadoc {
3435
private final Map<String, FieldJavadoc> fields;
3536
private final Map<String, FieldJavadoc> enumConstants;
36-
private final Map<MethodJavadocKey, MethodJavadoc> methods;
37-
private final Map<MethodJavadocKey, MethodJavadoc> constructors;
37+
private final Map<MethodSignature, MethodJavadoc> methods;
38+
private final Map<MethodSignature, MethodJavadoc> constructors;
3839
private final Map<String, ParamJavadoc> recordComponents;
3940

4041
public ClassJavadoc(String name, Comment comment, List<FieldJavadoc> fields, List<FieldJavadoc> enumConstants,
@@ -54,16 +55,16 @@ public ClassJavadoc(String name, Comment comment, List<FieldJavadoc> fields, Lis
5455
}
5556
this.enumConstants = Collections.unmodifiableMap(enumMap);
5657

57-
Map<MethodJavadocKey, MethodJavadoc> methodsMap = new LinkedHashMap<>();
58+
Map<MethodSignature, MethodJavadoc> methodsMap = new LinkedHashMap<>();
5859
if (methods != null) {
59-
methods.forEach(methodJavadoc -> methodsMap.put(methodJavadoc.toMethodJavadocKey(), methodJavadoc));
60+
methods.forEach(methodJavadoc -> methodsMap.put(MethodSignature.from(methodJavadoc), methodJavadoc));
6061
}
6162
this.methods = Collections.unmodifiableMap(methodsMap);
6263

63-
Map<MethodJavadocKey, MethodJavadoc> constructorsMap = new LinkedHashMap<>();
64+
Map<MethodSignature, MethodJavadoc> constructorsMap = new LinkedHashMap<>();
6465
if (constructors != null) {
6566
constructors.forEach(
66-
methodJavadoc -> constructorsMap.put(methodJavadoc.toMethodJavadocKey(), methodJavadoc));
67+
methodJavadoc -> constructorsMap.put(MethodSignature.from(methodJavadoc), methodJavadoc));
6768
}
6869
this.constructors = Collections.unmodifiableMap(constructorsMap);
6970

@@ -75,8 +76,8 @@ public ClassJavadoc(String name, Comment comment, List<FieldJavadoc> fields, Lis
7576
}
7677

7778
private ClassJavadoc(String name, Comment comment, Map<String, FieldJavadoc> fields,
78-
Map<String, FieldJavadoc> enumConstants, Map<MethodJavadocKey, MethodJavadoc> methods,
79-
Map<MethodJavadocKey, MethodJavadoc> constructors, List<OtherJavadoc> other,
79+
Map<String, FieldJavadoc> enumConstants, Map<MethodSignature, MethodJavadoc> methods,
80+
Map<MethodSignature, MethodJavadoc> constructors, List<OtherJavadoc> other,
8081
List<SeeAlsoJavadoc> seeAlso, Map<String, ParamJavadoc> recordComponents) {
8182
super(name, comment, seeAlso, other);
8283
this.fields = Collections.unmodifiableMap(fields);
@@ -110,9 +111,9 @@ ClassJavadoc createEnhancedClassJavadoc(Class<?> clazz) {
110111
classJavadocCache.put(clazz.getCanonicalName(), this);
111112
getAllTypeAncestors(clazz).forEach(cls -> classJavadocCache.put(cls.getCanonicalName(), RuntimeJavadoc.getSkinnyClassJavadoc(cls)));
112113

113-
Map<MethodJavadocKey, MethodJavadoc> methodJavadocs = new LinkedHashMap<>();
114+
Map<MethodSignature, MethodJavadoc> methodJavadocs = new LinkedHashMap<>();
114115
Arrays.stream(clazz.getDeclaredMethods())
115-
.forEach(method -> methodJavadocs.put(executableToMethodJavadocKey(method),
116+
.forEach(method -> methodJavadocs.put(MethodSignature.from(method),
116117
RuntimeJavadoc.getJavadoc(method, classJavadocCache)));
117118

118119
return new ClassJavadoc(getName(), getComment(), fields, enumConstants, methodJavadocs, constructors,
@@ -155,13 +156,13 @@ FieldJavadoc findMatchingEnumConstant(Enum<?> enumConstant) {
155156
}
156157

157158
MethodJavadoc findMatchingMethod(Method method) {
158-
MethodJavadocKey methodJavadocKey = executableToMethodJavadocKey(method);
159-
return methods.getOrDefault(methodJavadocKey, MethodJavadoc.createEmpty(method));
159+
MethodSignature methodSignature = MethodSignature.from(method);
160+
return methods.getOrDefault(methodSignature, MethodJavadoc.createEmpty(method));
160161
}
161162

162163
MethodJavadoc findMatchingConstructor(Constructor<?> constructor) {
163-
MethodJavadocKey methodJavadocKey = executableToMethodJavadocKey(constructor);
164-
return constructors.getOrDefault(methodJavadocKey, MethodJavadoc.createEmpty(constructor));
164+
MethodSignature methodSignature = MethodSignature.from(constructor);
165+
return constructors.getOrDefault(methodSignature, MethodJavadoc.createEmpty(constructor));
165166
}
166167

167168
ParamJavadoc findRecordComponent(String recordComponent) {

Diff for: therapi-runtime-javadoc/src/main/java/com/github/therapi/runtimejavadoc/MethodJavadoc.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.github.therapi.runtimejavadoc;
1818

19-
import com.github.therapi.runtimejavadoc.internal.MethodJavadocKey;
19+
import com.github.therapi.runtimejavadoc.internal.MethodSignature;
2020
import com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper;
2121
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.unmodifiableDefensiveCopy;
2222
import java.lang.reflect.Constructor;
@@ -191,10 +191,6 @@ private static List<String> getCanonicalNames(Class<?>[] paramTypes) {
191191
return methodParamsTypes;
192192
}
193193

194-
MethodJavadocKey toMethodJavadocKey() {
195-
return new MethodJavadocKey(getName(), paramTypes);
196-
}
197-
198194
public List<String> getParamTypes() {
199195
return paramTypes;
200196
}

Diff for: therapi-runtime-javadoc/src/main/java/com/github/therapi/runtimejavadoc/internal/MethodJavadocKey.java

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2022 David Nault and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.therapi.runtimejavadoc.internal;
18+
19+
import com.github.therapi.runtimejavadoc.MethodJavadoc;
20+
21+
import java.lang.reflect.Constructor;
22+
import java.lang.reflect.Executable;
23+
import java.lang.reflect.Method;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.stream.Collectors;
28+
29+
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.INIT;
30+
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.unmodifiableDefensiveCopy;
31+
import static java.util.Objects.requireNonNull;
32+
33+
public class MethodSignature {
34+
private final String name;
35+
private final List<String> paramTypes;
36+
37+
private MethodSignature(String methodName, List<String> paramTypes) {
38+
this.name = requireNonNull(methodName);
39+
this.paramTypes = unmodifiableDefensiveCopy(paramTypes);
40+
}
41+
42+
public static MethodSignature from(MethodJavadoc methodJavadoc) {
43+
return new MethodSignature(methodJavadoc.getName(), methodJavadoc.getParamTypes());
44+
}
45+
46+
public static MethodSignature from(Executable executable) {
47+
List<String> paramTypes = Arrays.stream(executable.getParameterTypes())
48+
.map(Class::getCanonicalName)
49+
.collect(Collectors.toList());
50+
String name;
51+
if (executable instanceof Method) {
52+
name = executable.getName();
53+
} else if (executable instanceof Constructor) {
54+
name = INIT;
55+
} else {
56+
throw new IllegalArgumentException("Unexpected executable type: " + executable.getClass());
57+
}
58+
59+
return new MethodSignature(name, paramTypes);
60+
}
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (this == o) {
65+
return true;
66+
}
67+
if (o == null || getClass() != o.getClass()) {
68+
return false;
69+
}
70+
MethodSignature that = (MethodSignature) o;
71+
return Objects.equals(name, that.name) && Objects.equals(paramTypes, that.paramTypes);
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(name, paramTypes);
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return name + "(" + String.join(",", paramTypes) + ")";
82+
}
83+
}

Diff for: therapi-runtime-javadoc/src/main/java/com/github/therapi/runtimejavadoc/internal/RuntimeJavadocHelper.java

-16
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,6 @@ public static String join(CharSequence delimiter, Iterable<? extends CharSequenc
5959
return result.toString();
6060
}
6161

62-
public static MethodJavadocKey executableToMethodJavadocKey(Executable executable) {
63-
List<String> paramTypes = Arrays.stream(executable.getParameterTypes())
64-
.map(Class::getCanonicalName)
65-
.collect(Collectors.toList());
66-
String name;
67-
if (executable instanceof Method) {
68-
name = executable.getName();
69-
} else if (executable instanceof Constructor) {
70-
name = INIT;
71-
} else {
72-
throw new UnsupportedOperationException("Unknown executable type");
73-
}
74-
75-
return new MethodJavadocKey(name, paramTypes);
76-
}
77-
7862
public static List<Class<?>> getAllTypeAncestors(Class<?> clazz) {
7963
if (clazz == null) {
8064
return Collections.emptyList();

0 commit comments

Comments
 (0)