KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 /*
25  * ConstantPoolInfo.java
26  *
27  * Created on May 24, 2005, 4:43 PM
28  *
29  * To change this template, choose Tools | Options and locate the template under
30  * the Source Creation and Management node. Right-click the template and choose
31  * Open. You can then make changes to the template in the Source Editor.
32  */

33
34 package com.sun.enterprise.deployment.annotation.introspection;
35
36 import java.nio.channels.ReadableByteChannel JavaDoc;
37 import java.nio.ByteBuffer JavaDoc;
38 import java.nio.CharBuffer JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 import com.sun.enterprise.deployment.util.DOLUtils;
42 /**
43  *
44  * @author dochez
45  */

46 public class ConstantPoolInfo {
47     
48     byte[] bytes = new byte[Short.MAX_VALUE];
49     private CustomAnnotationScanner customScanner = null;
50     
51     /** Creates a new instance of ConstantPoolInfo */
52     public ConstantPoolInfo() {
53     }
54
55     public ConstantPoolInfo(CustomAnnotationScanner scanner) {
56         customScanner = scanner;
57     }
58
59     /**
60      * Read the input channel and initialize instance data
61      * structure.
62      */

63     public boolean containsAnnotation(int constantPoolSize, final ByteBuffer JavaDoc buffer) throws IOException JavaDoc {
64             
65         for (int i=1;i<constantPoolSize;i++) {
66             final byte type = buffer.get();
67             switch(type) {
68                 case ASCIZ:
69                 case UNICODE:
70                     final short length = buffer.getShort();
71                     if (length<0 || length>Short.MAX_VALUE) {
72                         return true;
73                     }
74                     buffer.get(bytes, 0, length);
75                     /* to speed up the process, I am comparing the first few
76                      * bytes to Ljava since all annotations are in the java
77                      * package, the reduces dramatically the number or String
78                      * construction
79                      */

80                     if (bytes[0]=='L' && bytes[1]=='j' && bytes[2]=='a') {
81                         String JavaDoc stringValue;
82                         if (type==ASCIZ) {
83                             stringValue = new String JavaDoc(bytes, 0, length,"US-ASCII");
84                         } else {
85                             stringValue = new String JavaDoc(bytes, 0, length);
86                         }
87                         if (customScanner != null) {
88                             if (customScanner.isAnnotation(stringValue)) {
89                                 return true;
90                             }
91                         } else {
92                             if (AnnotationScanner.isAnnotation(stringValue)) {
93                                 return true;
94                             }
95                         }
96                     }
97                     break;
98                 case CLASS:
99                 case STRING:
100                     buffer.getShort();
101                     break;
102                 case FIELDREF:
103                 case METHODREF:
104                 case INTERFACEMETHODREF:
105                 case INTEGER:
106                 case FLOAT:
107                     buffer.position(buffer.position()+4);
108                     break;
109                 case LONG:
110                 case DOUBLE:
111                     buffer.position(buffer.position()+8);
112                     // for long, and double, they use 2 constantPool
113
i++;
114                     break;
115                 case NAMEANDTYPE:
116                     buffer.getShort();
117                     buffer.getShort();
118                     break;
119                 default:
120                     DOLUtils.getDefaultLogger().severe("Unknow type constant pool " + type + " at position" + i);
121                     break;
122             }
123         }
124         return false;
125     }
126     
127     
128     public static final byte CLASS = 7;
129     public static final int FIELDREF = 9;
130     public static final int METHODREF = 10;
131     public static final int STRING = 8;
132     public static final int INTEGER = 3;
133     public static final int FLOAT = 4;
134     public static final int LONG = 5;
135     public static final int DOUBLE = 6;
136     public static final int INTERFACEMETHODREF = 11;
137     public static final int NAMEANDTYPE = 12;
138     public static final int ASCIZ = 1;
139     public static final int UNICODE = 2;
140 }
141
Popular Tags