KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > annotation > introspection > ClassFile


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment.annotation.introspection;
24
25 import java.nio.channels.ReadableByteChannel JavaDoc;
26 import java.nio.ByteBuffer JavaDoc;
27 import java.nio.CharBuffer JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * This class is encapsulating binary .class file information as
32  * defined at
33  * http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html
34  *
35  * This is used by the annotation frameworks to quickly scan .class files
36  * for the presence of annotations. This avoid the annotation framework having
37  * to load each .class file in the class loader.
38  *
39  * @author Jerome Dochez
40  */

41 public class ClassFile {
42     
43     ByteBuffer JavaDoc header;
44     ConstantPoolInfo constantPoolInfo = new ConstantPoolInfo();
45     
46     /** Creates a new instance of ClassFile */
47     public ClassFile() {
48         header = ByteBuffer.allocate(12000);
49     }
50
51     public void setConstantPoolInfo(ConstantPoolInfo poolInfo) {
52         constantPoolInfo = poolInfo;
53     }
54     
55     /**
56      * Read the input channel and initialize instance data
57      * structure.
58      */

59     public boolean containsAnnotation(ReadableByteChannel JavaDoc in, long size) throws IOException JavaDoc {
60
61         /**
62          * this is the .class file layout
63          *
64         ClassFile {
65             u4 magic;
66             u2 minor_version;
67             u2 major_version;
68             u2 constant_pool_count;
69             cp_info constant_pool[constant_pool_count-1];
70             u2 access_flags;
71             u2 this_class;
72             u2 super_class;
73             u2 interfaces_count;
74             u2 interfaces[interfaces_count];
75             u2 fields_count;
76             field_info fields[fields_count];
77             u2 methods_count;
78             method_info methods[methods_count];
79             u2 attributes_count;
80             attribute_info attributes[attributes_count];
81         }
82          **/

83         header.clear();
84         if (size!=-1 && size>header.capacity()) {
85             // time to expand...
86
header = ByteBuffer.allocate((int) size);
87         }
88         long read = (long) in.read(header);
89         if (size!=-1 && read!=size) {
90             return false;
91         }
92         header.rewind();
93                 
94         if (header.getInt()!=magic) {
95             return false;
96         }
97         
98         majorVersion = header.getShort();
99         minorVersion = header.getShort();
100         int constantPoolSize = header.getShort();
101
102         return constantPoolInfo.containsAnnotation(constantPoolSize, header);
103         
104     }
105     
106     public short majorVersion;
107     public short minorVersion;
108     public ConstantPoolInfo constantPool[];
109     public short accessFlags;
110     public ConstantPoolInfo thisClass;
111     public ConstantPoolInfo superClass;
112     public ConstantPoolInfo interfaces[];
113     /**
114      * bunch of stuff I really don't care too much for now.
115      *
116     FieldInfo fields[];
117     MethodInfo methods[];
118     AttributeInfo attributes[];
119      */

120     boolean isValidClass = false;
121
122     private static final int magic = 0xCAFEBABE;
123     
124     public static final int ACC_PUBLIC = 0x1;
125     public static final int ACC_PRIVATE = 0x2;
126     public static final int ACC_PROTECTED = 0x4;
127     public static final int ACC_STATIC = 0x8;
128     public static final int ACC_FINAL = 0x10;
129     public static final int ACC_SYNCHRONIZED = 0x20;
130     public static final int ACC_THREADSAFE = 0x40;
131     public static final int ACC_TRANSIENT = 0x80;
132     public static final int ACC_NATIVE = 0x100;
133     public static final int ACC_INTERFACE = 0x200;
134     public static final int ACC_ABSTRACT = 0x400;
135     
136 }
137
Popular Tags