Java Code Examples for spoon.reflect.declaration.CtElement#toString()

The following examples show how to use spoon.reflect.declaration.CtElement#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnhancedFeatureVisitor.java    From coming with MIT License 5 votes vote down vote up
private void putValueFeature(CtElement v, AtomicFeature af) {
        if (v == null) {
            if (!resMap.containsKey("@")) {
                resMap.put("@", new HashSet<>());
            }
            resMap.get("@").add(af);
        }
        else {
//            CtExpression e = stripParenAndCast(v);
//            std::string tmp = stmtToString(*ast, e);
            String tmp = v.toString();
            // i can not know why there is one return here
//            if (v instanceof CtAssignment) {
//                return;
//            }
            // CtInvocation or CtExecutable todo check
//            if (v.getElements(new TypeFilter<>(CtInvocation.class)).size() > 0 && !isAbstractStub(v)) {
//                return;
//            }
            if (!resMap.containsKey(tmp)) {
                resMap.put(tmp, new HashSet<>());
            }
            resMap.get(tmp).add(af);
            if (!valueExprInfo.containsKey(tmp)) {
                valueExprInfo.put(tmp, v);
            }
        }
    }
 
Example 2
Source File: OriginalFeatureVisitor.java    From coming with MIT License 5 votes vote down vote up
private void putValueFeature(CtElement v, AtomicFeature af) {
        if (v == null) {
            if (!resMap.containsKey("@")) {
                resMap.put("@", new HashSet<>());
            }
            resMap.get("@").add(af);
        }
        else {
//            CtExpression e = stripParenAndCast(v);
//            std::string tmp = stmtToString(*ast, e);
            String tmp = v.toString();
            // i can not know why there is one return here
//            if (v instanceof CtAssignment) {
//                return;
//            }
            // CtInvocation or CtExecutable todo check
//            if (v.getElements(new TypeFilter<>(CtInvocation.class)).size() > 0 && !isAbstractStub(v)) {
//                return;
//            }
            if (!resMap.containsKey(tmp)) {
                resMap.put(tmp, new HashSet<>());
            }
            resMap.get(tmp).add(af);
            if (!valueExprInfo.containsKey(tmp)) {
                valueExprInfo.put(tmp, v);
            }
        }
    }
 
Example 3
Source File: S4ROFeatureVisitor.java    From coming with MIT License 5 votes vote down vote up
private void putValueFeature(CtElement v, AtomicFeature af) {
        if (v == null) {
            if (!resMap.containsKey("@")) {
                resMap.put("@", new HashSet<>());
            }
            resMap.get("@").add(af);
        }
        else {
//            CtExpression e = stripParenAndCast(v);
//            std::string tmp = stmtToString(*ast, e);
            String tmp = v.toString();
            // i can not know why there is one return here
//            if (v instanceof CtAssignment) {
//                return;
//            }
            // CtInvocation or CtExecutable todo check
//            if (v.getElements(new TypeFilter<>(CtInvocation.class)).size() > 0 && !isAbstractStub(v)) {
//                return;
//            }
            if (!resMap.containsKey(tmp)) {
                resMap.put(tmp, new HashSet<>());
            }
            resMap.get(tmp).add(af);
            if (!valueExprInfo.containsKey(tmp)) {
                valueExprInfo.put(tmp, v);
            }
        }
    }
 
Example 4
Source File: ExtendedFeatureVisitor.java    From coming with MIT License 5 votes vote down vote up
private void putValueFeature(CtElement v, AtomicFeature af) {
        if (v == null) {
            if (!resMap.containsKey("@")) {
                resMap.put("@", new HashSet<>());
            }
            resMap.get("@").add(af);
        }
        else {
//            CtExpression e = stripParenAndCast(v);
//            std::string tmp = stmtToString(*ast, e);
            String tmp = v.toString();
            // i can not know why there is one return here
//            if (v instanceof CtAssignment) {
//                return;
//            }
            // CtInvocation or CtExecutable todo check
//            if (v.getElements(new TypeFilter<>(CtInvocation.class)).size() > 0 && !isAbstractStub(v)) {
//                return;
//            }
            if (!resMap.containsKey(tmp)) {
                resMap.put(tmp, new HashSet<>());
            }
            resMap.get(tmp).add(af);
            if (!valueExprInfo.containsKey(tmp)) {
                valueExprInfo.put(tmp, v);
            }
        }
    }
 
Example 5
Source File: RandomSelectionTransformedIngredientStrategy.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if the ingredient was already used
 * 
 * @param id       program instance id.
 * @param fix
 * @param location
 * @return
 */
protected boolean alreadySelected(ModificationPoint gen, CtElement fixElement, AstorOperator operator) {
	// we add the instance identifier to the patch.
	String lockey = getKey(gen, operator);
	String fix = "";
	try {
		fix = fixElement.toString();
	} catch (Exception e) {
		log.error("to string fails");
	}
	List<String> prev = appliedCache.get(lockey);
	// The element does not have any mutation applied
	if (prev == null) {
		prev = new ArrayList<String>();
		prev.add(fix);
		appliedCache.put(lockey, prev);
		log.debug(
				"\nChache: New Element with new Key: " + StringUtil.trunc(fix) + " in " + StringUtil.trunc(lockey));
		return false;
	} else {
		// The element has mutation applied
		if (prev.contains(fix)) {
			log.debug("\nChache: Already stored: " + StringUtil.trunc(fix) + " in " + (lockey));
			return true;
		} else {
			prev.add(fix);
			log.debug("\nChache: New Element with existing Key: " + StringUtil.trunc(fix) + " in "
					+ StringUtil.trunc(lockey));
			return false;
		}
	}
}
 
Example 6
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
protected IngredientPoolScope determineIngredientScope(CtElement modificationpoint, CtElement selectedFix,
		List<?> ingredients) {
	// This is the original ingredient scope
	IngredientPoolScope orig = VariableResolver.determineIngredientScope(modificationpoint, selectedFix);

	String fixStr = selectedFix.toString();

	// Now, we search for equivalent fixes with different scopes
	for (Object ing : ingredients) {
		try {
			ing.toString();
		} catch (Exception e) {
			// if we cannot print the ingredient, we return
			logger.error(e.toString());
			continue;
		}
		// if it's the same fix
		if (ing.toString().equals(fixStr)) {
			IngredientPoolScope n = VariableResolver.determineIngredientScope(modificationpoint, (CtElement) ing);
			// if the scope of the ingredient ing is narrower than the fix,
			// we keep it.
			if (n.ordinal() < orig.ordinal()) {
				orig = n;
				// if it's local, we return
				if (IngredientPoolScope.values()[0].equals(orig))
					return orig;
			}

		}
	}
	return orig;
}
 
Example 7
Source File: ASTInfoResolver.java    From coming with MIT License 4 votes vote down vote up
public static String getCleanedName(CtElement element) {
	String elementName = element.toString();
	return getCleanedName(elementName);
}
 
Example 8
Source File: DetectorChangePatternInstanceEngine.java    From coming with MIT License 4 votes vote down vote up
/**
     * Match the element affected by an operation from the diff and the elements in
     * the pattern specification
     *
     * @param affectedOperation
     * @param affectedEntity
     * @return
     */
    private List<MatchingEntity> matchElements(Operation affectedOperation, PatternEntity affectedEntity) {

        List<MatchingEntity> matching = new ArrayList<>();

        int parentLevel = 1;
        PatternEntity parentEntity = affectedEntity;
        // Let's get the parent of the affected
        CtElement currentNodeFromAction = null;
        boolean matchnewvalue = false;

        // Search the node to select according to the type of operation and the pattern
        if (affectedOperation.getDstNode() != null && affectedEntity.getNewValue() != null) {
            currentNodeFromAction = affectedOperation.getDstNode();
            matchnewvalue = true;
        } else if (affectedOperation instanceof UpdateOperation && (affectedEntity.getOldValue() != null)) {
            currentNodeFromAction = affectedOperation.getSrcNode();
            matchnewvalue = false;
        } else {
            matchnewvalue = true;
            currentNodeFromAction = affectedOperation.getNode();
        }

        int i_levels = 1;
        // Scale the parent hierarchy and check types.
        while (currentNodeFromAction != null && i_levels <= parentLevel) {
            String typeOfNode = EntityTypesInfoResolver.getNodeLabelFromCtElement(currentNodeFromAction);
            String valueOfNode = currentNodeFromAction.toString();
            String roleInParent = (currentNodeFromAction.getRoleInParent() != null)
                    ? currentNodeFromAction.getRoleInParent().toString().toLowerCase()
                    : "";

            String patternEntityValue = (matchnewvalue) ? parentEntity.getNewValue() : parentEntity.getOldValue();
            if ( // type of element
                    ("*".equals(parentEntity.getEntityType())
//                            || (typeOfNode != null && typeOfNode.equals(parentEntity.getEntityType())))
                            || (typeOfNode != null &&
                                    EntityTypesInfoResolver.getInstance().isAChildOf(typeOfNode, parentEntity.getEntityType())))
                            ///
                            &&
                            // value of element
                            ("*".equals(patternEntityValue) || (valueOfNode != null && valueOfNode.equals(patternEntityValue)))
                            //
                            &&
                            // role
                            ("*".equals(parentEntity.getRoleInParent()) || (roleInParent != null
                                    && roleInParent.equals(parentEntity.getRoleInParent().toLowerCase())))) {
                MatchingEntity match = new MatchingEntity(currentNodeFromAction, parentEntity);
                matching.add(match);

                ParentPatternEntity parentEntityFromPattern = parentEntity.getParentPatternEntity();
                if (parentEntityFromPattern == null) {
                    return matching;
                }
                i_levels = 1;
                parentLevel = parentEntityFromPattern.getParentLevel();
                parentEntity = parentEntityFromPattern.getParent();

            } else {
                // Not match
                i_levels++;
            }
            currentNodeFromAction = currentNodeFromAction.getParent();
        }
        // Not all matched
        return null;

    }