/** * This is a mapping between the created facade's field (e.g., field_0) and * the JCommander parameter (param) which lives in the object it was parsed * from, 'item'. * * @param newFieldName * @param item * @param param * @param names * - the arguments values for this item. */ protected void addEntry( String newFieldName, Object item, Parameterized param, String prefix, AnnotatedElement member ) { translations.put( newFieldName, new TranslationEntry( param, item, prefix, member)); }
public JCommanderPrefixTranslator() { try { // HP Fortify "Access Specifier Manipulation" // These fields are being modified by trusted code, // in a way that is not influenced by user input paraField = Parameterized.class.getDeclaredField("m_field"); paraField.setAccessible(true); paraMethod = Parameterized.class.getDeclaredField("m_method"); paraMethod.setAccessible(true); } catch (NoSuchFieldException e) { // This is a programmer error, and will only happen if another // version // of JCommander is being used. throw new RuntimeException( e); } }
@Before public void setUp() { try { Arguments args = new Arguments(); ArrayList<Parameterized> params = (ArrayList<Parameterized>) Parameterized.parseArg(args); if (params.size() == 0) { fail("Could not find parameter"); } param = params.get(0); } catch (SecurityException e) { // Should never trigger e.printStackTrace(); } obj = 4; prefix = "prefix"; aElement = Integer.class; entry = new TranslationEntry( param, (Object) obj, prefix, aElement); }
@Override public Object convertValue(Parameterized parameterized, Class targetType, String src) { if (!Resource.class.isAssignableFrom(targetType)) { return super.convertValue(parameterized, targetType, src); } boolean out = WritableResource.class.isAssignableFrom(targetType); if (src.equals(STD_STREAM_ARG)) { return (out ? new StandardOutputResource() : new StandardInputResource()); } if (!ResourcePatternUtils.isUrl(src) && !StringUtils.startsWithAny(src, ResourceUtils.FILE_URL_PREFIX, ResourceUtils.JAR_URL_PREFIX)) { src = ResourceUtils.FILE_URL_PREFIX + src; } try { ResourceSource resourceSrc = CliValidatorServiceImpl.this.resourceSrcResolver.resolve(src); if (resourceSrc == null) { throw new CliValidatorException(String.format("Unable to resolve resource: path=%s", src)); } return resourceSrc.getResource(); } catch (IOException e) { throw new CliValidatorException(String.format("Unable to parse resource command line option (names=[%s], value=%s).", StringUtils.join(parameterized.getWrappedParameter().names(), ", "), src), e); } }
protected TranslationEntry( Parameterized param, Object object, String prefix, AnnotatedElement member ) { this.param = param; this.object = object; this.prefix = prefix; this.member = member; this.prefixedNames = addPrefixToNames(); }
private int getParameterOrder(ParameterDescription parameterDescription){ Field field; try { field = Parameterized.class.getDeclaredField("m_field"); } catch(NoSuchFieldException nsfe){ throw new RuntimeException(nsfe); } Field parameterField; try { Parameterized parameterized = parameterDescription.getParameterized(); if(!field.isAccessible()){ field.setAccessible(true); } parameterField = (Field)field.get(parameterized); } catch(IllegalAccessException iae){ throw new RuntimeException(iae); } if(parameterField != null){ ParameterOrder parameterOrder = parameterField.getAnnotation(ParameterOrder.class); if(parameterOrder != null){ return parameterOrder.value(); } } return -1; }
public Parameterized getParam() { return param; }