/** * replace assertTrue * * @param ctInvocation * @return */ private CtStatement replaceAssertTrueFalse(boolean type, CtInvocation<?> ctInvocation) { List<?> arguments = ctInvocation.getArguments(); CtIf newIf = ctInvocation.getFactory().Core().createIf(); Object elem1 = arguments.get(0); if (arguments.size() > 1) { elem1 = arguments.get(1); } if (!type) { CtExpression<Boolean> condition = ctInvocation.getFactory().Code().createCodeSnippetExpression("!(" + elem1 + ")"); newIf.setCondition(condition); } else { newIf.setCondition((CtExpression<Boolean>) elem1); } // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement( // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")")); /* * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core() * .createBlock(); // thenStatement.addStatement((getFactory().Code(). * createCodeSnippetStatement("System.out.println(\"Then...\")"))); * thenStatement.addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(" + * Debug.class.getCanonicalName() + ".getSolvedPC())"))); * newIf.setThenStatement(thenStatement); */ newIf.setThenStatement(createThen(ctInvocation)); return newIf; }
/** Actually invokes from a compile-time invocation (by using runtime reflection). */ @SuppressWarnings("unchecked") public static <T> T invoke(CtInvocation<T> i) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Object target = i.getTarget() == null ? null : ((CtLiteral<?>) i.getTarget()).getValue(); List<Object> args = new ArrayList<Object>(); for (CtExpression<?> e : i.getArguments()) { args.add(((CtLiteral<?>) e).getValue()); } Class<?> c = i.getExecutable().getDeclaringType().getActualClass(); ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(); for (CtTypeReference<?> type : i.getExecutable().getActualTypeArguments()) { argTypes.add(type.getActualClass()); } return (T) c.getMethod(i.getExecutable().getSimpleName(), argTypes.toArray(new Class[argTypes.size()])) .invoke(target, args.toArray()); }
/** * replace assertNotNull * * @param ctInvocation * @return */ private CtStatement replaceAssertNotNull(CtInvocation<?> ctInvocation) { List<?> arguments = ctInvocation.getArguments(); CtIf newIf = ctInvocation.getFactory().Core().createIf(); Object elem1 = arguments.get(0); CtExpression<Boolean> condition = ctInvocation.getFactory().Code().createCodeSnippetExpression("(" + elem1 + ") != null"); newIf.setCondition(condition); // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement( // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")")); /* * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core() * .createBlock(); thenStatement .addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(\"Then...\")"))); * thenStatement.addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(" + * Debug.class.getCanonicalName() + ".getSolvedPC())"))); * newIf.setThenStatement(thenStatement); */ newIf.setThenStatement(createThen(ctInvocation)); return newIf; }
@Override public void process(CtConditional ctConditional) { CtStatement parent = ctConditional.getParent(CtStatement.class); while (!(parent.getParent() instanceof CtStatementList)) { parent = parent.getParent(CtStatement.class); } CtExpression condition = ctConditional.getCondition(); CtIf anIf = getFactory().Core().createIf(); anIf.setPosition(ctConditional.getPosition()); if (parent instanceof CtReturn) { if (!((CtReturn) parent).getReturnedExpression().equals(ctConditional)) { return; } CtReturn returnThen = (CtReturn) parent.clone(); CtReturn returnElse = (CtReturn) parent.clone(); returnThen.setReturnedExpression(ctConditional.getThenExpression()); returnElse.setReturnedExpression(ctConditional.getElseExpression()); List<CtTypeReference> typeCasts = ctConditional.getTypeCasts(); for (int i = 0; i < typeCasts.size(); i++) { CtTypeReference ctTypeReference = typeCasts.get(i); returnThen.getReturnedExpression().addTypeCast(ctTypeReference.clone()); returnElse.getReturnedExpression().addTypeCast(ctTypeReference.clone()); } anIf.setElseStatement(getFactory().Code().createCtBlock(returnElse)); anIf.setThenStatement(getFactory().Code().createCtBlock(returnThen)); } else if (parent instanceof CtAssignment) { CtAssignment assignment = (CtAssignment) parent; CtExpression ctExpression = assignment.getAssignment(); if (!ctExpression.equals(ctConditional)) { if (ctExpression instanceof CtBinaryOperator) { CtBinaryOperator ctBinaryOperator = (CtBinaryOperator) ctExpression; createAssignment(ctConditional, anIf, assignment); CtBinaryOperator cloneThen = ctBinaryOperator.clone(); CtBinaryOperator cloneElse = ctBinaryOperator.clone(); if (ctBinaryOperator.getLeftHandOperand().equals(ctConditional)) { cloneThen.setLeftHandOperand(ctConditional.getThenExpression()); ctConditional.getThenExpression().setParent(cloneThen); cloneElse.setLeftHandOperand(ctConditional.getElseExpression()); } else if (ctBinaryOperator.getRightHandOperand().equals(ctConditional)) { cloneThen.setRightHandOperand(ctConditional.getThenExpression()); cloneElse.setRightHandOperand(ctConditional.getElseExpression()); } cloneThen.getLeftHandOperand().setParent(cloneThen); cloneElse.getLeftHandOperand().setParent(cloneElse); ((CtAssignment) ((CtBlock) anIf.getThenStatement()).getStatement(0)) .setAssignment(cloneThen); ((CtAssignment) ((CtBlock) anIf.getElseStatement()).getStatement(0)) .setAssignment(cloneElse); } else { return; } } else { createAssignment(ctConditional, anIf, assignment); } } else if (parent instanceof CtLocalVariable) { CtLocalVariable localVariable = (CtLocalVariable) parent; if (!localVariable.getDefaultExpression().equals(ctConditional)) { return; } CtLocalVariable clone = localVariable.clone(); clone.setDefaultExpression(null); localVariable.insertBefore(clone); CtAssignment variableAssignment = getFactory() .Code() .createVariableAssignment(localVariable.getReference(), false, ctConditional); variableAssignment.setType(localVariable.getType().clone()); variableAssignment.setPosition(ctConditional.getPosition()); createAssignment(ctConditional, anIf, variableAssignment); } else if (parent instanceof CtInvocation) { CtInvocation invocation = (CtInvocation) parent; CtInvocation cloneThen = invocation.clone(); CtInvocation cloneElse = invocation.clone(); List arguments = cloneThen.getArguments(); boolean found = false; for (int i = 0; i < arguments.size(); i++) { Object o = arguments.get(i); if (o.equals(ctConditional)) { ctConditional.getThenExpression().setParent(invocation); arguments.set(i, ctConditional.getThenExpression()); ctConditional.getElseExpression().setParent(invocation); cloneElse.getArguments().set(i, ctConditional.getElseExpression()); found = true; break; } } if (!found) { return; } cloneThen.setParent(anIf); cloneElse.setParent(anIf); anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse)); anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen)); } else if (parent instanceof CtConstructorCall) { CtConstructorCall invocation = (CtConstructorCall) parent; CtConstructorCall cloneThen = invocation.clone(); CtConstructorCall cloneElse = invocation.clone(); List arguments = cloneThen.getArguments(); boolean found = false; for (int i = 0; i < arguments.size(); i++) { Object o = arguments.get(i); if (o.equals(ctConditional)) { arguments.set(i, ctConditional.getThenExpression()); cloneElse.getArguments().set(i, ctConditional.getElseExpression()); found = true; break; } } if (!found) { return; } cloneThen.setParent(anIf); cloneElse.setParent(anIf); anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse)); anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen)); } else if (parent instanceof CtIf) { CtIf elem = (CtIf) parent; if (!elem.getCondition().equals(ctConditional)) { return; } CtIf cloneThen = elem.clone(); cloneThen.setParent(anIf); CtIf cloneElse = elem.clone(); cloneElse.setParent(anIf); cloneThen.setCondition(ctConditional.getThenExpression()); ctConditional.getThenExpression().setParent(cloneThen); cloneElse.setCondition(ctConditional.getElseExpression()); ctConditional.getElseExpression().setParent(cloneElse); anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse)); anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen)); } else if (parent instanceof CtThrow) { return; } else if (parent instanceof CtLoop) { return; } else if (parent instanceof CtUnaryOperator) { return; } else { System.err.println(parent); throw new RuntimeException("Other " + parent.getClass()); } /*if(ctConditional.getThenExpression().getTypeCasts() == null || ctConditional.getThenExpression().getTypeCasts().isEmpty()) { ((CtExpression)anIf.getThenStatement()).setTypeCasts(ctConditional.getTypeCasts()); } if(ctConditional.getElseExpression().getTypeCasts() == null || ctConditional.getElseExpression().getTypeCasts().isEmpty()) { ((CtExpression) anIf.getElseStatement()) .setTypeCasts(ctConditional.getTypeCasts()); }*/ anIf.setCondition(condition); condition.setParent(anIf); parent.replace(anIf); }
/** * replace assert Equals * * @param ctInvocation * @return */ private CtStatement replaceAssertEquals(CtInvocation<?> ctInvocation) { List<CtExpression<?>> arguments = ctInvocation.getArguments(); CtIf newIf = ctInvocation.getFactory().Core().createIf(); CtCodeSnippetExpression<Boolean> condition = ctInvocation.getFactory().Core().createCodeSnippetExpression(); CtExpression<?> elem1 = arguments.get(0); CtExpression<?> elem2 = arguments.get(1); if (arguments.size() > 2 && (elem1.getType().equals(ctInvocation.getFactory().Class().STRING) || elem1.getType().equals(getFactory().Class().nullType()))) { elem1 = arguments.get(1); elem2 = arguments.get(2); } boolean isNull = false; Class<?> classArg1 = null; if (elem1.toString().equals("null")) { isNull = true; } else { try { classArg1 = ((CtTypedElement<?>) elem1).getType().getActualClass(); } catch (SpoonException e) { e.printStackTrace(); } } if (isNull || (classArg1 != null && (classArg1.equals(int.class) || classArg1.equals(Integer.class) || classArg1.equals(boolean.class) || classArg1.equals(Boolean.class) || classArg1.equals(byte.class) || classArg1.equals(Byte.class) || classArg1.equals(long.class) || classArg1.equals(Long.class) || classArg1.equals(double.class) || classArg1.equals(Double.class) || classArg1.equals(float.class) || classArg1.equals(Float.class) || classArg1.equals(short.class) || classArg1.equals(Short.class) || classArg1.equals(char.class) || classArg1.equals(Character.class)))) { condition.setValue("" + elem1 + " == " + elem2 + ""); } else { condition.setValue("(" + elem1 + ".equals(" + elem2 + "))"); } newIf.setCondition(condition); // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement( // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")")); // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement("System.out.println("+Debug.class.getCanonicalName()+".getSolvedPC())")); /* * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core() * .createBlock(); thenStatement.addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(\"Then...\")"))); * thenStatement.addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(" + * Debug.class.getCanonicalName() + ".getSolvedPC())"))); * newIf.setThenStatement(thenStatement); */ newIf.setThenStatement(createThen(ctInvocation)); return newIf; }