Attribute.java revision 3d2df35d983a31bc7a68e1d76b7c71956f477871
1/*
2 * Copyright 2016 Google Inc. All Rights Reserved.
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
17package com.google.turbine.bytecode;
18
19import com.google.common.collect.ImmutableList;
20import com.google.turbine.bytecode.ClassFile.AnnotationInfo;
21import com.google.turbine.bytecode.ClassFile.MethodInfo.ParameterInfo;
22import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo;
23import com.google.turbine.model.Const.Value;
24import java.util.List;
25
26/** Well-known JVMS §4.1 attributes. */
27interface Attribute {
28
29  enum Kind {
30    SIGNATURE("Signature"),
31    EXCEPTIONS("Exceptions"),
32    INNER_CLASSES("InnerClasses"),
33    CONSTANT_VALUE("ConstantValue"),
34    RUNTIME_VISIBLE_ANNOTATIONS("RuntimeVisibleAnnotations"),
35    RUNTIME_INVISIBLE_ANNOTATIONS("RuntimeInvisibleAnnotations"),
36    ANNOTATION_DEFAULT("AnnotationDefault"),
37    RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS("RuntimeVisibleParameterAnnotations"),
38    RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS("RuntimeInvisibleParameterAnnotations"),
39    RUNTIME_VISIBLE_TYPE_ANNOTATIONS("RuntimeVisibleTypeAnnotations"),
40    RUNTIME_INVISIBLE_TYPE_ANNOTATIONS("RuntimeInvisibleTypeAnnotations"),
41    METHOD_PARAMETERS("MethodParameters");
42
43    private final String signature;
44
45    Kind(String signature) {
46      this.signature = signature;
47    }
48
49    public String signature() {
50      return signature;
51    }
52  }
53
54  Kind kind();
55
56  /** A JVMS §4.7.6 InnerClasses attribute. */
57  class InnerClasses implements Attribute {
58
59    final List<ClassFile.InnerClass> inners;
60
61    public InnerClasses(List<ClassFile.InnerClass> inners) {
62      this.inners = inners;
63    }
64
65    @Override
66    public Kind kind() {
67      return Kind.INNER_CLASSES;
68    }
69  }
70
71  /** A JVMS §4.7.9 Signature attribute. */
72  class Signature implements Attribute {
73
74    final String signature;
75
76    public Signature(String signature) {
77      this.signature = signature;
78    }
79
80    @Override
81    public Kind kind() {
82      return Kind.SIGNATURE;
83    }
84  }
85
86  /** A JVMS §4.7.5 Exceptions attribute. */
87  class ExceptionsAttribute implements Attribute {
88
89    final List<String> exceptions;
90
91    public ExceptionsAttribute(List<String> exceptions) {
92      this.exceptions = exceptions;
93    }
94
95    @Override
96    public Kind kind() {
97      return Kind.EXCEPTIONS;
98    }
99  }
100
101  interface Annotations extends Attribute {
102    List<AnnotationInfo> annotations();
103  }
104
105  /** A JVMS §4.7.16 RuntimeVisibleAnnotations attribute. */
106  class RuntimeVisibleAnnotations implements Annotations {
107    List<AnnotationInfo> annotations;
108
109    public RuntimeVisibleAnnotations(List<AnnotationInfo> annotations) {
110      this.annotations = annotations;
111    }
112
113    @Override
114    public List<AnnotationInfo> annotations() {
115      return annotations;
116    }
117
118    @Override
119    public Kind kind() {
120      return Kind.RUNTIME_VISIBLE_ANNOTATIONS;
121    }
122  }
123
124  /** A JVMS §4.7.17 RuntimeInvisibleAnnotations attribute. */
125  class RuntimeInvisibleAnnotations implements Annotations {
126    List<AnnotationInfo> annotations;
127
128    public RuntimeInvisibleAnnotations(List<AnnotationInfo> annotations) {
129      this.annotations = annotations;
130    }
131
132    @Override
133    public List<AnnotationInfo> annotations() {
134      return annotations;
135    }
136
137    @Override
138    public Kind kind() {
139      return Kind.RUNTIME_INVISIBLE_ANNOTATIONS;
140    }
141  }
142
143  /** A JVMS §4.7.2 ConstantValue attribute. */
144  class ConstantValue implements Attribute {
145
146    final Value value;
147
148    public ConstantValue(Value value) {
149      this.value = value;
150    }
151
152    @Override
153    public Kind kind() {
154      return Kind.CONSTANT_VALUE;
155    }
156  }
157
158  /** A JVMS §4.7.22 AnnotationDefault attribute. */
159  class AnnotationDefault implements Attribute {
160
161    private final AnnotationInfo.ElementValue value;
162
163    public AnnotationDefault(AnnotationInfo.ElementValue value) {
164      this.value = value;
165    }
166
167    @Override
168    public Kind kind() {
169      return Kind.ANNOTATION_DEFAULT;
170    }
171
172    public AnnotationInfo.ElementValue value() {
173      return value;
174    }
175  }
176
177  interface ParameterAnnotations extends Attribute {
178    List<List<AnnotationInfo>> annotations();
179  }
180
181  /** A JVMS §4.7.18 RuntimeVisibleParameterAnnotations attribute. */
182  class RuntimeVisibleParameterAnnotations implements ParameterAnnotations {
183
184    @Override
185    public List<List<AnnotationInfo>> annotations() {
186      return annotations;
187    }
188
189    final List<List<AnnotationInfo>> annotations;
190
191    public RuntimeVisibleParameterAnnotations(List<List<AnnotationInfo>> annotations) {
192      this.annotations = annotations;
193    }
194
195    @Override
196    public Kind kind() {
197      return Kind.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS;
198    }
199  }
200
201  /** A JVMS §4.7.19 RuntimeInvisibleParameterAnnotations attribute. */
202  class RuntimeInvisibleParameterAnnotations implements ParameterAnnotations {
203
204    @Override
205    public List<List<AnnotationInfo>> annotations() {
206      return annotations;
207    }
208
209    final List<List<AnnotationInfo>> annotations;
210
211    public RuntimeInvisibleParameterAnnotations(List<List<AnnotationInfo>> annotations) {
212      this.annotations = annotations;
213    }
214
215    @Override
216    public Kind kind() {
217      return Kind.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS;
218    }
219  }
220
221  interface TypeAnnotations extends Attribute {
222    ImmutableList<TypeAnnotationInfo> annotations();
223  }
224
225  /** A JVMS §4.7.20 RuntimeInvisibleTypeAnnotations attribute. */
226  class RuntimeInvisibleTypeAnnotations implements TypeAnnotations {
227    final ImmutableList<TypeAnnotationInfo> annotations;
228
229    public RuntimeInvisibleTypeAnnotations(ImmutableList<TypeAnnotationInfo> annotations) {
230      this.annotations = annotations;
231    }
232
233    @Override
234    public Kind kind() {
235      return Kind.RUNTIME_INVISIBLE_TYPE_ANNOTATIONS;
236    }
237
238    @Override
239    public ImmutableList<TypeAnnotationInfo> annotations() {
240      return annotations;
241    }
242  }
243
244  /** A JVMS §4.7.20 RuntimeVisibleTypeAnnotations attribute. */
245  class RuntimeVisibleTypeAnnotations implements TypeAnnotations {
246    final ImmutableList<TypeAnnotationInfo> annotations;
247
248    public RuntimeVisibleTypeAnnotations(ImmutableList<TypeAnnotationInfo> annotations) {
249      this.annotations = annotations;
250    }
251
252    @Override
253    public Kind kind() {
254      return Kind.RUNTIME_VISIBLE_TYPE_ANNOTATIONS;
255    }
256
257    @Override
258    public ImmutableList<TypeAnnotationInfo> annotations() {
259      return annotations;
260    }
261  }
262
263  /** A JVMS §4.7.24 MethodParameters attribute. */
264  class MethodParameters implements Attribute {
265    private final ImmutableList<ParameterInfo> parameters;
266
267    public MethodParameters(ImmutableList<ParameterInfo> parameters) {
268      this.parameters = parameters;
269    }
270
271    /** The parameters. */
272    public ImmutableList<ParameterInfo> parameters() {
273      return parameters;
274    }
275
276    @Override
277    public Kind kind() {
278      return Kind.METHOD_PARAMETERS;
279    }
280  }
281}
282