001: /*
002: * Annotation.java
003: *
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
005: *
006: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common
010: * Development and Distribution License("CDDL") (collectively, the
011: * "License"). You may not use this file except in compliance with the
012: * License. You can obtain a copy of the License at
013: * http://www.netbeans.org/cddl-gplv2.html
014: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
015: * specific language governing permissions and limitations under the
016: * License. When distributing the software, include this License Header
017: * Notice in each file and include the License file at
018: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
019: * particular file as subject to the "Classpath" exception as provided
020: * by Sun in the GPL Version 2 section of the License file that
021: * accompanied this code. If applicable, add the following below the
022: * License Header, with the fields enclosed by brackets [] replaced by
023: * your own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Contributor(s):
027: *
028: * The Original Software is NetBeans. The Initial Developer of the Original
029: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: *
032: * If you wish your version of this file to be governed by only the CDDL
033: * or only the GPL Version 2, indicate your decision by adding
034: * "[Contributor] elects to include this software in this distribution
035: * under the [CDDL or GPL Version 2] license." If you do not indicate a
036: * single choice of license, a recipient has the option to distribute
037: * your version of this file under either the CDDL, the GPL Version 2 or
038: * to extend the choice of license to its licensees as provided above.
039: * However, if you add GPL Version 2 code and therefore, elected the GPL
040: * Version 2 license, then the option applies only if the new code is
041: * made subject to such option by the copyright holder.
042: *
043: * Contributor(s): Thomas Ball
044: *
045: * Version: $Revision$
046: */
047:
048: package org.netbeans.modules.classfile;
049:
050: import java.io.*;
051: import java.util.*;
052:
053: /**
054: * Annotation: a single annotation on a program element.
055: *
056: * @author Thomas Ball
057: */
058: public class Annotation {
059: ClassName type;
060: AnnotationComponent[] components;
061: boolean runtimeVisible;
062:
063: /**
064: * Reads a classfile annotation section and adds its annotations to
065: * a specified map.
066: */
067: static void load(DataInputStream in, ConstantPool pool,
068: boolean visible, Map<ClassName, Annotation> map)
069: throws IOException {
070: int nattrs = in.readUnsignedShort();
071: for (int i = 0; i < nattrs; i++) {
072: Annotation ann = loadAnnotation(in, pool, visible);
073: map.put(ann.getType(), ann);
074: }
075: }
076:
077: static Annotation loadAnnotation(DataInputStream in,
078: ConstantPool pool, boolean visible) throws IOException {
079: final ClassName type;
080: CPEntry entry = pool.get(in.readUnsignedShort());
081: if (entry.getTag() == ConstantPool.CONSTANT_Class)
082: // 1.5 build 51 and earlier
083: type = ((CPClassInfo) entry).getClassName();
084: else {
085: String s = ((CPName) entry).getName();
086: type = ClassName.getClassName(s);
087: }
088: int npairs = in.readUnsignedShort();
089: List<AnnotationComponent> pairList = new ArrayList<AnnotationComponent>();
090: for (int j = 0; j < npairs; j++)
091: pairList.add(AnnotationComponent.load(in, pool, visible));
092: AnnotationComponent[] acs = new AnnotationComponent[pairList
093: .size()];
094: pairList.toArray(acs);
095: return new Annotation(pool, type, acs, visible);
096: }
097:
098: Annotation(ConstantPool pool, ClassName type,
099: AnnotationComponent[] components, boolean runtimeVisible) {
100: this .type = type;
101: this .components = components;
102: this .runtimeVisible = runtimeVisible;
103: }
104:
105: /**
106: * Returns the annotation type.
107: */
108: public final ClassName getType() {
109: return type;
110: }
111:
112: /**
113: * Returns the named components for this annotation, as an
114: * array of AnnotationComponents.
115: */
116: public final AnnotationComponent[] getComponents() {
117: return components.clone();
118: }
119:
120: /**
121: * Returns the named component for this annotation, or null if
122: * no component with that name exists.
123: */
124: public final AnnotationComponent getComponent(String name) {
125: for (int i = 0; i < components.length; i++) {
126: AnnotationComponent comp = components[i];
127: if (comp.getName().equals(name))
128: return comp;
129: }
130: return null;
131: }
132:
133: /**
134: * Returns true if this annotation is loaded by the Java Virtual
135: * Machine to be available via the Java reflection facility.
136: */
137: public boolean isRuntimeVisible() {
138: return runtimeVisible;
139: }
140:
141: public String toString() {
142: StringBuffer sb = new StringBuffer("@");
143: sb.append(type);
144: sb.append(" runtimeVisible=");
145: sb.append(runtimeVisible);
146: int n = components.length;
147: if (n > 0) {
148: sb.append(" { ");
149: for (int i = 0; i < n; i++) {
150: sb.append(components[i]);
151: if (i < n - 1)
152: sb.append(", ");
153: }
154: sb.append(" }");
155: }
156: return sb.toString();
157: }
158: }
|