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

Fixing issue 486 @SubParameter Problems #515

Open
wants to merge 8 commits 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
3 changes: 3 additions & 0 deletions src/main/java/com/beust/jcommander/ParameterDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ private Object handleSubParameters(String value, int currentIndex, Class<?> type
objectValue = type.newInstance();
parameterized.set(object, objectValue);
}
boolean access = sai.field.isAccessible();
sai.field.setAccessible(true); // before using the field, set accessible to true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a unit test for this bug. Thanks.

wrappedParameter.addValue(parameterized, objectValue, value, sai.field);
sai.field.setAccessible(access); // after using the field, set accessible to origin
finalValue = objectValue;
} catch (InstantiationException | IllegalAccessException e) {
throw new ParameterException("Couldn't instantiate " + type, e);
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/beust/jcommander/WrappedParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ public void addValue(Parameterized parameterized, Object object, Object value, F
throws IllegalAccessException {
if (parameter != null) {
if (field != null) {
Class<?> fieldClass = field.getType();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a unit test for this bug. Thanks.

if (fieldClass != value.getClass()) {
if (fieldClass == boolean.class || fieldClass == Boolean.class)
value = Boolean.parseBoolean(value.toString());
else if (fieldClass == byte.class || fieldClass == Byte.class)
value = Byte.parseByte(value.toString());
else if (fieldClass == short.class || fieldClass == Short.class)
value = Short.parseShort(value.toString());
else if (fieldClass == int.class || fieldClass == Integer.class)
value = Integer.parseInt(value.toString());
else if (fieldClass == long.class || fieldClass == Long.class)
value = Long.parseLong(value.toString());
else if (fieldClass == char.class || fieldClass == Character.class)
value = value.toString().charAt(0);
else if (fieldClass == float.class || fieldClass == Float.class)
value = Float.parseFloat(value.toString());
else if (fieldClass == double.class || fieldClass == Double.class)
value = Double.parseDouble(value.toString());
else {
try {
value = fieldClass.getConstructor(value.getClass()).newInstance(value);
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not print to stack trace, but instead throw ParameterException. Thanks.

}
}
}
field.set(object, value);
} else {
parameterized.set(object, value);
Expand Down