• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Bazel Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 package com.google.devtools.build.android.desugar.testdata.java8;
15 
16 /**
17  * The base interface, which is generic, and has two default methods. These two default methods will
18  * introduce bridge methods in the child-interfaces
19  */
20 interface GenericInterfaceWithDefaultMethod<T extends Number> {
copy(T t)21   default T copy(T t) {
22     return t;
23   }
24 
getNumber()25   default Number getNumber() {
26     return 1;
27   }
28 }
29 
30 /** This interface generate two additional bridge methods */
31 interface InterfaceWithDefaultAndBridgeMethods extends GenericInterfaceWithDefaultMethod<Integer> {
32   @Override
copy(Integer t)33   default Integer copy(Integer t) {
34     return GenericInterfaceWithDefaultMethod.super.copy(t);
35   }
36 
37   @Override
getNumber()38   default Double getNumber() {
39     return 2.3d;
40   }
41 }
42 
43 /** A class implementing the interface. */
44 class ClassWithDefaultAndBridgeMethods implements InterfaceWithDefaultAndBridgeMethods {}
45 
46 /** The client class that uses the interfaces and the class that implements the interfaces. */
47 public class DefaultInterfaceWithBridges {
48   private final ClassWithDefaultAndBridgeMethods c = new ClassWithDefaultAndBridgeMethods();
49 
copy(Integer i)50   public Integer copy(Integer i) {
51     return c.copy(i);
52   }
53 
getNumber()54   public Number getNumber() {
55     return ((GenericInterfaceWithDefaultMethod) c).getNumber();
56   }
57 
getDouble()58   public Double getDouble() {
59     return c.getNumber();
60   }
61 
62   @SuppressWarnings({"rawtypes", "unchecked"})
copy(Number n)63   public Number copy(Number n) {
64     return ((GenericInterfaceWithDefaultMethod) c).copy(n);
65   }
66 }
67