KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ElementValue.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.4 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.DataInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * ElementValue: the value portion of the name-value pair of a
33  * single annotation element.
34  *
35  * @author Thomas Ball
36  */

37 public abstract class ElementValue {
38     static ElementValue load(DataInputStream JavaDoc in, ConstantPool pool,
39                  boolean runtimeVisible)
40     throws IOException JavaDoc {
41     char tag = (char)in.readByte();
42     switch (tag) {
43     case 'e': return loadEnumValue(in, pool);
44       case 'c': {
45           int classType = in.readUnsignedShort();
46           return new ClassElementValue(pool, classType);
47       }
48       case '@': {
49           Annotation value =
50           Annotation.loadAnnotation(in, pool, runtimeVisible);
51           return new NestedElementValue(pool, value);
52       }
53       case '[': {
54           ElementValue[] values = new ElementValue[in.readUnsignedShort()];
55           for (int i = 0; i < values.length; i++)
56           values[i] = load(in, pool, runtimeVisible);
57           return new ArrayElementValue(pool, values);
58       }
59       default:
60           assert "BCDFIJSZs".indexOf(tag) >= 0 : "invalid annotation tag";
61           return new PrimitiveElementValue(pool, in.readUnsignedShort());
62     }
63     }
64
65     private static ElementValue loadEnumValue(DataInputStream JavaDoc in,
66                           ConstantPool pool)
67     throws IOException JavaDoc {
68     int type = in.readUnsignedShort();
69     CPEntry cpe = pool.get(type);
70     if (cpe.getTag() == ConstantPool.CONSTANT_FieldRef) {
71         // workaround for 1.5 beta 1 and earlier builds
72
CPFieldInfo fe = (CPFieldInfo)cpe;
73         String JavaDoc enumType = fe.getClassName().getInternalName();
74         String JavaDoc enumName = fe.getFieldName();
75         return new EnumElementValue(enumType, enumName);
76     } else {
77         int name = in.readUnsignedShort();
78         return new EnumElementValue(pool, type, name);
79     }
80     }
81
82     /* Package-private constructor so that only classes in this
83      * package can subclass.
84      */

85     ElementValue() {
86     }
87 }
88
Popular Tags