在Java中获取Class的部分名称

时间:2022-10-31 07:55:02

Okay, so I am trying to do my network packet handling in Java using classes. For reading data from my stream I use a DataInputStream. My reading thread for my server looks like this:

好的,所以我正在尝试使用类在Java中进行网络数据包处理。为了从我的流中读取数据,我使用DataInputStream。我的服务器的阅读线程如下所示:

public void run()
    {
        while(client.isActive())
        {
            try{
                handle(is.readShort());
            }catch (IOException e){
                client.stop(e);
                break;
            }
        }
    }

Now I've got a method handle:

现在我有一个方法句柄:

public void handle(short id) throws IOException
{
    InPacket packet = null;

    try {
        packet = ClassUtils.newInstance(InPacket.class, "server.client.InPacket"+id);
    }catch (Exception e){
        e.printStackTrace();
    }

    if (packet!=null){
        packet.handle(this);
    }
    else{
        throw new IOException("Invalid packet");
    }
}

I try to instantiate a new class using the

我尝试使用the实例化一个新类

packet = ClassUtils.newInstance(InPacket.class, "server.client.InPacket"+id);

line.

In ClassUtils this is that function:

在ClassUtils中,这是该函数:

public static <T> T newInstance(Class<? extends T> type, String className) throws
ClassNotFoundException,
InstantiationException,
IllegalAccessException {
    Class<?> clazz = Class.forName(className);
    Class<? extends T> targetClass = clazz.asSubclass(type);
    T result = targetClass.newInstance();
    return result;
}

My problem is: when I try to get that class with only part of the name (I try to get it by "InPacket1", while the class is called "InPacket1Connect"), it can't find it. Is it possible to do this in Java, and if so how? If not, what method do you recommend for handling my network packets?

我的问题是:当我尝试仅使用部分名称获取该类时(我尝试通过“InPacket1”获取它,而该类被称为“InPacket1Connect”),它无法找到它。是否可以在Java中执行此操作,如果是这样,如何?如果没有,您建议使用什么方法处理我的网络数据包?

3 个解决方案

#1


0  

An alternative approach would be to use a map (or enum) which maps the id to the full class name.

另一种方法是使用将id映射到完整类名的map(或枚举)。

Pulling in the stuff from the comments, ensure that this mapping class is available as a jar (or may be in the same jar which contains the implementations of the packet handlers) as your "messaging layer" jar.

从注释中提取内容,确保此映射类可用作jar(或者可能位于包含数据包处理程序实现的同一jar中)作为“消息传递层”jar。

#2


0  

Like this, maybe?

像这样,也许吧?

packet = ClassUtils.newInstance(InPacket.class, "server.client.InPacket"+id+"Connect");
                                                                           ^^^^^^^^^^

(but I may have misunderstood your question)

(但我可能误解了你的问题)

#3


0  

Why can you not create the full class name?

为什么不能创建完整的类名?

packet = ClassUtils.newInstance(InPacket.class, "server.client.InPacket"+id + "Connect"

You could implement the functionality you seem to be asking for - a kind of "fuzzy" classname matching - by writing your own classloader. The classloader could search some directories for class files partially matching the type.

您可以通过编写自己的类加载器来实现您似乎要求的功能 - 一种“模糊”类名匹配。类加载器可以在某些目录中搜索与该类型部分匹配的类文件。

My feeling is that this is potentially a brittle solution, there's a danger of loading unexpected classes. I prefer Nim's suggestion of explicitly having a mapping table if you can't use an algorthimic classname generator.

我的感觉是,这可能是一个脆弱的解决方案,存在加载意外类的危险。如果你不能使用algorthimic classname生成器,我更喜欢Nim建议明确拥有映射表。

#1


0  

An alternative approach would be to use a map (or enum) which maps the id to the full class name.

另一种方法是使用将id映射到完整类名的map(或枚举)。

Pulling in the stuff from the comments, ensure that this mapping class is available as a jar (or may be in the same jar which contains the implementations of the packet handlers) as your "messaging layer" jar.

从注释中提取内容,确保此映射类可用作jar(或者可能位于包含数据包处理程序实现的同一jar中)作为“消息传递层”jar。

#2


0  

Like this, maybe?

像这样,也许吧?

packet = ClassUtils.newInstance(InPacket.class, "server.client.InPacket"+id+"Connect");
                                                                           ^^^^^^^^^^

(but I may have misunderstood your question)

(但我可能误解了你的问题)

#3


0  

Why can you not create the full class name?

为什么不能创建完整的类名?

packet = ClassUtils.newInstance(InPacket.class, "server.client.InPacket"+id + "Connect"

You could implement the functionality you seem to be asking for - a kind of "fuzzy" classname matching - by writing your own classloader. The classloader could search some directories for class files partially matching the type.

您可以通过编写自己的类加载器来实现您似乎要求的功能 - 一种“模糊”类名匹配。类加载器可以在某些目录中搜索与该类型部分匹配的类文件。

My feeling is that this is potentially a brittle solution, there's a danger of loading unexpected classes. I prefer Nim's suggestion of explicitly having a mapping table if you can't use an algorthimic classname generator.

我的感觉是,这可能是一个脆弱的解决方案,存在加载意外类的危险。如果你不能使用algorthimic classname生成器,我更喜欢Nim建议明确拥有映射表。