Skip to content

Commit

Permalink
Changed the method processing to only ignore bridge and synthetic met…
Browse files Browse the repository at this point in the history
…hods which are not duplicate of other normal methods.
  • Loading branch information
Stuart Fehr authored and mkarg committed Jul 6, 2024
1 parent 41cbc75 commit ef4879d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
59 changes: 43 additions & 16 deletions src/main/java/com/beust/jcommander/Parameterized.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

/**
* Encapsulate a field or a method annotated with @Parameter or @DynamicParameter
Expand Down Expand Up @@ -98,6 +97,9 @@ public static List<Parameterized> parseArg(Object arg) {
// and all of its parent types
Set<Class<?>> types = describeClassTree(rootClass);

Set<Method> bridgeOrSyntheticMethods = new HashSet<>();
Map<String, Parameterized> methods = new HashMap<>();

// analyze each type
for(Class<?> cls : types) {

Expand All @@ -120,29 +122,54 @@ public static List<Parameterized> parseArg(Object arg) {

// check methods
for (Method m : cls.getDeclaredMethods()) {
m.setAccessible(true);
// Ignore bridge and synthetic methods for now
if (m.isBridge() || m.isSynthetic()) {
continue;
}
Annotation annotation = m.getAnnotation(Parameter.class);
Annotation delegateAnnotation = m.getAnnotation(ParametersDelegate.class);
Annotation dynamicParameter = m.getAnnotation(DynamicParameter.class);
if (annotation != null) {
result.add(new Parameterized(new WrappedParameter((Parameter) annotation), null,
null, m));
} else if (dynamicParameter != null) {
result.add(new Parameterized(new WrappedParameter((DynamicParameter) dynamicParameter), null,
null, m));
} else if (delegateAnnotation != null) {
result.add(new Parameterized(null, (ParametersDelegate) delegateAnnotation,
null, m));

Parameterized parameterized = createParameterizedFromMethod(m);
if (parameterized != null) {
methods.put(m.getName(), parameterized);
}
}

// Accumulate the bridge and synthetic methods to check later
bridgeOrSyntheticMethods.addAll(Arrays.stream(cls.getDeclaredMethods())
.filter(method -> method.isBridge() || method.isSynthetic())
.collect(Collectors.toList()));
}

// If there are any bridge or synthetic methods that do not have a name which is already present, add them to the
// methods map. Otherwise, the non-bridge or non-synthetic method of the same name will take precedence
bridgeOrSyntheticMethods.stream()
.map(Parameterized::createParameterizedFromMethod)
.filter(Objects::nonNull)
.forEach(parameterized -> methods.putIfAbsent(parameterized.method.getName(), parameterized));

result.addAll(methods.values());

return result;
}

private static Parameterized createParameterizedFromMethod(Method m) {
m.setAccessible(true);
Annotation annotation = m.getAnnotation(Parameter.class);
Annotation delegateAnnotation = m.getAnnotation(ParametersDelegate.class);
Annotation dynamicParameter = m.getAnnotation(DynamicParameter.class);
if (annotation != null) {
return new Parameterized(new WrappedParameter((Parameter) annotation), null,
null, m);
} else if (dynamicParameter != null) {
return new Parameterized(new WrappedParameter((DynamicParameter) dynamicParameter), null,
null, m);
} else if (delegateAnnotation != null) {
return new Parameterized(null, (ParametersDelegate) delegateAnnotation,
null, m);
}

return null;
}

public WrappedParameter getWrappedParameter() {
return wrappedParameter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void jsonParameterizedParsingTest() {

@Test
public void ignoreBridgeMethodsTest() {
BuilderExample builder = new BuilderExample();
BridgeMethodsExample builder = new BridgeMethodsExample();

JCommander jCommander = new JCommander();
jCommander.addObject(builder);
Expand Down

0 comments on commit ef4879d

Please sign in to comment.