static StackFrameType get(int frame_type) { if (frame_type >= Const.SAME_FRAME && frame_type <= Const.SAME_FRAME_MAX) { return SAME_FRAME; } else if (frame_type >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frame_type <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) { return SAME_LOCALS_1_STACK_ITEM_FRAME; } else if (frame_type == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { return SAME_LOCALS_1_STACK_ITEM_FRAME; } else if (frame_type >= Const.CHOP_FRAME && frame_type <= Const.CHOP_FRAME_MAX) { return CHOP_FRAME; } else if (frame_type == Const.SAME_FRAME_EXTENDED) { return SAME_FRAME; } else if (frame_type >= Const.APPEND_FRAME && frame_type <= Const.APPEND_FRAME_MAX) { return APPEND_FRAME; } else if (frame_type == Const.FULL_FRAME) { return FULL_FRAME; } else { /* Can't happen */ throw new ClassFormatException("Invalid frame type : " + frame_type); } } }
logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage());
return new VerificationResult(VerificationResult.VERIFIED_REJECTED, e.getMessage());
throw new ClassFormatException("Unknown constant type " + tag);
/** * Get constant from constant pool. * * @param index Index in constant pool * @return Constant value * @see Constant */ public Constant getConstant( final int index ) { if (index >= constant_pool.length || index < 0) { throw new ClassFormatException("Invalid constant pool reference: " + index + ". Constant pool size is: " + constant_pool.length); } return constant_pool[index]; }
/** * Get constant from constant pool. * * @param index Index in constant pool * @return Constant value * @see Constant */ public Constant getConstant(int index) { if (index >= constant_pool.length || index < 0) throw new ClassFormatException("Invalid constant pool reference: " + index + ". Constant pool size is: " + constant_pool.length); return constant_pool[index]; }
static StackFrameType get(int frame_type) { if (frame_type >= Constants.SAME_FRAME && frame_type <= Constants.SAME_FRAME_MAX) { return SAME_FRAME; } else if (frame_type >= Constants.SAME_LOCALS_1_STACK_ITEM_FRAME && frame_type <= Constants.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) { return SAME_LOCALS_1_STACK_ITEM_FRAME; } else if (frame_type == Constants.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { return SAME_LOCALS_1_STACK_ITEM_FRAME; } else if (frame_type >= Constants.CHOP_FRAME && frame_type <= Constants.CHOP_FRAME_MAX) { return CHOP_FRAME; } else if (frame_type == Constants.SAME_FRAME_EXTENDED) { return SAME_FRAME; } else if (frame_type >= Constants.APPEND_FRAME && frame_type <= Constants.APPEND_FRAME_MAX) { return APPEND_FRAME; } else if (frame_type == Constants.FULL_FRAME) { return FULL_FRAME; } else { /* Can't happen */ throw new ClassFormatException("Invalid frame type : " + frame_type); } } }
/** * Check whether the header of the file is ok. * Of course, this has to be the first action on successive file reads. * @throws IOException * @throws ClassFormatException */ private void readID() throws IOException, ClassFormatException { if (dataInputStream.readInt() != Const.JVM_CLASSFILE_MAGIC) { throw new ClassFormatException(file_name + " is not a Java .class file"); } }
/** * Check whether the header of the file is ok. * Of course, this has to be the first action on successive file reads. * @throws IOException * @throws ClassFormatException */ private final void readID() throws IOException, ClassFormatException { int magic = 0xCAFEBABE; if(file.readInt() != magic) throw new ClassFormatException(file_name + " is not a Java .class file"); } /**
/** * Return type of signature as a byte value as defined in <em>Constants</em> * * @param signature in format described above * @return type of signature * @see Constants */ public static final byte typeOfSignature(String signature) throws ClassFormatException { try { switch(signature.charAt(0)) { case 'B' : return Constants.T_BYTE; case 'C' : return Constants.T_CHAR; case 'D' : return Constants.T_DOUBLE; case 'F' : return Constants.T_FLOAT; case 'I' : return Constants.T_INT; case 'J' : return Constants.T_LONG; case 'L' : return Constants.T_REFERENCE; case '[' : return Constants.T_ARRAY; case 'V' : return Constants.T_VOID; case 'Z' : return Constants.T_BOOLEAN; case 'S' : return Constants.T_SHORT; default: throw new ClassFormatException("Invalid method signature: " + signature); } } catch(StringIndexOutOfBoundsException e) { throw new ClassFormatException("Invalid method signature: " + signature); } }
/** * Read information about the class and its super class. * @throws IOException * @throws ClassFormatException */ private void readClassInfo() throws IOException, ClassFormatException { access_flags = dataInputStream.readUnsignedShort(); /* Interfaces are implicitely abstract, the flag should be set * according to the JVM specification. */ if ((access_flags & Const.ACC_INTERFACE) != 0) { access_flags |= Const.ACC_ABSTRACT; } if (((access_flags & Const.ACC_ABSTRACT) != 0) && ((access_flags & Const.ACC_FINAL) != 0)) { throw new ClassFormatException("Class " + file_name + " can't be both final and abstract"); } class_name_index = dataInputStream.readUnsignedShort(); superclass_name_index = dataInputStream.readUnsignedShort(); }
/** * Read information about the class and its super class. * @throws IOException * @throws ClassFormatException */ private final void readClassInfo() throws IOException, ClassFormatException { access_flags = file.readUnsignedShort(); /* Interfaces are implicitely abstract, the flag should be set * according to the JVM specification. */ if((access_flags & Constants.ACC_INTERFACE) != 0) access_flags |= Constants.ACC_ABSTRACT; if(((access_flags & Constants.ACC_ABSTRACT) != 0) && ((access_flags & Constants.ACC_FINAL) != 0 )) throw new ClassFormatException("Class can't be both final and abstract"); class_name_index = file.readUnsignedShort(); superclass_name_index = file.readUnsignedShort(); } /**
/** * Return type of method signature as a byte value as defined in <em>Constants</em> * * @param signature in format described above * @return type of method signature * @see Constants */ public static final byte typeOfMethodSignature(String signature) throws ClassFormatException { int index; try { if(signature.charAt(0) != '(') throw new ClassFormatException("Invalid method signature: " + signature); index = signature.lastIndexOf(')') + 1; return typeOfSignature(signature.substring(index)); } catch(StringIndexOutOfBoundsException e) { throw new ClassFormatException("Invalid method signature: " + signature); } }
/** * Convert return value of a method (signature) to a Type object. * * @param signature signature string such as (Ljava/lang/String;)V * @return return type */ public static Type getReturnType( final String signature ) { try { // Read return type after `)' final int index = signature.lastIndexOf(')') + 1; return getType(signature.substring(index)); } catch (final StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature, e); } }
/** * Return type of method signature as a byte value as defined in <em>Constants</em> * * @param signature in format described above * @return type of method signature * @see Const * * @throws ClassFormatException if signature is not a method signature */ public static byte typeOfMethodSignature( final String signature ) throws ClassFormatException { int index; try { if (signature.charAt(0) != '(') { throw new ClassFormatException("Invalid method signature: " + signature); } index = signature.lastIndexOf(')') + 1; return typeOfSignature(signature.substring(index)); } catch (final StringIndexOutOfBoundsException e) { throw new ClassFormatException("Invalid method signature: " + signature, e); } }
/** * Convert return value of a method (signature) to a Type object. * * @param signature signature string such as (Ljava/lang/String;)V * @return return type */ public static Type getReturnType(String signature) { try { // Read return type after `)' int index = signature.lastIndexOf(')') + 1; return getType(signature.substring(index)); } catch(StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature); } }
/** * Convert arguments of a method (signature) to an array of Type objects. * @param signature signature string such as (Ljava/lang/String;)V * @return array of argument types */ public static Type[] getArgumentTypes(String signature) { ArrayList vec = new ArrayList(); int index; Type[] types; try { // Read all declarations between for `(' and `)' if(signature.charAt(0) != '(') throw new ClassFormatException("Invalid method signature: " + signature); index = 1; // current string position while(signature.charAt(index) != ')') { vec.add(getType(signature.substring(index))); index += consumed_chars; // update position } } catch(StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature); } types = new Type[vec.size()]; vec.toArray(types); return types; }
/** * @param signature Method signature * @param chopit Shorten class names ? * @return return type of method * @throws ClassFormatException */ public static String methodSignatureReturnType( final String signature, final boolean chopit ) throws ClassFormatException { int index; String type; try { // Read return type after `)' index = signature.lastIndexOf(')') + 1; type = signatureToString(signature.substring(index), chopit); } catch (final StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature, e); } return type; }
/** * @param signature Method signature * @param chopit Shorten class names ? * @return Array of argument types * @throws ClassFormatException */ public static final String[] methodSignatureArgumentTypes(String signature, boolean chopit) throws ClassFormatException { ArrayList vec = new ArrayList(); int index; String[] types; try { // Read all declarations between for `(' and `)' if(signature.charAt(0) != '(') throw new ClassFormatException("Invalid method signature: " + signature); index = 1; // current string position while(signature.charAt(index) != ')') { vec.add(signatureToString(signature.substring(index), chopit)); index += consumed_chars; // update position } } catch(StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature); } types = new String[vec.size()]; vec.toArray(types); return types; } /**
/** * @param signature Method signature * @param chopit Shorten class names ? * @return Array of argument types * @throws ClassFormatException */ public static String[] methodSignatureArgumentTypes( final String signature, final boolean chopit ) throws ClassFormatException { final List<String> vec = new ArrayList<>(); int index; try { // Read all declarations between for `(' and `)' if (signature.charAt(0) != '(') { throw new ClassFormatException("Invalid method signature: " + signature); } index = 1; // current string position while (signature.charAt(index) != ')') { vec.add(signatureToString(signature.substring(index), chopit)); //corrected concurrent private static field acess index += unwrap(consumed_chars); // update position } } catch (final StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature, e); } return vec.toArray(new String[vec.size()]); }