public void fixMethods() throws NotFoundException, CannotCompileException { for (CtMethod ctMethod : ctClass.getDeclaredMethods()) { fixMethod(ctMethod, true); } CtMethod equalsMethod = ctClass.getMethod("equals", "(Ljava/lang/Object;)Z"); CtMethod hashCodeMethod = ctClass.getMethod("hashCode", "()I"); CtMethod toStringMethod = ctClass.getMethod("toString", "()Ljava/lang/String;"); fixMethod(equalsMethod, false); fixMethod(hashCodeMethod, false); fixMethod(toStringMethod, false); }
public void fixConstructor(CtConstructor ctConstructor) throws NotFoundException, CannotCompileException { String methodBody = generateConstructorBody(ctConstructor.getParameterTypes()); ctConstructor.setBody("{\n" + methodBody + "}\n"); wrapMethodInvocation(ctConstructor, false); }
public void fixMethod(final CtMethod ctMethod, boolean wasFoundInClass) throws NotFoundException { String describeBefore = describe(ctMethod); try { CtClass declaringClass = ctMethod.getDeclaringClass(); String methodBody = generateMethodBody(ctMethod, wasNative, wasAbstract, returnCtClass, returnType, isStatic, !wasFoundInClass); CtMethod newMethod = makeNewMethod(ctMethod, returnCtClass, methodName, paramTypes, "{\n" + methodBody + generateCallToSuper(methodName, paramTypes) + "\n}"); newMethod.setModifiers(newModifiers); if (wasDeclaredInClass) { ctMethod.insertBefore("{\n" + methodBody + "}\n"); wrapMethodInvocation(ctMethod, isStatic); } else { wrapMethodInvocation(newMethod, isStatic); ctClass.addMethod(newMethod); CtMethod newMethod = makeNewMethod(ctMethod, returnCtClass, methodName, paramTypes, "{\n" + methodBody + "\n}"); ctMethod.setBody(newMethod, null); wrapMethodInvocation(ctMethod, isStatic); } else { ctMethod.insertBefore("{\n" + methodBody + "}\n"); wrapMethodInvocation(ctMethod, isStatic);
public void fixConstructors() throws CannotCompileException, NotFoundException { if (ctClass.isEnum()) { // skip enum constructors because they are not stubs in android.jar return; } boolean hasDefault = false; for (CtConstructor ctConstructor : ctClass.getDeclaredConstructors()) { try { fixConstructor(ctConstructor); if (ctConstructor.getParameterTypes().length == 0) { hasDefault = true; } } catch (Exception e) { throw new RuntimeException("problem instrumenting " + ctConstructor, e); } } if (!hasDefault) { String methodBody = generateConstructorBody(new CtClass[0]); CtConstructor defaultConstructor = CtNewConstructor.make(new CtClass[0], new CtClass[0], "{\n" + methodBody + "}\n", ctClass); wrapMethodInvocation(defaultConstructor, false); ctClass.addConstructor(defaultConstructor); } }
appendParamTypeArray(buf, ctMethod); buf.append(", "); appendParamArray(buf, ctMethod); buf.append(";\n"); if (shouldGenerateCallToSuper) { buf.append(generateCallToSuper(ctMethod.getName(), ctMethod.getParameterTypes())); } else { buf.append("return ");
MethodGenerator methodGenerator = new MethodGenerator(ctClass); methodGenerator.fixConstructors(); methodGenerator.fixMethods();