1 package serp.bytecode;
2
3 /***
4 * An annotated entity.
5 *
6 * @author Abe White
7 */
8 public abstract class Annotated extends Attributes {
9 /***
10 * Return runtime <b>invisible</b> annotation information for the entity.
11 * Acts internally through the {@link Attributes} interface.
12 *
13 * @param add if true, a new annotations attribute will be added if not
14 * already present
15 * @return the annotation information, or null if none and the
16 * <code>add</code> param is set to false
17 */
18 public Annotations getDeclaredAnnotations(boolean add) {
19 Annotations ann = (Annotations) getAttribute
20 (Constants.ATTR_ANNOTATIONS);
21 if (!add || ann != null)
22 return ann;
23 ensureBytecodeVersion();
24 return (Annotations) addAttribute(Constants.ATTR_ANNOTATIONS);
25 }
26
27 /***
28 * Remove the runtime <b>invisible</b> annotations attribute for the entity.
29 * Acts internally through the {@link Attributes} interface.
30 *
31 * @return true if there was an attribute to remove
32 */
33 public boolean removeDeclaredAnnotations() {
34 return removeAttribute(Constants.ATTR_ANNOTATIONS);
35 }
36
37 /***
38 * Return runtime visible annotation information for the entity.
39 * Acts internally through the {@link Attributes} interface.
40 *
41 * @param add if true, a new runtime annotations attribute will be
42 * added if not already present
43 * @return the annotation information, or null if none and the
44 * <code>add</code> param is set to false
45 */
46 public Annotations getDeclaredRuntimeAnnotations(boolean add) {
47 Annotations ann = (Annotations) getAttribute
48 (Constants.ATTR_RUNTIME_ANNOTATIONS);
49 if (!add || ann != null)
50 return ann;
51 ensureBytecodeVersion();
52 return (Annotations) addAttribute(Constants.ATTR_RUNTIME_ANNOTATIONS);
53 }
54
55 /***
56 * Remove the runtime visible annotations attribute for the entity.
57 * Acts internally through the {@link Attributes} interface.
58 *
59 * @return true if there was an attribute to remove
60 */
61 public boolean removeDeclaredRuntimeAnnotations() {
62 return removeAttribute(Constants.ATTR_RUNTIME_ANNOTATIONS);
63 }
64
65 /***
66 * When adding annotations, make sure the bytecode spec supports them.
67 */
68 private void ensureBytecodeVersion() {
69 BCClass bc = getBCClass();
70 if (bc.getMajorVersion() < Constants.MAJOR_VERSION_JAVA5) {
71 bc.setMajorVersion(Constants.MAJOR_VERSION_JAVA5);
72 bc.setMinorVersion(Constants.MINOR_VERSION_JAVA5);
73 }
74 }
75
76 /***
77 * Internal access to the owning class.
78 */
79 abstract BCClass getBCClass();
80 }