本文整理汇总了Java中spoon.reflect.declaration.CtMethod类的典型用法代码示例。如果您正苦于以下问题:Java CtMethod类的具体用法?Java CtMethod怎么用?Java CtMethod使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CtMethod类属于spoon.reflect.declaration包,在下文中一共展示了CtMethod类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:26,代码来源:TestLogProcessor.java
示例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;
}
开发者ID:STAMP-project,项目名称:Ex2Amplifier,代码行数:21,代码来源:Ex2Amplifier.java
示例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());
}
开发者ID:STAMP-project,项目名称:Ex2Amplifier,代码行数:23,代码来源:JBSERunner.java
示例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());
}
开发者ID:STAMP-project,项目名称:Ex2Amplifier,代码行数:18,代码来源:Ex2AmplifierTest.java
示例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());
}
开发者ID:STAMP-project,项目名称:Ex2Amplifier,代码行数:26,代码来源:JBSERunnerTest.java
示例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());
}
开发者ID:STAMP-project,项目名称:Ex2Amplifier,代码行数:23,代码来源:JBSERunnerTest.java
示例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);
}
开发者ID:camunda,项目名称:camunda-bpm-swagger,代码行数:24,代码来源:ApiModelPropertyProcessor.java
示例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;
}
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:19,代码来源:PitResult.java
示例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());
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:22,代码来源:TestDataMutatorTest.java
示例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
}
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:24,代码来源:TestMethodCallRemove.java
示例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")));
}
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:21,代码来源:TestProcessor.java
示例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;
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:19,代码来源:TestProcessor.java
示例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());
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:24,代码来源:MethodsAssertGeneratorTest.java
示例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()));
}
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:22,代码来源:NumberLiteralAmplifierTest.java
示例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;
}
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:25,代码来源:Amplification.java
示例16: generateSingletonList
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@SuppressWarnings("unchecked")
static CtExpression<?> generateSingletonList(CtTypeReference type, String nameMethod, Class<?> typeOfCollection) {
final Factory factory = type.getFactory();
final CtType<?> collectionsType = factory.Type().get(Collections.class);
final CtTypeAccess<?> accessToCollections = factory.createTypeAccess(collectionsType.getReference());
final CtMethod<?> singletonListMethod = collectionsType.getMethodsByName(nameMethod).get(0);
final CtExecutableReference executableReference = factory.Core().createExecutableReference();
executableReference.setStatic(true);
executableReference.setSimpleName(singletonListMethod.getSimpleName());
executableReference.setDeclaringType(collectionsType.getReference());
executableReference.setType(factory.createCtTypeReference(typeOfCollection));
if (!type.getActualTypeArguments().isEmpty() &&
type.getActualTypeArguments().stream().allMatch(ValueCreatorHelper::canGenerateAValueForType)) {
executableReference.setParameters(type.getActualTypeArguments());
List<CtExpression<?>> parameters = type.getActualTypeArguments().stream()
.map(ValueCreator::generateRandomValue).collect(Collectors.toList());
return factory.createInvocation(accessToCollections, executableReference, parameters);
} else {
return factory.createInvocation(accessToCollections, executableReference,
factory.createConstructorCall(factory.Type().createReference(Object.class))
);
}
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:24,代码来源:CollectionCreator.java
示例17: apply
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Override
public List<CtMethod> apply(CtMethod method) {
List<CtLocalVariable<?>> existingObjects = getExistingObjects(method);
final Stream<? extends CtMethod<?>> gen_o1 = existingObjects.stream() // must use tmp variable because javac is confused
.flatMap(localVariable -> ConstructorCreator.generateAllConstructionOf(localVariable.getType()).stream())
.map(ctExpression -> {
final CtMethod<?> clone = AmplificationHelper.cloneMethodTest(method, "_sd");
clone.getBody().insertBegin(
clone.getFactory().createLocalVariable(
ctExpression.getType(), "__DSPOT_gen_o" + counterGenerateNewObject++, ctExpression
)
);
return clone;
}
);
return gen_o1.collect(Collectors.toList());
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:18,代码来源:ObjectGenerator.java
示例18: testStatementAdd
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void testStatementAdd() throws Exception {
/*
Test the StatementAdd amplifier. It reuse existing object to add method call of accessible method.
It can reuse return value to add method call. It results here with 7 new test cases.
*/
final String packageName = "fr.inria.statementadd";
InputProgram inputProgram = Utils.getInputProgram();
final Factory factory = inputProgram.getFactory();
inputProgram.setFactory(factory);
AmplificationHelper.setSeedRandom(42L);
StatementAdd amplifier = new StatementAdd(packageName);
amplifier.reset(factory.Class().get(packageName + ".ClassTargetAmplify"));
CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
System.out.println(amplifiedMethods);
assertEquals(6, amplifiedMethods.size());
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:24,代码来源:StatementAddTest.java
示例19: testStatementAddOnUnderTest
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
@Test
public void testStatementAddOnUnderTest() throws Exception {
Factory factory = Utils.getFactory();
CtClass<Object> ctClass = factory.Class().get("fr.inria.mutation.ClassUnderTestTest");
AmplificationHelper.setSeedRandom(23L);
StatementAdd amplificator = new StatementAdd();
amplificator.reset(ctClass);
CtMethod originalMethod = Utils.findMethod(ctClass, "testLit");
List<CtMethod> amplifiedMethods = amplificator.apply(originalMethod);
System.out.println(amplifiedMethods);
assertEquals(2, amplifiedMethods.size());
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:18,代码来源:StatementAddTest.java
示例20: apply
import spoon.reflect.declaration.CtMethod; //导入依赖的package包/类
public List<CtMethod> apply(CtMethod method) {
List<CtMethod> methods = new ArrayList<>();
if (method.getDeclaringType() != null) {
//get the list of method calls
List<CtInvocation> invocations = Query.getElements(method, new TypeFilter(CtInvocation.class));
//this index serves to replace ith literal is replaced by zero in the ith clone of the method
int invocation_index = 0;
for (CtInvocation invocation : invocations) {
try {
if (AmplificationChecker.canBeAdded(invocation) && !AmplificationChecker.isAssert(invocation)) {
methods.add(apply(method, invocation_index));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
invocation_index++;
}
}
return AmplificationHelper.updateAmpTestToParent(methods, method);
}
开发者ID:STAMP-project,项目名称:dspot,代码行数:22,代码来源:TestMethodCallAdder.java
注:本文中的spoon.reflect.declaration.CtMethod类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论