001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist.bytecode;
017:
018: import javassist.CtClass;
019: import javassist.bytecode.annotation.AnnotationsWriter;
020: import javassist.bytecode.annotation.MemberValue;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.DataInputStream;
024: import java.io.IOException;
025: import java.util.Map;
026:
027: /**
028: * A class representing <code>AnnotationDefault_attribute</code>.
029: *
030: * <p>For example, if you declare the following annotation type:
031: *
032: * <ul><pre>
033: * @interface Author {
034: * String name() default "Shakespeare";
035: * int age() default 99;
036: * }
037: * </pre></ul>
038: *
039: * <p>The defautl values of <code>name</code> and <code>age</code>
040: * are stored as annotation default attributes in <code>Author.class</code>.
041: * The following code snippet obtains the default value of <code>name</code>:
042: *
043: * <ul><pre>
044: * ClassPool pool = ...
045: * CtClass cc = pool.get("Author");
046: * CtMethod cm = cc.getDeclaredMethod("age");
047: * MethodInfo minfo = cm.getMethodInfo();
048: * AnnotationDefaultAttribute ada
049: * = (AnnotationDefaultAttribute)
050: * minfo.getAttribute(AnnotationDefaultAttribute.tag);
051: * MemberValue value = ada.getDefaultValue()); // default value of age
052: * </pre></ul>
053: *
054: * <p>If the following statement is executed after the code above,
055: * the default value of age is set to 80:
056: *
057: * <ul><pre>
058: * ada.setDefaultValue(new IntegerMemberValue(minfo.getConstPool(), 80));
059: * </pre></ul>
060: *
061: * @see AnnotationsAttribute
062: * @see javassist.bytecode.annotation.MemberValue
063: */
064:
065: public class AnnotationDefaultAttribute extends AttributeInfo {
066: /**
067: * The name of the <code>AnnotationDefault</code> attribute.
068: */
069: public static final String tag = "AnnotationDefault";
070:
071: /**
072: * Constructs an <code>AnnotationDefault_attribute</code>.
073: *
074: * @param cp constant pool
075: * @param info the contents of this attribute. It does not
076: * include <code>attribute_name_index</code> or
077: * <code>attribute_length</code>.
078: */
079: public AnnotationDefaultAttribute(ConstPool cp, byte[] info) {
080: super (cp, tag, info);
081: }
082:
083: /**
084: * Constructs an empty <code>AnnotationDefault_attribute</code>.
085: * The default value can be set by <code>setDefaultValue()</code>.
086: *
087: * @param cp constant pool
088: * @see #setDefaultValue(javassist.bytecode.annotation.MemberValue)
089: */
090: public AnnotationDefaultAttribute(ConstPool cp) {
091: this (cp, new byte[] { 0, 0 });
092: }
093:
094: /**
095: * @param n the attribute name.
096: */
097: AnnotationDefaultAttribute(ConstPool cp, int n, DataInputStream in)
098: throws IOException {
099: super (cp, n, in);
100: }
101:
102: /**
103: * Copies this attribute and returns a new copy.
104: */
105: public AttributeInfo copy(ConstPool newCp, Map classnames) {
106: AnnotationsAttribute.Copier copier = new AnnotationsAttribute.Copier(
107: info, constPool, newCp, classnames);
108: try {
109: copier.memberValue(0);
110: return new AnnotationDefaultAttribute(newCp, copier.close());
111: } catch (Exception e) {
112: throw new RuntimeException(e.toString());
113: }
114: }
115:
116: /**
117: * Obtains the default value represented by this attribute.
118: */
119: public MemberValue getDefaultValue() {
120: try {
121: return new AnnotationsAttribute.Parser(info, constPool)
122: .parseMemberValue();
123: } catch (Exception e) {
124: throw new RuntimeException(e.toString());
125: }
126: }
127:
128: /**
129: * Changes the default value represented by this attribute.
130: *
131: * @param value the new value.
132: * @see javassist.bytecode.annotation.Annotation#createMemberValue(ConstPool, CtClass)
133: */
134: public void setDefaultValue(MemberValue value) {
135: ByteArrayOutputStream output = new ByteArrayOutputStream();
136: AnnotationsWriter writer = new AnnotationsWriter(output,
137: constPool);
138: try {
139: value.write(writer);
140: writer.close();
141: } catch (IOException e) {
142: throw new RuntimeException(e); // should never reach here.
143: }
144:
145: set(output.toByteArray());
146:
147: }
148:
149: /**
150: * Returns a string representation of this object.
151: */
152: public String toString() {
153: return getDefaultValue().toString();
154: }
155: }
|