1 19 20 package org.netbeans.modules.j2ee.archive.wizard; 21 22 import java.nio.ByteBuffer ; 23 import java.io.IOException ; 24 import java.nio.channels.ReadableByteChannel ; 25 import java.util.Set ; 26 import java.util.HashSet ; 27 28 32 33 34 44 class EJBClassFile { 45 46 private ByteBuffer header; 47 private EJBConstantPoolInfo constantPoolInfo = new EJBConstantPoolInfo(); 48 49 50 EJBClassFile() { 51 header = ByteBuffer.allocate(12000); 52 } 53 54 55 56 60 boolean containsAnnotation(ReadableByteChannel in, long size) throws IOException { 61 83 boolean retVal = false; 84 if (null != in) { 85 header.clear(); 86 if (size!=-1 && size>header.capacity()) { 87 header = ByteBuffer.allocate((int) size); 89 } 90 long read = (long) in.read(header); 91 if (size!=-1 && read!=size) { 92 return false; 93 } 94 header.rewind(); 95 96 if (header.getInt()!=magic) { 97 return false; 98 } 99 100 majorVersion = header.getShort(); 101 minorVersion = header.getShort(); 102 int constantPoolSize = header.getShort(); 103 104 retVal = constantPoolInfo.containsAnnotation(constantPoolSize, header); 105 } 106 return retVal; 107 108 } 109 110 public short majorVersion; 111 public short minorVersion; 112 public EJBConstantPoolInfo constantPool[]; 113 public short accessFlags; 114 public EJBConstantPoolInfo thisClass; 115 public EJBConstantPoolInfo superClass; 116 public EJBConstantPoolInfo interfaces[]; 117 119 private static final int magic = 0xCAFEBABE; 120 121 public static final int ACC_PUBLIC = 0x1; 122 public static final int ACC_PRIVATE = 0x2; 123 public static final int ACC_PROTECTED = 0x4; 124 public static final int ACC_STATIC = 0x8; 125 public static final int ACC_FINAL = 0x10; 126 public static final int ACC_SYNCHRONIZED = 0x20; 127 public static final int ACC_THREADSAFE = 0x40; 128 public static final int ACC_TRANSIENT = 0x80; 129 public static final int ACC_NATIVE = 0x100; 130 public static final int ACC_INTERFACE = 0x200; 131 public static final int ACC_ABSTRACT = 0x400; 132 133 134 static class EJBConstantPoolInfo { 135 136 byte[] bytes = new byte[Short.MAX_VALUE]; 137 138 private Set <String > annotations=null; 139 140 141 public EJBConstantPoolInfo() { 142 annotations = new HashSet (); 143 annotations.add("Ljavax/ejb/Stateless;"); 144 annotations.add("Ljavax/ejb/Stateful;"); 145 annotations.add("Ljavax/ejb/MessageDriven;"); 146 } 147 148 152 public boolean containsAnnotation(int constantPoolSize, final ByteBuffer buffer) throws IOException { 153 154 for (int i=1;i<constantPoolSize;i++) { 155 final byte type = buffer.get(); 156 switch(type) { 157 case ASCIZ: 158 case UNICODE: 159 final short length = buffer.getShort(); 160 if (length<0 || length>Short.MAX_VALUE) { 161 return true; 162 } 163 buffer.get(bytes, 0, length); 164 169 if (bytes[0]=='L' && bytes[1]=='j' && bytes[2]=='a') { 170 String stringValue; 171 if (type==ASCIZ) { 172 stringValue = new String (bytes, 0, length,"US-ASCII"); 173 } else { 174 stringValue = new String (bytes, 0, length); 175 } 176 177 if (annotations.contains(stringValue)) { 178 return true; 179 } 180 181 182 } 183 break; 184 case CLASS: 185 case STRING: 186 buffer.getShort(); 187 break; 188 case FIELDREF: 189 case METHODREF: 190 case INTERFACEMETHODREF: 191 case INTEGER: 192 case FLOAT: 193 buffer.position(buffer.position()+4); 194 break; 195 case LONG: 196 case DOUBLE: 197 buffer.position(buffer.position()+8); 198 i++; 200 break; 201 case NAMEANDTYPE: 202 buffer.getShort(); 203 buffer.getShort(); 204 break; 205 default: 206 break; 208 } 209 } 210 return false; 211 } 212 213 214 public static final byte CLASS = 7; 215 public static final int FIELDREF = 9; 216 public static final int METHODREF = 10; 217 public static final int STRING = 8; 218 public static final int INTEGER = 3; 219 public static final int FLOAT = 4; 220 public static final int LONG = 5; 221 public static final int DOUBLE = 6; 222 public static final int INTERFACEMETHODREF = 11; 223 public static final int NAMEANDTYPE = 12; 224 public static final int ASCIZ = 1; 225 public static final int UNICODE = 2; 226 227 } 228 } 229 | Popular Tags |