📄 annotationsattribute.java
字号:
/* * Javassist, a Java-bytecode translator toolkit. * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved. * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. Alternatively, the contents of this file may be used under * the terms of the GNU Lesser General Public License Version 2.1 or later. * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. */package javassist.bytecode;import java.util.Map;import java.io.IOException;import java.io.DataInputStream;import java.io.ByteArrayOutputStream;import javassist.bytecode.annotation.*;/** * A class representing * <code>RuntimeVisibleAnnotations_attribute</code> and * <code>RuntimeInvisibleAnnotations_attribute</code>. * * <p>To obtain an AnnotationAttribute object, invoke * <code>getAttribute(AnnotationsAttribute.visibleTag)</code> * in <code>ClassFile</code>, <code>MethodInfo</code>, * or <code>FieldInfo</code>. The obtained attribute is a * runtime visible annotations attribute. * If the parameter is * <code>AnnotationAttribute.invisibleTag</code>, then the obtained * attribute is a runtime invisible one. * * <p>For example, * * <ul><pre> * import javassist.bytecode.annotation.Annotation; * : * CtMethod m = ... ; * MethodInfo minfo = m.getMethodInfo(); * AnnotationsAttribute attr = (AnnotationsAttribute) * minfo.getAttribute(AnnotationsAttribute.invisibleTag); * Annotation an = attr.getAnnotation("Author"); * String s = ((StringMemberValue)an.getMemberValue("name")).getValue(); * System.out.println("@Author(name=" + s + ")"); * </pre></ul> * * <p>This code snippet retrieves an annotation of the type <code>Author</code> * from the <code>MethodInfo</code> object specified by <code>minfo</code>. * Then, it prints the value of <code>name</code> in <code>Author</code>. * * <p>If the annotation type <code>Author</code> is annotated by a meta annotation: * * <ul><pre> * @Retention(RetentionPolicy.RUNTIME) * </pre></ul> * * <p>Then <code>Author</code> is visible at runtime. Therefore, the third * statement of the code snippet above must be changed into: * * <ul><pre> * AnnotationsAttribute attr = (AnnotationsAttribute) * minfo.getAttribute(AnnotationsAttribute.visibleTag); * </pre></ul> * * <p>The attribute tag must be <code>visibleTag</code> instead of * <code>invisibleTag</code>. * * <p>If the member value of an annotation is not specified, the default value * is used as that member value. If so, <code>getMemberValue()</code> in * <code>Annotation</code> returns <code>null</code> * since the default value is not included in the * <code>AnnotationsAttribute</code>. It is included in the * <code>AnnotationDefaultAttribute</code> of the method declared in the * annotation type. * * <p>If you want to record a new AnnotationAttribute object, execute the * following snippet: * * <ul><pre> * ClassFile cf = ... ; * ConstPool cp = cf.getConstPool(); * AnnotationsAttribute attr * = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); * Annotation a = new Annotation("Author", cp); * a.addMemberValue("name", new StringMemberValue("Chiba", cp)); * attr.setAnnotation(a); * cf.addAttribute(attr); * cf.setVersionToJava5(); * </pre></ul> * * <p>The last statement is necessary if the class file was produced by * Javassist or JDK 1.4. Otherwise, it is not necessary. * * @see AnnotationDefaultAttribute * @see javassist.bytecode.annotation.Annotation */public class AnnotationsAttribute extends AttributeInfo { /** * The name of the <code>RuntimeVisibleAnnotations</code> attribute. */ public static final String visibleTag = "RuntimeVisibleAnnotations"; /** * The name of the <code>RuntimeInvisibleAnnotations</code> attribute. */ public static final String invisibleTag = "RuntimeInvisibleAnnotations"; /** * Constructs a <code>Runtime(In)VisisbleAnnotations_attribute</code>. * * @param cp constant pool * @param attrname attribute name (<code>visibleTag</code> or * <code>invisibleTag</code>). * @param info the contents of this attribute. It does not * include <code>attribute_name_index</code> or * <code>attribute_length</code>. */ public AnnotationsAttribute(ConstPool cp, String attrname, byte[] info) { super(cp, attrname, info); } /** * Constructs an empty * <code>Runtime(In)VisisbleAnnotations_attribute</code>. * A new annotation can be later added to the created attribute * by <code>setAnnotations()</code>. * * @param cp constant pool * @param attrname attribute name (<code>visibleTag</code> or * <code>invisibleTag</code>). * @see #setAnnotations(Annotation[]) */ public AnnotationsAttribute(ConstPool cp, String attrname) { this(cp, attrname, new byte[] { 0, 0 }); } /** * @param n the attribute name. */ AnnotationsAttribute(ConstPool cp, int n, DataInputStream in) throws IOException { super(cp, n, in); } /** * Returns <code>num_annotations</code>. */ public int numAnnotations() { return ByteArray.readU16bit(info, 0); } /** * Copies this attribute and returns a new copy. */ public AttributeInfo copy(ConstPool newCp, Map classnames) { Copier copier = new Copier(info, constPool, newCp, classnames); try { copier.annotationArray(); return new AnnotationsAttribute(newCp, getName(), copier.close()); } catch (Exception e) { throw new RuntimeException(e.toString()); } } /** * Parses the annotations and returns a data structure representing * the annotation with the specified type. See also * <code>getAnnotations()</code> as to the returned data structure. * * @param type the annotation type. * @return null if the specified annotation type is not included. * @see #getAnnotations() */ public Annotation getAnnotation(String type) { Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) return annotations[i]; } return null; } /** * Adds an annotation. If there is an annotation with the same type, * it is removed before the new annotation is added. * * @param annotation the added annotation. */ public void addAnnotation(Annotation annotation) { String type = annotation.getTypeName(); Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) { annotations[i] = annotation; setAnnotations(annotations); return; } } Annotation[] newlist = new Annotation[annotations.length + 1]; System.arraycopy(annotations, 0, newlist, 0, annotations.length); newlist[annotations.length] = annotation; setAnnotations(newlist); } /** * Parses the annotations and returns a data structure representing * that parsed annotations. Note that changes of the node values of the * returned tree are not reflected on the annotations represented by * this object unless the tree is copied back to this object by * <code>setAnnotations()</code>. * * @see #setAnnotations(Annotation[]) */ public Annotation[] getAnnotations() { try { return new Parser(info, constPool).parseAnnotations(); } catch (Exception e) { throw new RuntimeException(e.toString()); } } /** * Changes the annotations represented by this object according to * the given array of <code>Annotation</code> objects. * * @param annotations the data structure representing the * new annotations. */ public void setAnnotations(Annotation[] annotations) { ByteArrayOutputStream output = new ByteArrayOutputStream(); AnnotationsWriter writer = new AnnotationsWriter(output, constPool); try { int n = annotations.length; writer.numAnnotations(n); for (int i = 0; i < n; ++i) annotations[i].write(writer); writer.close(); } catch (IOException e) { throw new RuntimeException(e); // should never reach here. } set(output.toByteArray()); } /** * Changes the annotations. A call to this method is equivalent to: * <ul><pre>setAnnotations(new Annotation[] { annotation })</pre></ul> * * @param annotation the data structure representing * the new annotation. */ public void setAnnotation(Annotation annotation) { setAnnotations(new Annotation[] { annotation }); } /** * Returns a string representation of this object. */ public String toString() { Annotation[] a = getAnnotations(); StringBuffer sbuf = new StringBuffer(); int i = 0; while (i < a.length) { sbuf.append(a[i++].toString()); if (i != a.length) sbuf.append(", "); } return sbuf.toString(); } static class Walker { byte[] info; Walker(byte[] attrInfo) { info = attrInfo; } final void parameters() throws Exception { int numParam = info[0] & 0xff; parameters(numParam, 1); } void parameters(int numParam, int pos) throws Exception { for (int i = 0; i < numParam; ++i) pos = annotationArray(pos); } final void annotationArray() throws Exception { annotationArray(0); } final int annotationArray(int pos) throws Exception { int num = ByteArray.readU16bit(info, pos); return annotationArray(pos + 2, num); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -