Java Code Examples for spoon.reflect.declaration.CtMethod#isPublic()
The following examples show how to use
spoon.reflect.declaration.CtMethod#isPublic() .
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: APICheckingProcessor.java From spoon-examples with GNU General Public License v2.0 | 6 votes |
@Override public boolean isToBeProcessed(CtMethod method) { // get the CtClass the method is attached to CtType parentClass = method.getParent(CtType.class); // check that the class belongs to the public API if (parentClass.getQualifiedName().contains(PACKAGE_PUBLIC_API)) { // check that the method is public if (method.isPublic()) { // check that the return type belongs to the private API CtTypeReference returnType = method.getType(); return returnType.getQualifiedName().contains(PACKAGE_PRIVATE_API); } } return false; }
Example 2
Source File: MethodXMethodReplacementDiffArgumentsOp.java From astor with GNU General Public License v2.0 | 4 votes |
/** * * * @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 3
Source File: MethodXMethodReplacementArgumentRemoveOp.java From astor with GNU General Public License v2.0 | 4 votes |
/** * 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 4
Source File: SupportOperators.java From astor with GNU General Public License v2.0 | 4 votes |
public static List<CtInvocation> retrieveInvocationsFromVar(CtTypeReference variableToReplaceType, CtClass classUnderAnalysis, ModificationPoint point) { List<CtInvocation> newInvocations = new ArrayList<>(); List<CtVariable> variablesInScope = point.getContextOfModificationPoint(); boolean isParentMethodStatic = isParentMethodStatic(point.getCodeElement()); for (CtVariable varInScope : variablesInScope) { if (varInScope.getType() == null || varInScope.getType().isPrimitive()) { continue; } // if (isParentMethodStatic && // !varInScope.getModifiers().contains(ModifierKind.STATIC)) { // if the modification point is in a static method, the variable to target must // be // static continue; } // List<CtMethod> allMethods = varInScope.getType().getAllExecutables().stream() .filter(e -> e.getExecutableDeclaration() instanceof CtMethod) .map(e -> e.getExecutableDeclaration()).map(CtMethod.class::cast).collect(Collectors.toList()); for (CtMethod anotherMethod : allMethods) { if (anotherMethod.getSimpleName().startsWith(VarReplacementByMethodCallOp.META_METHOD_LABEL)) // It's a meta-method, discard continue; if (!anotherMethod.isPublic()) continue; boolean compatibleReturnTypes = SupportOperators.checkIsSubtype(anotherMethod.getType(), variableToReplaceType); if (compatibleReturnTypes) { List<CtInvocation> newInvToMethods = createRealInvocationsAllPosibilities(point, anotherMethod, MutationSupporter.getFactory().createVariableRead(varInScope.getReference(), varInScope.isStatic())); newInvocations.addAll(newInvToMethods); } } } return newInvocations; }
Example 5
Source File: MethodXMethodReplacementDiffNameOp.java From astor with GNU General Public License v2.0 | 2 votes |
/** * 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; }