本文整理汇总了Java中spoon.reflect.declaration.CtMethod类的典型用法代码示例。如果您正苦于以下问题:Java CtMethod类的具体用法?Java CtMethod怎么用?Java CtMethod使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CtMethod类属于spoon.reflect.declaration包,在下文中一共展示了CtMethod类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
public void process(CtMethod element) {
Factory factory = this.getFactory();
CtTry ctTry = factory.Core().createTry();
ctTry.setBody(element.getBody());
String snippet;
String testName;
if(element.getModifiers().contains(ModifierKind.STATIC)) {
testName = element.getPosition().getCompilationUnit().getMainType().getQualifiedName() + "." + element.getSimpleName();
snippet = this.getLogName() + ".testIn(Thread.currentThread(), \"" + testName + "\")";
} else {
testName = element.getSimpleName();
snippet = this.getLogName() + ".testIn(Thread.currentThread(),this, \"" + testName + "\")";
}
CtCodeSnippetStatement snippetStatement = factory.Code().createCodeSnippetStatement(snippet);
element.getBody().insertBegin(snippetStatement);
snippet = this.getLogName() + ".testOut(Thread.currentThread())";
CtCodeSnippetStatement snippetFinish = factory.Code().createCodeSnippetStatement(snippet);
CtBlock finalizerBlock = factory.Core().createBlock();
finalizerBlock.addStatement(snippetFinish);
ctTry.setFinalizer(finalizerBlock);
CtBlock methodBlock = factory.Core().createBlock();
methodBlock.addStatement(ctTry);
element.setBody(methodBlock);
}
示例2: generateNewTestMethod
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
private CtMethod<?> generateNewTestMethod(CtMethod<?> testMethod,
Map<String, List<String>> conditionForParameter) {
final CtMethod clone = AmplificationHelper.cloneMethodTest(testMethod, "_examplifier");
final List<?> solutions = SMTSolver.solve(conditionForParameter);
final Iterator<?> iterator = solutions.iterator();
final List<CtLiteral> originalLiterals =
clone.getElements(new TypeFilter<>(CtLiteral.class));
conditionForParameter.keySet()
.forEach(s -> {
final int indexOfLit = Integer.parseInt(s.substring("param".length()));
final CtLiteral literalToBeReplaced = originalLiterals.get(indexOfLit);
final CtLiteral<?> newLiteral = testMethod.getFactory().createLiteral(iterator.next());
if (literalToBeReplaced.getParent() instanceof CtUnaryOperator) {
literalToBeReplaced.getParent().replace(newLiteral);
} else {
literalToBeReplaced.replace(newLiteral);
}
});
return clone;
}
示例3: runJBSE
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
public static List<Map<String, List<String>>> runJBSE(String classpath, CtMethod<?> testMethod) {
final RunParameters p = new RunParameters();
p.addClasspath(addRequiredJARsToClasspath(classpath).split(":"));
p.setMethodSignature(
testMethod.getParent(CtClass.class).getQualifiedName().replaceAll("\\.", "/"),
methodToDescriptor.apply(testMethod),
testMethod.getSimpleName()
);
p.setDecisionProcedureType(RunParameters.DecisionProcedureType.Z3);
p.setExternalDecisionProcedurePath("lib/z3/build/bin/z3");
p.setOutputFileName("out/runIf_z3.txt");
p.setStepShowMode(RunParameters.StepShowMode.LEAVES);
p.setStateFormatMode(RunParameters.StateFormatMode.FULLTEXTHISTORY);
p.setShowOnConsole(Main.verbose);
final Run r = new Run(p);
r.run();
return filterDistinctLeaves(StateFormatterTextWithHistory.getStates())
.stream()
.map(JBSERunner::buildConditionsOnArguments)
.collect(Collectors.toList());
}
示例4: test
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void test() throws Exception {
this.configuration.getInputProgram().setFactory(this.launcher.getFactory());
final Ex2Amplifier amplifier = new Ex2Amplifier(this.configuration);
final CtClass<?> testClass = this.launcher.getFactory().Class().get("fr.inria.calculator.CalculatorTest");
amplifier.reset(testClass);
final List<CtMethod> amplifiedTestAccumulate = amplifier.apply(testClass.getMethodsByName("testAccumulate").get(0));
assertEquals(2, amplifiedTestAccumulate.size());
final String expectedAmplifiedTestMethod = "{" + AmplificationHelper.LINE_SEPARATOR +
" final Calculator calculator1 = new Calculator(0);" + AmplificationHelper.LINE_SEPARATOR +
" Assert.assertEquals((-5), calculator1.getCurrentValue());" + AmplificationHelper.LINE_SEPARATOR +
" calculator1.accumulate((-5));" + AmplificationHelper.LINE_SEPARATOR +
" Assert.assertEquals((-15), calculator1.getCurrentValue());" + AmplificationHelper.LINE_SEPARATOR +
"}";
assertEquals(expectedAmplifiedTestMethod, amplifiedTestAccumulate.get(0).getBody().toString());
}
示例5: test
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void test() throws Exception {
/*
Test the run method of JBSERunner. JBSE should return a List of Map,
that associates a name of parameter to its constraints to reach each state.
Parameter are extracted literals from test.
*/
final InputProgram program = this.configuration.getInputProgram();
final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(
this.configuration
).buildClasspath(program.getProgramDir()) + ":" +
program.getProgramDir() + program.getClassesDir() + ":" +
program.getProgramDir() + program.getTestClassesDir();
final CtMethod<?> testMethod = this.launcher.getFactory().Class()
.get("fr.inria.calculator.CalculatorTest")
.getMethodsByName("testAccumulateWithParameters")
.get(0);
final List<Map<String, List<String>>> conditionOnVariablesForEachState =
JBSERunner.runJBSE(classpath, testMethod);
assertEquals(2, conditionOnVariablesForEachState.size());
assertEquals("[{param1=[param1 % 3 == 0]}, {param1=[param1 % 3 != 0]}]", conditionOnVariablesForEachState.toString());
}
示例6: test2
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void test2() throws Exception {
/*
Same as Test, but on another test method (compareTo).
In this case, there is also a parameter in the second operand
*/
final InputProgram program = configuration.getInputProgram();
final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(
configuration
).buildClasspath(program.getProgramDir()) + ":" +
program.getProgramDir() + program.getClassesDir() + ":" +
program.getProgramDir() + program.getTestClassesDir();
final CtMethod<?> testMethod = this.launcher.getFactory().Class()
.get("fr.inria.calculator.CalculatorTest")
.getMethodsByName("testCompareToWithParameters")
.get(0);
final List<Map<String, List<String>>> conditionOnVariablesForEachState =
JBSERunner.runJBSE(classpath, testMethod);
assertEquals("[{param1=[param1 > param2]}, {param1=[param1 <= param2]}]", conditionOnVariablesForEachState.toString());
}
示例7: process
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Override
public void process(final CtMethod<?> method) {
final CtAnnotation<Annotation> annotation = getFactory().Code().createAnnotation(getFactory().Code().createCtTypeReference(ApiModelProperty.class));
final String fieldName = uncapitalize(removeStart(method.getSimpleName(), "get"));
final String classFqn = TypeHelper.getClassname(method);
final Map<String, ParameterDescription> docs = context.getDtoDocumentation().get(classFqn);
if (docs != null) {
final ParameterDescription parameterDescription = docs.get(fieldName);
if (parameterDescription != null) {
log.debug("Found parameter description for {} {}", classFqn, fieldName);
annotation.addValue("value", parameterDescription.getDescription());
if (parameterDescription.getRequired() != null) {
annotation.addValue("required", parameterDescription.getRequired().booleanValue());
}
}
}
annotation.addValue("name", fieldName);
method.addAnnotation(annotation);
}
示例8: getMethod
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
public CtMethod getMethod(CtType<?> ctClass) {
if ("none".equals(this.simpleNameMethod)) {
return null;
} else {
if (this.testCase == null) {
List<CtMethod<?>> methodsByName = ctClass.getMethodsByName(this.simpleNameMethod);
if (methodsByName.isEmpty()) {
if (ctClass.getSuperclass() != null) {
return getMethod(ctClass.getSuperclass().getDeclaration());
} else {
return null;
}
}
this.testCase = methodsByName.get(0);
}
return this.testCase;
}
}
示例9: testBooleanMutation
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void testBooleanMutation() throws Exception {
/*
Test the amplification on boolean literal
*/
final String nameMethod = "methodBoolean";
final boolean originalValue = true;
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
TestDataMutator mutator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<CtMethod> mutantMethods = mutator.apply(method);
CtMethod mutantMethod = mutantMethods.get(0);
assertEquals(1, mutantMethods.size());
assertEquals(nameMethod + SUFFIX_MUTATION + "Boolean" + "1", mutantMethod.getSimpleName());
CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
assertEquals(!(originalValue), mutantLiteral.getValue());
}
示例10: testMethodCallRemoveAll
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void testMethodCallRemoveAll() throws Exception {
/*
Test that we remove method call in a test for each used method in the test.
3 method are called in the original test, we produce 3 test methods.
*/
CtClass<Object> testClass = Utils.getFactory().Class().get("fr.inria.mutation.ClassUnderTestTest");
TestMethodCallRemover methodCallRemove = new TestMethodCallRemover();
methodCallRemove.reset(null);
final CtMethod<?> originalMethod = testClass.getMethods().stream().filter(m -> "testAddCall".equals(m.getSimpleName())).findFirst().get();
List<CtMethod> amplifiedMethods = methodCallRemove.apply(originalMethod);
assertEquals(2, amplifiedMethods.size());
for (int i = 0; i < amplifiedMethods.size(); i++) {
assertEquals(originalMethod.getBody().getStatements().size() - 1, amplifiedMethods.get(i).getBody().getStatements().size());
assertNotEquals(amplifiedMethods.get((i+1) % amplifiedMethods.size()).getBody(), amplifiedMethods.get(i).getBody());//checks that all generated methods are different
}
}
示例11: isTest
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
protected boolean isTest(CtMethod candidate) {
if(candidate.isImplicit()
|| !candidate.getModifiers().contains(ModifierKind.PUBLIC)
|| candidate.getBody() == null
|| candidate.getBody().getStatements().size() == 0) {
return false;
}
if(!guavaTestlib) {
return candidate.getSimpleName().contains("test")
|| candidate.getAnnotations().stream()
.map(annotation -> annotation.toString())
.anyMatch(annotation -> annotation.startsWith("@org.junit.Test"));
} else {
return candidate.getDeclaringType().getSimpleName().endsWith("Tester")
&& (candidate.getSimpleName().contains("test")
|| candidate.getAnnotations().stream()
.map(annotation -> annotation.toString())
.anyMatch(annotation -> annotation.startsWith("@org.junit.Test")));
}
}
示例12: cloneMethod
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
protected CtMethod cloneMethod(CtMethod method, String suffix) {
count++;
CtMethod cloned_method = this.getFactory().Core().clone(method);
cloned_method.setParent(method.getParent());
//rename the clone
cloned_method.setSimpleName(method.getSimpleName()+suffix+cloneNumber);
cloneNumber++;
CtAnnotation toRemove = cloned_method.getAnnotations().stream()
.filter(annotation -> annotation.toString().contains("Override"))
.findFirst().orElse(null);
if(toRemove != null) {
cloned_method.removeAnnotation(toRemove);
}
mutatedMethod.add(cloned_method);
return cloned_method;
}
示例13: testBuildAssertOnSpecificCases
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void testBuildAssertOnSpecificCases() throws Exception {
CtClass testClass = Utils.findClass("fr.inria.sample.TestClassWithSpecificCaseToBeAsserted");
MethodsAssertGenerator mag = new MethodsAssertGenerator(testClass, Utils.getInputConfiguration(), Utils.getCompiler());
CtMethod test1 = Utils.findMethod("fr.inria.sample.TestClassWithSpecificCaseToBeAsserted", "test1");
List<CtMethod<?>> test1_buildNewAssert = mag.generateAsserts(testClass, Collections.singletonList(test1));
final String expectedBody = "{" + nl +
" int a = 0;" + nl +
" int b = 1;" + nl +
" // AssertGenerator create local variable with return value of invocation" + nl +
" int o_test1__3 = new java.util.Comparator<java.lang.Integer>() {" + nl +
" @java.lang.Override" + nl +
" public int compare(java.lang.Integer integer, java.lang.Integer t1) {" + nl +
" return integer - t1;" + nl +
" }" + nl +
" }.compare(a, b);" + nl +
" // AssertGenerator add assertion" + nl +
" org.junit.Assert.assertEquals(-1, ((int) (o_test1__3)));" + nl +
"}";
assertEquals(expectedBody, test1_buildNewAssert.get(0).getBody().toString());
}
示例14: testIntMutation
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void testIntMutation() throws Exception {
final String nameMethod = "methodInteger";
final int originalValue = 23;
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Integer> expectedValues = Arrays.asList(22, 24, 2147483647, -2147483648, 0);
List<CtMethod> mutantMethods = amplificator.apply(method);
assertEquals(5, mutantMethods.size());
for (int i = 0; i < mutantMethods.size(); i++) {
CtMethod mutantMethod = mutantMethods.get(i);
assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
assertNotEquals(originalValue, mutantLiteral.getValue());
assertTrue(mutantLiteral.getValue() + " not in expected values",
expectedValues.contains(mutantLiteral.getValue()));
}
}
示例15: compileAndRunTests
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
private TestListener compileAndRunTests(CtType classTest, List<CtMethod<?>> currentTestList) {
CtType amplifiedTestClass = this.testSelector.buildClassForSelection(classTest, currentTestList);
final TestListener result = TestCompiler.compileAndRun(
amplifiedTestClass,
this.compiler,
currentTestList,
this.configuration
);
final long numberOfSubClasses = classTest.getFactory().Class().getAll().stream()
.filter(subClass -> classTest.getReference().equals(subClass.getSuperclass()))
.count();
if (result == null ||
!result.getFailingTests().isEmpty() ||
(!classTest.getModifiers().contains(ModifierKind.ABSTRACT) &&
result.getRunningTests().size() != currentTestList.size()) ||
(classTest.getModifiers().contains(ModifierKind.ABSTRACT) &&
numberOfSubClasses != result.getRunningTests().size())) {
return null;
} else {
LOGGER.info("update test testCriterion");
testSelector.update();
return result;
}
}