1 /* 2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package com.sun.org.apache.bcel.internal.generic; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 25 import com.sun.org.apache.bcel.internal.Const; 26 import com.sun.org.apache.bcel.internal.classfile.ClassFormatException; 27 import com.sun.org.apache.bcel.internal.classfile.Utility; 28 29 /** 30 * Abstract super class for all possible java types, namely basic types 31 * such as int, object types like String and array types, e.g. int[] 32 * 33 * @version $Id$ 34 * @LastModified: Jun 2019 35 */ 36 public abstract class Type { 37 38 private final byte type; 39 private String signature; // signature for the type 40 /** 41 * Predefined constants 42 */ 43 public static final BasicType VOID = new BasicType(Const.T_VOID); 44 public static final BasicType BOOLEAN = new BasicType(Const.T_BOOLEAN); 45 public static final BasicType INT = new BasicType(Const.T_INT); 46 public static final BasicType SHORT = new BasicType(Const.T_SHORT); 47 public static final BasicType BYTE = new BasicType(Const.T_BYTE); 48 public static final BasicType LONG = new BasicType(Const.T_LONG); 49 public static final BasicType DOUBLE = new BasicType(Const.T_DOUBLE); 50 public static final BasicType FLOAT = new BasicType(Const.T_FLOAT); 51 public static final BasicType CHAR = new BasicType(Const.T_CHAR); 52 public static final ObjectType OBJECT = new ObjectType("java.lang.Object"); 53 public static final ObjectType CLASS = new ObjectType("java.lang.Class"); 54 public static final ObjectType STRING = new ObjectType("java.lang.String"); 190 // @since 6.0 no longer final 191 public static Type getType( final String signature ) throws StringIndexOutOfBoundsException { 192 final byte type = Utility.typeOfSignature(signature); 193 if (type <= Const.T_VOID) { 194 //corrected concurrent private static field acess 195 wrap(consumed_chars, 1); 196 return BasicType.getType(type); 197 } else if (type == Const.T_ARRAY) { 198 int dim = 0; 199 do { // Count dimensions 200 dim++; 201 } while (signature.charAt(dim) == '['); 202 // Recurse, but just once, if the signature is ok 203 final Type t = getType(signature.substring(dim)); 204 //corrected concurrent private static field acess 205 // consumed_chars += dim; // update counter - is replaced by 206 final int _temp = unwrap(consumed_chars) + dim; 207 wrap(consumed_chars, _temp); 208 return new ArrayType(t, dim); 209 } else { // type == T_REFERENCE 210 // Utility.signatureToString understands how to parse 211 // generic types. 212 final String parsedSignature = Utility.signatureToString(signature, false); 213 wrap(consumed_chars, parsedSignature.length() + 2); // "Lblabla;" `L' and `;' are removed 214 return ObjectType.getInstance(parsedSignature.replace('/', '.')); 215 } 216 } 217 218 219 /** 220 * Convert return value of a method (signature) to a Type object. 221 * 222 * @param signature signature string such as (Ljava/lang/String;)V 223 * @return return type 224 */ 225 public static Type getReturnType( final String signature ) { 226 try { 227 // Read return type after `)' 228 final int index = signature.lastIndexOf(')') + 1; 229 return getType(signature.substring(index)); 230 } catch (final StringIndexOutOfBoundsException e) { // Should never occur 231 throw new ClassFormatException("Invalid method signature: " + signature, e); 232 } 233 } 234 235 236 /** 237 * Convert arguments of a method (signature) to an array of Type objects. 238 * @param signature signature string such as (Ljava/lang/String;)V 239 * @return array of argument types 240 */ 241 public static Type[] getArgumentTypes( final String signature ) { 242 final List<Type> vec = new ArrayList<>(); 243 int index; 244 Type[] types; 245 try { // Read all declarations between for `(' and `)' 246 if (signature.charAt(0) != '(') { 247 throw new ClassFormatException("Invalid method signature: " + signature); 248 } 249 index = 1; // current string position 250 while (signature.charAt(index) != ')') { 251 vec.add(getType(signature.substring(index))); 252 //corrected concurrent private static field acess 253 index += unwrap(consumed_chars); // update position 254 } 255 } catch (final StringIndexOutOfBoundsException e) { // Should never occur 256 throw new ClassFormatException("Invalid method signature: " + signature, e); 257 } 258 types = new Type[vec.size()]; 259 vec.toArray(types); 260 return types; 261 } 262 263 264 /** Convert runtime java.lang.Class to BCEL Type object. 265 * @param cl Java class 266 * @return corresponding Type object 267 */ 268 public static Type getType( final java.lang.Class<?> cl ) { 269 if (cl == null) { 327 sb.append(")"); 328 sb.append(getType(meth.getReturnType()).getSignature()); 329 return sb.toString(); 330 } 331 332 static int size(final int coded) { 333 return coded & 3; 334 } 335 336 static int consumed(final int coded) { 337 return coded >> 2; 338 } 339 340 static int encode(final int size, final int consumed) { 341 return consumed << 2 | size; 342 } 343 344 static int getArgumentTypesSize( final String signature ) { 345 int res = 0; 346 int index; 347 try { // Read all declarations between for `(' and `)' 348 if (signature.charAt(0) != '(') { 349 throw new ClassFormatException("Invalid method signature: " + signature); 350 } 351 index = 1; // current string position 352 while (signature.charAt(index) != ')') { 353 final int coded = getTypeSize(signature.substring(index)); 354 res += size(coded); 355 index += consumed(coded); 356 } 357 } catch (final StringIndexOutOfBoundsException e) { // Should never occur 358 throw new ClassFormatException("Invalid method signature: " + signature, e); 359 } 360 return res; 361 } 362 363 static int getTypeSize( final String signature ) throws StringIndexOutOfBoundsException { 364 final byte type = Utility.typeOfSignature(signature); 365 if (type <= Const.T_VOID) { 366 return encode(BasicType.getType(type).getSize(), 1); 367 } else if (type == Const.T_ARRAY) { 368 int dim = 0; 369 do { // Count dimensions 370 dim++; 371 } while (signature.charAt(dim) == '['); | 1 /* 2 * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package com.sun.org.apache.bcel.internal.generic; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 25 import com.sun.org.apache.bcel.internal.Const; 26 import com.sun.org.apache.bcel.internal.classfile.ClassFormatException; 27 import com.sun.org.apache.bcel.internal.classfile.Utility; 28 29 /** 30 * Abstract super class for all possible java types, namely basic types 31 * such as int, object types like String and array types, e.g. int[] 32 * 33 * @LastModified: Jan 2020 34 */ 35 public abstract class Type { 36 37 private final byte type; 38 private String signature; // signature for the type 39 /** 40 * Predefined constants 41 */ 42 public static final BasicType VOID = new BasicType(Const.T_VOID); 43 public static final BasicType BOOLEAN = new BasicType(Const.T_BOOLEAN); 44 public static final BasicType INT = new BasicType(Const.T_INT); 45 public static final BasicType SHORT = new BasicType(Const.T_SHORT); 46 public static final BasicType BYTE = new BasicType(Const.T_BYTE); 47 public static final BasicType LONG = new BasicType(Const.T_LONG); 48 public static final BasicType DOUBLE = new BasicType(Const.T_DOUBLE); 49 public static final BasicType FLOAT = new BasicType(Const.T_FLOAT); 50 public static final BasicType CHAR = new BasicType(Const.T_CHAR); 51 public static final ObjectType OBJECT = new ObjectType("java.lang.Object"); 52 public static final ObjectType CLASS = new ObjectType("java.lang.Class"); 53 public static final ObjectType STRING = new ObjectType("java.lang.String"); 189 // @since 6.0 no longer final 190 public static Type getType( final String signature ) throws StringIndexOutOfBoundsException { 191 final byte type = Utility.typeOfSignature(signature); 192 if (type <= Const.T_VOID) { 193 //corrected concurrent private static field acess 194 wrap(consumed_chars, 1); 195 return BasicType.getType(type); 196 } else if (type == Const.T_ARRAY) { 197 int dim = 0; 198 do { // Count dimensions 199 dim++; 200 } while (signature.charAt(dim) == '['); 201 // Recurse, but just once, if the signature is ok 202 final Type t = getType(signature.substring(dim)); 203 //corrected concurrent private static field acess 204 // consumed_chars += dim; // update counter - is replaced by 205 final int _temp = unwrap(consumed_chars) + dim; 206 wrap(consumed_chars, _temp); 207 return new ArrayType(t, dim); 208 } else { // type == T_REFERENCE 209 // Utility.typeSignatureToString understands how to parse generic types. 210 final String parsedSignature = Utility.typeSignatureToString(signature, false); 211 wrap(consumed_chars, parsedSignature.length() + 2); // "Lblabla;" `L' and `;' are removed 212 return ObjectType.getInstance(parsedSignature.replace('/', '.')); 213 } 214 } 215 216 217 /** 218 * Convert return value of a method (signature) to a Type object. 219 * 220 * @param signature signature string such as (Ljava/lang/String;)V 221 * @return return type 222 */ 223 public static Type getReturnType( final String signature ) { 224 try { 225 // Read return type after `)' 226 final int index = signature.lastIndexOf(')') + 1; 227 return getType(signature.substring(index)); 228 } catch (final StringIndexOutOfBoundsException e) { // Should never occur 229 throw new ClassFormatException("Invalid method signature: " + signature, e); 230 } 231 } 232 233 234 /** 235 * Convert arguments of a method (signature) to an array of Type objects. 236 * @param signature signature string such as (Ljava/lang/String;)V 237 * @return array of argument types 238 */ 239 public static Type[] getArgumentTypes( final String signature ) { 240 final List<Type> vec = new ArrayList<>(); 241 int index; 242 Type[] types; 243 try { 244 // Skip any type arguments to read argument declarations between `(' and `)' 245 index = signature.indexOf('(') + 1; 246 if (index <= 0) { 247 throw new ClassFormatException("Invalid method signature: " + signature); 248 } 249 while (signature.charAt(index) != ')') { 250 vec.add(getType(signature.substring(index))); 251 //corrected concurrent private static field acess 252 index += unwrap(consumed_chars); // update position 253 } 254 } catch (final StringIndexOutOfBoundsException e) { // Should never occur 255 throw new ClassFormatException("Invalid method signature: " + signature, e); 256 } 257 types = new Type[vec.size()]; 258 vec.toArray(types); 259 return types; 260 } 261 262 263 /** Convert runtime java.lang.Class to BCEL Type object. 264 * @param cl Java class 265 * @return corresponding Type object 266 */ 267 public static Type getType( final java.lang.Class<?> cl ) { 268 if (cl == null) { 326 sb.append(")"); 327 sb.append(getType(meth.getReturnType()).getSignature()); 328 return sb.toString(); 329 } 330 331 static int size(final int coded) { 332 return coded & 3; 333 } 334 335 static int consumed(final int coded) { 336 return coded >> 2; 337 } 338 339 static int encode(final int size, final int consumed) { 340 return consumed << 2 | size; 341 } 342 343 static int getArgumentTypesSize( final String signature ) { 344 int res = 0; 345 int index; 346 try { 347 // Skip any type arguments to read argument declarations between `(' and `)' 348 index = signature.indexOf('(') + 1; 349 if (index <= 0) { 350 throw new ClassFormatException("Invalid method signature: " + signature); 351 } 352 while (signature.charAt(index) != ')') { 353 final int coded = getTypeSize(signature.substring(index)); 354 res += size(coded); 355 index += consumed(coded); 356 } 357 } catch (final StringIndexOutOfBoundsException e) { // Should never occur 358 throw new ClassFormatException("Invalid method signature: " + signature, e); 359 } 360 return res; 361 } 362 363 static int getTypeSize( final String signature ) throws StringIndexOutOfBoundsException { 364 final byte type = Utility.typeOfSignature(signature); 365 if (type <= Const.T_VOID) { 366 return encode(BasicType.getType(type).getSize(), 1); 367 } else if (type == Const.T_ARRAY) { 368 int dim = 0; 369 do { // Count dimensions 370 dim++; 371 } while (signature.charAt(dim) == '['); |