private boolean shouldSetBracket(CtExpression<?> e) { if (!e.getTypeCasts().isEmpty()) { return true; } try { if ((e.getParent() instanceof CtBinaryOperator) || (e.getParent() instanceof CtUnaryOperator)) { return (e instanceof CtTargetedExpression) || (e instanceof CtAssignment) || (e instanceof CtConditional) || (e instanceof CtUnaryOperator) || e instanceof CtBinaryOperator; } if (e.getParent() instanceof CtTargetedExpression) { return (e instanceof CtBinaryOperator) || (e instanceof CtAssignment) || (e instanceof CtConditional) || (e instanceof CtUnaryOperator); } } catch (ParentNotInitializedException ex) { // nothing we accept not to have a parent } return false; }
/** * Creates a constructor reference. * @param type Declaring type of the constructor. * @param parameters Constructor parameters. * @param <T> Infered type of the constructor. * @return CtExecutablereference if a constructor. */ public <T> CtExecutableReference<T> createReference(CtTypeReference<T> type, CtExpression<?>...parameters) { final CtExecutableReference<T> executableReference = factory.Core().createExecutableReference(); executableReference.setType(type); executableReference.setDeclaringType(type == null ? null : type.clone()); executableReference.setSimpleName(CtExecutableReference.CONSTRUCTOR_NAME); List<CtTypeReference<?>> typeReferences = new ArrayList<>(); for (CtExpression<?> parameter : parameters) { typeReferences.add(parameter.getType() == null ? null : parameter.getType().clone()); } executableReference.setParameters(typeReferences); return executableReference; }
private <C extends CtAbstractInvocation<T>> C addArgument(int position, CtExpression<?> argument) { if (argument == null) { return (C) this; } if (arguments == CtElementImpl.<CtExpression<?>>emptyList()) { arguments = new ArrayList<>(PARAMETERS_CONTAINER_DEFAULT_CAPACITY); } argument.setParent(this); getFactory().getEnvironment().getModelChangeListener().onListAdd(this, ARGUMENT, this.arguments, position, argument); arguments.add(position, argument); return (C) this; }
@SuppressWarnings("unchecked") public static <T> CtExpression<T> compileExpression( CtCodeSnippetExpression<T> expr) throws SnippetCompilationError { CtReturn<T> ret = (CtReturn<T>) internalCompileStatement(expr, expr.getFactory().Type().OBJECT); CtExpression<T> returnedExpression = ret.getReturnedExpression(); // this compiled expression is not in a parent, ready to be used returnedExpression.delete(); returnedExpression.setParent(null); return returnedExpression; }
newArray.setType(ctExpression.getType()); newArray.setParent(this); newArray.addElement(ctExpression); expression.setParent(this); getFactory().getEnvironment().getModelChangeListener().onMapAdd(this, CtRole.VALUE, this.elementValues, elementName, expression); elementValues.put(elementName, expression);
@Override public <T extends CtExpression> T getWrappedValue(String key) { CtExpression ctExpression = this.getValue(key); CtTypeReference typeReference = this.getAnnotationType(); CtType type = typeReference.getTypeDeclaration(); if (type != null) { CtMethod method = type.getMethod(key); if (method != null) { CtTypeReference returnType = method.getType(); if (returnType instanceof CtArrayTypeReference && !(ctExpression instanceof CtNewArray)) { CtNewArray newArray = getFactory().Core().createNewArray(); CtArrayTypeReference typeReference2 = this.getFactory().createArrayTypeReference(); typeReference2.setComponentType(ctExpression.getType().clone()); newArray.setType(typeReference2); newArray.addElement(ctExpression.clone()); return (T) newArray; } } } return (T) ctExpression; }
CtType<?> target = expToChange.getParent(CtType.class); Set<ModifierKind> modifiers = new HashSet<>(); modifiers.add(ModifierKind.PRIVATE); CtTypeReference returnType = expToChange.getType(); String name = "_meta_" + id; CtExpression expCloned = expToChange.clone(); expCloned.setPosition(new NoSourcePosition()); clearPosition(expCloned);
(boolean) invocation.getMetadata(AssertGeneratorHelper.METADATA_ASSERT_AMPLIFICATION))) { for (CtExpression<?> argument : invocation.getArguments()) { CtExpression clone = ((CtExpression) argument).clone(); if (clone instanceof CtUnaryOperator) { clone = ((CtUnaryOperator) clone).getOperand(); clone.getTypeCasts().clear(); invocation.getParent(CtStatementList.class).insertBefore(statementTypeFilter, (CtStatement) clone); } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) { CtTypeReference<?> typeOfParameter = clone.getType(); if (clone.getType().equals(factory.Type().NULL_TYPE)) { typeOfParameter = factory.Type().createReference(Object.class);
toReplace.replace(factory.Code() .createClassAccess(factory.Type().createReference(((Class<?>) value).getName()))); } else if (value instanceof Enum) { CtTypeReference<?> enumType = factory.Type().createReference(value.getClass()); toReplace.replace(factory.Code().createVariableRead( factory.Field().createReference(enumType, enumType, ((Enum<?>) value).name()), true)); } else if (value instanceof List) { inv.getExecutable().getActualTypeArguments().remove(i); for (CtExpression<?> va : vas) { va.setParent(fieldAccess.getParent()); inv.getArguments().add(i, va); inv.getExecutable().getActualTypeArguments().add(i, va.getType()); i++; toReplace.replace(factory.Code().createLiteralArray((Object[]) value)); } else { toReplace.replace(factory.Code().createLiteral(value));
/** * Gets the index of a one-dimension array (helper). */ @SuppressWarnings("unchecked") public static Integer getIndex(CtExpression<?> e) { if (e.getParent() instanceof CtArrayAccess) { CtExpression<Integer> indexExpression = ((CtArrayAccess<?, CtExpression<Integer>>) e.getParent()).getIndexExpression(); return ((CtLiteral<Integer>) indexExpression).getValue(); } return null; }
.getElements(e -> e.getType() != null && e.getType().unbox().getSimpleName().equals("boolean")); CtExpression expCloned = expressionToExpand.clone(); expCloned.setPosition(new NoSourcePosition()); MutationSupporter.clearPosition(expCloned);
private List<Ingredient> computeIngredientsFromExpressionExplansion(ModificationPoint modificationPoint, CtExpression previousExpression, List<IngredientFromDyna> ingredientsDynamoth, BinaryOperatorKind operatorKind2) { List<Ingredient> ingredientsNewBinaryExpressions = new ArrayList(); for (IngredientFromDyna ingredientFromDyna : ingredientsDynamoth) { CtBinaryOperator binaryOperator = new CtBinaryOperatorImpl<>(); binaryOperator.setKind(operatorKind2); CtExpression previousExpressionCloned = previousExpression.clone(); MutationSupporter.clearPosition(previousExpressionCloned); binaryOperator.setLeftHandOperand(previousExpressionCloned); CtExpression newRightExpression = MutationSupporter.getFactory() .createCodeSnippetExpression(ingredientFromDyna.getDynmothExpression().toString()); binaryOperator.setRightHandOperand(newRightExpression); // binaryOperator.setFactory(MutationSupporter.getFactory()); binaryOperator.setParent(previousExpression.getParent()); Ingredient newIngredientExtended = new Ingredient(binaryOperator); newIngredientExtended.setDerivedFrom(previousExpression); ingredientsNewBinaryExpressions.add(newIngredientExtended); } return ingredientsNewBinaryExpressions; }
private void replaceDouble(CtExpression ctElement) { if (getValue() == null) { CtLocalVariable<Double> evaluation = newLocalVariableDeclaration( ctElement.getFactory(), double.class, "guess_fix", Debug.class.getCanonicalName() + ".makeSymbolicReal(\"guess_fix\")"); CtStatement firstStatement = getFirstStatement(ctElement); if (firstStatement == null) { return; } SpoonStatementLibrary.insertBeforeUnderSameParent(evaluation, firstStatement); // SpoonStatementLibrary.insertAfterUnderSameParent(getFactory().Code().createCodeSnippetStatement("System.out.println(\"guess_fix: \" + guess_fix)"), // getFirstStatement(ctElement)); ctElement.replace(getFactory().Code().createCodeSnippetExpression("guess_fix")); } else { ctElement.replace(getFactory().Code().createCodeSnippetExpression(getValue())); } }
public static boolean undoReplace(OperatorInstance operation) { try { CtExpression ctst = (CtExpression) operation.getOriginal(); CtExpression fix = (CtExpression) operation.getModified(); fix.replace(ctst); return true; } catch (Exception ex) { log.error("Error applying an operation, exception: " + ex.getMessage()); return false; } }
@Override public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) { boolean successful = false; CtExpression rightTerm = null, leftTerm = null; try { CtExpression ctst = (CtExpression) operation.getOriginal(); CtExpression fix = (CtExpression) operation.getModified(); ctst.replace((CtExpression) fix); successful = true; operation.setSuccessfulyApplied((successful)); log.debug(" applied: " + ctst.getParent().toString()); } catch (Exception ex) { log.error("Error applying an operation, exception: " + ex.getMessage()); operation.setExceptionAtApplied(ex); operation.setSuccessfulyApplied(false); } return true; }
if (! literal.getTypeCasts().isEmpty()){ classOfLiteral = literal.getTypeCasts().get(0).getActualClass(); } else { classOfLiteral = literal.getType().getActualClass();
public List<CtExpression<?>> cloneList(List<CtExpression<?>> list) { List<CtExpression<?>> clone = new ArrayList<CtExpression<?>>(list.size()); for (CtExpression<?> item : list) clone.add(item.clone()); return clone; }
@SuppressWarnings("unchecked") @Override public <T, U> U getValue(T element) { return ((U) ((Object) (castTarget(element).getTypeCasts()))); }
private static void addTypeCastIfNeeded(CtExpression<?> variableRead, Object value) { if (value instanceof Short) { variableRead.addTypeCast(variableRead.getFactory().Type().shortPrimitiveType()); } else if (value instanceof Integer) { variableRead.addTypeCast(variableRead.getFactory().Type().integerPrimitiveType()); } else if (value instanceof Long) { variableRead.addTypeCast(variableRead.getFactory().Type().longPrimitiveType()); } else if (value instanceof Byte) { variableRead.addTypeCast(variableRead.getFactory().Type().bytePrimitiveType()); } else if (value instanceof Float) { variableRead.addTypeCast(variableRead.getFactory().Type().floatPrimitiveType()); } else if (value instanceof Double) { variableRead.addTypeCast(variableRead.getFactory().Type().doublePrimitiveType()); } else if (value instanceof Character) { variableRead.addTypeCast(variableRead.getFactory().Type().characterPrimitiveType()); } }
@SuppressWarnings("unchecked") void enter(CtElement e, ASTNode node) { stack.push(new ASTPair(e, node)); if (!(e instanceof CtPackage) || (compilationUnitSpoon.getFile() != null && compilationUnitSpoon.getFile().getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) { if (compilationunitdeclaration != null && !e.isImplicit()) { e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node)); } } ASTPair pair = stack.peek(); CtElement current = pair.element; if (current instanceof CtExpression) { while (!casts.isEmpty()) { ((CtExpression<?>) current).addTypeCast(casts.remove(0).typeRef); } } try { if (e instanceof CtTypedElement && !(e instanceof CtConstructorCall) && !(e instanceof CtCatchVariable) && node instanceof Expression) { if (((CtTypedElement<?>) e).getType() == null) { ((CtTypedElement<Object>) e).setType(this.jdtTreeBuilder.getReferencesBuilder().getTypeReference(((Expression) node).resolvedType)); } } } catch (UnsupportedOperationException ignore) { // For some element, we throw an UnsupportedOperationException when we call setType(). } }