本文整理汇总了Java中spoon.reflect.declaration.CtMethod.setSimpleName方法的典型用法代码示例。如果您正苦于以下问题:Java CtMethod.setSimpleName方法的具体用法?Java CtMethod.setSimpleName怎么用?Java CtMethod.setSimpleName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spoon.reflect.declaration.CtMethod
的用法示例。
在下文中一共展示了CtMethod.setSimpleName方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: cloneMethod
import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
private static CtMethod cloneMethod(CtMethod method, String suffix) {
CtMethod cloned_method = method.clone();
//rename the clone
cloned_method.setSimpleName(method.getSimpleName() + (suffix.isEmpty() ? "" : 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);
}
return cloned_method;
}
示例3: createTestWithLog
import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
static CtMethod<?> createTestWithLog(CtMethod test, final String filter) {
CtMethod clone = AmplificationHelper.cloneMethodTest(test, "");
clone.setSimpleName(test.getSimpleName() + "_withlog");
final List<CtStatement> allStatement = clone.getElements(new TypeFilter<>(CtStatement.class));
allStatement.stream()
.filter(statement -> isStmtToLog(filter, statement))
.forEach(statement ->
addLogStmt(statement,
test.getSimpleName() + "__" + indexOfByRef(allStatement, statement))
);
return clone;
}
示例4: apply
import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
@Override
public List<CtMethod> apply(CtMethod testMethod) {
final CtCodeSnippetStatement snippet = testMethod.getFactory().Code().
createCodeSnippetStatement("UncompilableClass class = new UncompilableClass()");
final CtMethod method = testMethod.clone();
method.getBody().insertEnd(snippet);
method.setSimpleName("uncompilableTest");
final CtCodeSnippetStatement snippet1 = testMethod.getFactory().Code().createCodeSnippetStatement("String clazz = new String()");
final CtMethod method1 = testMethod.clone();
method1.getBody().insertEnd(snippet1);
method1.setSimpleName("compilableTest");
return Arrays.asList(method, method1);
}
示例5: getClass
import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
private CtClass<?> getClass(Factory factory) {
final CtClass<?> aClass = factory.Class().create("MyTestClass");
final CtMethod<Void> method = factory.createMethod();
method.setSimpleName("method");
method.setType(factory.Type().VOID_PRIMITIVE);
method.setBody(factory.createCodeSnippetStatement("System.out.println()"));
method.addModifier(ModifierKind.PUBLIC);
aClass.addMethod(method);
return aClass;
}
示例6: performExtraction
import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
public static CtMethod<?> performExtraction(CtMethod<?> ctMethod) {
// remove assertion
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);
extractedMethod.setParent(ctMethod.getParent());
ctMethodWithoutAssertion.getAnnotations().forEach(extractedMethod::removeAnnotation);
final int[] count = new int[1];
final Map<CtAbstractInvocation<?>, List<CtVariableAccess>> parametersPerInvocation =
new HashMap<>();
extractedMethod.getElements(new TypeFilter<>(CtLiteral.class))
.stream()
.filter(ctLiteral -> !(ctLiteral.getValue() instanceof String)) // TODO
.forEach(ctLiteral -> {
final CtParameter<?> parameter = factory.createParameter();
parameter.setType(ctLiteral.getType());
parameter.setSimpleName("lit" + count[0]++);
extractedMethod.addParameter(parameter);
final CtVariableAccess<?> variableRead = factory.createVariableRead(factory.createParameterReference(parameter), false);
final CtAbstractInvocation invocation = ctLiteral.getParent(CtAbstractInvocation.class);
if (invocation != null) {
if (!parametersPerInvocation.containsKey(invocation)) {
parametersPerInvocation.put(invocation, new ArrayList<>());
}
if (ctLiteral.getParent() instanceof CtUnaryOperator) {
parametersPerInvocation.get(invocation)
.add(invocation.getArguments().indexOf(ctLiteral.getParent()), variableRead);
} else {
parametersPerInvocation.get(invocation)
.add(invocation.getArguments().indexOf(ctLiteral), variableRead);
}
} else {
ctLiteral.replace(variableRead);
}
});
extractedMethod.getElements(new TypeFilter<>(CtAbstractInvocation.class))
.stream()
.filter(parametersPerInvocation::containsKey)
.forEach(ctAbstractInvocation ->
ctAbstractInvocation.setArguments(parametersPerInvocation.get(ctAbstractInvocation))
);
return extractedMethod;
}
示例7: makeFailureTest
import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
protected CtMethod<?> makeFailureTest(CtMethod<?> test, Failure failure) {
CtMethod cloneMethodTest = AmplificationHelper.cloneMethodTest(test, "");
cloneMethodTest.setSimpleName(test.getSimpleName());
Factory factory = cloneMethodTest.getFactory();
Throwable exception = failure.getException();
if (exception instanceof TestTimedOutException || // TestTimedOutException means infinite loop
exception instanceof AssertionError) { // AssertionError means that some assertion remained in the test: TODO
return null;
}
Class exceptionClass;
if (exception == null) {
exceptionClass = Exception.class;
} else {
exceptionClass = exception.getClass();
}
CtTry tryBlock = factory.Core().createTry();
tryBlock.setBody(cloneMethodTest.getBody());
String snippet = "org.junit.Assert.fail(\"" + test.getSimpleName() + " should have thrown " + exceptionClass.getSimpleName() + "\")";
tryBlock.getBody().addStatement(factory.Code().createCodeSnippetStatement(snippet));
DSpotUtils.addComment(tryBlock, "AssertGenerator generate try/catch block with fail statement", CtComment.CommentType.INLINE);
CtCatch ctCatch = factory.Core().createCatch();
CtTypeReference exceptionType = factory.Type().createReference(exceptionClass);
ctCatch.setParameter(factory.Code().createCatchVariable(exceptionType, "eee"));
ctCatch.setBody(factory.Core().createBlock());
List<CtCatch> catchers = new ArrayList<>(1);
catchers.add(ctCatch);
tryBlock.setCatchers(catchers);
CtBlock body = factory.Core().createBlock();
body.addStatement(tryBlock);
cloneMethodTest.setBody(body);
cloneMethodTest.setSimpleName(cloneMethodTest.getSimpleName() + "_failAssert" + (numberOfFail++));
Counter.updateAssertionOf(cloneMethodTest, 1);
AmplificationHelper.getAmpTestToParent().put(cloneMethodTest, test);
return cloneMethodTest;
}
示例8: test
import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
final File javaFile = new File(outputDirectory.getAbsolutePath() + "/" + "example.TestSuiteExample".replaceAll("\\.", "\\/") + ".java");
try {
FileUtils.forceDelete(javaFile);
} catch (IOException ignored) {
//ignored
}
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/test-projects/src/test/java/example/TestSuiteExample.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
final CtType<?> type = launcher.getFactory().Type().get("example.TestSuiteExample");
assertFalse(javaFile.exists());
DSpotUtils.printAmplifiedTestClass(type, outputDirectory);
assertTrue(javaFile.exists());
final CtMethod<?> clone = type.getMethods().stream()
.findFirst().get().clone();
final int nbMethodStart = type.getMethods().size();
type.getMethods().forEach(type::removeMethod);
clone.setSimpleName("MyNewMethod");
type.addMethod(clone);
DSpotUtils.printAmplifiedTestClass(type, outputDirectory);
launcher = new Launcher();
launcher.addInputResource(outputDirectory.getAbsolutePath() + "/" + "example.TestSuiteExample".replaceAll("\\.", "\\/") + ".java");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
assertEquals(nbMethodStart + 1, launcher.getFactory().Class().get("example.TestSuiteExample").getMethods().size());
type.getMethods().forEach(type::removeMethod);
clone.setSimpleName("MyNewMethod2");
type.addMethod(clone);
DSpotUtils.printAmplifiedTestClass(type, outputDirectory);
launcher = new Launcher();
launcher.addInputResource(outputDirectory.getAbsolutePath() + "/" + "example.TestSuiteExample".replaceAll("\\.", "\\/") + ".java");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
assertEquals(nbMethodStart + 2, launcher.getFactory().Class().get("example.TestSuiteExample").getMethods().size());
}