File: TypeParameterShadowing.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (57 lines) | stat: -rw-r--r-- 1,580 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
When declaring type parameters, it's possible to declare a type parameter with
the same name as another type parameter in scope causing unintended
incompatibilities when trying to use them together.

```java
public class Foo<T> {
    void instanceMethod(T t) {}
    <T> void genericMethod(T t) {
      instanceMethod(t); // FAIL: T declared in this method doesn't correspond to Foo<T>'s T
    }
}
```

In some cases, the type variable being declared has no relation to the type
variable being shadowed. It may be appropriate to rename the shadowing type
variable:

```java
class Logger<T> {
  void log(T t) {}
  <T> logOther(T t) { ... } // Really should be <O> void logOther(O o), since this T is unrelated.
}
```

Depending on the nature of the surrounding code, you might be able to remove the
generic declaration on a method, or convert the generic method into a static
method that doesn't inherit the surrounding type parameter:

```java
class Holder<T> {
  T held;

  public Foo<T> fooIt() {
    return fooify(held);
  }

  private <T> Foo<T> fooify(T t) { ... } // Could be static, or non-generic
}
```

If an inner class declaration shadows a type variable, you may be able to remove
the type variable, make it a static inner class, or rename the type variable:

```java
class BoxingBox<T> {
  // Works if you make the class static.
  // If you remove the type parameter, you'll need to update stuff to List<Container>
  class Container<T> {
    T held;
  }
  List<Container<T>> stuff = new ArrayList<>();

  T get(int index) {
    return stuff.get(index).held;
  }
}
```