1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/javap/src/main/java/org/apidesign/javap/MethodData.java Fri Nov 16 08:08:36 2012 +0100
1.3 @@ -0,0 +1,425 @@
1.4 +/*
1.5 + * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package org.apidesign.javap;
1.30 +
1.31 +import java.io.*;
1.32 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.33 +
1.34 +import static org.apidesign.javap.RuntimeConstants.*;
1.35 +
1.36 +/**
1.37 + * Strores method data informastion.
1.38 + *
1.39 + * @author Sucheta Dambalkar (Adopted code from jdis)
1.40 + */
1.41 +public class MethodData {
1.42 +
1.43 + ClassData cls;
1.44 + int access;
1.45 + int name_index;
1.46 + int descriptor_index;
1.47 + int attributes_count;
1.48 + byte[] code;
1.49 + Vector exception_table = new Vector(0);
1.50 + Vector lin_num_tb = new Vector(0);
1.51 + Vector loc_var_tb = new Vector(0);
1.52 + StackMapTableData[] stackMapTable;
1.53 + StackMapData[] stackMap;
1.54 + int[] exc_index_table=null;
1.55 + Vector attrs=new Vector(0);
1.56 + Vector code_attrs=new Vector(0);
1.57 + int max_stack, max_locals;
1.58 + boolean isSynthetic=false;
1.59 + boolean isDeprecated=false;
1.60 +
1.61 + public MethodData(ClassData cls){
1.62 + this.cls=cls;
1.63 + }
1.64 +
1.65 + /**
1.66 + * Read method info.
1.67 + */
1.68 + public void read(DataInputStream in) throws IOException {
1.69 + access = in.readUnsignedShort();
1.70 + name_index=in.readUnsignedShort();
1.71 + descriptor_index =in.readUnsignedShort();
1.72 + int attributes_count = in.readUnsignedShort();
1.73 + for (int i = 0; i < attributes_count; i++) {
1.74 + int attr_name_index=in.readUnsignedShort();
1.75 +
1.76 + readAttr: {
1.77 + if (cls.getTag(attr_name_index)==CONSTANT_UTF8) {
1.78 + String attr_name=cls.getString(attr_name_index);
1.79 + if ( attr_name.equals("Code")){
1.80 + readCode (in);
1.81 + AttrData attr=new AttrData(cls);
1.82 + attr.read(attr_name_index);
1.83 + attrs.addElement(attr);
1.84 + break readAttr;
1.85 + } else if ( attr_name.equals("Exceptions")){
1.86 + readExceptions(in);
1.87 + AttrData attr=new AttrData(cls);
1.88 + attr.read(attr_name_index);
1.89 + attrs.addElement(attr);
1.90 + break readAttr;
1.91 + } else if (attr_name.equals("Synthetic")){
1.92 + if (in.readInt()!=0)
1.93 + throw new ClassFormatError("invalid Synthetic attr length");
1.94 + isSynthetic=true;
1.95 + AttrData attr=new AttrData(cls);
1.96 + attr.read(attr_name_index);
1.97 + attrs.addElement(attr);
1.98 + break readAttr;
1.99 + } else if (attr_name.equals("Deprecated")){
1.100 + if (in.readInt()!=0)
1.101 + throw new ClassFormatError("invalid Synthetic attr length");
1.102 + isDeprecated = true;
1.103 + AttrData attr=new AttrData(cls);
1.104 + attr.read(attr_name_index);
1.105 + attrs.addElement(attr);
1.106 + break readAttr;
1.107 + }
1.108 + }
1.109 + AttrData attr=new AttrData(cls);
1.110 + attr.read(attr_name_index, in);
1.111 + attrs.addElement(attr);
1.112 + }
1.113 + }
1.114 + }
1.115 +
1.116 + /**
1.117 + * Read code attribute info.
1.118 + */
1.119 + public void readCode(DataInputStream in) throws IOException {
1.120 +
1.121 + int attr_length = in.readInt();
1.122 + max_stack=in.readUnsignedShort();
1.123 + max_locals=in.readUnsignedShort();
1.124 + int codelen=in.readInt();
1.125 +
1.126 + code=new byte[codelen];
1.127 + int totalread = 0;
1.128 + while(totalread < codelen){
1.129 + totalread += in.read(code, totalread, codelen-totalread);
1.130 + }
1.131 + // in.read(code, 0, codelen);
1.132 + int clen = 0;
1.133 + readExceptionTable(in);
1.134 + int code_attributes_count = in.readUnsignedShort();
1.135 +
1.136 + for (int k = 0 ; k < code_attributes_count ; k++) {
1.137 + int table_name_index=in.readUnsignedShort();
1.138 + int table_name_tag=cls.getTag(table_name_index);
1.139 + AttrData attr=new AttrData(cls);
1.140 + if (table_name_tag==CONSTANT_UTF8) {
1.141 + String table_name_tstr=cls.getString(table_name_index);
1.142 + if (table_name_tstr.equals("LineNumberTable")) {
1.143 + readLineNumTable(in);
1.144 + attr.read(table_name_index);
1.145 + } else if (table_name_tstr.equals("LocalVariableTable")) {
1.146 + readLocVarTable(in);
1.147 + attr.read(table_name_index);
1.148 + } else if (table_name_tstr.equals("StackMapTable")) {
1.149 + readStackMapTable(in);
1.150 + attr.read(table_name_index);
1.151 + } else if (table_name_tstr.equals("StackMap")) {
1.152 + readStackMap(in);
1.153 + attr.read(table_name_index);
1.154 + } else {
1.155 + attr.read(table_name_index, in);
1.156 + }
1.157 + code_attrs.addElement(attr);
1.158 + continue;
1.159 + }
1.160 +
1.161 + attr.read(table_name_index, in);
1.162 + code_attrs.addElement(attr);
1.163 + }
1.164 + }
1.165 +
1.166 + /**
1.167 + * Read exception table info.
1.168 + */
1.169 + void readExceptionTable (DataInputStream in) throws IOException {
1.170 + int exception_table_len=in.readUnsignedShort();
1.171 + exception_table=new Vector(exception_table_len);
1.172 + for (int l = 0; l < exception_table_len; l++) {
1.173 + exception_table.addElement(new TrapData(in, l));
1.174 + }
1.175 + }
1.176 +
1.177 + /**
1.178 + * Read LineNumberTable attribute info.
1.179 + */
1.180 + void readLineNumTable (DataInputStream in) throws IOException {
1.181 + int attr_len = in.readInt(); // attr_length
1.182 + int lin_num_tb_len = in.readUnsignedShort();
1.183 + lin_num_tb=new Vector(lin_num_tb_len);
1.184 + for (int l = 0; l < lin_num_tb_len; l++) {
1.185 + lin_num_tb.addElement(new LineNumData(in));
1.186 + }
1.187 + }
1.188 +
1.189 + /**
1.190 + * Read LocalVariableTable attribute info.
1.191 + */
1.192 + void readLocVarTable (DataInputStream in) throws IOException {
1.193 + int attr_len=in.readInt(); // attr_length
1.194 + int loc_var_tb_len = in.readUnsignedShort();
1.195 + loc_var_tb = new Vector(loc_var_tb_len);
1.196 + for (int l = 0; l < loc_var_tb_len; l++) {
1.197 + loc_var_tb.addElement(new LocVarData(in));
1.198 + }
1.199 + }
1.200 +
1.201 + /**
1.202 + * Read Exception attribute info.
1.203 + */
1.204 + public void readExceptions(DataInputStream in) throws IOException {
1.205 + int attr_len=in.readInt(); // attr_length in prog
1.206 + int num_exceptions = in.readUnsignedShort();
1.207 + exc_index_table=new int[num_exceptions];
1.208 + for (int l = 0; l < num_exceptions; l++) {
1.209 + int exc=in.readShort();
1.210 + exc_index_table[l]=exc;
1.211 + }
1.212 + }
1.213 +
1.214 + /**
1.215 + * Read StackMapTable attribute info.
1.216 + */
1.217 + void readStackMapTable(DataInputStream in) throws IOException {
1.218 + int attr_len = in.readInt(); //attr_length
1.219 + int stack_map_tb_len = in.readUnsignedShort();
1.220 + stackMapTable = new StackMapTableData[stack_map_tb_len];
1.221 + for (int i=0; i<stack_map_tb_len; i++) {
1.222 + stackMapTable[i] = StackMapTableData.getInstance(in, this);
1.223 + }
1.224 + }
1.225 +
1.226 + /**
1.227 + * Read StackMap attribute info.
1.228 + */
1.229 + void readStackMap(DataInputStream in) throws IOException {
1.230 + int attr_len = in.readInt(); //attr_length
1.231 + int stack_map_len = in.readUnsignedShort();
1.232 + stackMap = new StackMapData[stack_map_len];
1.233 + for (int i = 0; i<stack_map_len; i++) {
1.234 + stackMap[i] = new StackMapData(in, this);
1.235 + }
1.236 + }
1.237 +
1.238 + /**
1.239 + * Return access of the method.
1.240 + */
1.241 + public String[] getAccess(){
1.242 +
1.243 + Vector v = new Vector();
1.244 + if ((access & ACC_PUBLIC) !=0) v.addElement("public");
1.245 + if ((access & ACC_PRIVATE) !=0) v.addElement("private");
1.246 + if ((access & ACC_PROTECTED) !=0) v.addElement("protected");
1.247 + if ((access & ACC_STATIC) !=0) v.addElement("static");
1.248 + if ((access & ACC_FINAL) !=0) v.addElement("final");
1.249 + if ((access & ACC_SYNCHRONIZED) !=0) v.addElement("synchronized");
1.250 + if ((access & ACC_NATIVE) !=0) v.addElement("native");
1.251 + if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract");
1.252 + if ((access & ACC_STRICT) !=0) v.addElement("strictfp");
1.253 +
1.254 + String[] accflags = new String[v.size()];
1.255 + v.copyInto(accflags);
1.256 + return accflags;
1.257 + }
1.258 +
1.259 + /**
1.260 + * Return name of the method.
1.261 + */
1.262 + public String getName(){
1.263 + return cls.getStringValue(name_index);
1.264 + }
1.265 +
1.266 + /**
1.267 + * Return internal siganature of the method.
1.268 + */
1.269 + public String getInternalSig(){
1.270 + return cls.getStringValue(descriptor_index);
1.271 + }
1.272 +
1.273 + /**
1.274 + * Return java return type signature of method.
1.275 + */
1.276 + public String getReturnType(){
1.277 +
1.278 + String rttype = (new TypeSignature(getInternalSig())).getReturnType();
1.279 + return rttype;
1.280 + }
1.281 +
1.282 + /**
1.283 + * Return java type parameter signature.
1.284 + */
1.285 + public String getParameters(){
1.286 + String ptype = (new TypeSignature(getInternalSig())).getParameters();
1.287 +
1.288 + return ptype;
1.289 + }
1.290 +
1.291 + /**
1.292 + * Return code attribute data of a method.
1.293 + */
1.294 + public byte[] getCode(){
1.295 + return code;
1.296 + }
1.297 +
1.298 + /**
1.299 + * Return LineNumberTable size.
1.300 + */
1.301 + public int getnumlines(){
1.302 + return lin_num_tb.size();
1.303 + }
1.304 +
1.305 + /**
1.306 + * Return LineNumberTable
1.307 + */
1.308 + public Vector getlin_num_tb(){
1.309 + return lin_num_tb;
1.310 + }
1.311 +
1.312 + /**
1.313 + * Return LocalVariableTable size.
1.314 + */
1.315 + public int getloc_var_tbsize(){
1.316 + return loc_var_tb.size();
1.317 + }
1.318 +
1.319 +
1.320 + /**
1.321 + * Return LocalVariableTable.
1.322 + */
1.323 + public Vector getloc_var_tb(){
1.324 + return loc_var_tb;
1.325 + }
1.326 +
1.327 + /**
1.328 + * Return StackMap.
1.329 + */
1.330 + public StackMapData[] getStackMap() {
1.331 + return stackMap;
1.332 + }
1.333 +
1.334 + /**
1.335 + * Return StackMapTable.
1.336 + */
1.337 + public StackMapTableData[] getStackMapTable() {
1.338 + return stackMapTable;
1.339 + }
1.340 +
1.341 + /**
1.342 + * Return number of arguments of that method.
1.343 + */
1.344 + public int getArgumentlength(){
1.345 + return new TypeSignature(getInternalSig()).getArgumentlength();
1.346 + }
1.347 +
1.348 + /**
1.349 + * Return true if method is static
1.350 + */
1.351 + public boolean isStatic(){
1.352 + if ((access & ACC_STATIC) !=0) return true;
1.353 + return false;
1.354 + }
1.355 +
1.356 +
1.357 + /**
1.358 + * Return max depth of operand stack.
1.359 + */
1.360 + public int getMaxStack(){
1.361 + return max_stack;
1.362 + }
1.363 +
1.364 +
1.365 + /**
1.366 + * Return number of local variables.
1.367 + */
1.368 + public int getMaxLocals(){
1.369 + return max_locals;
1.370 + }
1.371 +
1.372 +
1.373 + /**
1.374 + * Return exception index table in Exception attribute.
1.375 + */
1.376 + public int []get_exc_index_table(){
1.377 + return exc_index_table;
1.378 + }
1.379 +
1.380 +
1.381 + /**
1.382 + * Return exception table in code attributre.
1.383 + */
1.384 + public Vector getexception_table(){
1.385 + return exception_table;
1.386 + }
1.387 +
1.388 +
1.389 + /**
1.390 + * Return method attributes.
1.391 + */
1.392 + public Vector getAttributes(){
1.393 + return attrs;
1.394 + }
1.395 +
1.396 +
1.397 + /**
1.398 + * Return code attributes.
1.399 + */
1.400 + public Vector getCodeAttributes(){
1.401 + return code_attrs;
1.402 + }
1.403 +
1.404 +
1.405 + /**
1.406 + * Return true if method id synthetic.
1.407 + */
1.408 + public boolean isSynthetic(){
1.409 + return isSynthetic;
1.410 + }
1.411 +
1.412 +
1.413 + /**
1.414 + * Return true if method is deprecated.
1.415 + */
1.416 + public boolean isDeprecated(){
1.417 + return isDeprecated;
1.418 + }
1.419 +
1.420 + public byte[] findAnnotationData(boolean classRetention) {
1.421 + String n = classRetention ?
1.422 + "RuntimeInvisibleAnnotations" : // NOI18N
1.423 + "RuntimeVisibleAnnotations"; // NOI18N
1.424 + AttrData[] arr = new AttrData[attrs.size()];
1.425 + attrs.copyInto(arr);
1.426 + return ClassData.findAttr(n, arr);
1.427 + }
1.428 +}