ByteBuddy 子类构造函数

ByteBuddy subclass constructor

我正在尝试在 bytebuddy 中创建抽象 class 的子class,并想用我自己的函数覆盖构造函数。我不能让它与 defineConstructor 一起工作。

超级class:

public abstract class AbstractDMTable {
protected HashMap<String, DMEntry<?>> parameterMap;

public DMEntry<?> getParameter(String paramName) {
    if (parameterMap.containsKey(paramName))
        return parameterMap.get(paramName);
    return null;
}...

子class:

public class DMTable_DEBUGOUT extends AbstractDMTable {
/**
 * Table entry
 * prints the value of the specified parameter
 */
public DMEntry<DMEntry<?>> DEBUG_PARAM;

/**
 * Table entry
 * execution interval of the step handler (s)
 */
public DMEntry<Double> EXEC_INTERVAL;

/**
 * Table entry
 * active / not active status of this subsystem
 */
public DMEntry<Boolean> IS_ACTIVE;

/**
 * Standard constructor. Creates a new table and initializes all entry fields with all entry values set to {@code null}
 */
public DMTable_DEBUGOUT() {
    super();
    DEBUG_PARAM = new DMEntry<>();
    parameterMap.put("DEBUG_PARAM", DEBUG_PARAM);
    EXEC_INTERVAL = new DMEntry<>();
    parameterMap.put("EXEC_INTERVAL", EXEC_INTERVAL);
    IS_ACTIVE = new DMEntry<>();
    parameterMap.put("IS_ACTIVE", IS_ACTIVE);
}
}

我的字节好友:

    DynamicType.Builder<? extends AbstractDMTable> subsystem = new ByteBuddy().subclass(AbstractDMTable.class)
            .name("DMTable_" + name).defineConstructor(Collections.<Class<AbstractDMTable>> emptyList(), Visibility.PUBLIC);
    for (Entry<String, Pair<String, String>> p : t.getValue().entrySet()) {
        subsystem.defineField(p.getKey(), this.createSubSystemEntry(p).getClass(), Visibility.PUBLIC);
    }
    // subsystem.defineConstructor(Arrays.<Class<AbstractDMTable>>
    // asList(int.class), Visibility.PUBLIC);
    return subsystem.make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();

错误:

defineConstructor(ModifierContributor.ForMethod...) in the type
DynamicType.Builder<AbstractDMTable> is not applicable for the
arguments (List<Class<?>>, Visibility)  DynamicDatabaseGenerator.java
line 66 Java Problem

您正在使用模仿超级 class 构造函数的默认构造函数策略。重载 subclass 方法是为了避免这种重复定义,方法是使用不模仿 super class 的不同构造函数策略。

此外,您应该更新 Byte Buddy,这样您会收到更好的错误消息。