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.implementation.attribute;
17
18 import net.bytebuddy.build.HashCodeAndEqualsPlugin;
19 import net.bytebuddy.description.annotation.AnnotationDescription;
20 import net.bytebuddy.description.field.FieldDescription;
21 import net.bytebuddy.description.type.TypeDescription;
22 import net.bytebuddy.jar.asm.FieldVisitor;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 /**
29  * An appender that writes attributes or annotations to a given ASM {@link net.bytebuddy.jar.asm.FieldVisitor}.
30  */

31 public interface FieldAttributeAppender {
32
33     /**
34      * Applies this attribute appender to a given field visitor.
35      *
36      * @param fieldVisitor          The field visitor to which the attributes that are represented by this attribute appender are written to.
37      * @param fieldDescription      The description of the field to which the field visitor belongs to.
38      * @param annotationValueFilter The annotation value filter to apply when writing annotations.
39      */

40     void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter);
41
42     /**
43      * A field attribute appender that does not append any attributes.
44      */

45     enum NoOp implements FieldAttributeAppender, Factory {
46
47         /**
48          * The singleton instance.
49          */

50         INSTANCE;
51
52         /**
53          * {@inheritDoc}
54          */

55         public FieldAttributeAppender make(TypeDescription typeDescription) {
56             return this;
57         }
58
59         /**
60          * {@inheritDoc}
61          */

62         public void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter) {
63             /* do nothing */
64         }
65     }
66
67     /**
68      * A factory that creates field attribute appenders for a given type.
69      */

70     interface Factory {
71
72         /**
73          * Returns a field attribute appender that is applicable for a given type description.
74          *
75          * @param typeDescription The type for which a field attribute appender is to be applied for.
76          * @return The field attribute appender which should be applied for the given type.
77          */

78         FieldAttributeAppender make(TypeDescription typeDescription);
79
80         /**
81          * A field attribute appender factory that combines several field attribute appender factories to be
82          * represented as a single factory.
83          */

84         @HashCodeAndEqualsPlugin.Enhance
85         class Compound implements Factory {
86
87             /**
88              * The factories that this compound factory represents in their application order.
89              */

90             private final List<Factory> factories;
91
92             /**
93              * Creates a new compound field attribute appender factory.
94              *
95              * @param factory The factories to represent in the order of their application.
96              */

97             public Compound(Factory... factory) {
98                 this(Arrays.asList(factory));
99             }
100
101             /**
102              * Creates a new compound field attribute appender factory.
103              *
104              * @param factories The factories to represent in the order of their application.
105              */

106             public Compound(List<? extends Factory> factories) {
107                 this.factories = new ArrayList<Factory>();
108                 for (Factory factory : factories) {
109                     if (factory instanceof Compound) {
110                         this.factories.addAll(((Compound) factory).factories);
111                     } else if (!(factory instanceof NoOp)) {
112                         this.factories.add(factory);
113                     }
114                 }
115             }
116
117             /**
118              * {@inheritDoc}
119              */

120             public FieldAttributeAppender make(TypeDescription typeDescription) {
121                 List<FieldAttributeAppender> fieldAttributeAppenders = new ArrayList<FieldAttributeAppender>(factories.size());
122                 for (Factory factory : factories) {
123                     fieldAttributeAppenders.add(factory.make(typeDescription));
124                 }
125                 return new FieldAttributeAppender.Compound(fieldAttributeAppenders);
126             }
127         }
128     }
129
130     /**
131      * An attribute appender that writes all annotations that are declared on a field.
132      */

133     enum ForInstrumentedField implements FieldAttributeAppender, Factory {
134
135         /**
136          * The singleton instance.
137          */

138         INSTANCE;
139
140         /**
141          * {@inheritDoc}
142          */

143         public void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter) {
144             AnnotationAppender annotationAppender = new AnnotationAppender.Default(new AnnotationAppender.Target.OnField(fieldVisitor));
145             annotationAppender = fieldDescription.getType().accept(AnnotationAppender.ForTypeAnnotations.ofFieldType(annotationAppender, annotationValueFilter));
146             for (AnnotationDescription annotation : fieldDescription.getDeclaredAnnotations()) {
147                 annotationAppender = annotationAppender.append(annotation, annotationValueFilter);
148             }
149         }
150
151         /**
152          * {@inheritDoc}
153          */

154         public FieldAttributeAppender make(TypeDescription typeDescription) {
155             return this;
156         }
157     }
158
159     /**
160      * Appends an annotation to a field. The visibility of the annotation is determined by the annotation type's
161      * {@link java.lang.annotation.RetentionPolicy} annotation.
162      */

163     @HashCodeAndEqualsPlugin.Enhance
164     class Explicit implements FieldAttributeAppender, Factory {
165
166         /**
167          * The annotations that this appender appends.
168          */

169         private final List<? extends AnnotationDescription> annotations;
170
171         /**
172          * Creates a new annotation attribute appender for explicit annotation values. All values, including default values, are copied.
173          *
174          * @param annotations The annotations to be appended to the field.
175          */

176         public Explicit(List<? extends AnnotationDescription> annotations) {
177             this.annotations = annotations;
178         }
179
180         /**
181          * {@inheritDoc}
182          */

183         public void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter) {
184             AnnotationAppender appender = new AnnotationAppender.Default(new AnnotationAppender.Target.OnField(fieldVisitor));
185             for (AnnotationDescription annotation : annotations) {
186                 appender = appender.append(annotation, annotationValueFilter);
187             }
188         }
189
190         /**
191          * {@inheritDoc}
192          */

193         public FieldAttributeAppender make(TypeDescription typeDescription) {
194             return this;
195         }
196     }
197
198     /**
199      * A field attribute appender that combines several method attribute appenders to be represented as a single
200      * field attribute appender.
201      */

202     @HashCodeAndEqualsPlugin.Enhance
203     class Compound implements FieldAttributeAppender {
204
205         /**
206          * The field attribute appenders this appender represents in their application order.
207          */

208         private final List<FieldAttributeAppender> fieldAttributeAppenders;
209
210         /**
211          * Creates a new compound field attribute appender.
212          *
213          * @param fieldAttributeAppender The field attribute appenders that are to be combined by this compound appender
214          *                               in the order of their application.
215          */

216         public Compound(FieldAttributeAppender... fieldAttributeAppender) {
217             this(Arrays.asList(fieldAttributeAppender));
218         }
219
220         /**
221          * Creates a new compound field attribute appender.
222          *
223          * @param fieldAttributeAppenders The field attribute appenders that are to be combined by this compound appender
224          *                                in the order of their application.
225          */

226         public Compound(List<? extends FieldAttributeAppender> fieldAttributeAppenders) {
227             this.fieldAttributeAppenders = new ArrayList<FieldAttributeAppender>();
228             for (FieldAttributeAppender fieldAttributeAppender : fieldAttributeAppenders) {
229                 if (fieldAttributeAppender instanceof Compound) {
230                     this.fieldAttributeAppenders.addAll(((Compound) fieldAttributeAppender).fieldAttributeAppenders);
231                 } else if (!(fieldAttributeAppender instanceof NoOp)) {
232                     this.fieldAttributeAppenders.add(fieldAttributeAppender);
233                 }
234             }
235         }
236
237         /**
238          * {@inheritDoc}
239          */

240         public void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter) {
241             for (FieldAttributeAppender fieldAttributeAppender : fieldAttributeAppenders) {
242                 fieldAttributeAppender.apply(fieldVisitor, fieldDescription, annotationValueFilter);
243             }
244         }
245     }
246 }
247