gpt4 book ai didi

java - 如何使用 Java Reflection 将协议(protocol)字符串转换为 Spymemcached 协议(protocol)枚举?

转载 作者:行者123 更新时间:2023-12-01 23:12:51 24 4
gpt4 key购买 nike

我有一个使用 Spymemcached 的 Java 5 应用程序,当我想将协议(protocol)字符串转换为 Enum 对象时遇到问题。让我们看一下以下内容:

  1. 枚举协议(protocol)位于 ConnectionFactoryBuilder 类中。
package net.spy.memcached;

...

public class ConnectionFactoryBuilder {

public enum Protocol {
TEXT, BINARY

private Protocol() {
// compiled code
}
}
  • 现在我需要将字符串“TEXT”或“BINARY”转换为上面的 ConnectionFactoryBuilder.Protocol 枚举。我尝试了以下代码,但没有任何效果。
  • package mypackage;

    import java.lang.reflect.Field;

    public class Test {

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

    String protocolString = "TEXT";

    // code 1:
    String protocolClassName1 = "net.spy.memcached.ConnectionFactoryBuilder.Protocol";
    Class protocolType1 = Class.forName(protocolClassName1);
    // The above line throws: Exception in thread "main" java.lang.ClassNotFoundException: net.spy.memcached.ConnectionFactoryBuilder.Protocol
    Object protocolEnumObject1 = Enum.valueOf(protocolType1, protocolString);


    // code 2:
    Class connectionFactoryBuilderClass2 = Class.forName("net.spy.memcached.ConnectionFactoryBuilder");
    Field protocolField2 = null;
    for (Field f : connectionFactoryBuilderClass2.getDeclaredFields()) {
    System.out.print(f.getName() + ", ");
    if (f.getName().equalsIgnoreCase("Protocol")) {
    protocolField2 = f;
    }
    }
    Class protocolType2 = protocolField2.getType();
    Object protocolEnumObject2 = Enum.valueOf(protocolType2, protocolString);
    // In this approach, i can not get protocol field in that ConnectionFactoryBuilder class, here are output of getDeclaredFields():
    // opQueueFactory, readQueueFactory, writeQueueFactory, transcoder, failureMode,
    // initialObservers, opFact, locator, opTimeout, isDaemon, shouldOptimize, useNagle,
    // maxReconnectDelay, readBufSize, hashAlg, authDescriptor, opQueueMaxBlockTime,
    // timeoutExceptionThreshold, metricType, collector, executorService, authWaitTime, $assertionsDisabled,

    }

    }

    请帮忙,谢谢!

    最佳答案

    好的,我已经找到解决这个问题的方法了。基本上,枚举协议(protocol)是 ConnectionFactoryBuilder 中的嵌套类,因此我需要使用 $ 来定位它。

    这里是已解决的代码:

    package mypackage;

    public class Test {

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

    String protocolString = "TEXT";

    // code 1:
    // OLD: String protocolClassName1 = "net.spy.memcached.ConnectionFactoryBuilder.Protocol";
    String protocolClassName1 = "net.spy.memcached.ConnectionFactoryBuilder$Protocol";
    Class protocolType1 = Class.forName(protocolClassName1);
    Object protocolEnumObject1 = Enum.valueOf(protocolType1, protocolString);

    System.out.println(protocolEnumObject1.getClass());
    // class net.spy.memcached.ConnectionFactoryBuilder$Protocol

    }

    }

    关于java - 如何使用 Java Reflection 将协议(protocol)字符串转换为 Spymemcached 协议(protocol)枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58365909/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com