Java Reference
In-Depth Information
Example 10•1: ShowComponent.java (continued)
// Store the converted value in an Object[] so it can
// be easily passed when we invoke the property setter
try {
if (type == String.class) { // no conversion needed
methodArgs[0] = value;
}
else if (type == int.class) { // String to int
methodArgs[0] = Integer.valueOf(value);
}
else if (type == boolean.class) { // to boolean
methodArgs[0] = Boolean.valueOf(value);
}
else if (type == Color.class) { // to Color
methodArgs[0] = Color.decode(value);
}
else if (type == Font.class) { // String to Font
methodArgs[0] = Font.decode(value);
}
else {
// If we can't convert, ignore the property
System.err.println("Property " + name +
" is of unsupported type " +
type.getName());
continue nextarg;
}
}
catch (Exception e) {
// If conversion failed, continue with the next arg
System.err.println("Can't convert '" + value +
"' to type " + type.getName() +
" for property " + name);
continue nextarg;
}
// Finally, use reflection to invoke the property
// setter method of the component we created, and pass
// in the converted property value.
try { setter.invoke(component, methodArgs); }
catch (Exception e) {
System.err.println("Can't set property: " + name);
}
// Now go on to next command-line arg
continue nextarg;
}
}
// If we get here, we didn't find the named property
System.err.println("Warning: No such property: " + name);
}
}
return components;
}
}
Search WWH ::




Custom Search