KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > classfile > Annotation


1 /*
2  * Annotation.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.9 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.*;
29 import java.util.*;
30
31 /**
32  * Annotation: a single annotation on a program element.
33  *
34  * @author Thomas Ball
35  */

36 public class Annotation {
37     ClassName type;
38     AnnotationComponent[] components;
39     boolean runtimeVisible;
40
41     /**
42      * Reads a classfile annotation section and adds its annotations to
43      * a specified map.
44      */

45     static void load(DataInputStream in, ConstantPool pool,
46              boolean visible, Map<ClassName,Annotation> map) throws IOException {
47     int nattrs = in.readUnsignedShort();
48     for (int i = 0; i < nattrs; i++) {
49         Annotation ann = loadAnnotation(in, pool, visible);
50         map.put(ann.getType(), ann);
51     }
52     }
53
54     static Annotation loadAnnotation(DataInputStream in, ConstantPool pool,
55                      boolean visible) throws IOException {
56     final ClassName type;
57     CPEntry entry = pool.get(in.readUnsignedShort());
58     if (entry.getTag() == ConstantPool.CONSTANT_Class)
59         // 1.5 build 51 and earlier
60
type = ((CPClassInfo)entry).getClassName();
61     else {
62         String JavaDoc s = ((CPName)entry).getName();
63         type = ClassName.getClassName(s);
64     }
65     int npairs = in.readUnsignedShort();
66     List<AnnotationComponent> pairList = new ArrayList<AnnotationComponent>();
67     for (int j = 0; j < npairs; j++)
68         pairList.add(AnnotationComponent.load(in, pool, visible));
69     AnnotationComponent[] acs =
70         new AnnotationComponent[pairList.size()];
71     pairList.toArray(acs);
72     return new Annotation(pool, type, acs, visible);
73     }
74
75     Annotation(ConstantPool pool, ClassName type,
76            AnnotationComponent[] components, boolean runtimeVisible) {
77     this.type = type;
78     this.components = components;
79     this.runtimeVisible = runtimeVisible;
80     }
81
82     /**
83      * Returns the annotation type.
84      */

85     public final ClassName getType() {
86     return type;
87     }
88
89     /**
90      * Returns the named components for this annotation, as an
91      * array of AnnotationComponents.
92      */

93     public final AnnotationComponent[] getComponents() {
94     return components.clone();
95     }
96
97     /**
98      * Returns the named component for this annotation, or null if
99      * no component with that name exists.
100      */

101     public final AnnotationComponent getComponent(String JavaDoc name) {
102     for (int i = 0; i < components.length; i++) {
103         AnnotationComponent comp = components[i];
104         if (comp.getName().equals(name))
105         return comp;
106     }
107     return null;
108     }
109
110     /**
111      * Returns true if this annotation is loaded by the Java Virtual
112      * Machine to be available via the Java reflection facility.
113      */

114     public boolean isRuntimeVisible() {
115     return runtimeVisible;
116     }
117
118     public String JavaDoc toString() {
119     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("@");
120     sb.append(type);
121     sb.append(" runtimeVisible=");
122     sb.append(runtimeVisible);
123     int n = components.length;
124     if (n > 0) {
125         sb.append(" { ");
126         for (int i = 0; i < n; i++) {
127         sb.append(components[i]);
128         if (i < n - 1)
129             sb.append(", ");
130         }
131         sb.append(" }");
132     }
133     return sb.toString();
134     }
135 }
136
Popular Tags