Skip to content
Jonathan Pobst edited this page Apr 6, 2021 · 3 revisions

When the bindings process sees something in the Java API that looks like an "event", it creates a C# event so that it feels more familiar for .NET users.

For example:

public class MyClass
{
    public final void addOnPropertyChangeListener (OnPropertyChangeListener listener) { ... }
}

Generates:

public class MyClass
{
    public unsafe void AddOnPropertyChangeListener (IOnPropertyChangeListener p0) { ... }

    public event EventHandler<PropertyChangeEventArgs> PropertyChange;
}

The bindings process recognizes this pattern for:

  • addXXXListener
  • setXXXListener

Normally this is fine, however sometimes *both* of these methods exist in the same class for the same listener, which causes duplicate events to get generated:

public class MyClass
{
    public unsafe void AddOnPropertyChangeListener (IOnPropertyChangeListener listener) { ... }
    public unsafe void SetOnPropertyChangeListener (IOnPropertyChangeListener listener) { ... }

    public event EventHandler<PropertyChangeEventArgs> PropertyChange;
    public event EventHandler<PropertyChangeEventArgs> PropertyChange;
}

This results in compilation errors like:

Error CS0102: The type 'MyClass' already contains a definition for 'PropertyChange'

In order to fix this, we can use metadata on either method to specify that an event should not be generated for it, thus removing the duplicate. This is accomplished by specifying an empty value for the eventName attribute:

<attr path="/api/package[@name='com.example']/class[@name='MyClass']/method[@name='setOnPropertyChangeListener']" name="eventName"></attr>

Now the event will be generated for addOnPropertyChangeListener but not setOnPropertyChangeListener, which fixes the duplication:

public class MyClass
{
    public unsafe void AddOnPropertyChangeListener (IOnPropertyChangeListener listener) { ... }
    public unsafe void SetOnPropertyChangeListener (IOnPropertyChangeListener listener) { ... }

    public event EventHandler<Com.Example.PropertyChangeEventArgs> PropertyChange;
}

Note: The eventName attribute can also be used to specify a different name for the event if desired.