lundi 1 février 2016

Reflection : How to call dynamic methods based on which object is being send

I am new to reflection, I am having a scenario where :

 public static void main(String[] args) throws IOException {

Searcher search = new Searcher();

    String folderToSearch = search.filepath;
    File folder = new File(folderToSearch);
    Set<File> list = new HashSet<File>();

    search.getFiles(folder, list);
      for (File file : list) {
          BruteForceSearch bSearch = new BruteForceSearch(file, toSearch);
          DisplaySearch(toSearch, bSearch);
        }

        System.out.println("\RegEx Search");

        for (File file : list) {
          RegExSearch rSearch = new RegExSearch(file, toSearch);
          DisplaySearch(toSearch, rSearch);
        }

      }

      private static void DisplaySearch(String toSearch, Object bSearch) {

        int count = 0;

       if (bSearch instanceof BruteForceSearch) {
          BruteForceSearch search = (BruteForceSearch) bSearch;     
        } else if (bSearch instanceof RegExSearch) {
          RegExSearch search = (RegExSearch) bSearch;     
        }

        if ( search.getCount(toSearch) > 0) {
          System.out.printf("%s - %s matches.%n", search.getFile(),
              search.getCount(toSearch));
          count++;
        }
        if (count == 0) {
          System.out.printf("Empty Result Set");
        }
      }

But I am having in the DisplaySearch method. Can someone tell me how can ?I use reflection here ? or can I just write different methods ?





Aucun commentaire:

Enregistrer un commentaire