1 /*
2  * Copyright 2014 - 2020 Rafael Winterhalter
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.bytebuddy.dynamic;
17
18 import net.bytebuddy.description.method.MethodDescription;
19
20 /**
21  * Implements a strategy for deciding if a visibility bridge should be generated. A visibility bridge is necessary
22  * if a public type inherits a public method from a package-private type to allow for invoking that method without
23  * specific privileges as the reflection API only considers the method's declaring type.
24  */

25 public interface VisibilityBridgeStrategy {
26
27     /**
28      * Determines if a visibility bridge should be generated for a method that is eligable.
29      *
30      * @param methodDescription The method that would require a visibility bridge.
31      * @return {@code trueif a visibility bridge should be generated.
32      */

33     boolean generateVisibilityBridge(MethodDescription methodDescription);
34
35     /**
36      * Default implementations of visibility bridge strategies.
37      */

38     enum Default implements VisibilityBridgeStrategy {
39
40         /**
41          * Always generates a visibility bridge.
42          */

43         ALWAYS {
44             /**
45              * {@inheritDoc}
46              */

47             public boolean generateVisibilityBridge(MethodDescription methodDescription) {
48                 return true;
49             }
50         },
51
52         /**
53          * Only generates visibility bridges for non-generified methods what was the behavior of <i>javac</i> until Java 11.
54          */

55         ON_NON_GENERIC_METHOD {
56             /**
57              * {@inheritDoc}
58              */

59             public boolean generateVisibilityBridge(MethodDescription methodDescription) {
60                 return !methodDescription.isGenerified();
61             }
62         },
63
64         /**
65          * Never generates a visibility bridge.
66          */

67         NEVER {
68             /**
69              * {@inheritDoc}
70              */

71             public boolean generateVisibilityBridge(MethodDescription methodDescription) {
72                 return false;
73             }
74         }
75     }
76 }
77