use of spoon.reflect.code.CtAbstractInvocation in project Ex2Amplifier by STAMP-project.
the class Utils method getRealTypeOfLiteral.
public static CtTypeReference<?> getRealTypeOfLiteral(CtLiteral<?> literal) {
if (literal.getValue() instanceof Number) {
final CtTypedElement typedParent = literal.getParent(CtTypedElement.class);
if (typedParent != null) {
// special treatment for int literal
if (typedParent instanceof CtAbstractInvocation) {
final CtExecutableReference<?> executable = ((CtAbstractInvocation) typedParent).getExecutable();
final int indexOf = ((CtAbstractInvocation) typedParent).getArguments().indexOf(literal);
final CtTypeReference<?> ctTypeReference = executable.getParameters().get(indexOf);
if (Number.class.isAssignableFrom(ctTypeReference.getActualClass())) {
return ctTypeReference;
} else {
return literal.getType();
}
} else if (typedParent.getType() instanceof CtArrayTypeReference) {
return ((CtArrayTypeReference) typedParent.getType()).getComponentType();
} else {
return typedParent.getType();
}
} else {
throw new IllegalArgumentException(literal.toString());
}
} else {
return literal.getType();
}
}
use of spoon.reflect.code.CtAbstractInvocation in project spoon by INRIA.
the class ExecutableRefTest method getInvocationFromMethod.
private CtAbstractInvocation<?> getInvocationFromMethod(String methodName) throws Exception {
Factory factory = build(ExecutableRefTestSource.class, MyIntf.class);
CtClass<ExecutableRefTestSource> clazz = factory.Class().get(ExecutableRefTestSource.class);
Assert.assertNotNull(clazz);
List<CtMethod<?>> methods = clazz.getMethodsByName(methodName);
assertEquals(1, methods.size());
CtMethod<?> ctMethod = methods.get(0);
CtBlock<?> ctBody = (CtBlock<?>) ctMethod.getBody();
Assert.assertNotNull(ctBody);
List<CtStatement> ctStatements = ctBody.getStatements();
assertEquals(1, ctStatements.size());
CtStatement ctStatement = ctStatements.get(0);
Assert.assertTrue(ctStatement instanceof CtAbstractInvocation<?>);
return (CtAbstractInvocation<?>) ctStatement;
}
use of spoon.reflect.code.CtAbstractInvocation in project spoon by INRIA.
the class VariableReferencesTest method getLiteralValue.
private Integer getLiteralValue(CtVariable<?> var) {
CtExpression<?> exp = var.getDefaultExpression();
if (exp != null) {
try {
return getLiteralValue(exp);
} catch (ClassCastException e) {
}
}
if (var instanceof CtParameter) {
CtParameter param = (CtParameter) var;
CtExecutable<?> l_exec = param.getParent(CtExecutable.class);
int l_argIdx = l_exec.getParameters().indexOf(param);
assertTrue(l_argIdx >= 0);
if (l_exec instanceof CtLambda) {
CtLambda<?> lambda = (CtLambda<?>) l_exec;
CtLocalVariable<?> lamVar = (CtLocalVariable) lambda.getParent();
CtLocalVariableReference<?> lamVarRef = lamVar.getParent().filterChildren((CtLocalVariableReference ref) -> ref.getSimpleName().equals(lamVar.getSimpleName())).first();
CtAbstractInvocation inv = lamVarRef.getParent(CtAbstractInvocation.class);
return getLiteralValue((CtExpression<?>) inv.getArguments().get(l_argIdx));
} else {
CtExecutableReference<?> l_execRef = l_exec.getReference();
List<CtAbstractInvocation<?>> list = l_exec.getFactory().Package().getRootPackage().filterChildren((CtAbstractInvocation inv) -> {
// return inv.getExecutable().equals(l_execRef);
return inv.getExecutable().getExecutableDeclaration() == l_exec;
}).list();
CtAbstractInvocation inv = list.get(0);
Integer firstValue = getLiteralValue((CtExpression<?>) inv.getArguments().get(l_argIdx));
// check that all found method invocations are using same key
list.forEach(inv2 -> {
assertEquals(firstValue, getLiteralValue((CtExpression<?>) inv2.getArguments().get(l_argIdx)));
});
return firstValue;
}
}
return getCommentValue(var);
}
use of spoon.reflect.code.CtAbstractInvocation in project Ex2Amplifier by STAMP-project.
the class ArgumentsExtractor method performExtraction.
public static CtMethod<?> performExtraction(CtMethod<?> ctMethod) {
final CtMethod<?> ctMethodWithoutAssertion = new AssertionRemover().removeAssertion(ctMethod);
final Factory factory = ctMethodWithoutAssertion.getFactory();
final CtMethod<?> extractedMethod = ctMethodWithoutAssertion.clone();
extractedMethod.setSimpleName("extract_" + ctMethodWithoutAssertion.getSimpleName());
new ArrayList<>(extractedMethod.getThrownTypes()).forEach(extractedMethod::removeThrownType);
ctMethodWithoutAssertion.getAnnotations().forEach(extractedMethod::removeAnnotation);
final int[] count = new int[1];
final Map<CtAbstractInvocation<?>, List<CtVariableAccess>> parametersPerInvocation = new HashMap<>();
extractedMethod.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).stream().filter(literal -> !(literal.getValue() instanceof String)).forEach(ctLiteral -> {
final CtParameter parameter = factory.createParameter(extractedMethod, Utils.getRealTypeOfLiteral(ctLiteral), "lit" + count[0]++);
final CtParameterReference<?> parameterReference = factory.createParameterReference();
parameterReference.setSimpleName(parameter.getSimpleName());
parameterReference.setType(parameter.getType());
final CtVariableAccess<?> variableRead = factory.createVariableRead(parameterReference, false);
final CtAbstractInvocation invocation = ctLiteral.getParent(CtAbstractInvocation.class);
if (invocation != null) {
if (!parametersPerInvocation.containsKey(invocation)) {
parametersPerInvocation.put(invocation, new ArrayList<>(invocation.getArguments()));
}
if (ctLiteral.getParent() instanceof CtUnaryOperator) {
ctLiteral.getParent().replace(variableRead);
} else {
ctLiteral.replace(variableRead);
}
} else {
ctLiteral.replace(variableRead);
}
});
extractedMethod.setThrownTypes(ctMethod.getThrownTypes());
return extractedMethod;
}
Aggregations