/** * Parse a method declaration. The declaration should be in the following format: * * <p>fully-qualified-method-name (args) * * <p>where the arguments are comma separated and all arguments other than primitives should have * fully qualified names. Arrays are indicating by trailing brackets. For example: * * <p>int int[] int[][] java.lang.String java.util.Date[] * * <p>The arguments are translated into BCEL types and a MethodDef is returned. */ private MethodDef parse_method(StrTok st) { // Get the method name String method_name = st.need_word(); // Get the opening paren st.need("("); // Read the arguments ArrayList<String> args = new ArrayList<String>(); String tok = st.nextToken(); if (tok != ")") { st.pushBack(); do { tok = st.need_word(); args.add(tok); } while (st.nextToken() == ","); st.pushBack(); st.need(")"); } // Convert the arguments to Type Type[] targs = new Type[args.size()]; for (int ii = 0; ii < args.size(); ii++) { targs[ii] = BCELUtil.classname_to_type(args.get(ii)); } return new MethodDef(method_name, targs); }
/** * Transforms invoke instructions that match the specified list for this class to call the * specified static call instead. */ private InstructionList xform_inst(MethodGen mg, Instruction inst) { switch (inst.getOpcode()) { case Constants.INVOKESTATIC: { InstructionList il = new InstructionList(); INVOKESTATIC is = (INVOKESTATIC) inst; String cname = is.getClassName(pgen); String mname = is.getMethodName(pgen); Type[] args = is.getArgumentTypes(pgen); MethodDef orig = new MethodDef(cname + "." + mname, args); MethodInfo call = method_map.get(orig); if (call != null) { call.cnt++; String classname = call.method_class; String methodname = mname; debug_map.log( "%s.%s: Replacing method %s.%s (%s) with %s.%s%n", mg.getClassName(), mg.getName(), cname, mname, UtilMDE.join(args, ", "), classname, methodname); il.append( ifact.createInvoke( classname, methodname, is.getReturnType(pgen), args, Constants.INVOKESTATIC)); } return (il); } case Constants.INVOKEVIRTUAL: { InstructionList il = new InstructionList(); INVOKEVIRTUAL iv = (INVOKEVIRTUAL) inst; String cname = iv.getClassName(pgen); String mname = iv.getMethodName(pgen); Type[] args = iv.getArgumentTypes(pgen); Type instance_type = iv.getReferenceType(pgen); Type[] new_args = BCELUtil.insert_type(instance_type, args); MethodDef orig = new MethodDef(cname + "." + mname, args); if (debug_class) System.out.printf("looking for %s in map %s%n", orig, method_map); MethodInfo call = method_map.get(orig); if (call != null) { call.cnt++; String classname = call.method_class; String methodname = mname; debug_map.log( "Replacing method %s.%s (%s) with %s.%s%n", cname, mname, ArraysMDE.toString(args), classname, methodname); il.append( ifact.createInvoke( classname, methodname, iv.getReturnType(pgen), new_args, Constants.INVOKESTATIC)); } return (il); } default: return (null); } }