Skip to content

Commit

Permalink
Rename MethodJavadocKey -> MethodSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
dnault committed Jun 10, 2022
1 parent 7e0e27d commit c45a90c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package com.github.therapi.runtimejavadoc;

import com.github.therapi.runtimejavadoc.internal.MethodJavadocKey;
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.executableToMethodJavadocKey;
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.getAllTypeAncestors;
import com.github.therapi.runtimejavadoc.internal.MethodSignature;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand All @@ -30,11 +29,13 @@
import java.util.List;
import java.util.Map;

import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.getAllTypeAncestors;

public class ClassJavadoc extends BaseJavadoc {
private final Map<String, FieldJavadoc> fields;
private final Map<String, FieldJavadoc> enumConstants;
private final Map<MethodJavadocKey, MethodJavadoc> methods;
private final Map<MethodJavadocKey, MethodJavadoc> constructors;
private final Map<MethodSignature, MethodJavadoc> methods;
private final Map<MethodSignature, MethodJavadoc> constructors;
private final Map<String, ParamJavadoc> recordComponents;

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

Map<MethodJavadocKey, MethodJavadoc> methodsMap = new LinkedHashMap<>();
Map<MethodSignature, MethodJavadoc> methodsMap = new LinkedHashMap<>();
if (methods != null) {
methods.forEach(methodJavadoc -> methodsMap.put(methodJavadoc.toMethodJavadocKey(), methodJavadoc));
methods.forEach(methodJavadoc -> methodsMap.put(MethodSignature.from(methodJavadoc), methodJavadoc));
}
this.methods = Collections.unmodifiableMap(methodsMap);

Map<MethodJavadocKey, MethodJavadoc> constructorsMap = new LinkedHashMap<>();
Map<MethodSignature, MethodJavadoc> constructorsMap = new LinkedHashMap<>();
if (constructors != null) {
constructors.forEach(
methodJavadoc -> constructorsMap.put(methodJavadoc.toMethodJavadocKey(), methodJavadoc));
methodJavadoc -> constructorsMap.put(MethodSignature.from(methodJavadoc), methodJavadoc));
}
this.constructors = Collections.unmodifiableMap(constructorsMap);

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

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

Map<MethodJavadocKey, MethodJavadoc> methodJavadocs = new LinkedHashMap<>();
Map<MethodSignature, MethodJavadoc> methodJavadocs = new LinkedHashMap<>();
Arrays.stream(clazz.getDeclaredMethods())
.forEach(method -> methodJavadocs.put(executableToMethodJavadocKey(method),
.forEach(method -> methodJavadocs.put(MethodSignature.from(method),
RuntimeJavadoc.getJavadoc(method, classJavadocCache)));

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

MethodJavadoc findMatchingMethod(Method method) {
MethodJavadocKey methodJavadocKey = executableToMethodJavadocKey(method);
return methods.getOrDefault(methodJavadocKey, MethodJavadoc.createEmpty(method));
MethodSignature methodSignature = MethodSignature.from(method);
return methods.getOrDefault(methodSignature, MethodJavadoc.createEmpty(method));
}

MethodJavadoc findMatchingConstructor(Constructor<?> constructor) {
MethodJavadocKey methodJavadocKey = executableToMethodJavadocKey(constructor);
return constructors.getOrDefault(methodJavadocKey, MethodJavadoc.createEmpty(constructor));
MethodSignature methodSignature = MethodSignature.from(constructor);
return constructors.getOrDefault(methodSignature, MethodJavadoc.createEmpty(constructor));
}

ParamJavadoc findRecordComponent(String recordComponent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.github.therapi.runtimejavadoc;

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

MethodJavadocKey toMethodJavadocKey() {
return new MethodJavadocKey(getName(), paramTypes);
}

public List<String> getParamTypes() {
return paramTypes;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2022 David Nault and contributors
*
* 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.github.therapi.runtimejavadoc.internal;

import com.github.therapi.runtimejavadoc.MethodJavadoc;

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.INIT;
import static com.github.therapi.runtimejavadoc.internal.RuntimeJavadocHelper.unmodifiableDefensiveCopy;
import static java.util.Objects.requireNonNull;

public class MethodSignature {
private final String name;
private final List<String> paramTypes;

private MethodSignature(String methodName, List<String> paramTypes) {
this.name = requireNonNull(methodName);
this.paramTypes = unmodifiableDefensiveCopy(paramTypes);
}

public static MethodSignature from(MethodJavadoc methodJavadoc) {
return new MethodSignature(methodJavadoc.getName(), methodJavadoc.getParamTypes());
}

public static MethodSignature from(Executable executable) {
List<String> paramTypes = Arrays.stream(executable.getParameterTypes())
.map(Class::getCanonicalName)
.collect(Collectors.toList());
String name;
if (executable instanceof Method) {
name = executable.getName();
} else if (executable instanceof Constructor) {
name = INIT;
} else {
throw new IllegalArgumentException("Unexpected executable type: " + executable.getClass());
}

return new MethodSignature(name, paramTypes);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MethodSignature that = (MethodSignature) o;
return Objects.equals(name, that.name) && Objects.equals(paramTypes, that.paramTypes);
}

@Override
public int hashCode() {
return Objects.hash(name, paramTypes);
}

@Override
public String toString() {
return name + "(" + String.join(",", paramTypes) + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ public static String join(CharSequence delimiter, Iterable<? extends CharSequenc
return result.toString();
}

public static MethodJavadocKey executableToMethodJavadocKey(Executable executable) {
List<String> paramTypes = Arrays.stream(executable.getParameterTypes())
.map(Class::getCanonicalName)
.collect(Collectors.toList());
String name;
if (executable instanceof Method) {
name = executable.getName();
} else if (executable instanceof Constructor) {
name = INIT;
} else {
throw new UnsupportedOperationException("Unknown executable type");
}

return new MethodJavadocKey(name, paramTypes);
}

public static List<Class<?>> getAllTypeAncestors(Class<?> clazz) {
if (clazz == null) {
return Collections.emptyList();
Expand Down

0 comments on commit c45a90c

Please sign in to comment.