Java Code Examples for spoon.reflect.code.CtVariableAccess#getParent()

The following examples show how to use spoon.reflect.code.CtVariableAccess#getParent() . 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: AbstractCodeAnalyzer.java    From coming with 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 2
Source File: VariableAnalyzer.java    From coming with MIT License 4 votes vote down vote up
/**
 * 
 * @param varsAffected
 * @param element
 * @param context
 */
private void analyzeV4(List<CtVariableAccess> varsAffected, CtElement element, Cntx<Object> context) {
	
	try {
		for (CtVariableAccess varInFaulty : varsAffected) {

			CtInvocation parentInvocation = varInFaulty.getParent(CtInvocation.class);
			int appearsInParams = 0;
			MapCounter<CtElement> parameterFound = new MapCounter<>();
			if (parentInvocation != null) {
				List<CtElement> arguments = parentInvocation.getArguments();
				for (CtElement i_Argument : arguments) {
					List<CtVariableAccess> varsAccessInParameter = VariableResolver.collectVariableRead(i_Argument);
					// .stream().filter(e -> e.getRoleInParent().equals(CtRole.PARAMETER))
					// .collect(Collectors.toList());
					if (varsAccessInParameter.contains(varInFaulty)) {
						appearsInParams++;

						if (!parameterFound.containsKey(varInFaulty)) {
							
							writeGroupedInfo(context, adjustIdentifyInJson(varInFaulty), 
									CodeFeatures.V4_FIRST_TIME_USED_AS_PARAMETER,
									true, "FEATURES_VARS");
						} else {
							
							writeGroupedInfo(context, adjustIdentifyInJson(varInFaulty), 
									CodeFeatures.V4_FIRST_TIME_USED_AS_PARAMETER,
									false, "FEATURES_VARS");
						}
						parameterFound.add(varInFaulty);
					}
					
					if(appearsInParams > 1)
						break;
				}
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(varInFaulty), 
					CodeFeatures.V4B_USED_MULTIPLE_AS_PARAMETER,
					(appearsInParams > 1), "FEATURES_VARS");
		}
		
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 3
Source File: VariableAnalyzer.java    From coming with MIT License 4 votes vote down vote up
private void analyzeV15_LastthreeVariableIntroduction (List<CtVariableAccess> varsAffected, CtElement element,
		Cntx<Object> context) {
	try {
		
		CtExecutable methodParent = element.getParent(CtExecutable.class);

		if (methodParent == null)
			// the element is not in a method.
			return;
		
		List<CtStatement> statements=methodParent.getElements(new LineFilter());

		// For each variable affected
		for (CtVariableAccess variableAffected : varsAffected) {

			List<CtStatement> statementbefore = new ArrayList<>();
			
			boolean lastthreesametypeloc=false;

			for (CtStatement aStatement : statements) {

				CtStatement parent = variableAffected.getParent(new LineFilter());
									
				if (!isElementBeforeVariable(variableAffected, aStatement))
					continue;
				
				if (isStatementInControl(parent, aStatement) || parent==aStatement)
					continue;
				
				if(aStatement instanceof CtIf || aStatement instanceof CtLoop) 
					continue;
				
				statementbefore.add(aStatement);
			}
			
			List<CtStatement> statinterest = new ArrayList<>();
			
			if(statementbefore.size()<=4)
				statinterest=statementbefore;
			else {
				statinterest.add(statementbefore.get(statementbefore.size()-1));
				statinterest.add(statementbefore.get(statementbefore.size()-2));
				statinterest.add(statementbefore.get(statementbefore.size()-3));
				statinterest.add(statementbefore.get(statementbefore.size()-4));
			}

			for (int index=0; index< statinterest.size(); index++) {
				if(statinterest.get(index) instanceof CtLocalVariable) {
					CtLocalVariable ctLocalVariable=(CtLocalVariable)statinterest.get(index);

					if (!ctLocalVariable.getReference().getSimpleName()
							.equals(variableAffected.getVariable().getSimpleName()) 
							&& compareTypes(ctLocalVariable.getType(), variableAffected.getType())) {
						lastthreesametypeloc = true;
						break;
					}
				}
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(variableAffected), 
					CodeFeatures.V15_VAR_LAST_THREE_SAME_TYPE_LOC,
					(lastthreesametypeloc), "FEATURES_VARS");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: AbstractCodeAnalyzer.java    From coming with MIT License 4 votes vote down vote up
public boolean[] analyze_SametypewithGuard(List<CtVariableAccess> varsAffected, CtElement element,
			CtClass parentClass, List<CtStatement> statements) {

		boolean hasPrimitiveSimilarTypeWithNormalGuard = false;
		boolean hasObjectSimilarTypeWithNormalGuard = false;
		boolean hasPrimitiveSimilarTypeWithNullGuard = false;
		boolean hasObjectSimilarTypeWithNullGuard = false;

		try {
			CtExecutable faultyMethodParent = element.getParent(CtExecutable.class);

			if (faultyMethodParent == null)
				// the element is not in a method.
				return null;

			// For each variable affected
			for (CtVariableAccess variableAffected : varsAffected) {
				// for (CtStatement aStatement : statements) {
				CtStatement parent = variableAffected.getParent(new LineFilter());

				if (isNormalGuard(variableAffected, (parent)) || isNullCheckGuard(variableAffected, (parent)))
					continue;

				// For each statement in the method (it includes the statements inside the
				// blocks (then, while)!)
				for (CtStatement aStatement : statements) {
					// for (CtVariableAccess variableAffected : varsAffected) {

					if (parent == aStatement)
						continue;

					List<CtVariableAccess> varsFromStatement;

					if (aStatement instanceof CtIf || aStatement instanceof CtLoop) {
						varsFromStatement = VariableResolver.collectVariableRead(retrieveElementToStudy(aStatement));
					} else
						varsFromStatement = VariableResolver.collectVariableRead(aStatement);
//
//					// let's collect the var access in the statement
//					List<CtVariableAccess> varsFromStatement = VariableResolver
//							.collectVariableReadIgnoringBlocks(aStatement);
					// if the var access is the same that the affected
					for (CtVariableAccess varInStatement : varsFromStatement) {
						// Has similar type but different name
						if (compareTypes(variableAffected.getVariable().getType(),
								varInStatement.getVariable().getType())) {
							// && !hasSameName(variableAffected, varInStatement)) {
							// Now, let's check if the parent is a guard
							// if (isGuard(getParentNotBlock(aStatement))) {
							if (isNormalGuard(varInStatement, (aStatement))) {

								// it's ok, now let's check the type
								if (variableAffected.getType() != null) {
									// for primitive type variables, we require it to be the same global variable
									if (variableAffected.getType().isPrimitive() && varInStatement.getVariable()
											.getSimpleName().equals(variableAffected.getVariable().getSimpleName()))
										hasPrimitiveSimilarTypeWithNormalGuard = true;
									else
										hasObjectSimilarTypeWithNormalGuard = true;
								}
							}

							if (isNullCheckGuard(varInStatement, (aStatement))) {

								// it's ok, now let's check the type
								if (variableAffected.getType() != null) {

									if (variableAffected.getType().isPrimitive() && varInStatement.getVariable()
											.getSimpleName().equals(variableAffected.getVariable().getSimpleName()))
										hasPrimitiveSimilarTypeWithNullGuard = true;
									else
										hasObjectSimilarTypeWithNullGuard = true;
								}
							}

						}
					}
					// If we find both cases, we can stop
					if (hasPrimitiveSimilarTypeWithNormalGuard && hasObjectSimilarTypeWithNormalGuard
							&& hasPrimitiveSimilarTypeWithNullGuard && hasObjectSimilarTypeWithNullGuard)
						break;
				}
			}

		} catch (Throwable e) {
			e.printStackTrace();
		}

		boolean[] expressionvalue = new boolean[4];
		expressionvalue[0] = hasObjectSimilarTypeWithNormalGuard;
		expressionvalue[1] = hasPrimitiveSimilarTypeWithNormalGuard;
		expressionvalue[2] = hasObjectSimilarTypeWithNullGuard;
		expressionvalue[3] = hasPrimitiveSimilarTypeWithNullGuard;

		return expressionvalue;

	}