vendredi 29 novembre 2019

Formatting space charaters into method names when using reflection

I have a method that uses reflection to read method names in a class and stores them into a collection. It filters out any method name which does not have the prefix "filter".

The method names are being displayed directly to a user via a java fx ui list view.

I would like to be able to automatical format these method names with spaces.

RemoveNoExitRooms RemoveNoItemRoom

becomes

Remove no exit rooms. Remove no item rooms.

What is the best way to encode space characters into my method names?

  public void getMethods(){
        try {
              Class classobj = MapConfiguration.class;
              List<Method> methodList = Arrays.asList(classobj.getDeclaredMethods());
              List<String> words = methodList.stream().map(Method::getName).filter(m -> m.startsWith("filter")).map(m -> m.substring(6)).collect(Collectors.toList());
             }
        catch (Exception e) {
        e.printStackTrace();
    }
}

output:
RemoveNoExitRooms
RemoveNoItemRooms

My first thought was to just pick an arbitrary ASCII char and stream to replace it with whitespace.





Aucun commentaire:

Enregistrer un commentaire