1 /*
  2  * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package java.lang.classfile;
 26 
 27 import java.lang.classfile.AttributeMapper.AttributeStability;
 28 import java.lang.classfile.attribute.*;
 29 
 30 import jdk.internal.classfile.impl.AbstractAttributeMapper.*;
 31 import jdk.internal.javac.PreviewFeature;
 32 
 33 /**
 34  * Attribute mappers for predefined (JVMS {@jvms 4.7}) and JDK-specific
 35  * nonstandard attributes.
 36  * <p>
 37  * Unless otherwise specified, each mapper returned by methods in this class:
 38  * <ul>
 39  * <li>is predefined in the JVMS instead of JDK-specific;
 40  * <li>does not permit {@linkplain AttributeMapper#allowMultiple() multiple
 41  * attribute instances} in the same structure;
 42  * <li>the attribute has a {@linkplain AttributeMapper#stability() data
 43  * dependency} on the {@linkplain AttributeStability#CP_REFS constant pool}.
 44  * </ul>
 45  *
 46  * @see AttributeMapper
 47  * @see java.lang.classfile.attribute
 48  * @since 24
 49  */
 50 public final class Attributes {
 51 
 52     /** AnnotationDefault */
 53     public static final String NAME_ANNOTATION_DEFAULT = "AnnotationDefault";
 54 
 55     /** BootstrapMethods */
 56     public static final String NAME_BOOTSTRAP_METHODS = "BootstrapMethods";
 57 
 58     /** CharacterRangeTable */
 59     public static final String NAME_CHARACTER_RANGE_TABLE = "CharacterRangeTable";
 60 
 61     /** Code */
 62     public static final String NAME_CODE = "Code";
 63 
 64     /** CompilationID */
 65     public static final String NAME_COMPILATION_ID = "CompilationID";
 66 
 67     /** ConstantValue */
 68     public static final String NAME_CONSTANT_VALUE = "ConstantValue";
 69 
 70     /** Deprecated */
 71     public static final String NAME_DEPRECATED = "Deprecated";
 72 
 73     /** EnclosingMethod */
 74     public static final String NAME_ENCLOSING_METHOD = "EnclosingMethod";
 75 
 76     /** Exceptions */
 77     public static final String NAME_EXCEPTIONS = "Exceptions";
 78 
 79     /** InnerClasses */
 80     public static final String NAME_INNER_CLASSES = "InnerClasses";
 81 
 82     /** LineNumberTable */
 83     public static final String NAME_LINE_NUMBER_TABLE = "LineNumberTable";
 84 
 85     /** LoadableDescriptors */
 86     public static final String NAME_LOADABLE_DESCRIPTORS = "LoadableDescriptors";
 87 
 88     /** LocalVariableTable */
 89     public static final String NAME_LOCAL_VARIABLE_TABLE = "LocalVariableTable";
 90 
 91     /** LocalVariableTypeTable */
 92     public static final String NAME_LOCAL_VARIABLE_TYPE_TABLE = "LocalVariableTypeTable";
 93 
 94     /** MethodParameters */
 95     public static final String NAME_METHOD_PARAMETERS = "MethodParameters";
 96 
 97     /** Module */
 98     public static final String NAME_MODULE = "Module";
 99 
100     /** ModuleHashes */
101     public static final String NAME_MODULE_HASHES = "ModuleHashes";
102 
103     /** ModuleMainClass */
104     public static final String NAME_MODULE_MAIN_CLASS = "ModuleMainClass";
105 
106     /** ModulePackages */
107     public static final String NAME_MODULE_PACKAGES = "ModulePackages";
108 
109     /** ModuleResolution */
110     public static final String NAME_MODULE_RESOLUTION = "ModuleResolution";
111 
112     /** ModuleTarget */
113     public static final String NAME_MODULE_TARGET = "ModuleTarget";
114 
115     /** NestHost */
116     public static final String NAME_NEST_HOST = "NestHost";
117 
118     /** NestMembers */
119     public static final String NAME_NEST_MEMBERS = "NestMembers";
120 
121     /** PermittedSubclasses */
122     public static final String NAME_PERMITTED_SUBCLASSES = "PermittedSubclasses";
123 
124     /** Record */
125     public static final String NAME_RECORD = "Record";
126 
127     /** RuntimeInvisibleAnnotations */
128     public static final String NAME_RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations";
129 
130     /** RuntimeInvisibleParameterAnnotations */
131     public static final String NAME_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS = "RuntimeInvisibleParameterAnnotations";
132 
133     /** RuntimeInvisibleTypeAnnotations */
134     public static final String NAME_RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations";
135 
136     /** RuntimeVisibleAnnotations */
137     public static final String NAME_RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations";
138 
139     /** RuntimeVisibleParameterAnnotations */
140     public static final String NAME_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = "RuntimeVisibleParameterAnnotations";
141 
142     /** RuntimeVisibleTypeAnnotations */
143     public static final String NAME_RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations";
144 
145     /** Signature */
146     public static final String NAME_SIGNATURE = "Signature";
147 
148     /** SourceDebugExtension */
149     public static final String NAME_SOURCE_DEBUG_EXTENSION = "SourceDebugExtension";
150 
151     /** SourceFile */
152     public static final String NAME_SOURCE_FILE = "SourceFile";
153 
154     /** SourceID */
155     public static final String NAME_SOURCE_ID = "SourceID";
156 
157     /** StackMapTable */
158     public static final String NAME_STACK_MAP_TABLE = "StackMapTable";
159 
160     /** Synthetic */
161     public static final String NAME_SYNTHETIC = "Synthetic";
162 
163     private Attributes() {
164     }
165 
166     /**
167      * {@return the mapper for the {@code AnnotationDefault} attribute}
168      */
169     public static AttributeMapper<AnnotationDefaultAttribute> annotationDefault() {
170         return AnnotationDefaultMapper.INSTANCE;
171     }
172 
173     /**
174      * {@return the mapper for the {@code BootstrapMethods} attribute}
175      */
176     public static AttributeMapper<BootstrapMethodsAttribute> bootstrapMethods() {
177         return BootstrapMethodsMapper.INSTANCE;
178     }
179 
180     /**
181      * {@return the mapper for the {@code CharacterRangeTable} attribute}
182      * This is a JDK-specific attribute.
183      * The mapper permits multiple instances in a {@code Code} attribute, but this
184      * attribute should be only emitted once.
185      * This has a data dependency on {@linkplain AttributeStability#LABELS labels}.
186      */
187     public static AttributeMapper<CharacterRangeTableAttribute> characterRangeTable() {
188         return CharacterRangeTableMapper.INSTANCE;
189     }
190 
191     /**
192      * {@return the mapper for the {@code Code} attribute}
193      */
194     public static AttributeMapper<CodeAttribute> code() {
195         return CodeMapper.INSTANCE;
196     }
197 
198     /**
199      * {@return the mapper for the {@code CompilationID} attribute}
200      * This is a JDK-specific attribute.
201      */
202     public static AttributeMapper<CompilationIDAttribute> compilationId() {
203         return CompilationIDMapper.INSTANCE;
204     }
205 
206     /**
207      * {@return the mapper for the {@code ConstantValue} attribute}
208      */
209     public static AttributeMapper<ConstantValueAttribute> constantValue() {
210         return ConstantValueMapper.INSTANCE;
211     }
212 
213     /**
214      * {@return the mapper for the {@code Deprecated} attribute}
215      * The mapper permits multiple instances in a given location.
216      * This has {@linkplain AttributeStability#STATELESS no data dependency}.
217      */
218     public static AttributeMapper<DeprecatedAttribute> deprecated() {
219         return DeprecatedMapper.INSTANCE;
220     }
221 
222     /**
223      * {@return the mapper for the {@code EnclosingMethod} attribute}
224      */
225     public static AttributeMapper<EnclosingMethodAttribute> enclosingMethod() {
226         return EnclosingMethodMapper.INSTANCE;
227     }
228 
229     /**
230      * {@return the mapper for the {@code Exceptions} attribute}
231      */
232     public static AttributeMapper<ExceptionsAttribute> exceptions() {
233         return ExceptionsMapper.INSTANCE;
234     }
235 
236     /**
237      * {@return the mapper for the {@code InnerClasses} attribute}
238      */
239     public static AttributeMapper<InnerClassesAttribute> innerClasses() {
240         return InnerClassesMapper.INSTANCE;
241     }
242 
243     /**
244      * {@return the mapper for the {@code LineNumberTable} attribute}
245      * The mapper permits multiple instances in a {@code Code} attribute.
246      * This has a data dependency on {@linkplain AttributeStability#LABELS labels}.
247      */
248     public static AttributeMapper<LineNumberTableAttribute> lineNumberTable() {
249         return LineNumberTableMapper.INSTANCE;
250     }
251 
252     /**
253      * {@return Attribute mapper for the {@code LoadableDescriptors} attribute}
254      * @since Valhalla
255      */
256     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
257     public static AttributeMapper<LoadableDescriptorsAttribute> loadableDescriptors() {
258         return LoadableDescriptorsMapper.INSTANCE;
259     }
260 
261     /**
262      * {@return the mapper for the {@code LocalVariableTable} attribute}
263      * The mapper permits multiple instances in a {@code Code} attribute.
264      * This has a data dependency on {@linkplain AttributeStability#LABELS labels}.
265      */
266     public static AttributeMapper<LocalVariableTableAttribute> localVariableTable() {
267         return LocalVariableTableMapper.INSTANCE;
268     }
269 
270     /**
271      * {@return the mapper for the {@code LocalVariableTypeTable} attribute}
272      * The mapper permits multiple instances in a given location.
273      * This has a data dependency on {@linkplain AttributeStability#LABELS labels}.
274      */
275     public static AttributeMapper<LocalVariableTypeTableAttribute> localVariableTypeTable() {
276         return LocalVariableTypeTableMapper.INSTANCE;
277     }
278 
279     /**
280      * {@return the mapper for the {@code MethodParameters} attribute}
281      */
282     public static AttributeMapper<MethodParametersAttribute> methodParameters() {
283         return MethodParametersMapper.INSTANCE;
284     }
285 
286     /**
287      * {@return the mapper for the {@code Module} attribute}
288      */
289     public static AttributeMapper<ModuleAttribute> module() {
290         return ModuleMapper.INSTANCE;
291     }
292 
293     /**
294      * {@return the mapper for the {@code ModuleHashes} attribute}
295      * This is a JDK-specific attribute.
296      */
297     public static AttributeMapper<ModuleHashesAttribute> moduleHashes() {
298         return ModuleHashesMapper.INSTANCE;
299     }
300 
301     /**
302      * {@return the mapper for the {@code ModuleMainClass} attribute}
303      */
304     public static AttributeMapper<ModuleMainClassAttribute> moduleMainClass() {
305         return ModuleMainClassMapper.INSTANCE;
306     }
307 
308     /**
309      * {@return the mapper for the {@code ModulePackages} attribute}
310      */
311     public static AttributeMapper<ModulePackagesAttribute> modulePackages() {
312         return ModulePackagesMapper.INSTANCE;
313     }
314 
315     /**
316      * {@return the mapper for the {@code ModuleResolution} attribute}
317      * This is a JDK-specific attribute.
318      * This has {@linkplain AttributeStability#STATELESS no data dependency}.
319      */
320     public static AttributeMapper<ModuleResolutionAttribute> moduleResolution() {
321         return ModuleResolutionMapper.INSTANCE;
322     }
323 
324     /**
325      * {@return the mapper for the {@code ModuleTarget} attribute}
326      * This is a JDK-specific attribute.
327      */
328     public static AttributeMapper<ModuleTargetAttribute> moduleTarget() {
329         return ModuleTargetMapper.INSTANCE;
330     }
331 
332     /**
333      * {@return the mapper for the {@code NestHost} attribute}
334      */
335     public static AttributeMapper<NestHostAttribute> nestHost() {
336         return NestHostMapper.INSTANCE;
337     }
338 
339     /**
340      * {@return the mapper for the {@code NestMembers} attribute}
341      */
342     public static AttributeMapper<NestMembersAttribute> nestMembers() {
343         return NestMembersMapper.INSTANCE;
344     }
345 
346     /**
347      * {@return the mapper for the {@code PermittedSubclasses} attribute}
348      */
349     public static AttributeMapper<PermittedSubclassesAttribute> permittedSubclasses() {
350         return PermittedSubclassesMapper.INSTANCE;
351     }
352 
353     /**
354      * {@return the mapper for the {@code Record} attribute}
355      */
356     public static AttributeMapper<RecordAttribute> record() {
357         return RecordMapper.INSTANCE;
358     }
359 
360     /**
361      * {@return the mapper for the {@code RuntimeInvisibleAnnotations} attribute}
362      */
363     public static AttributeMapper<RuntimeInvisibleAnnotationsAttribute> runtimeInvisibleAnnotations() {
364         return RuntimeInvisibleAnnotationsMapper.INSTANCE;
365     }
366 
367     /**
368      * {@return the mapper for the {@code RuntimeInvisibleParameterAnnotations} attribute}
369      */
370     public static AttributeMapper<RuntimeInvisibleParameterAnnotationsAttribute> runtimeInvisibleParameterAnnotations() {
371         return RuntimeInvisibleParameterAnnotationsMapper.INSTANCE;
372     }
373 
374     /**
375      * {@return the mapper for the {@code RuntimeInvisibleTypeAnnotations} attribute}
376      * This has a data dependency on {@linkplain AttributeStability#UNSTABLE
377      * arbitrary indices} in the {@code class} file format.
378      */
379     public static AttributeMapper<RuntimeInvisibleTypeAnnotationsAttribute> runtimeInvisibleTypeAnnotations() {
380         return RuntimeInvisibleTypeAnnotationsMapper.INSTANCE;
381     }
382 
383     /**
384      * {@return the mapper for the {@code RuntimeVisibleAnnotations} attribute}
385      */
386     public static AttributeMapper<RuntimeVisibleAnnotationsAttribute> runtimeVisibleAnnotations() {
387         return RuntimeVisibleAnnotationsMapper.INSTANCE;
388     }
389 
390     /**
391      * {@return the mapper for the {@code RuntimeVisibleParameterAnnotations} attribute}
392      */
393     public static AttributeMapper<RuntimeVisibleParameterAnnotationsAttribute> runtimeVisibleParameterAnnotations() {
394         return RuntimeVisibleParameterAnnotationsMapper.INSTANCE;
395     }
396 
397     /**
398      * {@return the mapper for the {@code RuntimeVisibleTypeAnnotations} attribute}
399      * This has a data dependency on {@linkplain AttributeStability#UNSTABLE
400      * arbitrary indices} in the {@code class} file format.
401      */
402     public static AttributeMapper<RuntimeVisibleTypeAnnotationsAttribute> runtimeVisibleTypeAnnotations() {
403         return RuntimeVisibleTypeAnnotationsMapper.INSTANCE;
404     }
405 
406     /**
407      * {@return the mapper for the {@code Signature} attribute}
408      */
409     public static AttributeMapper<SignatureAttribute> signature() {
410         return SignatureMapper.INSTANCE;
411     }
412 
413     /**
414      * {@return the mapper for the {@code SourceDebugExtension} attribute}
415      * This has {@linkplain AttributeStability#STATELESS no data dependency}.
416      */
417     public static AttributeMapper<SourceDebugExtensionAttribute> sourceDebugExtension() {
418         return SourceDebugExtensionMapper.INSTANCE;
419     }
420 
421     /**
422      * {@return the mapper for the {@code SourceFile} attribute}
423      */
424     public static AttributeMapper<SourceFileAttribute> sourceFile() {
425         return SourceFileMapper.INSTANCE;
426     }
427 
428     /**
429      * {@return the mapper for the {@code SourceID} attribute}
430      * This is a JDK-specific attribute.
431      */
432     public static AttributeMapper<SourceIDAttribute> sourceId() {
433         return SourceIDMapper.INSTANCE;
434     }
435 
436     /**
437      * {@return the mapper for the {@code StackMapTable} attribute}
438      * This has a data dependency on {@linkplain AttributeStability#LABELS labels}.
439      */
440     public static AttributeMapper<StackMapTableAttribute> stackMapTable() {
441         return StackMapTableMapper.INSTANCE;
442     }
443 
444     /**
445      * {@return the mapper for the {@code Synthetic} attribute}
446      * The mapper permits multiple instances in a given location.
447      * This has {@linkplain AttributeStability#STATELESS no data dependency}.
448      */
449     public static AttributeMapper<SyntheticAttribute> synthetic() {
450         return SyntheticMapper.INSTANCE;
451     }
452 }