@SuppressWarnings("unchecked") protected <T> T cloneIfNeeded(T value) { if (value instanceof CtElement) { return (T) ((CtElement) value).clone(); } return value; } }
@Override public void scanCtElement(CtElement e) { if (child instanceof CtAnnotation && this.jdtTreeBuilder.getContextBuilder().annotationValueName.isEmpty()) { // we check if the current element can have the annotation attached CtAnnotatedElementType annotatedElementType = CtAnnotation.getAnnotatedElementTypeForCtElement(e); annotatedElementType = (e instanceof CtTypeParameter || e instanceof CtTypeParameterReference) ? CtAnnotatedElementType.TYPE_USE : annotatedElementType; // in case of noclasspath, we cannot be 100% sure, so we guess it must be attached... if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() || (annotatedElementType != null && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, annotatedElementType))) { e.addAnnotation((CtAnnotation<?>) child); } // in this case the annotation should be (also) attached to the type if (e instanceof CtTypedElement && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, CtAnnotatedElementType.TYPE_USE)) { List<CtAnnotation> annotations = new ArrayList<>(); if (!annotationsMap.containsKey(e)) { annotationsMap.put((CtTypedElement<?>) e, annotations); } else { annotations = annotationsMap.get(e); } annotations.add((CtAnnotation) child.clone()); annotationsMap.put((CtTypedElement<?>) e, annotations); } } }
@Override @SuppressWarnings("unchecked") public <R extends CtElement> R evaluate(R element) { if (element == null) { return null; } element.accept(this); if (result != null) { CtElement r = result; //reset result, to not influence another evaluation. result = null; if (element.isParentInitialized()) { r.setParent(element.getParent()); } return (R) r; } // otherwise nothing has been changed return (R) element.clone(); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<OperatorInstance> createOperatorInstances(ModificationPoint modificationPoint, Ingredient ingredient, IngredientTransformationStrategy transformationStrategy) { List<OperatorInstance> instances = new ArrayList<>(); List<Ingredient> ingredientsTransformed = transformationStrategy.transform(modificationPoint, ingredient); for (Ingredient iIngredient : ingredientsTransformed) { CtExpression condition = (CtExpression) iIngredient.getCode(); CtIf precondition = MutationSupporter.factory.createIf(); precondition.setCondition(condition); precondition.setThenStatement((CtStatement) modificationPoint.getCodeElement().clone()); OperatorInstance operatorInstance = new StatementOperatorInstance(modificationPoint, this, precondition); instances.add(operatorInstance); } return instances; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<OperatorInstance> createOperatorInstances(ModificationPoint modificationPoint) { List<OperatorInstance> instances = new ArrayList<>(); List<CtVariableAccess> varAccess = VariableResolver.collectVariableAccess(modificationPoint.getCodeElement()); for (CtVariableAccess iVariableAccess : varAccess) { if (!iVariableAccess.getType().isPrimitive()) { CtIf precodition = MutationSupporter.factory.createIf(); CtExpression ifcondition = MutationSupporter.factory.createBinaryOperator(iVariableAccess, MutationSupporter.factory.createCodeSnippetExpression("null").compile(), BinaryOperatorKind.NE); precodition.setCondition(ifcondition); precodition.setThenStatement((CtStatement) modificationPoint.getCodeElement().clone()); OperatorInstance operatorInstance = new StatementOperatorInstance(modificationPoint, this, precodition); instances.add(operatorInstance); } } return instances; };