ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kev Jackson <foamd...@gmail.com>
Subject Use of reflection in DirectoryScanner to remove duplicated code
Date Wed, 23 Aug 2006 04:26:24 GMT
Hi,

I want to run this by people here to see what people think of this.   
Basically DirectoryScanner has some duplicated code  
[ accountForIncludedFile, accountForIncludedDir ], I think I have a  
refactored processIncluded method that can replace the duplication,  
but it relies on using Reflection to get access to the classes fields  
and then does the work [1].  I know a lot of Java developers are a  
little leary about using reflection in general as it has a reputation  
for being slow and obtuse, so I'd like to see what you guys think  
before committing something that will be veto'd.

Thanks,
Kev

[1]
/**
      * Can process included dirs or files
      * @param name path of dir/file relative to the directory of the
      *              FileSet
      * @param file dir/file as File
      * @param dirOrFile dir or file ("dirs" | "files")
      * @throws BuildException
      */
     private void processIncluded(String name, File file, String  
dirOrFile) {
         try {
             Vector inc = (Vector)this.getClass().getField(dirOrFile 
+"Included").get(this);
             Vector exc = (Vector)this.getClass().getField(dirOrFile 
+"Excluded").get(this);
             Vector des = (Vector)this.getClass().getField(dirOrFile 
+"Deselected").get(this);
             if (inc.contains(name) || exc.contains(name) ||  
des.contains(name)) { return; }

             boolean included = false;
             if (isExcluded(name)) {
                 exc.add(name);
             } else if (isSelected(name, file)) {
                 included = true;
                 inc.add(name);
             } else {
                 des.add(name);
             }
             everythingIncluded &= included;
             //is this required? <-- I think you must 'reset' the  
values that you used as the API states that only the values are  
returned from get, not the original reference, if the original  
vectors are returned, then the following 3 lines are unnecessary
             this.getClass().getField(dirOrFile+"Included").set(this,  
inc);
             this.getClass().getField(dirOrFile+"Excluded").set(this,  
exc);
             this.getClass().getField(dirOrFile+"Deselected").set 
(this, des);
         } catch (IllegalAccessException e) {
             //do nothing
         } catch (NoSuchFieldException e) {
             //do nothing
         }
     }

--
"Government is begotten of aggression, by aggression" - Herbert Spencer


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message