Skip to content

Commit 3297a39

Browse files
committed
| #218 uses in-build parameter name in JDK8
Haven't really seen this work yet, but no worse than before. Also updates guava and codemodel dependencies.
1 parent cd899f1 commit 3297a39

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/main/java/unquietcode/tools/flapi/IntrospectorSupport.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.lang.annotation.Annotation;
2424
import java.lang.reflect.Method;
25+
import java.lang.reflect.Parameter;
2526

2627
import static com.google.common.base.Preconditions.checkState;
2728

@@ -31,8 +32,8 @@
3132
*/
3233
public class IntrospectorSupport {
3334

34-
protected static <T extends Annotation> T getParameterAnnotation(Method method, int parameterIndex, Class<T> annotationClass) {
35-
for (Annotation annotation : method.getParameterAnnotations()[parameterIndex]) {
35+
protected static <T extends Annotation> T getParameterAnnotation(Parameter parameter, Class<T> annotationClass) {
36+
for (Annotation annotation : parameter.getAnnotations()) {
3637
if (annotation.annotationType() == annotationClass) {
3738
@SuppressWarnings("unchecked") T annotation1 = (T) annotation;
3839
return annotation1;
@@ -42,8 +43,8 @@ protected static <T extends Annotation> T getParameterAnnotation(Method method,
4243
return null;
4344
}
4445

45-
protected static <T extends Annotation> boolean hasParameterAnnotation(Method method, int parameterIndex, Class<T> annotationClass) {
46-
return getParameterAnnotation(method, parameterIndex, annotationClass) != null;
46+
protected static <T extends Annotation> boolean hasParameterAnnotation(Parameter parameter, Class<T> annotationClass) {
47+
return getParameterAnnotation(parameter, annotationClass) != null;
4748
}
4849

4950
protected static String getMethodSignature(Method method) {
@@ -57,13 +58,14 @@ protected static String getMethodSignature(final Method method, final String nor
5758
signature.append(name).append("(");
5859

5960
boolean first = true;
60-
Class<?>[] parameterTypes = method.getParameterTypes();
61+
final Parameter[] parameters = method.getParameters();
6162

62-
for (int i=0; i < parameterTypes.length; i++) {
63-
Class<?> parameterType = parameterTypes[i];
63+
for (int i=0; i < parameters.length; ++i) {
64+
final Parameter parameter = parameters[i];
65+
final Class<?> parameterType = parameter.getType();
6466

6567
// skip BlockChain parameters
66-
if (hasParameterAnnotation(method, i, BlockChain.class)) {
68+
if (hasParameterAnnotation(parameter, BlockChain.class)) {
6769
continue;
6870
}
6971

@@ -76,7 +78,7 @@ protected static String getMethodSignature(final Method method, final String nor
7678
final String typeName;
7779

7880
// varargs
79-
if (i == parameterTypes.length-1 && method.isVarArgs()) {
81+
if (parameter.isVarArgs()) {
8082
checkState(parameterType.isArray());
8183
typeName = parameterType.getComponentType().getName()+"...";
8284
}
@@ -95,9 +97,8 @@ else if (parameterType.isArray()) {
9597
Class<?>[] generics = ResolvableType.forMethodParameter(MethodParameter.forMethodOrConstructor(method, i)).resolveGenerics();
9698
signature.append(makeTypeWithGenerics(typeName, generics));
9799

98-
// parameter name
99-
// TODO improve in JDK8 (#218)
100-
signature.append(" p").append(i);
100+
// parameter name (or argX by default)
101+
signature.append(" ").append(parameter.getName());
101102
}
102103

103104
signature.append(")");

src/main/java/unquietcode/tools/flapi/annotations/AnnotationIntrospector.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.lang.annotation.Annotation;
4141
import java.lang.reflect.AnnotatedElement;
4242
import java.lang.reflect.Method;
43+
import java.lang.reflect.Parameter;
4344
import java.util.*;
4445
import java.util.concurrent.atomic.AtomicReference;
4546
import java.util.function.Consumer;
@@ -300,14 +301,16 @@ else if (selector == null) {
300301
}
301302

302303
// block chaining
303-
for (int i=0; i < method.getParameterTypes().length; ++i) {
304-
Class<?> parameterType = method.getParameterTypes()[i];
305-
BlockChain blockChain = getParameterAnnotation(method, i, BlockChain.class);
304+
final Parameter[] parameters = method.getParameters();
305+
306+
for (int i=0; i < parameters.length; ++i) {
307+
final Parameter parameter = parameters[i];
308+
final BlockChain blockChain = getParameterAnnotation(parameter, BlockChain.class);
306309

307310
if (blockChain != null) {
308311

309312
// check type
310-
if (parameterType != AtomicReference.class) {
313+
if (parameter.getType() != AtomicReference.class) {
311314
throw new DescriptorBuilderException("@BlockChain parameters must be of type AtomicReference");
312315
}
313316

0 commit comments

Comments
 (0)