/** * 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; }
public MethodTypingContext setInvocation(CtInvocation<?> invocation) { if (classTypingContext == null) { CtExpression<?> target = invocation.getTarget(); if (target != null) { CtTypeReference<?> targetTypeRef = target.getType(); if (targetTypeRef != null) { classTypingContext = new ClassTypingContext(targetTypeRef); } } } setExecutableReference(invocation.getExecutable()); return this; }
/** * Creates a constructor call. The correct constructor is inferred based on parameters * * @param type the decelerating type of the constructor * @param parameters the arguments of the constructor call * @param <T> the actual type of the decelerating type of the constructor if available * @return the constructor call */ public <T> CtConstructorCall<T> createConstructorCall(CtTypeReference<T> type, CtExpression<?>...parameters) { CtConstructorCall<T> constructorCall = factory.Core() .createConstructorCall(); CtExecutableReference<T> executableReference = factory.Core() .createExecutableReference(); executableReference.setType(type); executableReference.setDeclaringType(type == null ? type : type.clone()); executableReference.setSimpleName(CtExecutableReference.CONSTRUCTOR_NAME); List<CtTypeReference<?>> typeReferences = new ArrayList<>(); for (CtExpression<?> parameter : parameters) { typeReferences.add(parameter.getType()); } executableReference.setParameters(typeReferences); constructorCall.setArguments(Arrays.asList(parameters)); constructorCall.setExecutable(executableReference); return constructorCall; }
newArray.setType(ctExpression.getType()); newArray.setParent(this); newArray.addElement(ctExpression);
@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; }
@Override public void process(CtExpression element) { if (element instanceof CtAssignment || element instanceof CtNewArray || element instanceof CtTypeAccess || element instanceof CtVariableAccess || element instanceof CtLiteral) return; if (element.getType() != null) this.add(element); }
@Override public void process(CtExpression element) { if (element.getType() != null) this.add(element); }
@Override public List<Ingredient> getIngredients(CtElement element) { if (element instanceof CtExpression) { String keyLocation = mapKey(element); CtExpression ctExpr = (CtExpression) element; String returnTypeExpression = (ctExpr.getType() == null) ? "null" : ctExpr.getType().getSimpleName(); List<Ingredient> ingredients = (List<Ingredient>) mkp.get(keyLocation, returnTypeExpression); return ingredients; } log.error("Element is not a expression: " + element.getClass().getCanonicalName()); return null; }
private List<String> resolveTypes(List<CtExpression<?>> origParam) { List<String> types = new ArrayList<String>(); for (int i = 0; i < origParam.size(); i++) { String type = origParam.get(i).getType().getQualifiedName(); // type = objToPrim.containsKey(type) ? objToPrim.get(type) : type; types.add(type); if (!typeCandidates.containsKey(type)) typeCandidates.put(type, ExpressionGenerator.fetchEXP(this.mutSupporter, this.modificationPoint, type, querytype)); } return types; }
@Override public List<Ingredient> getIngredients(CtElement element) { if (element instanceof CtExpression) { String keyLocation = mapKey(element); CtExpression ctExpr = (CtExpression) element; String typeExpression = ctExpr.getClass().getSimpleName(); String returnTypeExpression = (ctExpr.getType() == null) ? "null" : ctExpr.getType().getSimpleName(); List ingredients = (List<Ingredient>) mkp.get(keyLocation, typeExpression, returnTypeExpression); return ingredients; } log.error("Element is not a expression: " + element.getClass().getCanonicalName()); return null; }
@SuppressWarnings({ "rawtypes", "unused" }) private List<List<CtExpression>> createAllPossibleArgsListForMethod(CtMethod method, List<CtExpression> variables) { try { List<CtParameter> argumentTypes = method.getParameters(); List<List<CtExpression>> argumentCandidates = new ArrayList<>(); for (int j = 0; j < argumentTypes.size(); j++) { CtParameter par = argumentTypes.get(j); List<CtExpression> compatiblePar = new ArrayList<>(); for (CtExpression ctVariableRead : variables) { if (ctVariableRead.getType().isSubtypeOf(par.getType())) { compatiblePar.add(ctVariableRead); } } argumentCandidates.add(compatiblePar); } return argumentCandidates; } catch (Exception e) { e.printStackTrace(); } return null; }
@Override protected List<Ingredient> getIngrediedientsFromKey(String keyLocation, CtExpression ctExpr) { String typeExpression = ctExpr.getClass().getSimpleName(); String returnTypeExpression = (ctExpr.getType() != null) ? ctExpr.getType().getSimpleName() : "null"; List<Ingredient> ingredientsKey = (List<Ingredient>) mkp.get(keyLocation, typeExpression, returnTypeExpression); if (!mkp.containsKey(keyLocation, typeExpression, returnTypeExpression)) { ingredientsKey = new CacheList<Ingredient>(); mkp.put(keyLocation, typeExpression, returnTypeExpression, ingredientsKey); } return ingredientsKey; } }
protected List<Ingredient> getIngrediedientsFromKey(String keyLocation, CtExpression ctExpr) { String returnTypeExpression = (ctExpr.getType() != null) ? ctExpr.getType().getSimpleName() : "null"; List<Ingredient> ingredientsKey = (List<Ingredient>) mkp.get(keyLocation, returnTypeExpression); if (!mkp.containsKey(keyLocation, returnTypeExpression)) { ingredientsKey = new CacheList<Ingredient>(); mkp.put(keyLocation, returnTypeExpression, ingredientsKey); } return ingredientsKey; } }
@SuppressWarnings("rawtypes") @Override public String getType(Ingredient ingredient) { if (ingredient.getCode() instanceof CtExpression) { CtExpression exp = (CtExpression) ingredient.getCode(); return exp.getType().getSimpleName(); } return ingredient.getCode().getClass().getSimpleName(); }
public static List<CtExpression<Boolean>> getExpressions(CtExpression<Boolean> element) { List<CtExpression<Boolean>> expsRetrieved = new ArrayList<CtExpression<Boolean>>(); if (element instanceof CtUnaryOperator) { expsRetrieved.add(element); element = ((CtUnaryOperator) element).getOperand(); } if (element instanceof CtBinaryOperator) { expsRetrieved.add(element); CtBinaryOperator bin = (CtBinaryOperator) element; if (bin.getKind().equals(BinaryOperatorKind.AND) || bin.getKind().equals(BinaryOperatorKind.OR)) { expsRetrieved.addAll(getExpressions(bin.getLeftHandOperand())); expsRetrieved.addAll(getExpressions(bin.getRightHandOperand())); } } else { if (element instanceof CtInvocation && element.getType().getSimpleName().equals(boolean.class.getSimpleName())) { expsRetrieved.add(element); } } return expsRetrieved; }
@Override public Stream<CtMethod<?>> amplify(CtMethod<?> method, int iteration) { List<CtLocalVariable<?>> existingObjects = getExistingObjects(method); final Stream<CtMethod<?>> gen_o1 = existingObjects.stream() // must use tmp variable because javac is confused .flatMap(localVariable -> ConstructorCreator.generateAllConstructionOf(localVariable.getType()).stream()) .map(ctExpression -> { final CtMethod<?> clone = CloneHelper.cloneTestMethodForAmp(method, "_sd"); clone.getBody().insertBegin( clone.getFactory().createLocalVariable( ctExpression.getType(), "__DSPOT_gen_o" + counterGenerateNewObject++, ctExpression ) ); return clone; } ); return gen_o1; }
private MapList<CtInvocation, Ingredient> retrieveMethodHasCompatibleParameterAndReturnSameMethod( CtElement suspiciousElement) { MapList<CtInvocation, Ingredient> result = new MapList<CtInvocation, Ingredient>(); List<CtInvocation> invocations = suspiciousElement.getElements(e -> (e instanceof CtInvocation)).stream() .map(CtInvocation.class::cast).collect(Collectors.toList()); for (CtInvocation invocation : invocations) { for (Object oparameter : invocation.getArguments()) { CtExpression argument = (CtExpression) oparameter; if (SupportOperators.compareTypes(invocation.getType(), argument.getType())) { CtExpression clonedExpressionArgument = argument.clone(); MutationSupporter.clearPosition(clonedExpressionArgument); Ingredient newIngredient = new Ingredient(clonedExpressionArgument); result.add(invocation, newIngredient); } } } return result; }
@Override public List<Ingredient> transform(ModificationPoint modificationPoint, Ingredient ingredient) { ExecutionContext collectedValues = getContext(modificationPoint); CtType expectedType = null; if (modificationPoint.getCodeElement() instanceof CtExpression) { CtExpression exp = (CtExpression) modificationPoint.getCodeElement(); expectedType = exp.getType().getTypeDeclaration(); } List<CtElement> synthesizedElements = this.synthesizer.executeSynthesis(modificationPoint, modificationPoint.getCodeElement(), expectedType, modificationPoint.getContextOfModificationPoint(), collectedValues); List<Ingredient> ingredients = new ArrayList<>(); for (CtElement ctElement : synthesizedElements) { ingredients.add(new Ingredient(ctElement)); } return ingredients; }
private CtElement checkListStatements(List<?> teList) { for (Object tem : teList) { if (variables.contains(tem) && (tem instanceof CtInvocation)) { CtInvocation<?> listCand = (CtInvocation<?>) tem; boolean ok = listCand.getFactory().Type().createReference(TemplateParameter.class).isAssignableFrom(listCand.getTarget().getType()); return ok ? listCand : null; } if (tem instanceof CtVariable) { CtVariable<?> var = (CtVariable<?>) tem; String name = var.getSimpleName(); for (CtFieldReference<?> f : varArgs) { if (f.getSimpleName().equals(name)) { return f.getDeclaration(); } } } } return null; }