6

Triggered by this bug report AVRO-1814 I reduced the problem to this minimal example case in Java that simply shows the core of the effect.

package nl.basjes.experiment;

public class NamingClash {
  String nl = "foo";

  public void test() {
    nl.basjes.experiment.NamingClash.foo();
  }

  private static void foo() {
    // Do something
  }
}

Trying to compile this will give you

error: cannot find symbol
    nl.basjes.experiment.NamingClash.foo();
      ^
  symbol:   variable basjes
  location: variable nl of type String

In AVRO the code is generated and it must try to avoid name collisions under the assumption people will sometimes choose unexpected names.

So assume in this example that

  1. The fully qualified class name in the 'test()' method is needed to avoid a collision.
  2. The variable 'nl' is just the name used in the schema definition.
  3. Generating a field like _nl__ and have getters and setters would be a change that will break backwards compatibility because the nl field has always been public.

Other than telling people "Just don't do that".

Is there a solution to avoid these conflicts?


Note that for the AVRO bug that triggered this question I found a workaround. Here I'm looking for the 'generic answer'.

4

1 回答 1

1

我可以看到该问题的两种解决方案:

1) 使用仅由当前类名而不是完全限定名限定的方法名调用该方法:

public void option1() {
    NamingClash.foo();
}

2)通过当前类对象的this指针调用静态方法,并抑制“静态访问”警告。

@SuppressWarnings("static-access")
public void option2() {
    this.foo();
}
于 2016-04-28T15:26:10.170 回答