Java Code Examples for spoon.reflect.code.CtVariableAccess

The following examples show how to use spoon.reflect.code.CtVariableAccess. These examples are extracted from open source projects. 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 Project: coming   Source File: VariableAnalyzer.java    License: MIT License 6 votes vote down vote up
private void analyzeV19_VarWithSpecialName (List<CtVariableAccess> varsAffected, Cntx<Object> context) {
	
	 try {
		  for (CtVariableAccess aVarAffected : varsAffected) {
			
			boolean V19WithSpecialName = false;
			String varname= aVarAffected.getVariable().getSimpleName().toLowerCase(); 
			
			if(varname.endsWith("length") || varname.endsWith("size") || varname.endsWith("count") || varname.endsWith("value")
					|| varname.endsWith("key") || varname.equals("class"))
				V19WithSpecialName =true;
			
			if(!V19WithSpecialName && aVarAffected.getType()!=null && aVarAffected.getType().getSimpleName().toLowerCase().endsWith("exception"))
				V19WithSpecialName =true;
			
		
			writeGroupedInfo(context, adjustIdentifyInJson(aVarAffected),
					CodeFeatures.V19_With_Special_Name, 
					V19WithSpecialName, "FEATURES_VARS");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 2
Source Project: coming   Source File: VariableAnalyzer.java    License: MIT License 6 votes vote down vote up
/**
 * For each involved variable, is it constant?–can assume variables whose
 * identifier names are majorly capital letters are constant variables
 * 
 * @param varsAffected
 * @param element
 * @param context
 */
private void analyzeV3_AffectedHasConstant(List<CtVariableAccess> varsAffected, CtElement element,
		Cntx<Object> context) {
	try {
		
		for (CtVariableAccess aVarAffected : varsAffected) {
			boolean currentIsConstant = false;
			if (aVarAffected.getVariable() instanceof CtFieldReference &&
			// Check if it's uppercase
					aVarAffected.getVariable().getSimpleName().toUpperCase()
							.equals(aVarAffected.getVariable().getSimpleName())) {
				currentIsConstant = true;
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(aVarAffected), 
					CodeFeatures.V3_HAS_CONSTANT,
					currentIsConstant, "FEATURES_VARS");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 3
Source Project: coming   Source File: VariableAnalyzer.java    License: MIT License 6 votes vote down vote up
private void analyzV10_AffectedWithCompatibleTypes(List<CtVariableAccess> varsAffected, List<CtVariable> varsInScope,
		CtElement element, Cntx<Object> context) {
	try {
		
		for (CtVariableAccess aVariableAccessInStatement : varsAffected) {
			boolean currentHasSimType = false;
			for (CtVariable aVariableInScope : varsInScope) {
				if (!aVariableInScope.getSimpleName().equals(aVariableAccessInStatement.getVariable().getSimpleName())) {
					if (compareTypes(aVariableInScope.getType(), aVariableAccessInStatement.getType())) {
						currentHasSimType=true;
						break;
					}
				}
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(aVariableAccessInStatement),
					CodeFeatures.V10_VAR_TYPE_Similar_VAR, currentHasSimType, "FEATURES_VARS");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 4
Source Project: coming   Source File: AbstractCodeAnalyzer.java    License: MIT License 6 votes vote down vote up
public boolean isElementBeforeVariable(CtVariableAccess variableAffected, CtElement element) {

		try {
			CtStatement stst = (element instanceof CtStatement) ? (CtStatement) element
					: element.getParent(CtStatement.class);

			CtStatement target = (variableAffected instanceof CtStatement) ? (CtStatement) variableAffected
					: variableAffected.getParent(CtStatement.class);

			return target.getPosition() != null && getParentNotBlock(stst) != null
					&& target.getPosition().getSourceStart() > stst.getPosition().getSourceStart();
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return false;

	}
 
Example 5
Source Project: coming   Source File: LogicalExpressionAnalyzer.java    License: MIT License 6 votes vote down vote up
/**
 * Check if a method declaration has a parameter compatible with that one from
 * the var affected
 * 
 * @param allMethods
 * @param varAffected
 * @return
 */
public CtMethod checkBooleanMethodDeclarationWithTypeInParameter(List allMethods, CtVariableAccess varAffected) {
	
	for (Object omethod : allMethods) {

		if (!(omethod instanceof CtMethod))
			continue;

		CtMethod anotherMethodInBuggyClass = (CtMethod) omethod;

		for (Object oparameter : anotherMethodInBuggyClass.getParameters()) {
			CtParameter parameter = (CtParameter) oparameter;

			if (compareTypes(varAffected.getType(), parameter.getType())) {
				if (anotherMethodInBuggyClass.getType().unbox().toString().equals("boolean")) {

					return anotherMethodInBuggyClass;
				}
			}
		}
	}
	return null;
}
 
Example 6
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * It retrieves all variables access which declarations are inside the
 * ingredient.
 * 
 * @param ingredientRootElement
 * @param varAccessCollected
 * @return
 */
public static List<CtVariableAccess> collectInductionVariableAccess(CtElement ingredientRootElement,
		List<CtVariableAccess> varAccessCollected) {

	List<CtVariableAccess> induction = new ArrayList<>();

	for (CtVariableAccess ctVariableAccess : varAccessCollected) {

		CtVariableReference varref = ctVariableAccess.getVariable();

		// We are interesting in induction vars, they are modeled as
		// LocalVariables
		if (!(varref instanceof CtLocalVariableReference))
			continue;

		CtVariable var = varref.getDeclaration();

		boolean insideIngredient = checkParent(var, ingredientRootElement);
		if (insideIngredient)
			induction.add(ctVariableAccess);

	}
	return induction;
}
 
Example 7
Source Project: coming   Source File: VariableResolver.java    License: MIT License 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableReadIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableRead(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableRead(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableRead(((CtFor) element).getExpression());
		}

		return collectVariableRead(element);

	}
 
Example 8
Source Project: coming   Source File: VariableResolver.java    License: MIT License 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableAccessIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableAccess(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableAccess(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableAccess(((CtFor) element).getExpression());
		}

		return collectVariableAccess(element);

	}
 
Example 9
Source Project: coming   Source File: VariableResolver.java    License: MIT License 6 votes vote down vote up
public static List<CtVariableAccess> checkMapping(Map<CtVariableAccess, List<CtVariable>> matched) {
	List<CtVariableAccess> notMapped = new ArrayList<>();

	if (matched == null)
		return notMapped;

	// Now, we analyze if all access were matched
	for (CtVariableAccess ctVariableAccess : matched.keySet()) {
		List<CtVariable> mapped = matched.get(ctVariableAccess);
		if (mapped.isEmpty()) {
			// One var access was not mapped
			// return false;
			notMapped.add(ctVariableAccess);
		}
	}
	// All VarAccess were mapped
	// return true;
	return notMapped;
}
 
Example 10
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableReadIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableRead(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableRead(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableRead(((CtFor) element).getExpression());
		}

		return collectVariableRead(element);

	}
 
Example 11
Source Project: coming   Source File: VariableResolver.java    License: MIT License 6 votes vote down vote up
/**
 * It retrieves all variables access which declarations are inside the
 * ingredient.
 * 
 * @param ingredientRootElement
 * @param varAccessCollected
 * @return
 */
public static List<CtVariableAccess> collectInductionVariableAccess(CtElement ingredientRootElement,
		List<CtVariableAccess> varAccessCollected) {

	List<CtVariableAccess> induction = new ArrayList<>();

	for (CtVariableAccess ctVariableAccess : varAccessCollected) {

		CtVariableReference varref = ctVariableAccess.getVariable();

		// We are interesting in induction vars, they are modeled as
		// LocalVariables
		if (!(varref instanceof CtLocalVariableReference))
			continue;

		CtVariable var = varref.getDeclaration();

		boolean insideIngredient = checkParent(var, ingredientRootElement);
		if (insideIngredient)
			induction.add(ctVariableAccess);

	}
	return induction;
}
 
Example 12
Source Project: astor   Source File: VariablePlaceholder.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply() {
	olderNameOfVariable.clear();
	for (String placeholder : palceholdersToVariables.keySet()) {

		List<CtVariableAccess> variables = palceholdersToVariables.get(placeholder);
		for (CtVariableAccess variableUnderAnalysis : variables) {
			this.olderNameOfVariable.put(variableUnderAnalysis,
					variableUnderAnalysis.getVariable().getSimpleName());
			variableUnderAnalysis.getVariable().setSimpleName(placeholder);
			// workaround: Problems with var Shadowing
			variableUnderAnalysis.getFactory().getEnvironment().setNoClasspath(true);
			if (variableUnderAnalysis instanceof CtFieldAccess) {
				CtFieldAccess fieldAccess = (CtFieldAccess) variableUnderAnalysis;
				fieldAccess.getVariable().setDeclaringType(null);

			}
		}
	}
}
 
Example 13
protected List<Ingredient> computeIngredientsFromMInvokToReplace(ModificationPoint modificationPoint,
		CtInvocation invocationToReplace) {

	List<Ingredient> ingredients = new ArrayList<>();
	List<CtVariable> varsInContext = modificationPoint.getContextOfModificationPoint();

	for (CtVariable iVarInContext : varsInContext) {

		if (!SupportOperators.checkIsSubtype(iVarInContext.getType(), invocationToReplace.getType())) {
			continue;
		}

		CtVariableAccess iVarAccessFromContext = MutationSupporter.getFactory()
				.createVariableRead(iVarInContext.getReference(), false);
		Ingredient ingredient = new Ingredient(iVarAccessFromContext);
		// we use this property to indicate the old variable to replace
		ingredient.setDerivedFrom(invocationToReplace);
		ingredients.add(ingredient);

	}

	return ingredients;
}
 
Example 14
Source Project: astor   Source File: VariableResolver.java    License: 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 15
Source Project: astor   Source File: WrapwithIfNullCheck.java    License: GNU General Public License v2.0 6 votes vote down vote up
private List<Ingredient> computeIngredientsNullCheck(ModificationPoint modificationPoint,
		List<CtVariableAccess> varAccessInModificationPoints) {

	List<Ingredient> ingredients = new ArrayList();

	for (CtVariableAccess iVariableAccess : varAccessInModificationPoints) {

		// Let's check the type, if primitive discard it
		if (iVariableAccess.getVariable() != null && iVariableAccess.getVariable().getType() != null
				&& iVariableAccess.getVariable().getType().isPrimitive())
			continue;

		CtVariableAccess iVariableAccessC = iVariableAccess.clone();
		MutationSupporter.clearPosition(iVariableAccessC);

		CtBinaryOperator<Boolean> binaryOp = new CtBinaryOperatorImpl<>();
		binaryOp.setLeftHandOperand(iVariableAccessC);
		binaryOp.setRightHandOperand(MutationSupporter.getFactory().createCodeSnippetExpression("null"));
		binaryOp.setKind(BinaryOperatorKind.NE);

		ingredients.add(new Ingredient(binaryOp));

	}

	return ingredients;
}
 
Example 16
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static List<CtVariableAccess> collectVariableAccessIgnoringBlocks(CtElement element) {

		if (element instanceof CtIf) {
			return collectVariableAccess(((CtIf) element).getCondition());
		}
		if (element instanceof CtWhile) {
			return collectVariableAccess(((CtWhile) element).getLoopingExpression());
		}

		if (element instanceof CtFor) {
			return collectVariableAccess(((CtFor) element).getExpression());
		}

		return collectVariableAccess(element);

	}
 
Example 17
/**
 * Add all variables from the expression and candidates in a list
 * 
 * @param exptochange
 * @param candidates
 * @return
 */
private List<CtVariableAccess> collectAllVars(CtExpression exptochange, List<Ingredient> candidates) {
	List<CtVariableAccess> varAccess = VariableResolver.collectVariableAccess(exptochange);

	for (Ingredient candidateIngr : candidates) {
		CtElement candidate = candidateIngr.getCode();
		List<CtVariableAccess> varAccessCandidate = VariableResolver.collectVariableAccess(candidate);
		for (CtVariableAccess varX : varAccessCandidate) {
			if (!varAccess.contains(varX)) {
				varAccess.add(varX);
			}
		}
	}

	return varAccess;
}
 
Example 18
Source Project: astor   Source File: GramProcessor.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain all n-gram of the element give as parameter according to the
 * ingredient processor also passed as argument. The results (n-grams) are
 * stored in the ngram stored passed as argument
 * 
 * @param codeToProcess
 * @param ngramStore
 * @param ingp
 * @return
 */
private int getNGramsFromCodeElements(CtElement codeToProcess, NGrams ngramStore, CodeParserLauncher ingp) {

	int allElements = 0;

	List<CtCodeElement> elementsFromSpace = ingp.createFixSpace(codeToProcess, false);
	for (CtCodeElement ctCodeElement : elementsFromSpace) {

		List<CtVariableAccess> vars = VariableResolver.collectVariableAccess(ctCodeElement);
		if (vars.isEmpty())
			continue;

		if (vars.size() > ConfigurationProperties.getPropertyDouble("maxnumvariablesperingredient")) {
			logger.debug("Attention, Large Ingredient discarted: \n" + ctCodeElement.getShortRepresentation());
			continue;
		}

		cleanParenthesis(vars);
		sortVarsByNames(vars);

		for (int i = vars.size(); i >= 1; i--) {
			List allpermutations = getPermutationsOfVarNames(vars, i);
			ngramStore.add(allpermutations, i);
		}
		
		//logger.debug("\n: "+ctCodeElement+ " ng" + ngramStore);

		allElements++;
	}
	return allElements;
}
 
Example 19
Source Project: coming   Source File: VariableAnalyzer.java    License: MIT License 5 votes vote down vote up
/**
 * For each involved variable, whether has any other variables in scope that are
 * similar in identifier name and type compatible
 * 
 * @param varsAffected
 * @param varsInScope
 * @param element
 * @param context
 */
private void analyzeV2_AffectedDistanceVarName(List<CtVariableAccess> varsAffected, List<CtVariable> varsInScope,
		CtElement element, Cntx<Object> context) {
	try {

		for (CtVariableAccess aVarAffected : varsAffected) {

			boolean v2VarSimilarNameCompatibleType = false;
			boolean v2VarSimilarName = false;

			for (CtVariable aVarInScope : varsInScope) {
				if (!aVarInScope.getSimpleName().equals(aVarAffected.getVariable().getSimpleName())) {
					int dist = StringDistance.calculate(aVarInScope.getSimpleName(),
							aVarAffected.getVariable().getSimpleName());
					if ((dist > 0 && dist < 3) || nameStartEndWithOther (aVarInScope.getSimpleName(), 
							aVarAffected.getVariable().getSimpleName())) {
						v2VarSimilarName=true;
						if (compareTypes(aVarAffected.getType(), aVarInScope.getType())) {
							v2VarSimilarNameCompatibleType = true;
							break;
						}
					}
				}
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(aVarAffected), 
					CodeFeatures.V2_HAS_VAR_SIM_NAME,
					v2VarSimilarName, "FEATURES_VARS");
			
			writeGroupedInfo(context, adjustIdentifyInJson(aVarAffected), 
					CodeFeatures.V2_HAS_VAR_SIM_NAME_COMP_TYPE,
					v2VarSimilarNameCompatibleType, "FEATURES_VARS");
			
		}
		
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 20
@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);

}
 
Example 21
Source Project: coming   Source File: VariableAnalyzer.java    License: MIT License 5 votes vote down vote up
private int[] argumentDiff(List<CtElement> argumentsoriginal, List<CtElement> argumentsother, CtVariableAccess varaccess) {
	
	int numberdiffargument =0;
	int numberdiffvarreplacebyvar =0;
	int numberdiffvarreplacebymethod =0;
	
	for(int index=0; index<argumentsoriginal.size(); index++) {
		
		CtElement original = argumentsoriginal.get(index);
		CtElement other = argumentsother.get(index);
		
		if(original.equals(other) || original.toString().equals(other.toString())) {
			// same
		} else {
			numberdiffargument+=1;
			if(original instanceof CtVariableAccess && original.equals(varaccess)) {
				if(other instanceof CtVariableAccess)
					numberdiffvarreplacebyvar+=1;
				else if(other instanceof CtInvocation || other instanceof CtConstructorCall)
					numberdiffvarreplacebymethod+=1;
				else {
					// do nothing
				}
			}
		}
	}

	int diffarray[]=new int[3];
	diffarray[0]=numberdiffargument;
	diffarray[1]=numberdiffvarreplacebyvar;
	diffarray[2]=numberdiffvarreplacebymethod;

       return diffarray;
}
 
Example 22
Source Project: astor   Source File: VarMappingTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testVarsOutOfScope() throws Exception {

	List<CtVariable> varContextC1 = VariableResolver.searchVariablesInScope(c1);
	List<CtVariable> varContextC3 = VariableResolver.searchVariablesInScope(c3);//

	List<CtVariableAccess> variablesOutOfScope = VariableResolver.retriveVariablesOutOfContext(varContextC1,
			ingredientCtElementC7);
	assertNotNull(variablesOutOfScope);
	assertEquals(1, variablesOutOfScope.size());
	log.debug("Out scope: " + variablesOutOfScope);
	assertEquals("fmin", variablesOutOfScope.get(0).getVariable().getSimpleName());

}
 
Example 23
Source Project: coming   Source File: ExpressionAnalyzer.java    License: MIT License 5 votes vote down vote up
private boolean[] analyzeExpression(CtExpression expressionToStudy, Cntx<Object> context, List allMethods,
		List<CtInvocation> invocationsFromClass, CtClass parentclass) {
	
	try {
		
		if(expressionToStudy instanceof CtVariableAccess)
			return null;
		
		boolean hasExpressionReturnParmeterCompatible = false;
		boolean hasExpressionParmeterCompatible = false;
		
		if(checkMethodDeclarationWithParameterReturnCompatibleType(allMethods, expressionToStudy.getType()) != null 
			|| checkInvocationWithParameterReturnCompatibleType(invocationsFromClass,
						expressionToStudy.getType(), parentclass) != null) {
			hasExpressionReturnParmeterCompatible = true;
		}
		
		if (checkMethodDeclarationWithParemetrCompatibleType(allMethods, expressionToStudy.getType()) != null
				|| checkInvocationWithParemetrCompatibleType (invocationsFromClass,
						expressionToStudy.getType()) != null) {
			hasExpressionParmeterCompatible = true;
		}

		boolean[] expressionTypeFeatures= new boolean[2];
		
		expressionTypeFeatures[0] = hasExpressionReturnParmeterCompatible;
		
		expressionTypeFeatures[1] = hasExpressionParmeterCompatible;

		return expressionTypeFeatures;

	} catch (Throwable e) {
		e.printStackTrace();
	}
	
	return null;
}
 
Example 24
Source Project: coming   Source File: LogicalExpressionAnalyzer.java    License: MIT License 5 votes vote down vote up
/**
 * // For any variable involved in a logical expression,whether exist methods //
 * (method declaration or method call) in scope (that is in the same faulty //
 * class // since we do not assume full program) that take variable whose type
 * is same // with vas one of its parameters and return boolean
 * 
 * @param logicalExperssions
 * @param context
 * @param allMethods
 * @param logicalExperssions
 * @param invocationsFromClass
 * @param parentclass
 */
private void analyzeLE2_AffectedVariablesInMethod(List<CtExpression> logicalExperssions,
		Cntx<Object> context, List allMethods, List<CtInvocation> invocationsFromClass, CtClass parentclass) {
	
	try {

		for (int indexlogical=0; indexlogical<logicalExperssions.size(); indexlogical++) {
			
               CtExpression logicalexpression = logicalExperssions.get(indexlogical);
			
			List<CtVariableAccess> varsAffected = VariableResolver.collectVariableAccess(logicalexpression, false);
			
			boolean hasAnyLES2paramCompatibleWithBooleanReturn = false;

			for (CtVariableAccess varAffected : varsAffected) {

				if (// First, Let's analyze the method declaration
				checkBooleanMethodDeclarationWithTypeInParameter(allMethods, varAffected) != null
						// Second, let's inspect invocations
						|| checkBooleanInvocationWithParameterReturn(invocationsFromClass, varAffected, parentclass) != null) {
					hasAnyLES2paramCompatibleWithBooleanReturn = true;
				}

                   if(hasAnyLES2paramCompatibleWithBooleanReturn)
                   	break;
			}

			writeGroupedInfo(context, "logical_expression_"+Integer.toString(indexlogical)+"_"+logicalexpression, 
					CodeFeatures.LE2_IS_BOOLEAN_METHOD_PARAM_TYPE_VAR,
					hasAnyLES2paramCompatibleWithBooleanReturn, "FEATURES_LOGICAL_EXPRESSION");
		}

	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 25
Source Project: coming   Source File: MethodAnalyzer.java    License: MIT License 5 votes vote down vote up
private int[] argumentDiffMethod(List<CtElement> argumentsoriginal, List<CtElement> argumentsother, 
	   CtInvocation invocationaccess) {
	
	int numberdiffargument =0;
	int numberdiffmethodreplacebyvar =0;
	int numberdiffmethodreplacebymethod =0;
	
	for(int index=0; index<argumentsoriginal.size(); index++) {
		
		CtElement original = argumentsoriginal.get(index);
		CtElement other = argumentsother.get(index);
		
		if(original.equals(other) || original.toString().equals(other.toString())) {
			// same
		} else {
			numberdiffargument+=1;
			if(original instanceof CtInvocation && original.equals(invocationaccess)) {
				if(other instanceof CtVariableAccess)
					numberdiffmethodreplacebyvar+=1;
				else if(other instanceof CtInvocation || other instanceof CtConstructorCall)
					numberdiffmethodreplacebymethod+=1;
				else {
					// do nothing
				}
			}
		}
	}

	int diffarray[]=new int[3];
	diffarray[0]=numberdiffargument;
	diffarray[1]=numberdiffmethodreplacebyvar;
	diffarray[2]=numberdiffmethodreplacebymethod;

      return diffarray;
}
 
Example 26
Source Project: coming   Source File: VariableResolver.java    License: MIT License 5 votes vote down vote up
/**
 * Return a list of variables that match with the variable access passed as
 * parameter. The last argument indicate if we map also the vars name
 * 
 * @param varContext
 * @param vartofind
 * @param mapName
 * @return
 */
protected static List<CtVariable> matchVariable(List<CtVariable> varContext, CtVariableAccess vartofind,
		boolean mapName) {
	List<CtVariable> varMatched = new ArrayList<>();
	try {
		CtTypeReference typeToFind = vartofind.getType();

		// First we search for compatible variables according to the type
		List<CtVariable> types = compatiblesSubType(varContext, typeToFind);
		if (types.isEmpty()) {
			return varMatched;
		}
		// Then, we search
		for (CtVariable ctVariableWithTypes : types) {
			// comparing name is optional, according to argument.
			boolean matchName = !mapName
					|| ctVariableWithTypes.getSimpleName().equals(vartofind.getVariable().getSimpleName());
			if (matchName) {
				varMatched.add(ctVariableWithTypes);
			}
		}

	} catch (Exception ex) {
		logger.error("Variable verification error", ex);
	}

	return varMatched;
}
 
Example 27
Source Project: coming   Source File: VariableResolver.java    License: MIT License 5 votes vote down vote up
/**
 * Maps a variable access with a variable declaration.
 * 
 * @param varContext
 * @param varacc
 * @return
 */
public static Map<CtVariableAccess, List<CtVariable>> matchVars(List<CtVariable> varContext,
		List<CtVariableAccess> varacc, boolean mapName) {

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

	for (CtVariableAccess ctVariableAccess : varacc) {
		List<CtVariable> matched = matchVariable(varContext, ctVariableAccess, mapName);
		mapping.put(ctVariableAccess, matched);
	}

	return mapping;
}
 
Example 28
Source Project: astor   Source File: GramProcessor.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove parenthesis from a var access. e.g. (a)
 * 
 * @param vars
 */
private void cleanParenthesis(List<CtVariableAccess> vars) {
	for (CtVariableAccess ctVariableAccess : vars) {
		String name = ctVariableAccess.getVariable().getSimpleName();
		if (name.startsWith("(")) {
			int l = name.length();
			String nm = name.substring(1, l - 2);
		}

	}
}
 
Example 29
Source Project: coming   Source File: VariableResolver.java    License: MIT License 5 votes vote down vote up
/**
 * Retrieves the variables out of scope from the element given a context.
 */
public static List<CtVariableAccess> retriveVariablesOutOfContext(List<CtVariable> varContext,
		List<CtVariableAccess> variablesToChech) {
	List<CtVariableAccess> variablesOutOfScope = new ArrayList<>();

	for (CtVariableAccess variableAccessFromElement : variablesToChech) {
		if (!fitInPlace(varContext, variableAccessFromElement)) {
			variablesOutOfScope.add(variableAccessFromElement);
		}
	}
	return variablesOutOfScope;
}
 
Example 30
Source Project: coming   Source File: VariableResolver.java    License: MIT License 5 votes vote down vote up
public static List<CtVariableAccess> collectStaticVariableAccess(CtElement rootElement,
		List<CtVariableAccess> varAccessCollected) {
	List<CtVariableAccess> statics = new ArrayList<>();

	for (CtVariableAccess ctVariableAccess : varAccessCollected) {
		CtVariableReference varref = ctVariableAccess.getVariable();

		if (isStatic(varref)) {
			statics.add(ctVariableAccess);
		}
	}
	return statics;
}