ant-dev mailing list archives

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

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.


      * 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 
             Vector exc = (Vector)this.getClass().getField(dirOrFile 
             Vector des = (Vector)this.getClass().getField(dirOrFile 
             if (inc.contains(name) || exc.contains(name) ||  
des.contains(name)) { return; }

             boolean included = false;
             if (isExcluded(name)) {
             } else if (isSelected(name, file)) {
                 included = true;
             } else {
             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, des);
         } catch (IllegalAccessException e) {
             //do nothing
         } catch (NoSuchFieldException e) {
             //do nothing

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

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message