Get all classes extending a abstract class

Discussion in 'Plugin Development' started by MaTaMoR_, Apr 10, 2015.

Thread Status:
Not open for further replies.
  1. I was wondering if there's any way to get all classes extending abstract class
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MaTaMoR_ Not without looping through the loaded classes I believe. Looping through the jar content is probably the best way for this. Chances are that people will disagree with me on that one.
     
  3. Offline

    teej107

    @MaTaMoR_ Are you talking about all class files in a jar or all Objects of a certain class?
     
  4. I have a abstract class called "subcommand" and i have X classes extending that abstract class, so i was wondering if i can get all classes extending that abstract class : Photo
     
  5. Offline

    Rocoty

    @MaTaMoR_ I think when you ask such a question that you have a design flaw. Why do you want to do this. Surely there must be better ways of achieving your goal.
     
  6. Because i don't wanna have to manually add all classes to a list .
     
  7. Offline

    teej107

    Are you talking about the class files or the Objects when you already instantiated them?

    Ninja'D
    What is your purpose of doing this?
     
  8. not having to manually add all classes to a list
     
  9. Offline

    Rocoty

    @MaTaMoR_ you're not adding classes to that list. You're adding instances. You can loop through that list of instances. But sure you have to create the instances first and add them to the list. If this is too much work for you, try reflection or rethink your design.
     
  10. Sorry, when i said classes i was talking about the instance, but how i should re-design my class ? :

    SubCommand class:
    Show Spoiler

    Code:
    public abstract class SubCommand {
    
    public abstract void onCommand(CommandSender sender, String[] args);
    
    
    private final String message, usage, permission;
    private final Boolean onlyPlayer;
    private final String[] aliases;
    
    public SubCommand(String message, String usage, String permission, boolean onlyPlayer, String... aliases) {
        this.message = message;
        this.usage = usage;
        this.permission = permission;
        this.onlyPlayer = onlyPlayer;
        this.aliases = aliases;
    }
    
    public final String getMessage() {
        return message;
    }
    
    public final String getUsage() {
        return usage;
    }
    
    public final String getPermission() {
        return permission;
    }
    
    public final Boolean onlyPlayer() {
        return onlyPlayer;
    }
    
    public final String[] getAliases() {
        return aliases;
    }
    }
    

    PD : Can you give me a example or a link to a guide ?
     
    Last edited: Apr 10, 2015
  11. Offline

    1Rogue

    Code:java
    1. public abstract class SubCommand {
    2.  
    3. public SubCommand(/* ... */) {
    4. Class<? extends SubCommand> child = this.getClass(); //child class object
    5. String simpleName = child.getSimpleName(); // Example "MyCommand"
    6. }
    7.  
    8. }


    However, rather than having those values be returned via constructor, just require them via abstract methods:

    Code:java
    1. public abstract @NonNull String usage();
     
  12. First, ok we have the name but im using some aliases so how can i acces the class with his name ?
    Second, it looks so fancy so cool but do you have any library of yours where you use that ?
     
  13. Offline

    1Rogue

  14. Offline

    mythbusterma

    @MaTaMoR_

    Then just put all the commands into one method block, it works and takes almost zero effort.

    If you actually want to lay out a design, it's gonna take some effort, maybe a whole 20 lines of code or something ridiculous like that.
     
Thread Status:
Not open for further replies.

Share This Page