Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Parameterized and ParameterDescription class to improve separation of concern #586

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/main/java/com/beust/jcommander/ParameterDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ Object addValue(String name, String value, boolean isDefault, boolean validate,
}
boolean isCollection = Collection.class.isAssignableFrom(type);

Object finalValue;
finalValue = handleCollectionTypeParameter(value, isDefault, currentIndex, type, convertedValue, isCollection);
if (! isDefault) assigned = true;

this.value = finalValue;

return finalValue;
}

private Object handleCollectionTypeParameter(String value, boolean isDefault, int currentIndex, Class<?> type, Object convertedValue, boolean isCollection) {
Object finalValue;
if (isCollection) {
@SuppressWarnings("unchecked")
Expand All @@ -287,10 +297,6 @@ Object addValue(String name, String value, boolean isDefault, boolean validate,
finalValue = convertedValue;
}
}
if (! isDefault) assigned = true;

this.value = finalValue;

return finalValue;
}

Expand Down
70 changes: 39 additions & 31 deletions src/main/java/com/beust/jcommander/Parameterized.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,39 +104,10 @@ public static List<Parameterized> parseArg(Object arg) {
for(Class<?> cls : types) {

// check fields
for (Field f : cls.getDeclaredFields()) {
Annotation annotation = f.getAnnotation(Parameter.class);
Annotation delegateAnnotation = f.getAnnotation(ParametersDelegate.class);
Annotation dynamicParameter = f.getAnnotation(DynamicParameter.class);
if (annotation != null) {
result.add(new Parameterized(new WrappedParameter((Parameter) annotation), null,
f, null));
} else if (dynamicParameter != null) {
result.add(new Parameterized(new WrappedParameter((DynamicParameter) dynamicParameter), null,
f, null));
} else if (delegateAnnotation != null) {
result.add(new Parameterized(null, (ParametersDelegate) delegateAnnotation,
f, null));
}
}
analyzeFields(result, cls);

// check methods
for (Method m : cls.getDeclaredMethods()) {
// Ignore bridge and synthetic methods for now
if (m.isBridge() || m.isSynthetic()) {
continue;
}

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()));
analyzeMethods(bridgeOrSyntheticMethods, methods, cls);
}

// If there are any bridge or synthetic methods that do not have a name which is already present, add them to the
Expand All @@ -151,6 +122,43 @@ public static List<Parameterized> parseArg(Object arg) {
return result;
}

private static void analyzeMethods(Set<Method> bridgeOrSyntheticMethods, Map<String, Parameterized> methods, Class<?> cls) {
for (Method m : cls.getDeclaredMethods()) {
// Ignore bridge and synthetic methods for now
if (m.isBridge() || m.isSynthetic()) {
continue;
}

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()));
}

private static void analyzeFields(List<Parameterized> result, Class<?> cls) {
for (Field f : cls.getDeclaredFields()) {
Annotation annotation = f.getAnnotation(Parameter.class);
Annotation delegateAnnotation = f.getAnnotation(ParametersDelegate.class);
Annotation dynamicParameter = f.getAnnotation(DynamicParameter.class);
if (annotation != null) {
result.add(new Parameterized(new WrappedParameter((Parameter) annotation), null,
f, null));
} else if (dynamicParameter != null) {
result.add(new Parameterized(new WrappedParameter((DynamicParameter) dynamicParameter), null,
f, null));
} else if (delegateAnnotation != null) {
result.add(new Parameterized(null, (ParametersDelegate) delegateAnnotation,
f, null));
}
}
}

private static Parameterized createParameterizedFromMethod(Method m) {
m.setAccessible(true);
Annotation annotation = m.getAnnotation(Parameter.class);
Expand Down
Loading