简体   繁体   English

如何在类中获取方法

[英]How to get methods inside a class

So I'm working on a plugin loader for a game and what it does is load plugins ( class objects) from a .jar inside a folder, then add them to an ArrayList (so users can make their own mods). 因此,我正在为游戏开发插件加载器,它的工作是从文件夹内的.jar加载插件( class对象),然后将其添加到ArrayList (以便用户可以制作自己的mod)。 But my java experience isn't the best :/ So far it gets the classes in the jar but I need to check what methods are in the class (such as the constructor for labeling). 但是我的Java经验不是最好的:/到目前为止,它可以将类添加到jar但是我需要检查类中有哪些方法(例如用于标记的构造函数)。 Or if someone knows a better way to do this. 或者,如果有人知道更好的方法。 Any help would be appreciated. 任何帮助,将不胜感激。

Plugin.java : Plugin.java

package me.rigamortis.faurax.plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;

public class Plugin {

    public Minecraft mc = Minecraft.getMinecraft();
    final File folder = new File(mc.mcDataDir + File.separator + "Faurax/Plugins");
    public String jar;
    public static String className;
    private String name;
    private String desc;
    private int color;
    private int key;

    public Plugin() {
        if(!folder.exists())
            folder.mkdirs();

        loadPlugins(folder);

        if(folder != null)
            getClassNames(folder);
    }

    public void setName(String newName) {
        name = newName;
    }

    public void setDesc(String newDesc) {
        desc = newDesc;
    }

    public void setColor(int newColor) {
        color = newColor;
    }

    public void setKey(int newKey) {
        key = newKey;
    }

    public String getName() {
        return this.name;
    }

    public String getDesc() {
        return this.desc;
    }

    public int getKey() {
        return this.key;
    }

    public int getColor() {
        return this.color;
    }

    /**
     * A hook for plugins
     */
    public void onTick() {}

    public void loadPlugins(final File folder) {
        try{
            for (final File fileEntry : folder.listFiles()) {
                if ( !fileEntry.isDirectory()) {
                    if(fileEntry.getName().endsWith(".jar")){
                        jar = fileEntry.getName();
                        Core.logNormal("Loading " + fileEntry.getName());
                    }
                }
            }
        }
        catch(Exception e) {
            Core.logError("Error? jar isin't a plugin.");
            e.printStackTrace();
        }
    }

    public void getClassNames(final File dir) {
        try{
            ZipInputStream zip = new ZipInputStream(new FileInputStream(dir + File.seperator + jar));
            for(ZipEntry entry = zip.getNextEntry(); entry != null; entry =     zip.getNextEntry())
                if(entry.getName().endsWith(".class") && !entry.isDirectory()) {
                    Core.logNormal("Found "+entry);
                    className = entry.toString();
                }
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}

PluginLoader.java : PluginLoader.java

package me.rigamortis.faurax.plugin;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;

public class PluginLoader {

    public static List<Plugin> plugins = new ArrayList<Plugin>();

    public PluginLoader() {
        loadPlugins();
    }

    public void loadPlugins() {}

}

I answered a similar question to this not too long ago. 我不久前回答了类似的问题。 Here's the code to get all methods from a given class, I think it's fairly self-explanatory: 这是从给定类中获取所有方法的代码,我认为这是不言而喻的:

void foo(Object o)
{
    Class<?> c = o.getClass();
    for (Method m : c.getDeclaredMethods())
    {
        //do whatever
    }
}

This takes advantage of the Java's Reflection API. 这利用了Java的Reflection API。 You can read more about it here . 您可以在此处了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM