Java Code Examples for spoon.reflect.declaration.CtVariable#getSimpleName()

The following examples show how to use spoon.reflect.declaration.CtVariable#getSimpleName() . 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: VariableResolver.java    From coming with MIT License 6 votes vote down vote up
/**
 * Returns a map between the variables with name conflicts.
 * 
 * @param varsFromContext
 * @param varInductionCollected
 * @return
 */
public static Map<CtVariableAccess, List<CtVariable>> searchVarNameConflicts(List<CtVariable> varsFromContext,
		List<CtVariableAccess> varInductionCollected) {

	Map<CtVariableAccess, List<CtVariable>> mappingConflicts = new HashMap<>();

	for (CtVariableAccess inductionVar : varInductionCollected) {

		List<CtVariable> varsConf = new ArrayList<>();
		String nameInduction = inductionVar.getVariable().getSimpleName();

		for (CtVariable ctVariableContext : varsFromContext) {
			String nameVarContexr = ctVariableContext.getSimpleName();
			if (nameInduction.equals(nameVarContexr)) {
				varsConf.add(ctVariableContext);
			}
		}
		if (varsConf.size() > 0) {
			mappingConflicts.put(inductionVar, varsConf);
		}

	}
	return mappingConflicts;
}
 
Example 2
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a map between the variables with name conflicts.
 * 
 * @param varsFromContext
 * @param varInductionCollected
 * @return
 */
public static Map<CtVariableAccess, List<CtVariable>> searchVarNameConflicts(List<CtVariable> varsFromContext,
		List<CtVariableAccess> varInductionCollected) {

	Map<CtVariableAccess, List<CtVariable>> mappingConflicts = new HashMap<>();

	for (CtVariableAccess inductionVar : varInductionCollected) {

		List<CtVariable> varsConf = new ArrayList<>();
		String nameInduction = inductionVar.getVariable().getSimpleName();

		for (CtVariable ctVariableContext : varsFromContext) {
			String nameVarContexr = ctVariableContext.getSimpleName();
			if (nameInduction.equals(nameVarContexr)) {
				varsConf.add(ctVariableContext);
			}
		}
		if (varsConf.size() > 0) {
			mappingConflicts.put(inductionVar, varsConf);
		}

	}
	return mappingConflicts;
}
 
Example 3
Source File: ConstantAnalyzer.java    From coming with MIT License 5 votes vote down vote up
public static boolean isConstantVariable(CtVariable ctVariable) {
	
	Set<ModifierKind> modifiers = ctVariable.getModifiers();
	if (modifiers.contains(ModifierKind.FINAL)) {
		return true;
	} else {
		String simpleName = ctVariable.getSimpleName();
		if (simpleName.toUpperCase().equals(simpleName)) {
			return true;
		}
	}

	return false;
}
 
Example 4
Source File: MethodAnalyzer.java    From coming with MIT License 5 votes vote down vote up
private void analyzeVarMethodNameSimilarM12 (List<CtInvocation> invocationsaffected, List<CtVariable> scopevars, Cntx<Object> context) {
	
	 try {
		 for (CtInvocation invAffected : invocationsaffected) {
			
			boolean M12hasvarsimiplarinname = false;
			
			String methodname=invAffected.getExecutable().getSimpleName(); 
			
			for (CtVariable aVarInScope : scopevars) {
				
				String varname=aVarInScope.getSimpleName();
				varname=varname.replaceAll("[^a-zA-Z0-9]", "");
				if(varname.length()>3) {
					if(methodname.toLowerCase().endsWith(varname.toLowerCase()) || methodname.toLowerCase().equals("length") ||
							methodname.toLowerCase().contains("version") || methodname.toLowerCase().contains("clone")) {
						M12hasvarsimiplarinname = true;
						break;
					}
				}
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(invAffected),
					CodeFeatures.M12_Has_Var_Similar_In_Name, 
					M12hasvarsimiplarinname, "FEATURES_METHODS");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: VariableTransformation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public String toStringMap() {
	String r = "";
	for (String ph_name : this.varplaceholder.getPalceholders().keySet()) {

		List<CtVariableAccess> va = this.varplaceholder.getPalceholders().get(ph_name);
		CtVariableAccess va1 = va.get(0);

		CtVariable vcomb = this.combination.getCombination().get(va1.getVariable().getSimpleName());
		r += vcomb.getSimpleName() + " -->  " + ph_name;
		r += ", ";

	}

	return r;
}
 
Example 6
Source File: PatchGenerator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private List<Transformation> replaceByVars(VariablePlaceholder varplaceholder,
		ModificationPoint modificationPoint) {
	List<CtVariable> variablesInScope = modificationPoint.getContextOfModificationPoint();

	List<Transformation> transformation = new ArrayList<>();
	// Check Those vars not transformed must exist in context
	List<CtVariableAccess> concreteVars = varplaceholder.getVariablesNotModified();
	List<CtVariableAccess> outOfContext = VariableResolver.retriveVariablesOutOfContext(variablesInScope,
			concreteVars);
	if (outOfContext != null && !outOfContext.isEmpty()) {
		logger.debug("Concrete vars could not be mapped  " + outOfContext + "\nin context: " + variablesInScope);
		return transformation;

	}

	// Once we mapped all concrete variables (i.e., not transformed), and we
	// are sure they exist in
	// context.

	// Now we map placeholders with vars in scope:
	MapList<String, CtVariableAccess> placeholders = varplaceholder.getPalceholders();

	List<CtVariableAccess> placeholdersVariables = new ArrayList<>();
	for (List<CtVariableAccess> pvs : placeholders.values()) {
		placeholdersVariables.addAll(pvs);
	}

	logger.debug("Placeholder variables to map: " + placeholdersVariables);
	VarMapping mapping = VariableResolver.mapVariablesFromContext(variablesInScope, placeholdersVariables);

	// if we map all placeholder variables
	if (mapping.getNotMappedVariables().isEmpty()) {
		if (mapping.getMappedVariables().isEmpty()) {
			// nothing to transform, accept the ingredient
			logger.debug("Something is wrong: Any placeholder var was mapped ");

		} else {

			List<VarCombinationForIngredient> allCombinations = findAllVarMappingCombinationUsingRandom(
					mapping.getMappedVariables());

			if (allCombinations.size() > 0) {

				for (VarCombinationForIngredient varCombinationForIngredient : allCombinations) {
					transformation.add(new VariableTransformation(varplaceholder, placeholders,
							varCombinationForIngredient, mapping));
				}
			}
		}
	} else {

		// Placeholders without mapping: we discart it.
		logger.debug(
				String.format("Placeholders without mapping (%d/%d): %s ", mapping.getNotMappedVariables().size(),
						placeholdersVariables.size(), mapping.getNotMappedVariables().toString()));
		String varContext = "";
		for (CtVariable context : variablesInScope) {
			varContext += context.getSimpleName() + " " + context.getType().getQualifiedName() + ", ";
		}
		logger.debug("Context: " + varContext);
		for (CtVariableAccess ingredient : mapping.getNotMappedVariables()) {
			logger.debug("---out_of_context: " + ingredient.getVariable().getSimpleName() + ": "
					+ ingredient.getVariable().getType().getQualifiedName());
		}
	}
	return transformation;
}
 
Example 7
Source File: ProbabilisticTransformationStrategy.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<Ingredient> transform(ModificationPoint modificationPoint, Ingredient baseIngredient) {

	if (this.alreadyTransformed(modificationPoint, baseIngredient)) {
		return getCachedTransformations(modificationPoint, baseIngredient);
	}

	if (!this.ngramManager.initialized()) {

		logger.debug("Initializing probabilistics");
		try {
			calculateGramsProbs();
		} catch (JSAPException e) {
			logger.error(e);
			return null;
		}
	}

	List<Ingredient> transformedIngredientsResults = new ArrayList<>();

	CtCodeElement codeElementToModifyFromBase = (CtCodeElement) baseIngredient.getCode();

	if (modificationPoint.getContextOfModificationPoint().isEmpty()) {
		logger.debug("The modification point  has not any var in scope");
	}

	VarMapping mapping = VariableResolver.mapVariablesFromContext(modificationPoint.getContextOfModificationPoint(),
			codeElementToModifyFromBase);
	// if we map all variables
	if (mapping.getNotMappedVariables().isEmpty()) {
		if (mapping.getMappedVariables().isEmpty()) {
			// nothing to transform, accept the ingredient
			logger.debug("Any transf sucessful: The var Mapping is empty, we keep the ingredient");
			transformedIngredientsResults.add(baseIngredient);
		} else {// We have mappings between variables
			logger.debug("Ingredient before transformation: " + baseIngredient.getCode() + " mined from "
					+ baseIngredient.getCode().getParent(CtType.class).getQualifiedName());

			List<VarCombinationForIngredient> allCombinations = findAllVarMappingCombinationUsingProbab(
					mapping.getMappedVariables(), modificationPoint, baseIngredient);

			if (allCombinations.size() > 0) {

				for (VarCombinationForIngredient varCombinationForIngredient : allCombinations) {

					DynamicIngredient ding = new DynamicIngredient(varCombinationForIngredient, mapping,
							codeElementToModifyFromBase);
					transformedIngredientsResults.add(ding);
				}
			}
		}
	} else {
		logger.debug("Any transformation was sucessful: Vars not mapped: " + mapping.getNotMappedVariables());
		String varContext = "";
		for (CtVariable context : modificationPoint.getContextOfModificationPoint()) {
			varContext += context.getSimpleName() + " " + context.getType().getQualifiedName() + ", ";
		}
		logger.debug("context " + varContext);
		for (CtVariableAccess ingredient : mapping.getNotMappedVariables()) {
			logger.debug("---out_of_context: " + ingredient.getVariable().getSimpleName() + ": "
					+ ingredient.getVariable().getType().getQualifiedName());
		}
	}

	this.storingIngredients(modificationPoint, baseIngredient, transformedIngredientsResults);

	return transformedIngredientsResults;
}
 
Example 8
Source File: RandomTransformationStrategy.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<Ingredient> transform(ModificationPoint modificationPoint, Ingredient baseIngredient) {

	if (this.alreadyTransformed(modificationPoint, baseIngredient)) {
		return getCachedTransformations(modificationPoint, baseIngredient);
	}

	List<Ingredient> result = new ArrayList<>();

	CtCodeElement codeElementToModifyFromBase = (CtCodeElement) baseIngredient.getCode();

	if (modificationPoint.getContextOfModificationPoint().isEmpty()) {
		logger.debug("The modification point  has not any var in scope");
	}

	VarMapping mapping = VariableResolver.mapVariablesFromContext(modificationPoint.getContextOfModificationPoint(),
			codeElementToModifyFromBase);
	// if we map all variables
	if (mapping.getNotMappedVariables().isEmpty()) {
		if (mapping.getMappedVariables().isEmpty()) {
			// nothing to transform, accept the ingredient
			logger.debug("Any transf sucessful: The var Mapping is empty, we keep the ingredient");
			result.add(new Ingredient(codeElementToModifyFromBase));

		} else {// We have mappings between variables
			logger.debug("Ingredient before transformation: " + baseIngredient);

			List<VarCombinationForIngredient> allCombinations = findAllVarMappingCombinationUsingRandom(
					mapping.getMappedVariables(), modificationPoint);

			if (allCombinations.size() > 0) {

				for (VarCombinationForIngredient varCombinationForIngredient : allCombinations) {

					DynamicIngredient ding = new DynamicIngredient(varCombinationForIngredient, mapping,
							codeElementToModifyFromBase);
					result.add(ding);
				}
			}
		}
	} else {
		logger.debug("Any transformation was sucessful: Vars not mapped: " + mapping.getNotMappedVariables());
		String varContext = "";
		for (CtVariable context : modificationPoint.getContextOfModificationPoint()) {
			varContext += context.getSimpleName() + " " + context.getType().getQualifiedName() + ", ";
		}
		logger.debug("context " + varContext);
		for (CtVariableAccess ingredient : mapping.getNotMappedVariables()) {
			logger.debug("---out_of_context: " + ingredient.getVariable().getSimpleName() + ": "
					+ ingredient.getVariable().getType().getQualifiedName());
		}
	}

	this.storingIngredients(modificationPoint, baseIngredient, result);

	return result;
}