3

I am trying to create a subclass of an abstract class in bytebuddy and want to override the constructor with my own function. I can not make it work with defineConstructor.

Superclass:

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

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

Subclass:

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);
}
}

My ByteBuddy:

    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();

The Error:

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

1 Answers1

2

You are using the default constructor strategy which imitates the super class constructors. The subclass method is overloaded to avoid this duplicate definition by using a different constructor strategy that does not imitate the super class.

Also, you should update Byte Buddy, this way you would get a better error message.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192