Java Code Examples for spoon.reflect.declaration.CtClass#equals()

The following examples show how to use spoon.reflect.declaration.CtClass#equals() . 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: MethodXMethodReplacementDiffArgumentsOp.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * 
 * @param suspiciousElement
 * @param context
 * @return
 */
@Override
public MapList<CtInvocation, Ingredient> retrieveInvocationIngredient(ModificationPoint point) {
	CtElement suspiciousElement = point.getCodeElement();

	CtClass classUnderAnalysis = suspiciousElement.getParent(CtClass.class);

	MapList<CtInvocation, Ingredient> similarInvocationResult = new MapList<>();

	List<CtInvocation> invocations = getInvocations(suspiciousElement);
	for (CtInvocation invocationToReplace : invocations) {

		CtExecutable minvokedInAffected = invocationToReplace.getExecutable().getDeclaration();

		if (minvokedInAffected == null || !(minvokedInAffected instanceof CtMethod))
			continue;

		CtMethod affectedMethod = (CtMethod) minvokedInAffected;

		CtType typeOfTarget = invocationToReplace.getTarget().getType().getTypeDeclaration();

		if (!(typeOfTarget instanceof CtClass))
			continue;

		CtClass targetOfInvocation = (CtClass) typeOfTarget;

		List allMethods = SupportOperators.getAllMethodsFromClass(targetOfInvocation);

		for (Object omethod : allMethods) {

			if (!(omethod instanceof CtMethod))
				continue;

			CtMethod anotherMethod = (CtMethod) omethod;

			if (anotherMethod.getSimpleName().startsWith(VarReplacementByMethodCallOp.META_METHOD_LABEL))
				// It's a meta-method, discard
				continue;

			if (anotherMethod.getSignature().equals(affectedMethod.getSignature()))
				// It's the same, we discard it.
				continue;

			// Only if the target is the class we can call to non public methods
			if (!targetOfInvocation.equals(classUnderAnalysis) && !anotherMethod.isPublic())
				continue;

			// The name must be the same
			if (anotherMethod.getSimpleName().equals(affectedMethod.getSimpleName())) {

				if (anotherMethod.getType() != null && minvokedInAffected.getType() != null) {

					boolean compatibleReturnTypes = SupportOperators.checkIsSubtype(anotherMethod.getType(),
							minvokedInAffected.getType());
					// must return the same object
					if (compatibleReturnTypes) {

						// Case 3: Different number argument
						if (anotherMethod.getParameters().size() != affectedMethod.getParameters().size()
								// check the types
								|| (!anotherMethod.getParameters().isEmpty()
										&& !affectedMethod.getParameters().isEmpty()
										&& !anotherMethod.getParameters().equals(affectedMethod.getParameters()))) {

							List<CtInvocation> newInvToMethods = SupportOperators
									.createRealInvocationsReusingVars(point, anotherMethod, invocationToReplace);

							for (CtInvocation ctInvocation : newInvToMethods) {
								CtInvocation newInvocation = ctInvocation.clone();
								newInvocation.setExecutable(anotherMethod.getReference());
								Ingredient newIngredient = new Ingredient(newInvocation);
								newIngredient.setDerivedFrom(invocationToReplace);
								similarInvocationResult.add(invocationToReplace, newIngredient);
								newIngredient.getMetadata().put("original", invocationToReplace);
								newIngredient.getMetadata().put("replacement", ctInvocation);

							}

						}
					}

				}
			}
		}

	}
	return similarInvocationResult;

}
 
Example 2
Source File: MethodXMethodReplacementArgumentRemoveOp.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Case 1: Argument removement
 * 
 * @param suspiciousElement
 * @param context
 * @return
 */
@Override
public MapList<CtInvocation, Ingredient> retrieveInvocationIngredient(ModificationPoint point) {

	CtElement suspiciousElement = point.getCodeElement();
	CtClass classUnderAnalysis = suspiciousElement.getParent(CtClass.class);

	MapList<CtInvocation, Ingredient> similarInvocationResult = new MapList<>();

	List<CtInvocation> invocations = getInvocations(suspiciousElement);

	for (CtInvocation invocationToReplace : invocations) {

		CtExecutable minvokedInAffected = invocationToReplace.getExecutable().getDeclaration();

		if (minvokedInAffected == null || !(minvokedInAffected instanceof CtMethod))
			continue;

		CtMethod affectedMethod = (CtMethod) minvokedInAffected;

		CtType typeOfTarget = invocationToReplace.getTarget().getType().getTypeDeclaration();

		if (!(typeOfTarget instanceof CtClass))
			continue;

		CtClass targetOfInvocation = (CtClass) typeOfTarget;

		List allMethods = SupportOperators.getAllMethodsFromClass(targetOfInvocation);

		for (Object omethod : allMethods) {

			if (!(omethod instanceof CtMethod))
				continue;

			CtMethod anotherMethod = (CtMethod) omethod;

			if (anotherMethod.getSimpleName().startsWith(VarReplacementByMethodCallOp.META_METHOD_LABEL))
				// It's a meta-method, discard
				continue;

			if (anotherMethod.getSignature().equals(affectedMethod.getSignature()))
				// It's the same, we discard it.
				continue;

			// Only if the target is the class we can call to non public methods
			if (!targetOfInvocation.equals(classUnderAnalysis) && !anotherMethod.isPublic())
				continue;

			// The name must be the same
			if (anotherMethod.getSimpleName().equals(affectedMethod.getSimpleName())) {

				if (anotherMethod.getType() != null && minvokedInAffected.getType() != null) {

					boolean compatibleReturnTypes = SupportOperators.checkIsSubtype(anotherMethod.getType(),
							minvokedInAffected.getType());
					// must return the same object
					if (compatibleReturnTypes) {

						// CASE 1: Different method name
						if (anotherMethod.getParameters().size() < affectedMethod.getParameters().size()) {

							List newArguments = SupportOperators.checkOcurrenceOfOtherParameters(anotherMethod,
									affectedMethod, invocationToReplace.getArguments());
							if (newArguments != null) {

								CtInvocation newInvocation = MutationSupporter.getFactory().createInvocation(
										invocationToReplace.getTarget(), anotherMethod.getReference(),
										newArguments);
								newInvocation.setExecutable(anotherMethod.getReference());
								newInvocation.setArguments(newArguments);
								newInvocation.setTarget(invocationToReplace.getTarget());

								Ingredient newIngredient = new Ingredient(newInvocation);
								newIngredient.setDerivedFrom(invocationToReplace);

								similarInvocationResult.add(invocationToReplace, newIngredient);

								newIngredient.getMetadata().put("original", invocationToReplace);
								newIngredient.getMetadata().put("replacement", newInvocation);
							}
						}
					}

				}
			}
		}

	}
	return similarInvocationResult;

}
 
Example 3
Source File: MethodXMethodReplacementDiffNameOp.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * For case 2
 * 
 * @param suspiciousElement
 * @param context
 * @return
 */
@Override
public MapList<CtInvocation, Ingredient> retrieveInvocationIngredient(ModificationPoint point) {
	CtElement suspiciousElement = point.getCodeElement();

	CtClass classUnderAnalysis = suspiciousElement.getParent(CtClass.class);

	MapList<CtInvocation, Ingredient> similarInvocationResult = new MapList<>();

	List<CtInvocation> invocations = getInvocations(suspiciousElement);

	for (CtInvocation invocationToReplace : invocations) {

		CtExecutable minvokedInAffected = invocationToReplace.getExecutable().getDeclaration();

		if (minvokedInAffected == null || !(minvokedInAffected instanceof CtMethod))
			continue;

		CtMethod affectedMethod = (CtMethod) minvokedInAffected;

		CtType typeOfTarget = invocationToReplace.getTarget().getType().getTypeDeclaration();

		if (!(typeOfTarget instanceof CtClass))
			continue;

		CtClass targeClasstOfInvocation = (CtClass) typeOfTarget;

		List allMethods = SupportOperators.getAllMethodsFromClass(targeClasstOfInvocation);

		for (Object omethod : allMethods) {

			if (!(omethod instanceof CtMethod))
				continue;

			CtMethod anotherMethod = (CtMethod) omethod;

			if (anotherMethod.getSignature().equals(affectedMethod.getSignature()))
				// It's the same, we discard it.
				continue;

			if (anotherMethod.getSimpleName().startsWith(VarReplacementByMethodCallOp.META_METHOD_LABEL))
				// It's a meta-method, discard
				continue;

			// Only if the target is the class we can call to non public methods
			if (!targeClasstOfInvocation.equals(classUnderAnalysis) && !anotherMethod.isPublic())
				continue;

			if (anotherMethod.getSimpleName().equals(affectedMethod.getSimpleName())) {
				// It's override
				// TODO:
				// similarInvocationResult.add(affectedMethod, anotherMethod);

			}

			if (anotherMethod.getType() != null && minvokedInAffected.getType() != null) {

				boolean compatibleReturnTypes = SupportOperators.checkIsSubtype(anotherMethod.getType(),
						minvokedInAffected.getType());
				if (compatibleReturnTypes) {

					// CASE 2: Different method name
					if (anotherMethod.getParameters().size() == affectedMethod.getParameters().size()
							&& anotherMethod.getParameters().equals(affectedMethod.getParameters())) {

						List<CtInvocation> newInvToMethods = SupportOperators
								.createRealInvocationsReusingVars(point, anotherMethod, invocationToReplace);

						for (CtInvocation aNewInvocation : newInvToMethods) {
							CtInvocation newInvocationCloned = aNewInvocation.clone();
							newInvocationCloned.setExecutable(anotherMethod.getReference());
							Ingredient newIngredient = new Ingredient(newInvocationCloned);
							newIngredient.setDerivedFrom(invocationToReplace);
							newIngredient.getMetadata().put("original", invocationToReplace);
							newIngredient.getMetadata().put("replacement", aNewInvocation);

							similarInvocationResult.add(invocationToReplace, newIngredient);

						}
					}
				}

			}

		}

	}
	return similarInvocationResult;

}